//一些說(shuō)在前頭的廢話:
//本來(lái)打算用一個(gè)Map或者查查arrayList怎么用來(lái)解決之后的‘如何知道用戶租了哪幾輛車(chē)’這個(gè)問(wèn)題,但后來(lái)看了看置頂?shù)奶影l(fā)現(xiàn)有大神弄過(guò)了而我對(duì)這兩東西又恰好不是很熟(我是沒(méi)上過(guò)第一季的)……
//于是乎,心血來(lái)潮就想看看不用那種方法的話有沒(méi)有其他路子!
//結(jié)果吧,寫(xiě)是寫(xiě)出來(lái)了,可自己都覺(jué)得好長(zhǎng),肯定是有得改進(jìn)的地方,不過(guò)這些待我睡一覺(jué)起來(lái)再說(shuō)啊哈哈哈……關(guān)于后面那部分堅(jiān)決不使用arrayList的方法就請(qǐng)多指教啦,腦子笨目前只想出這種方式:(,但我想應(yīng)該還有很多其他解決方法
//以及,我并沒(méi)有使用interface,因?yàn)橛X(jué)得似乎沒(méi)什么必要
//===============關(guān)于想?yún)⒖嫉耐瑢W(xué),看代碼前請(qǐng)看這兒================
//我重寫(xiě)了toString()這個(gè)函數(shù),很驚訝很多人都沒(méi)想到重寫(xiě)這個(gè)(至少我看了幾個(gè)帖子都沒(méi)有),我想這是我蹩腳作業(yè)的唯一可取處了。
//同時(shí),使用了exception,但這個(gè)是看置頂大神貼里面的改進(jìn)以后自己查了查用法隨意加的,也許有些語(yǔ)法錯(cuò)誤或者可以更加完善的地方,因此不建議復(fù)制粘貼
//另外,目前這個(gè)版本的話,不喜歡自虐的朋友們最好別去用我最后那幾個(gè)笨笨的for,那是很燒效率不討好的行為:)(再加上我自己測(cè)試時(shí)還發(fā)覺(jué)有個(gè)不算bug的小bug……總之請(qǐng)千萬(wàn)別借鑒那部分)
//UlyXor,2015/1/30
public?abstract?class?Car
{
//Field
protected?String?name;
protected?int?price;
protected?int?carID;
//Mutator?&?Accessor
public?void?setName(String?name)
{
this.name=name;
}
public?void?setID(int?num)
{
this.carID=num;
}
public?void?setPrice?(int?price)
{
this.price=price;
}
public?String?getName()
{
return?this.name;
}
public?int?getPrice()
{
return?this.price;
}
public?int?getID()
{
return?this.carID;
}
}
public?class?PassengerCar?extends?Car
{
//Field
private?int?numOfPassenger;
//Constructor
public?PassengerCar(int?id,String?name,int?price,int?numOfPassenger)
{
super.setName(name);
super.setPrice(price);
super.setID(id);
this.numOfPassenger=numOfPassenger;
}
//Mutator?&?Accessor
public?int?getPassenger()
{
return?this.numOfPassenger;
}
public?void?setPassenger(int?num)
{
this.numOfPassenger=num;
}
//Utility
public?String?toString()
{
return?this.carID+"\t"+this.name+"\t"+this.price+"元/天"+"\t載客:"+this.numOfPassenger+"人";
}
}
public?class?CargoCar?extends?Car
{
//Field
private?int?tonOfCargo;
//Constructor
public?CargoCar(int?id,String?name,int?price,int?tonOfCargo)
{
super.setName(name);
super.setPrice(price);
this.setID(id);
this.tonOfCargo=tonOfCargo;
}
//Mutator?&?Accessor
public?int?getCargo()
{
return?this.tonOfCargo;
}
public?void?setCargo(int?num)
{
this.tonOfCargo=num;
}
//Utility
public?String?toString()
{
return?this.carID+"\t"+this.name+"\t"+this.price+"元/天"+"\t載貨:"+this.tonOfCargo+"噸";
}
}
public?class?PickUpCar?extends?Car
{
//Field
private?int?numOfPassenger;
private?int?tonOfCargo;
//Constructor
public?PickUpCar(int?id,String?name,int?price,int?numOfPassenger,int?tonOfCargo)
{
super.setName(name);
super.setPrice(price);
this.setID(id);
this.numOfPassenger=numOfPassenger;
this.tonOfCargo=tonOfCargo;
}
//Mutator?&?Accessor
public?int?getPassenger()
{
return?this.numOfPassenger;
}
public?void?setPassenger(int?num)
{
this.numOfPassenger=num;
}
public?int?getCargo()
{
return?this.tonOfCargo;
}
public?void?setCargo(int?num)
{
this.tonOfCargo=num;
}
//Utility
public?String?toString()
{
return?this.carID+"\t"+this.name+"\t"+this.price+"元/天"+"\t載客:"+this.numOfPassenger+"人"+"?載貨:"+this.tonOfCargo+"噸";
}
}
import?java.util.*;
public?class?Tester{
public?static?void?main(String?[]?args)
{
//①確認(rèn)是否租車(chē)
??Scanner?keybo=new?Scanner(System.in);
??System.out.println("歡迎使用答答租車(chē)系統(tǒng):");
??????System.out.println("您是否要租車(chē)?是=1,否=0");
??int?ifHire=-1;
??do
??{
??//當(dāng)用戶開(kāi)始搗亂時(shí)……
?try{
?ifHire=keybo.nextInt();
? }
? catch(Exception?e)
? {
//當(dāng)用戶用字母搗亂時(shí)
System.out.println("輸入錯(cuò)誤,請(qǐng)輸入或0或1的數(shù)字。");
keybo.next();
continue;
}
?? //當(dāng)用戶用其他數(shù)字搗亂時(shí)
?? if(ifHire<0||ifHire>1)
?? {
?? System.out.println("輸入錯(cuò)誤,請(qǐng)用1或0來(lái)表達(dá)您的意愿。");
?? }
??}while(ifHire!=1&&ifHire!=0);
??if(ifHire==0)
??{
??System.out.println("一百塊都不給我,系統(tǒng)自動(dòng)退出:(。");
??return;
??}
??//②展示可租車(chē)輛
??else
??{
??System.out.println("您可租車(chē)的類(lèi)型及其價(jià)目表:");
??System.out.println("序號(hào) 名稱(chēng) 租金 容量");
??Car?carAvaliable[]=
??{
??new?PassengerCar(1,"奧迪A4",500,4),
??new?PassengerCar(2,"馬自達(dá)6",400,4),
??new?PickUpCar(3,"皮卡雪6",450,4,2),
??new?PassengerCar(4,"金龍",800,20),
??new?CargoCar(5,"松花江",400,4),
??new?CargoCar(6,"依維柯",1000,20)
??};
for(int?i=0;i<6;i++)
{
??System.out.println(carAvaliable[i]);
?? }
//③?確定用戶需要的車(chē)的數(shù)量以及它們的序號(hào)
System.out.println("請(qǐng)輸入您要租的汽車(chē)的數(shù)量:");
int?numOfCar=0;
while(numOfCar<=0)
??{
??//當(dāng)用戶開(kāi)始搗亂時(shí)……
?try{
?numOfCar=keybo.nextInt();
? }
? catch(Exception?e)
? {
//當(dāng)用戶用字母搗亂時(shí)
System.out.println("輸入錯(cuò)誤,請(qǐng)輸入一個(gè)數(shù)字。");
keybo.next();
continue;
}
?? //當(dāng)用戶用數(shù)字搗亂時(shí)
?? if(numOfCar<=0)
?? {
?? System.out.println("輸入錯(cuò)誤,請(qǐng)輸入一個(gè)大于等于1的數(shù)字。");
?? }
?? }
int?totalDay=0;
int[]?theIndex=?new?int[numOfCar];
?? for(int?i?=?0?;i<numOfCar;i++)
?? {
System.out.println("請(qǐng)輸入第"+(i+1)+"輛車(chē)的序號(hào)");
while(theIndex[i]<=0||theIndex[i]>6)
??{
??//當(dāng)用戶開(kāi)始搗亂時(shí)……
?try{
?theIndex[i]=keybo.nextInt();
}
catch(Exception?e)
{
//當(dāng)用戶用字母搗亂時(shí)
System.out.println("輸入錯(cuò)誤,請(qǐng)輸入一個(gè)數(shù)字。");
keybo.next();
continue;
}
//當(dāng)用戶用數(shù)字搗亂時(shí)
if(theIndex[i]<=0||theIndex[i]>6)
{
System.out.println("輸入錯(cuò)誤,請(qǐng)輸入一個(gè)大于等于1,小于等于6的數(shù)字。");
}
}
}
System.out.println("請(qǐng)輸入租車(chē)天數(shù):");
while(totalDay<=0)
??{
??//當(dāng)用戶開(kāi)始搗亂時(shí)……
?try{
?totalDay=keybo.nextInt();
}
catch(Exception?e)
{
//當(dāng)用戶用字母或小數(shù)搗亂時(shí)
System.out.println("輸入錯(cuò)誤,請(qǐng)輸入一個(gè)整形數(shù)字。");
keybo.next();
continue;
}
//當(dāng)用戶用數(shù)字搗亂時(shí)
if(totalDay<=0)
{
System.out.println("輸入錯(cuò)誤,請(qǐng)輸入一個(gè)大于等于1的數(shù)字。");
}
}
//④計(jì)算總金額
int?totalPrice=0;
for(int?i?=?0?;i<numOfCar;i++)
{
switch(theIndex[i])
{
case?1:
totalPrice+=?carAvaliable[0].getPrice();
break;
case?2:
totalPrice+=?carAvaliable[1].getPrice();
break;
case?3:
totalPrice+=?carAvaliable[2].getPrice();
break;
case?4:
totalPrice+=?carAvaliable[3].getPrice();
break;
case?5:
totalPrice+=?carAvaliable[4].getPrice();
break;
case?6:
totalPrice+=?carAvaliable[5].getPrice();
break;
}
}
totalPrice?*=?totalDay;
?//⑤最終結(jié)果
int?totalPassenger=0,?totalTons=0;
System.out.println("您的賬單:");
System.out.print("**可載人的車(chē)有:");
for(int?i?=?0?;i<numOfCar;i++)
{
switch(theIndex[i])
{
case?1:
System.out.print("?"+?carAvaliable[0].getName()+totalPassenger);
totalPassenger+=?((PassengerCar)carAvaliable[0]).getPassenger();
break;
case?2:
System.out.print("?"+?carAvaliable[1].getName());
totalPassenger+=?((PassengerCar)carAvaliable[1]).getPassenger();
break;
case?3:
System.out.print("?"+?carAvaliable[2].getName());
totalPassenger+=?((PickUpCar)carAvaliable[2]).getPassenger();
break;
case?4:
System.out.print("?"+?carAvaliable[3].getName());
totalPassenger+=?((PassengerCar)carAvaliable[3]).getPassenger();
break;
default:
break;
}
}
System.out.println("\t共載人:"+totalPassenger+"人");
System.out.print("**可載貨的車(chē)有:");
for(int?i?=?0?;i<numOfCar;i++)
{
switch(theIndex[i])
{
case?3:
System.out.print("?"+?carAvaliable[2].getName());
totalTons+=?((PickUpCar)carAvaliable[2]).getCargo();
break;
case?5:
System.out.print("?"+?carAvaliable[4].getName());
totalTons+=?((CargoCar)carAvaliable[4]).getCargo();
break;
case?6:
System.out.print("?"+?carAvaliable[5].getName());
totalTons+=?((CargoCar)carAvaliable[5]).getCargo();
break;
default:
break;
}
}
System.out.println("\t共載物:"+totalTons+"噸");
System.out.println("**租車(chē)總價(jià)格:"+totalPrice+"元");
?}
}
}
2015-02-09
方法太煩了