java實現(xiàn)汽車租賃系統(tǒng)
更新時間:2021年01月20日 09:18:37 作者:妖精小狗
這篇文章主要為大家詳細介紹了java實現(xiàn)汽車租賃系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了java實現(xiàn)汽車租賃系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
//車類 public abstract class Vehicle { //車牌號 品牌 日租金 private String id; private String brand; private int perRent; public Vehicle(){} //Vehicle的帶參構(gòu)造方法 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的帶參構(gòu)造方法 public Car(String id, String brand, int perRent,String type) { super(id,brand,perRent); this.type = type; } //重寫父類的計算租金方法:根據(jù)自己的計算租金規(guī)則 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{ //座位數(shù) private int seatCount; public void setSeatCount(int seatCount){ this.seatCount=seatCount; } public int getSeatCount(){ return seatCount; } public Bus(){} //Bus的帶參構(gòu)造方法 public Bus(String id,String brand,int perRent,int seatCount){ super(id,brand,perRent); this.seatCount = seatCount; } //重寫父類的計算租金方法:根據(jù)自己的計算租金規(guī)則 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; } } //汽車業(yè)務(wù)類 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(); } //租車:根據(jù)用戶提供的條件去汽車數(shù)組中查找相應(yīng)車輛并返回 //如果租賃的是轎車 需要條件:品牌 型號 //如果租賃的是客車 需要條件:品牌 座位數(shù) //簡單工廠模式 public Vehicle zuChe(String brand,String type,int seatCount){ Vehicle che = null; //for循環(huán)遍歷數(shù)組vehicle for(Vehicle myche : vehicle){ //判斷Vehicle類的myche的類型是否和Car一樣 if(myche instanceof Car){ //Vehicle類的myche向下轉(zhuǎn)型變成子類Car Car car = (Car)myche; if(car.getBrand().equals(brand) && car.getType().equals(type)){ che=car; break; } }else{ //Vehicle類的myche向下轉(zhuǎn)型變成子類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;//座位數(shù) //收集用戶條件 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("請選擇您要租賃的客車座位數(shù):(1.16座 2.32座)"); seatCount=(input.nextInt() == 1)?16:34; } //租車 Vehicle che = operation.zuChe(brand, type, seatCount); System.out.println("請輸入您的租賃天數(shù):"); int days = input.nextInt(); double money = che.calcRent(days); System.out.println("租車成功,請按照如下車牌號提車:"+che.getId()); System.out.println("您需要支付:"+money+"元"); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java ant包中的org.apache.tools.zip實現(xiàn)壓縮和解壓縮實例詳解
這篇文章主要介紹了java ant包中的org.apache.tools.zip實現(xiàn)壓縮和解壓縮實例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04JAVAEE項目結(jié)構(gòu)以及并發(fā)隨想
每個代碼里面的工具都是工具,API是你最需要理解的,哪個好,哪個不好,沒有準(zhǔn)確答案。 一切皆對象,對于Java來講是純粹的,代理是對象,反射是對象,對象是對象,基本數(shù)據(jù)類型不是對象。2016-04-04SpringBoot實現(xiàn)redis延遲隊列的示例代碼
延時隊列場景在我們?nèi)粘I(yè)務(wù)開發(fā)中經(jīng)常遇到,它是一種特殊類型的消息隊列,本文就來介紹一下SpringBoot實現(xiàn)redis延遲隊列的示例代碼,具有一定的參考價值,感興趣的可以了解一下2024-02-02