7-5 sdut-oop-7 答答租车系统(类的继承与多态 面向对象综合练习) (10分)

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;
    }
}













  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值