Java面向?qū)ο髮?shí)現(xiàn)汽車租賃系統(tǒng)
本文實(shí)例為大家分享了Java實(shí)現(xiàn)汽車租賃系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
父類Vehicle
public abstract class Vehicle { ? ? private String num; ? ? private String brand; ? ? private ?double rent; ? ? //重寫equals方法 ? ? public abstract boolean equals(Vehicle v); ? ? //計(jì)算費(fèi)用 ? ? public abstract double cost(int days,double rent); ? ? //3個(gè)參數(shù)的有參構(gòu)造 ? ? public Vehicle(String num, String brand, double rent) { ? ? ? ? this.num = num; ? ? ? ? this.brand = brand; ? ? ? ? this.rent = rent; ? ? } ? ? //無(wú)參構(gòu)造 ? ? public Vehicle() { ? ? } ? ? //get,set方法 ? ? public String getNum() { ? ? ? ? return num; ? ? } ? ? public void setNum(String num) { ? ? ? ? this.num = num; ? ? } ? ? public String getBrand() { ? ? ? ? return brand; ? ? } ? ? public void setBrand(String brand) { ? ? ? ? this.brand = brand; ? ? } ? ? public double getRent() { ? ? ? ? return rent; ? ? } ? ? public void setRent(double rent) { ? ? ? ? this.rent = rent; ? ? } }
子類Cars
public class Cars extends Vehicle{ ? ? private String type; ? ? //重寫父類equals方法 ? ? @Override ? ? public boolean equals(Vehicle v) { ? ? ? ? if(v instanceof Cars){ ? ? ? ? ? ? Cars c=(Cars) v; ? ? ? ? ? ? return this.getBrand().equals(c.getBrand())&&this.getType().equals(c.getType()); ? ? ? ? } ? ? ? ? return false; ? ? } ? ? //重寫父類費(fèi)用方法 ? ? @Override ? ? public double cost(int days,double sent) { ? ? ? ? if (days>150){ ? ? ? ? ? ? return days*sent*0.7; ? ? ? ? }else if (days>30){ ? ? ? ? ? ? return days*sent*0.8; ? ? ? ? }else if (days>7){ ? ? ? ? ? ? return days*sent*0.9; ? ? ? ? }else { ? ? ? ? ? ? return days*sent; ? ? ? ? } ? ? } ? ? //無(wú)參構(gòu)造 ? ? public Cars() { ? ? } ? ? //有參構(gòu)造 ? ? public Cars(String num, String brand, double rent, String type) { ? ? ? ? super(num, brand, rent); ? ? ? ? this.type = type; ? ? } ? ? //2個(gè)有參構(gòu)造的方法 ? ? public Cars(String brand,String type){ ? ? ? ? super.setBrand(brand); ? ? ? ? this.type = type; ? ? } ? ? public String getType() { ? ? ? ? return type; ? ? } ? ? public void setType(String type) { ? ? ? ? this.type = type; ? ? } }
子類Bus
public class Bus extends Vehicle{ ? ? private int seat; ? ? //重寫父類的equals方法 ? ? @Override ? ? public boolean equals(Vehicle v) { ? ? ? ? if(v instanceof Bus){ ? ? ? ? ? ? Bus b=(Bus) v; ? ? ? ? ? ? return this.getBrand().equals(b.getBrand())&&this.getSeat()==b.getSeat(); ? ? ? ? } ? ? ? ? return false; ? ? } ? ? //重寫父類費(fèi)用方法 ? ? @Override ? ? public double cost(int days,double rent) { ? ? ? ? if (days>=150){ ? ? ? ? ? ? return days*rent*0.6; ? ? ? ? }else if (days>=30){ ? ? ? ? ? ? return days*rent*0.7; ? ? ? ? }else if (days>=7){ ? ? ? ? ? ? return days*rent*0.8; ? ? ? ? }else if (days>=3){ ? ? ? ? ? ? return days*rent*0.9; ? ? ? ? }else { ? ? ? ? ? ? return days*rent; ? ? ? ? } ? ? } ? ? //2個(gè)參數(shù)的有參構(gòu)造 ? ? public Bus(String brand,int seat){ ? ? ? ? super.setBrand(brand); ? ? ? ? this.seat=seat; ? ? } ? ? //子類有參構(gòu)造 ? ? public Bus(String num, String brand, double rent, int seat) { ? ? ? ? super(num, brand, rent); ? ? ? ? this.seat = seat; ? ? } ? ? //子類無(wú)參構(gòu)造 ? ? public Bus(){} ? ? //子類get set 方法 ? ? public int getSeat() { ? ? ? ? return seat; ? ? } ? ? public void setSeat(int seat) { ? ? ? ? this.seat = seat; ? ? } }
汽車業(yè)務(wù)類VehicleServicer
public class VehicleServicer { ? ? public static List initVehicle(){ ? ? ? ? Vehicle v1=new Bus("京6566754","金杯",800,16); ? ? ? ? Vehicle v2=new Bus("京9696996","金杯",1500,34); ? ? ? ? Vehicle v3=new Bus("京8696997","金龍",800,16); ? ? ? ? Vehicle v4=new Bus("京8696998","金龍",1500,34); ? ? ? ? Vehicle c1 =new Cars("京NT37465","別克",300,"林蔭大道"); ? ? ? ? Vehicle c2 =new Cars("京9696996","別克",600,"GLB"); ? ? ? ? Vehicle c3 =new Cars("京8696997","寶馬",800,"X6"); ? ? ? ? Vehicle c4 =new Cars("京8696998","寶馬",600,"550i"); ? ? ? ? //先裝入數(shù)組中 ? ? ? ? Vehicle[] ve = {v1,v2,v3,v4,c1,c2,c3,c4}; ? ? ? ? //將數(shù)組轉(zhuǎn)換成集合 ? ? ? ? List<Vehicle> vehicles = Arrays.asList(ve); ? ? ? ? return vehicles; ? ? } }
測(cè)試類Test
public class Test { ? ? public static void main(String[] args) { ? ? ? ? Scanner sc = new Scanner(System.in); ? ? ? ? System.out.println("**********歡迎光臨秋明山守望者汽車租賃公司**********"); ? ? ? ? System.out.println("1.轎車 ? ?2.客車"); ? ? ? ? System.out.print("請(qǐng)選擇你要租賃的汽車類型:"); ? ? ? ? int type = sc.nextInt(); ? ? ? ? //橋車 ? ? ? ? Vehicle ve; ? ? ? ? String brand; ? ? ? ? if(type==1){ ? ? ? ? ? ? System.out.print("請(qǐng)選擇你要租賃的汽車品牌(1.別克 ?2.寶馬):"); ? ? ? ? ? ? int pinpai = sc.nextInt(); ? ? ? ? ? ? String model=pinpai==1?"別克":"寶馬"; ? ? ? ? ? ? if(pinpai==1){ ? ? ? ? ? ? ? ? System.out.print("請(qǐng)輸入你要租賃的汽車類型(1.X6 ?2.550i):"); ? ? ? ? ? ? ? ? int leixin = sc.nextInt(); ? ? ? ? ? ? ? ? brand=leixin==1?"林蔭大道":"GL8"; ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? System.out.print("請(qǐng)輸入你要租賃的汽車類型(1.X6 ?2.550i):"); ? ? ? ? ? ? ? ? int leixin = sc.nextInt(); ? ? ? ? ? ? ? ? brand=leixin==1?"X6":"550i"; ? ? ? ? ? ? } ? ? ? ? ? ? ?ve = new Cars(model, brand); ? ? ? ? }else {//客車 ? ? ? ? ? ? int seat; ? ? ? ? ? ? System.out.print("請(qǐng)選擇你要租賃的汽車品牌(1.金龍 ?2.金杯):"); ? ? ? ? ? ? int pinpai = sc.nextInt(); ? ? ? ? ? ? String s=pinpai==1?"金龍":"金杯"; ? ? ? ? ? ? System.out.print("請(qǐng)選擇你要租賃的汽車座位數(shù)(1.16座 2.34座):"); ? ? ? ? ? ? int z = sc.nextInt(); ? ? ? ? ? ? seat =z==1?16:34; ? ? ? ? ? ? ve = new Bus(s, seat); ? ? ? ? } ? ? ? ? //根據(jù)選好的車型,輸出車牌和總價(jià) ? ? ? ? List<Vehicle> list = VehicleServicer.initVehicle(); ? ? ? ? for (Vehicle v:list) { ? ? ? ? ? ? if(ve.equals(v)){ ? ? ? ? ? ? ? ? System.out.print("請(qǐng)輸入你要租賃的天數(shù):"); ? ? ? ? ? ? ? ? int days = sc.nextInt(); ? ? ? ? ? ? ? ? System.out.println("分配給您的汽車牌號(hào)是:"+v.getNum()); ? ? ? ? ? ? ? ? System.out.println("您需要支付的租賃費(fèi)用是:"+v.cost(days,v.getRent())); ? ? ? ? ? ? } ? ? ? ? } ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Spring中一級(jí)緩存、二級(jí)緩存和三級(jí)緩存的那些事
Spring解決循環(huán)依賴的核心思想在于提前曝,下面這篇文章主要給大家介紹了關(guān)于Spring中一級(jí)緩存、二級(jí)緩存和三級(jí)緩存的那些事,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02Java發(fā)送http請(qǐng)求的示例(get與post方法請(qǐng)求)
這篇文章主要介紹了Java發(fā)送http請(qǐng)求的示例(get與post方法請(qǐng)求),幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2021-01-01利用SpringBoot和LiteFlow解鎖復(fù)雜流程
隨著業(yè)務(wù)的復(fù)雜化,企業(yè)需要更加高效、便捷地管理自己的業(yè)務(wù)流程,這就需要借助一些流程引擎實(shí)現(xiàn),今天,我們就來(lái)介紹一種基于Java語(yǔ)言開(kāi)發(fā)的輕量級(jí)工作流引擎——LiteFlow,以及如何在Spring Boot框架中集成它,從而提高企業(yè)的工作效率和開(kāi)發(fā)效率2023-06-06Spring Boot Web 靜態(tài)文件緩存處理的方法
本篇文章主要介紹了Spring Boot Web 靜態(tài)文件緩存處理的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02使用Java實(shí)現(xiàn)2048小游戲代碼實(shí)例
這篇文章主要介紹了使用Java實(shí)現(xiàn)2048小游戲代碼實(shí)例,2048 游戲是一款益智類游戲,玩家需要通過(guò)合并相同數(shù)字的方塊,不斷合成更大的數(shù)字,最終達(dá)到2048,游戲規(guī)則簡(jiǎn)單,但挑戰(zhàn)性很高,需要玩家靈活運(yùn)用策略和計(jì)算能力,本文將使用Java代碼實(shí)現(xiàn),需要的朋友可以參考下2023-10-10IDEA2022 提示更新 TKK失敗請(qǐng)檢查網(wǎng)絡(luò)連接的問(wèn)題
這篇文章主要介紹了IDEA2022 提示:更新 TKK 失敗,請(qǐng)檢查網(wǎng)絡(luò)連接,本文給大家分享解決方案,對(duì)idea2022提示更新TKK失敗感興趣的朋友跟隨小編一起看看吧2022-11-11