package?com.imooc.item;
/**
?*?汽車類
?*?@author?MeRos
?*
?*/
public?class?Car?{
public?int?rent;
public?String?name;
public?int?getRent()?{
return?rent;
}
public?void?setRent(int?rent)?{
this.rent?=?rent;
}
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
}
package?com.imooc.item;
/**
?*?載客車類
?*?
?*?@author?MeRos
?*
?*/
public?class?PassagerCar?extends?Car?{
private?int?peopleCapacity;
public?PassagerCar(String?name,?int?rent,?int?peopleCapacity)?{
this.name?=?name;
this.rent?=?rent;
this.peopleCapacity?=?peopleCapacity;
}
public?int?getPeopleCapacity()?{
return?peopleCapacity;
}
public?void?setPeopleCapacity(int?peopleCapacity)?{
this.peopleCapacity?=?peopleCapacity;
}
}
package?com.imooc.item;
/**
?*?載貨車類
?*?
?*?@author?MeRos
?*
?*/
public?class?Trunk?extends?Car?{
private?int?cargoCapacity;
public?Trunk(String?name,?int?rent,?int?cargoCapacity)?{
this.name?=?name;
this.rent?=?rent;
this.cargoCapacity?=?cargoCapacity;
}
public?int?getCargoCapacity()?{
return?cargoCapacity;
}
public?void?setCargoCapacity(int?cargoCapacity)?{
this.cargoCapacity?=?cargoCapacity;
}
}
package?com.imooc.item;
/**
?*?皮卡車類
?*?
?*?@author?MeRos
?*
?*/
public?class?Pickup?extends?Car?{
private?int?cargoCapacity;
private?int?peopleCapacity;
public?Pickup(String?name,?int?rent,?int?peopleCapacity,?int?cargoCapacity)?{
this.name?=?name;
this.rent?=?rent;
this.peopleCapacity?=?peopleCapacity;
this.cargoCapacity?=?cargoCapacity;
}
public?int?getCargoCapacity()?{
return?cargoCapacity;
}
public?void?setCargoCapacity(int?cargoCapacity)?{
this.cargoCapacity?=?cargoCapacity;
}
public?int?getPeopleCapacity()?{
return?peopleCapacity;
}
public?void?setPeopleCapacity(int?peopleCapacity)?{
this.peopleCapacity?=?peopleCapacity;
}
}
package?com.imooc.item;
import?java.util.Scanner;
public?class?Test?{
public?static?void?main(String[]?args)?{
double?totalMoney?=?0;?//?汽車總單價
int?totalPerson?=?0;?//?總載人數(shù)
double?totalCargo?=?0;?//?總載貨量
StringBuffer?perMessage?=?new?StringBuffer();?//?動態(tài)字符串數(shù)組存儲載人的車輛名
StringBuffer?cargoMessage?=?new?StringBuffer();?//?動態(tài)字符串數(shù)組存儲載物的車輛名
Car[]?cars?=?{?new?PassagerCar("奧迪A4",?500,?4),
new?PassagerCar("馬自達6",?400,?4),?new?Pickup("皮卡雪",?450,?4,?2),
new?PassagerCar("金龍",?800,?20),?new?Trunk("松花江",?400,?4),
new?Trunk("依維柯",?1000,?20)?};
System.out.println("歡迎使用嘀嘀租車系統(tǒng):");
System.out.println("您是否要租車:1.是??0.否");
Scanner?input?=?new?Scanner(System.in);
int?isNot?=?input.nextInt();
if?(isNot?==?1)?{
System.out.println("您可租車的類型及其價目表:");
System.out.println("序號\t汽車名稱\t租金\t\t容量");
int?i?=?1;
for?(Car?currentCar?:?cars)?{?//?遍歷輸出所有車輛信息
if?(currentCar?instanceof?PassagerCar)?{
System.out.println(i?+?"."?+?"\t"?+?currentCar.getName()
+?"\t"?+?currentCar.getRent()?+?"元/天?"?+?"\t"
+?"載人:"
+?((PassagerCar)?currentCar).getPeopleCapacity()
+?"人");
}
if?(currentCar?instanceof?Trunk)?{
System.out.println(i?+?"."?+?"\t"?+?currentCar.getName()
+?"\t"?+?currentCar.getRent()?+?"元/天?"?+?"\t"
+?"載貨:"?+?((Trunk)?currentCar).getCargoCapacity()
+?"噸");
}
if?(currentCar?instanceof?Pickup)?{
System.out.println(i?+?"."?+?"\t"?+?currentCar.getName()
+?"\t"?+?currentCar.getRent()?+?"元/天?"?+?"\t"
+?"載人:"?+?((Pickup)?currentCar).getPeopleCapacity()
+?"人?載貨:"
+?((Pickup)?currentCar).getCargoCapacity()?+?"噸");
}
i++;
}
System.out.println("請輸入您要租汽車的數(shù)量:");
int?num?=?input.nextInt();
for?(int?j?=?0;?j?<?num;?j++)?{
System.out.println("請輸入第"?+?(j?+?1)?+?"輛車的序號:");
int?serialNum?=?input.nextInt();
boolean?checkNum?=?false;
while?(!checkNum)?{
for?(int?k?=?0;?k?<?cars.length;?k++)?{
if?(serialNum?==?k?+?1)?{
checkNum?=?true;
break;
}
}
if?(!checkNum)?{
System.out.println("您輸入的序號不存在,請重新輸入:");
serialNum?=?input.nextInt();
}
}
serialNum?=?serialNum?-?1;
//?獲取不同車型的相關(guān)信息
if?(cars[serialNum]?instanceof?PassagerCar)?{
totalPerson?+=?((PassagerCar)?cars[serialNum])
.getPeopleCapacity();
totalMoney?+=?((PassagerCar)?cars[serialNum]).getRent();
perMessage.append(cars[serialNum].getName()?+?"?");
}
if?(cars[serialNum]?instanceof?Trunk)?{
totalCargo?+=?((Trunk)?cars[serialNum]).getCargoCapacity();
totalMoney?+=?((Trunk)?cars[serialNum]).getRent();
cargoMessage.append(cars[serialNum].getName()?+?"?");
}
if?(cars[serialNum]?instanceof?Pickup)?{
totalPerson?+=?((Pickup)?cars[serialNum])
.getPeopleCapacity();
totalCargo?+=?((Pickup)?cars[serialNum]).getCargoCapacity();
totalMoney?+=?((Pickup)?cars[serialNum]).getRent();
perMessage.append(cars[serialNum].getName()?+?"?");
cargoMessage.append(cars[serialNum].getName()?+?"?");
}
}
System.out.println("請輸入租車天數(shù):");
int?days?=?input.nextInt();
System.out.println("您的賬單:");
System.out.println("**可載人的車有:");
System.out.println(perMessage?+?"\t共載人:"?+?totalPerson?+?"人");
System.out.println("**可載貨的車有:");
System.out.println(cargoMessage?+?"\t共載貨:"?+?totalCargo?+?"噸");
System.out.println("**租車總價格:"?+?(totalMoney?*?days)?+?"元");
}
}
}
2015-09-24
做的很不錯。我只做了一部分 ╮(╯▽╰)╭。
2015-08-15
在哪可以找到Java界面編程的視頻?