租車代碼,求指教,新手,定義了抽象類,然后主方法按操作流程實現(xiàn),求指導(dǎo)不足之處
//父類 package?com.sc; public?abstract?class?Car?{ public?String?carName; public?float?carPrice; public?int?carWeight; public?int?carPerson; public?String?getCarName()?{ return?carName; } public?void?setCarName(String?carName)?{ this.carName?=?carName; } public?float?getCarPrice()?{ return?carPrice; } public?void?setCarPrice(float?carPrice)?{ this.carPrice?=?carPrice; } public?int?getCarWeight()?{ return?carWeight; } public?void?setCarWeight(int?carWeight)?{ this.carWeight?=?carWeight; } public?int?getCarPerson()?{ return?carPerson; } public?void?setCarPerson(int?carPerson)?{ this.carPerson?=?carPerson; } public?abstract?void?info(); }
//小汽車客車 package?com.sc; public?class?PassgerCar?extends?Car?{ public?PassgerCar(String?carName,float?carPrice,int?carPerson){ this.carName?=?carName; this.carPerson?=?carPerson; this.carPrice?=?carPrice; }? @Override public?void?info()?{ System.out.println(getCarName()?+?'\t'?+getCarPrice()?+"元/天"?+?'\t'?+?"載人:"+getCarPerson()?+"人"); } }
//貨車 package?com.sc; public?class?TrunkCar?extends?Car?{ public?TrunkCar(String?carName,float?carPrice,int?carWeight)?{ this.carName?=?carName; this.carWeight?=?carWeight; this.carPrice?=?carPrice; } @Override public?void?info()?{ //?TODO?自動生成的方法存根 System.out.println(getCarName()?+?'\t'?+getCarPrice()?+"元/天"?+?'\t'?+?"載重:"+getCarWeight()?+"噸"); } }
//皮卡 package?com.sc; public?class?PickupCar?extends?Car?{ public?PickupCar(String?carName,float?carPrice,int?carPerson,int?carWeight)?{ this.carName?=?carName; this.carPerson?=?carPerson; this.carWeight?=?carWeight; this.carPrice?=?carPrice; } @Override public?void?info()?{ //?TODO?自動生成的方法存根 System.out.println(getCarName()?+?'\t'?+getCarPrice()?+"元/天"?+?'\t'?+? "載人:"+getCarPerson()?+"人"+?"??載貨:"+getCarWeight()?+"噸"); } }
//測試代碼 package?com.sc; import?java.util.*; public?class?DiDi?{ public?static?void?main(String[]?args)?{ Car[]?allCar?=?{new?PassgerCar("奧迪A4",500,4),new?PassgerCar("馬自達(dá)6",400,4),new?PickupCar("皮卡雪6",450,4,2) ,new?PassgerCar("金龍??",800,20),new?TrunkCar("松花江",400,4),new?TrunkCar("依維河",1000,20)}; System.out.println("歡迎使用XX租車系統(tǒng)??!"); System.out.println("您是否需要租車:1.是?|2.否"); Scanner?scanner?=?new?Scanner(System.in); int?num?=?scanner.nextInt(); while?(num?!=?1?||?num?!=2)?{ if?(num?==?1)?{ System.out.println("***************************"); System.out.println("我們?yōu)槟鷾?zhǔn)備的車輛類型和價格如下:"); System.out.println("序號"?+?'\t'?+?"汽車名稱"?+?'\t'?+?"租金"?+?'\t'?+"容量"); for?(int?j?=?0;?j?<allCar.length?;?j++)?{ System.out.print((j+1)?+?".\t"?); allCar[j].info(); } break; }?else?if?(num?==?2)?{ System.out.println("謝謝您的使用,再見!"); System.exit(0); }else?{ System.out.println("輸入不正確,請重新輸入!"); System.out.println("您是否需要租車:1.是?|2.否"+'\n'); num?=?scanner.nextInt(); } } System.out.println("請輸入您要租車的序號:"); int?num1?=?scanner.nextInt(); while(num1?>?0){ if?(num1?>?0?&&?num1?<=?allCar.length)?{ System.out.println("您選擇的是第"+?num1?+"輛車,"+?allCar[num1-1].getCarName()?+ ",租金為:"+allCar[num1-1].getCarPrice()+?"元/天。"); System.out.println("請輸入你想租車的天數(shù):"); int?day?=?scanner.nextInt(); float?rentPrice?=?allCar[num1-1].getCarPrice()?*?day; System.out.println("您應(yīng)付的租金為:"?+?rentPrice?+?"元。"?); System.exit(0); }?else{ System.out.println("輸入車輛序號不正確,請重新輸入!"); System.out.println("請輸入您要租車的序號:"+'\n'); num1?=?scanner.nextInt(); } } } }
求指導(dǎo),第一次寫,費(fèi)勁,很多含糊的地方。
2015-09-20
考慮到封裝性,應(yīng)該用private關(guān)鍵字修飾屬性