public??class?Car?{
private?int?carNumber;
private?String?carName;
private?int?carRentMoney;
public?int?getCarNumber()?{
return?carNumber;
}
public?void?setCarNumber(int?carNumber)?{
this.carNumber?=?carNumber;
}
public?String?getCarName()?{
return?carName;
}
public?void?setCarName(String?carName)?{
this.carName?=?carName;
}
public?int?getCarRentMoney()?{
return?carRentMoney;
}
public?void?setCarRentMoney(int?carRentMoney)?{
this.carRentMoney?=?carRentMoney;
}
}
public?class?passengerCar?extends?Car?{
private?int?CarCapacity;
public?passengerCar(int?carNumber,String?carName,int?carRentMoney,int?carCapacity){
this.setCarNumber(carNumber);
this.setCarName(carName);
this.setCarRentMoney(carRentMoney);
CarCapacity?=?carCapacity;
}
public?int?getCarCapacity()?{
return?CarCapacity;
}
public?void?setCarCapacity(int?carCapacity)?{
CarCapacity?=?carCapacity;
}
}
public?class?pickUp?extends?Car?{
private?int?CarCapacity;
private?int?CarCarry;
public?pickUp(int?carNumber,String?carName,int?carRentMoney,int?CarCapacity,int?CarCarry){
this.setCarNumber(carNumber);
this.setCarName(carName);
this.setCarRentMoney(carRentMoney);
this.CarCapacity?=?CarCapacity;
this.CarCarry?=?CarCarry;
}
public?int?getCarCapacity()?{
return?CarCapacity;
}
public?void?setCarCapacity(int?carCapacity)?{
CarCapacity?=?carCapacity;
}
public?int?getCarCarry()?{
return?CarCarry;
}
public?void?setCarCarry(int?carCarry)?{
CarCarry?=?carCarry;
}
}
public?class?trunkCar?extends?Car?{
private?int?CarCarry;
public?trunkCar(int?carNumber,String?carName,int?carRentMoney,int?CarCarry){
this.setCarNumber(carNumber);
this.setCarName(carName);
this.setCarRentMoney(carRentMoney);
this.CarCarry?=?CarCarry;
}
public?int?getCarCarry()?{
return?CarCarry;
}
public?void?setCarCarry(int?carCarry)?{
CarCarry?=?carCarry;
}
}
import?java.util.Scanner;
public?class?Test?{
/**
?*?@param?args
?*/
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
System.out.println("歡迎使用答答租車系統(tǒng):");
System.out.println("你是否要租車???1是????2否");
Scanner?s?=?new?Scanner(System.in);
int??k?=?s.nextInt();
if(k?==?1){
System.out.println("您可租車的類型及其價(jià)格表:");
Car?RentCar[]?=?{new?passengerCar(1,"奧迪A4",500,4),new?passengerCar(2,"馬自達(dá)6",400,4),new?pickUp(3,"皮卡雪6",450,4,2),new?passengerCar(4,"金龍",800,20),new?trunkCar(5,"松花江",400,4),new?trunkCar(6,"依維柯",1000,20)};
System.out.println("序號(hào)"+"\t"+"汽車名稱"+"\t"+"\t"+"租金"+"\t"+"容量");
for(int?i?=?0;?i?<?6;?i++){
////若RentCar[i]它是客車類型的實(shí)例,則打印出客車應(yīng)該有的屬性值,這里注意強(qiáng)轉(zhuǎn),否則不會(huì)出現(xiàn)
if(RentCar[i]?instanceof?passengerCar){
System.out.println(RentCar[i].getCarNumber()+"\t"+RentCar[i].getCarName()+"\t"+"\t"+RentCar[i].getCarRentMoney()+"/天"+"\t"+"載人:"+((passengerCar)RentCar[i]).getCarCapacity()+"人");//將Car類型強(qiáng)制轉(zhuǎn)換成passengerCar類,得到passegerCard的屬性
}
if(RentCar[i]?instanceof?pickUp){
System.out.println(RentCar[i].getCarNumber()+"\t"+RentCar[i].getCarName()+"\t"+"\t"+RentCar[i].getCarRentMoney()+"/天"+"\t"+"載人:"+((pickUp)RentCar[i]).getCarCapacity()+"人??載貨:"+((pickUp)RentCar[i]).getCarCarry()+"噸");
}
if(RentCar[i]?instanceof?trunkCar){
System.out.println(RentCar[i].getCarNumber()+"\t"+RentCar[i].getCarName()+"\t"+"\t"+RentCar[i].getCarRentMoney()+"/天"+"\t"+"載人:"+((trunkCar)RentCar[i]).getCarCarry()+"噸");
}
}
System.out.println("請(qǐng)輸入您要租汽車的數(shù)量:");
//已經(jīng)有Scanner對(duì)象,直接調(diào)用
int?totalNum?=?s.nextInt();
int?sum?=?0;
for(int?j?=?1;?j?<=?totalNum;?j++){
System.out.println("請(qǐng)輸入第"+j+"輛車的序號(hào)");
int?q?=?s.nextInt();
switch(q){
case?1:
?RentCar[0].getCarRentMoney();
case?2:
RentCar[1].getCarRentMoney();
case?3:
RentCar[2].getCarRentMoney();
case?4:
RentCar[3].getCarRentMoney();
case?5:
RentCar[4].getCarRentMoney();
case?6:
RentCar[5].getCarRentMoney();
}
sum?=?sum?+?RentCar[q-1].getCarRentMoney();
}
System.out.println("請(qǐng)輸入租車的天數(shù):");
int?day?=?s.nextInt();
System.out.println("您的賬單:");
System.out.println(sum?*?day);
}
}
}
2015-12-23
怎么截圖的?。繉懙貌诲e(cuò)!