您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關使用java實現汽車租賃系統的案例的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
具體內容如下
//車類 public abstract class Vehicle { //車牌號 品牌 日租金 private String id; private String brand; private int perRent; public Vehicle(){} //Vehicle的帶參構造方法 public Vehicle(String id, String brand, int perRent) { this.id = id; this.brand = brand; this.perRent = perRent; } public void setId(String id){ this.id=id ; } public String getId(){ return id; } public void setBrand(String brand){ this.brand=brand; } public String getBrand(){ return brand; } public void setPerRent(int perRent){ this.perRent=perRent; } public int getPerRent(){ return perRent; } //抽象方法計算租金 public abstract double calcRent(int days); } //轎車類 public class Car extends Vehicle{ //型號 private String type; public void setType(String type){ this.type=type; } public String getType(){ return type; } public Car(){} //Car的帶參構造方法 public Car(String id, String brand, int perRent,String type) { super(id,brand,perRent); this.type = type; } //重寫父類的計算租金方法:根據自己的計算租金規則 public double calcRent(int days) { double price = this.getPerRent()*days; if(days>7 && days<=30){ price *= 0.9; }else if(days>30 && days<=150){ price *= 0.8; }else if(days>150){ price *= 0.7; } return price; } } //客車類 public class Bus extends Vehicle{ //座位數 private int seatCount; public void setSeatCount(int seatCount){ this.seatCount=seatCount; } public int getSeatCount(){ return seatCount; } public Bus(){} //Bus的帶參構造方法 public Bus(String id,String brand,int perRent,int seatCount){ super(id,brand,perRent); this.seatCount = seatCount; } //重寫父類的計算租金方法:根據自己的計算租金規則 public double calcRent(int days) { double price = this.getPerRent()*days; if(days>=3 && days<7){ price *= 0.9; }else if(days>=7 && days<30){ price *= 0.8; }else if(days>=30 && days<150){ price *= 0.7; }else if(days>150){ price *= 0.6; } return price; } } //汽車業務類 public class Operation { public Vehicle[] vehicle = new Vehicle[8]; //初始化汽車信息 public void init(){ vehicle[0] = new Car("京NY28588","寶馬",800,"X6"); //vehicle v = new Car(); vehicle[1] = new Car("京CNY32584","寶馬",600,"550i"); //vehicle v = new Car(); vehicle[2] = new Car("京NT37465","別克",300,"林蔭大道"); //vehicle v = new Car(); vehicle[3] = new Car("京NT96968","別克",600,"GL8"); //vehicle v = new Car(); vehicle[4] = new Bus("京6566754","金杯",800,16); //vehicle v = new Bus(); vehicle[5] = new Bus("京8696997","金龍",800,16); //vehicle v = new Bus(); vehicle[6] = new Bus("京9696996","金杯",1500,34); //vehicle v = new Bus(); vehicle[7] = new Bus("京8696998","金龍",1500,34); //vehicle v = new Bus(); } //租車:根據用戶提供的條件去汽車數組中查找相應車輛并返回 //如果租賃的是轎車 需要條件:品牌 型號 //如果租賃的是客車 需要條件:品牌 座位數 //簡單工廠模式 public Vehicle zuChe(String brand,String type,int seatCount){ Vehicle che = null; //for循環遍歷數組vehicle for(Vehicle myche : vehicle){ //判斷Vehicle類的myche的類型是否和Car一樣 if(myche instanceof Car){ //Vehicle類的myche向下轉型變成子類Car Car car = (Car)myche; if(car.getBrand().equals(brand) && car.getType().equals(type)){ che=car; break; } }else{ //Vehicle類的myche向下轉型變成子類Bus Bus bus = (Bus)myche; if(bus.getBrand().equals(brand) && bus.getSeatCount() == seatCount){ che=bus; break; } } } return che; } } //汽車租賃 public class Rent { public static void main(String[] args) { Scanner input = new Scanner(System.in); Operation operation = new Operation(); //租賃公司界面 operation.init(); System.out.println("**********歡迎光臨租賃公司**********"); System.out.println("1.轎車\t\t2.客車"); System.out.println("請選擇您要租賃的汽車類型:(1.轎車 2.客車)"); int cheType = input.nextInt(); String brand = "";//品牌 String type = "";//型號 int seatCount = 0;//座位數 //收集用戶條件 if(cheType == 1){ //租賃轎車 System.out.println("請選擇您要租賃的轎車品牌:(1.別克 2.寶馬)"); int choose = input.nextInt(); if(choose == 1){ brand="別克"; System.out.println("請選擇您要租賃的汽車型號:(1.林蔭大道 2.GL8)"); type=(input.nextInt() == 1)?"林蔭大道":"GL8"; }else if(choose == 2){ brand="寶馬"; System.out.println("請選擇您要租賃的汽車型號:(1.X6 2.550i)"); type=(input.nextInt() == 1)?"X6":"550i"; } }else if(cheType == 2){ //租賃客車 type= ""; System.out.println("請選擇您要租賃的客車品牌:(1.金杯 2.金龍)"); brand=(input.nextInt()==1)?"金杯":"金龍"; System.out.println("請選擇您要租賃的客車座位數:(1.16座 2.32座)"); seatCount=(input.nextInt() == 1)?16:34; } //租車 Vehicle che = operation.zuChe(brand, type, seatCount); System.out.println("請輸入您的租賃天數:"); int days = input.nextInt(); double money = che.calcRent(days); System.out.println("租車成功,請按照如下車牌號提車:"+che.getId()); System.out.println("您需要支付:"+money+"元"); } }
感謝各位的閱讀!關于“使用java實現汽車租賃系統的案例”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。