package project;
public abstract class car {
public String name;
public int price;
public float peoplevalue;
public float cargovalue;
public int flag;//标记车型0:people 1:cargo 2:pick-up
public int getprice(){
return price;
};
public String getname(){
return name;
}
public float getpeoplevalue(){
return peoplevalue;
}
public float getcargovalue(){
return cargovalue;
}
}
package project;
public class CargoCar extends car {
public CargoCar(int flag,String name,int price,float cargovalue ){
this.flag=1;
this.name=name;
this.price=price;
this.cargovalue=cargovalue;
}
public float getcargovalue(){
return cargovalue;}
public int getprice(){
return price;
}
public String getname(){
return name;
}
}
package project;
public class PeopleCar extends car{
public PeopleCar(int flag,String name,int price,float peoplevalue){
this.flag=0;
this.name=name;
this.price=price;
this.peoplevalue=peoplevalue;}
public int getprice(){
return price;
}
public float getpeoplevalue(){
return peoplevalue;
}
public String getname(){
return name;
}
}
package project;
public class PickUp extends car{
public PickUp(int flag,String name,int price,float cargovalue,float peoplevalue){
this.flag=2;
this.name=name;
this.price=price;
this.cargovalue=cargovalue;
this.peoplevalue=peoplevalue;
}
public int getprice(){
return price;
};
public String getname(){
return name;
}
public float getpeoplevalue(){
return peoplevalue;
}
public float getcargovalue(){
return cargovalue;
}
}
package project;
import java.util.*;
public class Test {
public static void main(String[]args){
System.out.println("---------欢迎使用答答租车系统--------");
System.out.println("您是否要租车:1 -是 0- 否");
Scanner input=new Scanner(System.in);
int a=input.nextInt();
if (a==0){
System.out.println("欢迎下次使用");
}else if(a!=0&&a!=1){
System.out.println("请输入有效数字");}
else if(a==1){
car cars[]={new PeopleCar(0,"奥迪A4",500,4),
new PeopleCar(0,"马自达6",400,4),
new PickUp(2,"皮卡雪6",450,4,2),
new PeopleCar(0,"金龙",800,20),
new CargoCar(1,"松花江",400,4),
new CargoCar(1,"依维柯",1000,20),
};
System.out.println("您可租车的类型及价目表:");
System.out.println("序号\t汽车名称\t租金\t容量");
int num;
for(int i=0;i<6;i++){
num=i+1;
if (cars[i].flag==0){
System.out.println(num+"\t"+cars[i].name+"\t"+cars[i].price+"元/天\t载人:"+cars[i].peoplevalue+"人");
}
else if (cars[i].flag==1){
System.out.println(num+"\t"+cars[i].name+"\t"+cars[i].price+"元/天\t载货:"+cars[i].cargovalue+"吨");
}
else {
System.out.println(num+"\t"+cars[i].name+"\t"+cars[i].price+"元/天\t载人:"+cars[i].peoplevalue+"人\t载货:"
+cars[i].cargovalue+"吨");
}
}
System.out.println("请输入您本次要租车的数量:");
int b=input.nextInt();
car[] carlist=new car[6];
for(int k=1;k<=b;k++)
{System.out.println("请输入第"+k+"辆车的序号");
int carnumber=input.nextInt();
carlist[k-1]=cars[carnumber-1];
}
System.out.println("请输入租车天数:");
int day=input.nextInt();
System.out.println("您的账单:");
int price=0;
float peoplevalue=0;
float cargovalue=0;
for(int j=0;j
if (carlist[j] instanceof PeopleCar){
PeopleCar car=(PeopleCar)carlist[j];
price=price+car.getprice()*day;
peoplevalue=peoplevalue+car.getpeoplevalue();
}
if(carlist[j] instanceof CargoCar){
CargoCar car=(CargoCar)carlist[j];
price=price+car.getprice()*day;
cargovalue=cargovalue+car.getcargovalue();
}
if(carlist[j] instanceof PickUp){
PickUp car=(PickUp)carlist[j];
price=price+car.getprice()*day;
peoplevalue=peoplevalue+car.getpeoplevalue();
cargovalue=cargovalue+car.getcargovalue();
}
}
System.out.println("***可载人的车有:");
for(int l=0;l
if (carlist[l] instanceof PeopleCarcarlist[l] instanceof PickUp){
System.out.print(carlist[l].getname()+"\t");
}
}
System.out.println("共载人:"+peoplevalue+"人");
System.out.println("***可载货的车有;");
for(int m=0;m
if(carlist[m] instanceof CargoCarcarlist[m] instanceof PickUp){
System.out.print(carlist[m].getname()+"\t");
}
}
System.out.println("共载货:"+cargovalue+"吨");
System.out.println("***租车总价格:"+price+"元");
}
}
}
以下为运行结果
---------欢迎使用答答租车系统--------
您是否要租车:1 -是 0- 否
1
您可租车的类型及价目表:
序号 汽车名称 租金 容量
1 奥迪A4 500元/天 载人:4.0人
2 马自达6 400元/天 载人:4.0人
3 皮卡雪6 450元/天 载人:2.0人 载货:4.0吨
4 金龙 800元/天 载人:20.0人
5 松花江 400元/天 载货:4.0吨
6 依维柯 1000元/天 载货:20.0吨
请输入您本次要租车的数量:
4
请输入第1辆车的序号
1
请输入第2辆车的序号
3
请输入第3辆车的序号
4
请输入第4辆车的序号
5
请输入租车天数:
5
您的账单:
可载人的车有:
奥迪A4 皮卡雪6 金龙 共载人:26.0人可载货的车有;
皮卡雪6 松花江 共载货:8.0吨
***租车总价格:10750元