/*
噠噠租車系統
*/
import?java.util.*;
abstract?class?Car //車
{
private?int?id,rent;
private?String?name;
Car(int?id,String?name,int?rent){
this.id=id;
this.name=name;
this.rent=rent;
}
public?int?getId() //車的ID
{
return?this.id;
}
public?String?getName() //車的名字
{
return?this.name;
}
public?int?getRent() //獲取價格
{
return?this.rent;
}
abstract?public?String?Show();
}
/*載人的汽車*/
class?MannedCar?extends?Car
{
private?int?capacity; //可載人數
MannedCar(int?id,String?name,int?rent,int?capacity)
{
super(id,name,rent);
this.capacity=capacity;
}
public?int?getCapacity()
{
return?capacity;
}
public?String?Show()
{
return?"載人:"+capacity+"人";
}
}
/*載貨的汽車*/
class?CargoCar?extends?Car
{
private?int?weight; //貨車載重量
public?CargoCar(int?id,String?name,int?rent,int?weight)
{
super(id,name,rent);
this.weight=weight;
}
public?int?getWeight()
{
return?weight;
}
public?String?Show()
{
return?"載貨:"+weight+"噸";
}
}
/*皮卡*/
class?Pickup?extends?MannedCar //繼承載人的汽車類
{
private?int?weight; //添加一個載重
public?Pickup(int?id,String?name,int?rent,int?capacity,int?weight)
{
super(id,name,rent,capacity);
this.weight=weight;
}
public?int?getWeight()
{
return?this.weight;
}
public?String?Show()
{
return?"載人:"+getCapacity()+"人"+"?載貨:"+this.weight+"噸";
}
}
public?class?Demo
{
public?static?void?main(String[]?args)
{
int?price=0,tWeight=0,tCapacity=0; //總價,總載重,總人數
Car[]?list={new?MannedCar(1,"奧迪A4",500,4),new?MannedCar(2,"馬自達6",400,4),
new?Pickup(3,"皮卡雪6",450,4,2),new?MannedCar(4,"金龍",800,20),
new?CargoCar(5,"松花江",400,4),new?CargoCar(6,"依維柯",1000,20)}; //價目表
Scanner?io=new?Scanner(System.in);
System.out.println("歡迎使用噠噠租車系統:");
System.out.println("您是否要租車:1是?0否");
int?n=io.nextInt();
if(n==1)
{
System.out.println("您可租車的類型及其價目表:");
System.out.println("序號\t汽車名稱?租金\t\t容量");
for(Car?i:list) //輸出價目表
System.out.println(i.getId()+".\t"+i.getName()+"\t?"+i.getRent()
+"元/天\t"+i.Show());
System.out.println("請輸入您要租汽車的數量:");
n=io.nextInt();
String[]?manned=new?String[n]; //存儲載人汽車名
String[]?cargo=new?String[n]; //存儲載貨汽車名
int?cCount=0,mCount=0;
for(int?j=1;j<=n;j++)
{
System.out.println("請輸入第"+j+"輛車的序號");
int?num=io.nextInt();
//遍歷汽車類數組
for(Car?i:list)
{ //比較輸入的汽車序號,找到對應的汽車對象
if(num==i.getId())
{ //instanceof判斷i對象是否屬于右邊的類
if(i?instanceof?Pickup)
{
tWeight+=((Pickup)i).getWeight();
tCapacity+=((Pickup)i).getCapacity();
manned[mCount++]=i.getName();
cargo[cCount++]=i.getName();
}
else?if(i?instanceof?CargoCar)
{
tWeight+=((CargoCar)i).getWeight();
cargo[cCount++]=i.getName();
}
else
{
tCapacity+=((MannedCar)i).getCapacity();
manned[mCount++]=i.getName();
}
price+=i.getRent();
}
}
}
System.out.println("請輸入租車天數:");
n=io.nextInt();
price*=n;
System.out.println("您的賬單:");
System.out.println("***可載人的車有:");
for(int?i=0;i<mCount;i++)
System.out.print(manned[i]+"?");
System.out.println("\t共載人:"+tCapacity+"人");
System.out.println("***可載貨的車有:");
for(int?i=0;i<cCount;i++)
System.out.print(cargo[i]+"?");
System.out.println("\t共載貨:"+tWeight+"噸");
System.out.println("***租車總價格:"+price+"元");
}
}
}
2016-01-03
Car中可以直接增加噸位、座位就通用了:
/*
?* 車類
?*/
public abstract class Car
{
? ? private int id,rent;
? ? private String name;
? ? private int seat;
? ? private float ton;
? ? Car(int id, String name, float ton, int seat, int rent){
? ? ? ? this.id=id;
? ? ? ? this.name=name;
? ? ? ? this.rent=rent;
? ? ? ? this.ton=ton;
? ? ? ? this.seat=seat;
? ? }
? ? public int getId() ? ?//車的ID
? ? {
? ? ? ? return this.id;
? ? }
? ? public String getName() ? //車的名字
? ? {
? ? ? ? return this.name;
? ? }
? ? public int getRent() ?//獲取價格
? ? {
? ? ? ? return this.rent;
? ? }
? ? public float getTon()
? ? {
? ? return this.ton;
? ? }
? ? public float getSeat()
? ? {?
? ? return this.seat;
? ? }
? ? abstract public String Show();
}
2016-01-03
將Car中的import java.util.*; 刪除,增加到Demo.java開首行