/*
哒哒租车系统
*/
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+"元");
}
}
}