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

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

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

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(含义同上)

Sample Input

1
2
1 1
2 2

Sample Output

15 0.00 1600

Hint

 

Source

zhouxq

 

学习到的主要内容:

1.类的继承

2.构建继承

3.@Override

 一般用途             https://www.cnblogs.com/bincoding/p/5725732.html                                                                                                                                         

  •   帮助自己检查是否正确的复写了父类中已有的方法
  •   告诉读代码的人,这是一个复写的方法

4.instanceof  

ava 中的instanceof 是一个二元操作符(运算符)运算符,由于是字母组成,所以是Java的保留关键字,但是和>=,<=,==属同一类,它的作用是用来判断,instanceof 左边对象是否为instanceof 右边类的实例,返回一个boolean类型值。还可以用来判断子父类的所属关系。

用法: 
boolean result = object instanceof class 
参数: 
Result:布尔类型。 
Object:必选项。任意对象表达式。 
Class:必选项。任意已定义的对象类。 
说明: 
如果 object 是 class 的一个实例,则 instanceof 运算符返回 true。如果 object 不是指定类的一个实例,或者 object 是 null,则返回 false

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 车辆信息表
        Car[] cars = {
                new peopleCar("A", 800, 5),
                new peopleCar("B", 400, 5),
                new peopleCar("C", 800, 5),
                new peopleCar("D", 1300, 51),
                new peopleCar("E", 1500, 55),
                new PickUp("F", 500, 5, 0.45),
                new PickUp("G", 450, 5, 2.0),
                new Truck("H", 200, 3),
                new Truck("I", 1500, 25),
                new Truck("J", 2000, 35)
        };
        int isok = in.nextInt(); // 判断是是否租车 1租 0 不租
        if (isok == 1) {
            int num = in.nextInt(); // 租车数量
            int sum_peo = 0; // 乘客总数
            double sum_things = 0; // 载货总数
            int sum_money = 0; // 租车总费用

            for (int i = 0; i < num; i++) {
                int cnt = in.nextInt(); // 租车编号
                int day = in.nextInt(); // 租车天数
                // istanceof 判断 左侧类 是否和  右侧是否为同一个类  (boolean类型)
                sum_money += cars[cnt - 1].getRent() * day;
                if (cars[cnt - 1] instanceof peopleCar) {
                    sum_peo += ((peopleCar) cars[cnt - 1]).getPeopleMax() * day;

                } else if (cars[cnt - 1] instanceof PickUp) {
                    sum_peo += ((PickUp) cars[cnt - 1]).getPeopleMax() * day;
                    sum_things += ((PickUp) cars[cnt - 1]).getThingMax() * day;
                } else if (cars[cnt - 1] instanceof Truck) {
                    sum_things += ((Truck) cars[cnt - 1]).getThingMax() * day;
                }
            }
            System.out.printf("%d %.2f %d\n", sum_peo, sum_things, sum_money);
        } else {
            System.out.printf("%d %.2f %d\n", 0, 0.0, 0);
        }
    }
}

class Car {
    String name; // 车的名字
    int rent; // 租金

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

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

    public String getName() {
        return name;
    }

    public int getRent() {
        return rent;
    }
}

class peopleCar extends Car {
    double peopleMax; // 最大载客量

    public peopleCar(String name, int rent, double peopleMax) {
        super.setName(name);
        this.peopleMax = peopleMax;
        super.setRent(rent);
    }

    public double getPeopleMax() {
        return peopleMax;
    }

    @Override //作用: <1>告诉读代码的人 这是一个复写方法 <2>帮助自己是否正确复写检查父类中已有方法
    public int getRent() {
        return super.getRent();
    }
}

class PickUp extends Car {
    double thingMax; // 最大载货量
    int peopleMax; // 最大载客量

    public PickUp(String name, int rent, int peopleMax, double thingMax) {
        super.setName(name);
        super.setRent(rent);
        this.peopleMax = peopleMax;
        this.thingMax = thingMax;
    }

    public int getPeopleMax() {
        return peopleMax;
    }

    public double getThingMax() {
        return thingMax;
    }

    @Override
    public int getRent() {
        return super.getRent();
    }
}

class Truck extends Car {
    double thingMax;

    public Truck(String name, int rent, double thingMax) {
        super.setName(name);
        super.setRent(rent);
        this.thingMax = thingMax;
    }

    public double getThingMax() {
        return thingMax;
    }

    @Override
    public int getRent() {
        return super.getRent();
    }
}

/***************************************************
User name: jk180602张国辉
Result: Accepted
Take time: 196ms
Take Memory: 12144KB
Submit time: 2019-01-10 23:37:30
****************************************************/

 

转载于:https://www.cnblogs.com/iQXQZX/p/10258730.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值