您的位置:首页 > 其它

答答租车系统(面向对象综合练习)

2017-04-20 22:05 288 查看


答答租车系统(面向对象综合练习)

Time Limit: 1000MS Memory Limit: 65536KB

Submit Statistic Discuss


Problem Description

各位面向对象的小伙伴们,在学习了面向对象的核心概念——类的封装、继承、多态之后,答答租车系统开始营运了。
请你充分利用面向对象思想,为公司解决智能租车问题,根据客户选定的车型和租车天数,来计算租车费用,最大载客人数,最大载载重量。
公司现有三种车型(客车、皮卡车、货车),每种车都有名称和租金的属性;其中:客车只能载人,货车只能载货,皮卡车是客货两用车,即可以载人,也可以载货。
下面是答答租车公司的可用车型、容量及价目表:

序号     名称     载客量      载货量        租金

                           (人)     (吨)    (元/天)

  1          A            5                                 800

  2          B            5                                 400

  3          C            5                                 800

  4          D            51                             1300

  5          E            55                             1500

  6          F             5            0.45             500

  7         G             5             2.0               450

  8         H                            3                  200

  9          I                             25              1500

 10        J                             35              2000

要求:根据客户输入的所租车型的序号及天数,计算所能乘载的总人数、货物总数量及租车费用总金额。


Input

首行是一个整数:代表要不要租车 1——要租车(程序继续),0——不租车(程序结束);
第二行是一个整数,代表要租车的数量N;
接下来是N行数据,每行2个整数m和n,其中:m表示要租车的编号,n表示租用该车型的天数。


Output

若成功租车,则输出一行数据,数据间有一个空格,含义为:
载客总人数 载货总重量(保留2位小数) 租车金额(整数)
若不租车,则输出: 
0 0.00 0(含义同上)


Example Input

1
2
1 1
2 2



Example Output

15 0.00 1600

import java.util.Scanner;

class Car
{
protected String name;
protected int rent;

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public int getRent()
{
return rent;
}

public void setRent(int rent)
{
this.rent = rent;
}
}

class PassengerCar extends Car
{
private double peopleCapacity;

public PassengerCar(String name, int rent, double peoplecapacity)
{
this.name = name;
this.rent = rent;
this.peopleCapacity = peoplecapacity;
}

public double getPeopleCapacity()
{
return peopleCapacity;
}

public void setPeopleCapacity(double peopleCapacity)
{
this.peopleCapacity = peopleCapacity;
}

}

class PickUp extends Car
{
private double cargoCapacity;
private int peopleCapacity;

public PickUp(String name, int rent, int peopleCapacity,
double cargoCapacity)
{
this.name = name;
this.rent = rent;
this.cargoCapacity = cargoCapacity;
this.peopleCapacity = peopleCapacity;
}

public double getCargoCapacity()
{
return cargoCapacity;
}

public void setCargoCapacity(double cargoCapacity)
{
this.cargoCapacity = cargoCapacity;
}

public int getPeopleCapacity()
{
return peopleCapacity;
}

public void setPeopleCapacity(int peopleCapacity)
{
this.peopleCapacity = peopleCapacity;
}
}

class Trunk extends Car
{
private double cargoCapacity;

public Trunk(String name, int rent, double cargoCapacity)
{
this.name = name;
this.rent = rent;
this.cargoCapacity = cargoCapacity;
}

public double getCargoCapacity()
{
return cargoCapacity;
}

public void setCargoCapacity(double cargoCapacity)
{
this.cargoCapacity = cargoCapacity;
}

}

public class Main
{
public static void main(String[] args)
{
Car[] carsForRent=
{
new PassengerCar("A",800,5),
new PassengerCar("B", 400, 5),
new PassengerCar("C", 800, 5),
new PassengerCar("D",1300, 51),
new PassengerCar("E", 1500, 55),
new PickUp("F", 500,5, 0.45),
new PickUp("G", 450,5, 2.0),
new Trunk("H",200, 3),
new Trunk("I", 1500, 25),
new Trunk("J", 2000, 35)
};
Scanner scan=new Scanner(System.in);
int input=scan.nextInt();
int totalPeopley=0; //载客总人数
double totalCargo=0; //载货总重量
int totalMoney=0; //租金总额
if(input==1)
{
int rentNum=scan.nextInt();
int[] carsId=new int[rentNum]; //租车 Id
int[] days=new int[rentNum]; //租车天数
for(int j=0; j<rentNum; j++)
{
carsId[j]=scan.nextInt();
days[j]=scan.nextInt();
}
for(int j=0; j<rentNum; j++)
{
totalMoney+=carsForRent[carsId[j]-1].getRent()*days[j];
if(carsForRent[carsId[j]-1] instanceof PassengerCar)
{
totalPeopley+=((PassengerCar)carsForRent[carsId[j]-1]).getPeopleCapacity()*days[j];
}
if(carsForRent[carsId[j]-1] instanceof PickUp)
{

totalPeopley+=((PickUp)carsForRent[carsId[j]-1]).getPeopleCapacity()*days[j];
totalCargo+=((PickUp)carsForRent[carsId[j]-1]).getCargoCapacity()*days[j];
}
if(carsForRent[carsId[j]-1] instanceof Trunk)
{
totalCargo+=((Trunk)carsForRent[carsId[j]-1]).getCargoCapacity()*days[j];
}
}
}
System.out.println(totalPeopley+" "+String.format("%.2f", totalCargo)+" "+totalMoney);
scan.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: