package?com.imooc;
public?abstract?class?Car?{?//所有車的父類
public??String?name; //車名
public?int?price; //價格
public?abstract?void?showInfo(); //抽象方法,子類繼承時重寫,顯示車名、價格、載客或載貨量
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?int?getPrice()?{
return?price;
}
public?void?setPrice(int?price)?{
this.price?=?price;
}
}
package?com.imooc;
public?class?Auto?extends?Car?{ //汽車類
public?String?name;
public?int?price;
public?int?capPerson; //載客量
public?Auto(String?name,int?price,int?capPerson){
this.name=name;
this.price=price;
this.capPerson=capPerson;
}
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?int?getPrice()?{
return?price;
}
public?void?setPrice(int?price)?{
this.price?=?price;
}
public?int?getCapPerson()?{
return?capPerson;
}
public?void?setCapPerson(int?capPerson)?{
this.capPerson?=?capPerson;
}
@Override
public?void?showInfo()?{
//?TODO?自動生成的方法存根
System.out.println(getName()?+?'\t'?+getPrice()?+"元/天"?+?'\t'?+?"載人:"+getCapPerson()?+"人");
}
}
package?com.imooc;
public?class?Pickup?extends?Car?{ //皮卡類
public?String?name;
public?int?price;
public?int?capPerson; //載客量
public?int?capThings; //載貨量
//構(gòu)造方法
public?Pickup(String?name,int?price,int?capPerson,int?capThings){
this.name?=?name;
this.price?=?price;
this.capPerson?=?capPerson;
this.capThings?=?capThings;
}
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?int?getPrice()?{
return?price;
}
public?void?setPrice(int?price)?{
this.price?=?price;
}
public?int?getCapPerson()?{
return?capPerson;
}
public?void?setCapPerson(int?capPerson)?{
this.capPerson?=?capPerson;
}
public?int?getCapThings()?{
return?capThings;
}
public?void?setCapThings(int?capThings)?{
this.capThings?=?capThings;
}
@Override
public?void?showInfo()?{
//?TODO?自動生成的方法存根
System.out.println(getName()?+?'\t'?+getPrice()?+"元/天"?+?'\t'?+?"載人:"+getCapPerson()?+"人"+?"??載貨:"+getCapThings()?+"噸");
}
}
package?com.imooc;
public?class?Truck?extends?Car?{ //貨車類
public?String?name;
public?int?price;
public?int?capThings;
//構(gòu)造方法
public?Truck(String?name,int?price,int?capThings){
this.name?=?name;
this.price?=?price;
this.capThings?=?capThings; //載貨量
}
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
public?int?getPrice()?{
return?price;
}
public?void?setPrice(int?price)?{
this.price?=?price;
}
public?int?getCapThings()?{
return?capThings;
}
public?void?setCapThings(int?capThings)?{
this.capThings?=?capThings;
}
@Override
public?void?showInfo()?{
//?TODO?自動生成的方法存根
System.out.println(getName()?+?'\t'?+getPrice()?+"元/天"?+?'\t'?+?"載貨:"+getCapThings()?+"噸");
}
}
package?com.imooc;
import?java.util.*;
public?class?DadaRent?{
public?static?void?main(String[]?args)?{
//?TODO?自動生成的方法存根
//創(chuàng)建車輛信息
Car[]?allRent?=?{new?Auto("奧迪A4",500,4),new?Auto("馬自達6",400,4),new?Pickup("皮卡雪6",450,4,2),new?Auto("金龍??",800,20),new?Truck("松花江",400,4),new?Truck("依維河",1000,20)};
System.out.println("歡迎使用嗒嗒租車系統(tǒng):");
System.out.println("您是否想要租車:1是??0否");
//顯示租車信息
Scanner?input?=?new?Scanner(System.in);
int?choice?=?input.nextInt();
while(choice!=0||choice?!=1)
{ //如果輸入不為0或1,則重新輸入
if(choice?==0){
System.out.println("感謝您使用嗒嗒租車系統(tǒng),下次再見!");
break;
}else?if(choice?==1){
System.out.println("您可租車的類型及其價目表:");
System.out.println("序號"?+?'\t'?+?"汽車名稱"?+?'\t'?+?"租金"?+?'\t'?+"容量");
for(int?i=0;i<allRent.length;i++){
System.out.print((i+1)?+?".\t");
allRent[i].showInfo();
}
System.out.println("請輸入想要租車的數(shù)量:");
break;
}else?{
System.out.println("請輸入正確的數(shù)字:1是??0否");
choice?=?input.nextInt();
}
}
int?carNum?=?input.nextInt(); //租車數(shù)量
Car[]?choiceCar?=?new?Car[carNum]; //將客戶選擇的車輛對象放入choiceCar數(shù)組
for(int?i=0;i<carNum;i++){
System.out.println("請輸入第"?+?(i+1)?+"輛車的序號:");
int?num?=input.nextInt();//每輛車的序號
choiceCar[i]=allRent[num-1];
}
System.out.println("請輸入想要租車的天數(shù):");
int?rentDay?=?input.nextInt();??//租車天數(shù)
//計算并顯示賬單
System.out.println("********************您的賬單信息如下:********************");
int?dayPrice=0; //每天租車總價
System.out.println(">>>>>>>您要租的車是:???");
for(int?i=0;i<choiceCar.length;i++){
dayPrice=choiceCar[i].getPrice()+dayPrice;
choiceCar[i].showInfo();
}
//System.out.println("每天總價:"+dayPrice);
System.out.println(">>>>>>>您總共要租借:??"?+?rentDay??+?"??天");
//計算總載客載貨量
int?totalCapPerson?=?0; //總載客量
int?totalCapThings?=?0; //總載貨量
for(int?i?=?0;?i?<?choiceCar.length;?i++){
//判斷所選車是Auto、Truck還是Pickup
if(choiceCar[i]?instanceof?Auto){ //汽車載客量
totalCapPerson?+=?((Auto)choiceCar[i]).getCapPerson();?
}
if(choiceCar[i]?instanceof?Truck){ //貨車載貨量
totalCapThings?+=?((Truck)choiceCar[i]).getCapThings();
}
if(choiceCar[i]?instanceof?Pickup){ //皮卡載客和載貨量
totalCapPerson?+=((Pickup)choiceCar[i]).getCapPerson();
totalCapThings?+=((Pickup)choiceCar[i]).getCapThings();
}
}
//輸出總載貨量和總載客量
System.out.println(">>>>>>>您所要租借的總載客量為:?"?+?totalCapPerson?+?"人\t"?+?"總載貨量為:"?+?totalCapThings?+?"噸");
int?totalPrice?=?dayPrice*rentDay; //總價
System.out.println(">>>>>>>您總共需要支付:??"?+?totalPrice??+?"??元");
System.out.println("感謝您使用嗒嗒租車系統(tǒng),下次再見!");
input.close();
}
}
2016-03-28
同問,Car[]?allRent是什么?是不是用父類名定義子類組成的數(shù)組?
2016-01-16
牛B 牛B?
2016-01-15
厲害,學(xué)的蠻好的
2015-12-19
樓主,你父類為什么要用get與set的方法呢?get與set不是針對封閉類的嗎?
2015-12-04
Car[]?allRent?=
這樣的寫法是數(shù)組還是什么?如果是數(shù)組的話前面應(yīng)該是類型才對。
如果是方法的話,那么【】代表數(shù)組的意思嗎?
2015-11-22
寫得不錯,值得我去借鑒
2015-11-10
這個while循環(huán)寫的好
2015-11-05
你的main方法里面的while循環(huán)條件可以直接為true
2015-11-05
不錯不錯