package day01;
import java.util.Scanner;
public class Demo05 {
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 sc = new Scanner(System.in);
int input = sc.nextInt();
int totalPeopley = 0; //载客总人数
double totalCargo = 0; //载货总重量
int totalMoney = 0; //租金总额
if(input==1){
int rentNum = sc.nextInt();
int[] carsId = new int[rentNum]; //租车的编号
int[] days = new int[rentNum]; //租车天数
for (int j=0;j<rentNum;j++){
carsId[j] = sc.nextInt();
days[j] = sc.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);
sc.close();
}
}
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 void setPeopleCapacity(int peopleCapacity) {
this.peopleCapacity = peopleCapacity;
}
public int getPeopleCapacity() {
return 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;
}
}
7-5 sdut-oop-7 答答租车系统(类的继承与多态 面向对象综合练习) (10分)
最新推荐文章于 2024-07-14 12:25:25 发布