答答租車系統(tǒng)代碼--引用數(shù)組存儲(chǔ)租車信息,能自動(dòng)計(jì)算賬單。也有自留坑,有需要的同學(xué)可以一起交流/ 請(qǐng)大神看看
看完第一季和第二季寫的答答租車系統(tǒng)代碼:
有需要的同學(xué)可以拿去試跑一下,也請(qǐng)和我交流一下,有什么可以改進(jìn)的。因?yàn)樽罱龅脰|西大多數(shù)跟數(shù)據(jù)庫有關(guān),做Java項(xiàng)目好缺乏
有不足的地方--》如何更好地把類提取出來 // 如何更好處理需要存儲(chǔ)客戶需求的數(shù)組
首次訪問頁:
package?com.imooc.carrentsys; import?java.util.Scanner; public?class?ClientService?{??//客戶服務(wù)層,首頁顯示都在這個(gè)類里進(jìn)行修改 public?int?indication?; public?int?ClientSearch(){ System.out.println("您好,歡迎來到租車網(wǎng)!"); System.out.println("如要租車,請(qǐng)按1?;如不租車,請(qǐng)按0"); Scanner??input?=?new?Scanner(System.in); indication?=?input.nextInt(); return?indication;?????//返回指示,在main方法里面只接收指示——是否租車 } }
車的信息
package?com.imooc.carrentsys; public?class?Car?{ public?int?num;?????//車的編碼 public?String?name; ???//車的名字 public?double?rent;?????//車的租金/天 public?String?load;?????//車的容量 /* ?*?車的構(gòu)造函數(shù)?,在我看來,這道題不需要給車的類型分類,但從長遠(yuǎn)的角度來看--》分類管理,當(dāng)車不是10臺(tái),而是800臺(tái) ?*?此時(shí)有分類創(chuàng)建并存儲(chǔ)在數(shù)據(jù)庫不同的表格里會(huì)更好,易檢查,易修復(fù) ?*/ public?Car(int?num,String?name?,double?rent,String?load){??? this.num=num; this.name?=name; this.rent=rent; this.load=load; } @Override public?String?toString()?{ return??num?+?"?"?+??name??+?"?"?+?rent +?"?"?+?load?+?"?"; } }
為車分類:私家車、皮卡和貨車 (運(yùn)用繼承知識(shí)點(diǎn)--》子類構(gòu)造函數(shù)必須創(chuàng)建)
package?com.imooc.carrentsys; public?class?PrivateCar?extends?Car?{ public?PrivateCar(int?num,?String?name,?double?rent,?String?load)?{??//子類也要定義構(gòu)造函數(shù),系統(tǒng)不會(huì)執(zhí)行默認(rèn)的無參構(gòu)造函數(shù) super(num,?name,?rent,?load);?//super()顯式執(zhí)行父類的構(gòu)造函數(shù)?,算是創(chuàng)建車子類的知識(shí)點(diǎn)提醒 //?TODO?Auto-generated?constructor?stub } }
package?com.imooc.carrentsys; public?class?Truck?extends?Car{ public?Truck(int?num,?String?name,?double?rent,?String?load)?{ super(num,?name,?rent,?load); //?TODO?Auto-generated?constructor?stub } }
package?com.imooc.carrentsys; public?class?Pickup??extends??Car{ public?Pickup(int?num,?String?name,?double?rent,?String?load)?{ super(num,?name,?rent,?load); //?TODO?Auto-generated?constructor?stub } }
車列表信息
package?com.imooc.carrentsys; public?class?CarInfo?{ public?String[]?title?={"序號(hào)?","?車型?","?租金(元/天)?","?容量?"}; public?Car[]?carInfo?={new?PrivateCar(001?,"??奧迪A4?",?500,"?載人:4人"),???//繼承關(guān)系,使用多態(tài)——》父類引用數(shù)組 ????????????? ?new?PrivateCar(002?,"??馬自達(dá)006",?400,"?載人:4人"), ?????????????new?Pickup(003?,"??皮卡雪006",450,"?載人:4人??載貨:2噸?"), ?????????????new?PrivateCar(004?,"??金龍?",800,"?載人:20人"), ?????????????new?Truck(005?,"??松花江?",800,"?載貨:4噸?"), ?????????????new?Truck(006?,"??依維?",1000,"?載貨:20噸?")};???//Car信息列表初始化,Car作為數(shù)組類型 public?void?CarList(){ //System.out.println("Test:"+carInfo[1]);???//寫引用類型數(shù)組,carInfo引用中存儲(chǔ)指向?qū)ο蟮牡刂??com.imooc.carrentsys.PrivateCar@a298b7 System.out.println("你可租用的車型以及租金列表:"); //System.out.println(title); for(int?i=0;i<title.length;i++){ System.out.print(title[i]);? //System.out.println(carInfo[i]);???打印carInfo的信息,com.imooc.carrentsys.PrivateCar@a298b7 } System.out.println();??//單行鍵,更加美觀 for(int?i=0;i<carInfo.length;i++){ System.out.println(carInfo[i]);? //System.out.println(carInfo[i]);???打印carInfo的信息,com.imooc.carrentsys.PrivateCar@a298b7 } } }
客戶租車需求的信息收集
package?com.imooc.carrentsys; import?java.util.Scanner; public?class?RentCount?{???//根據(jù)顧客的租車要求顯示賬單 int?rentNum?=0;?//租車數(shù)量 int[]?carChoose?=?new?int[7];?//客戶的需求車型,初始化數(shù)組內(nèi)存空間,才可以存放數(shù)值--不算是一個(gè)聰明的做法 int?rentDur=0; public?void?rentCar(){ System.out.println("您好,請(qǐng)輸入要租車的數(shù)量:"); Scanner?input?=?new?Scanner(System.in); rentNum?=?input.nextInt();???//顯示有客戶需要租用幾輛車,這里有個(gè)限制,客戶不能同時(shí)選擇一款車的多個(gè)數(shù)量,學(xué)習(xí)前端的必要性 for(int?temp=1?,?i=0;temp<=rentNum;temp++,i++){ System.out.println("您好,請(qǐng)輸入你想要租的第"+temp+"輛車型編碼"); Scanner?inputCarNum?=?new?Scanner(System.in); ?//String[]?carChoose?=?null??;???局部變量必須進(jìn)行初始化, ????carChoose[i]?=inputCarNum.nextInt();???//數(shù)組存儲(chǔ)客戶輸入的租車編碼,如果有數(shù)據(jù)庫,一般通過編碼去數(shù)據(jù)庫選取,這里只能hardcode } } public?void?rentDay(){ System.out.println("您好,請(qǐng)輸入要租車的天數(shù):"); Scanner?input?=?new?Scanner(System.in); rentDur?=?input.nextInt();??//知道客人租幾天 } }
最后就是測試類(但是也涉及了小部分邏輯操作,這個(gè)可以在分離嗎):
package?com.imooc.carrentsys; public?class?RentCarSys?{ public?static?void?main(String[]?args){ ClientService?client?=?new?ClientService();??//是否要租車,不租就退出,租車就繼續(xù)往下執(zhí)行程序 if?(client.ClientSearch()==0){ System.out.println("歡迎訪問答答租車網(wǎng)");? } else{ CarInfo?carInformation?=?new?CarInfo(); carInformation.CarList();???//顯示車列表信息 System.out.println();??//空一行,僅作為視覺上的美觀 RentCount?rentCount?=??new?RentCount();??//收集客戶的租車需求 rentCount.rentCar(); rentCount.rentDay(); System.out.println();? System.out.println("您的訂單如下:")??;?//對(duì)題目做了一點(diǎn)改動(dòng),把客戶的對(duì)車的需求列出來 System.out.println("您的租車天數(shù):"+rentCount.rentDur)??;? int?temp?=0; double?bill?=?0;?//帳的數(shù)目 for(;??temp<rentCount.rentNum;temp++){??//自留坑,不能用數(shù)組的length,否則會(huì)把所有空間的讀一遍 ?for(int?i?=0;?i<carInformation.carInfo.length;i++){ ?if(rentCount.carChoose[temp]==carInformation.carInfo[i].num){ ?System.out.println(carInformation.carInfo[i]); ?bill+=carInformation.carInfo[i].rent; ?} ?} } System.out.print("您的賬單為:"+(bill?*rentCount.rentDur));??? } } }
2015-12-30
少用public的成員變量,不安全哦,用private吧