为了账号安全,请及时绑定邮箱和手机立即绑定

JAVA第二季(答答租车系统)

标签:
Java

车父类:

package com.comit;

public class Car {

    protected int carID;
    protected String carName;
    protected double rentPrice;

    public int getCarID() {
        return carID;
    }

    public void setCarID(int carID) {
        this.carID = carID;
    }

    public String getCarName() {
        return carName;
    }

    public void setCarName(String carName) {
        this.carName = carName;
    }

    public double getRentPrice() {
        return rentPrice;
    }

    public void setRentPrice(double rentPrice) {
        this.rentPrice = rentPrice;
    }

    @Override
    public String toString() {
        return carName + "。日租金:" + rentPrice + "元。";
    }
}

载货量接口,单独作为接口存在,避免无载货能力车型具备载货能力:

package com.comit;

public interface ICargoCapacity {
    public double cargoCapacity();
}

载客量接口,单独作为接口存在,避免无载客能力车型具备载货能力:

package com.comit;

public interface IPassengerCapacity {
    public int passengerCapacity();
}

Bus子类车型:

package com.comit;

public class Bus extends Car implements IPassengerCapacity {

    public Bus() {
        carName = "巴士";
        rentPrice = 1000;
    }

    @Override
    public int passengerCapacity() {
        int passengerCapacity = 40;
        return passengerCapacity;
    }

    @Override
    public String toString() {
        return super.toString() + "载客量:" + passengerCapacity() + "人。";
    }
}

Pickup 子类车型:

package com.comit;

public class Pickup extends Car implements ICargoCapacity, IPassengerCapacity {

    public Pickup() {
        carName = "皮卡";
        rentPrice = 500.0;
    }

    @Override
    public int passengerCapacity() {
        int passengerCapacity = 5;
        return passengerCapacity;
    }

    @Override
    public double cargoCapacity() {
        double cargoCapacity = 5.0;
        return cargoCapacity;
    }

    @Override
    public String toString() {
        return super.toString() + "载货量:" + cargoCapacity() + "吨。" + "载客量:"
                + passengerCapacity() + "人。";
    }
}

主程序 :

package com.comit;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class CarRentalSystemInitial {

    public static void main(String[] args) {
        systemInitial();
    }

    // 初始化系统
    public static void systemInitial() {
        System.out.println("欢迎使用答答租车系统!");
        System.out.println("租车请输入数字'1',退出系统请输入任意键。");
        Scanner sn = new Scanner(System.in);

        // 判断是否租车
        try {
            int input = sn.nextInt();
            if (input == 1) {

                int count = 1;
                System.out.println("可选车型一览表:");
                // 遍历可租车辆
                Car[] car = new Car[] { new Bus(), new Pickup() };
                for (Car carList : car) {
                    System.out.println(count + "." + carList.toString());
                    count++;
                }
                // 记录所选车辆
                selectCar(car);
            }
        } catch (Exception e) {
            System.out.println("退出系统");
            sn.close();
            System.exit(0);
        }

    }

    // 记录所选车辆
    public static void selectCar(Car[] car) {
        System.out.println("请输入车辆序号确定租车,输入数字‘0’结束选车,输入数字‘100’退出系统。");
        Scanner sn = new Scanner(System.in);
        List<Integer> list = new ArrayList<Integer>();

        for (int i = 0; i <= car.length; i++) {
            try {
                int carNum = sn.nextInt();
                if (carNum != 0) {

                    if (carNum <= car.length) {
                        list.add(carNum);
                    } else if (carNum == 100) {
                        System.out.println("取消租车,退出系统。");
                        sn.close();
                        System.exit(0);
                    } else {
                        System.out.println("输入有误,系统关闭!");
                        System.exit(0);
                    }
                } else {
                    int count = 0;
                    int passengerCapacity = 0;
                    double cargoCapacity = 0;
                    double priceSum = 0;
                    System.out.println("选车完毕,你租用的车辆有以下几款:");
                    Iterator<Integer> it = list.iterator();
                    while (it.hasNext()) {
                        int num = it.next();
                        count++;
                        System.out.println(count + "."
                                + car[num - 1].toString());
                        passengerCapacity += getPassengerCapacity(car[num - 1]);
                        cargoCapacity += getCargoCapacity(car[num - 1]);
                        priceSum += car[num - 1].getRentPrice();
                    }
                    System.out.println("所选车辆载客总量 为" + passengerCapacity + "人。");
                    System.out.println("所选车辆载货总量 为" + cargoCapacity + "吨。");
                    System.out.println("所选车辆租金总额 为" + priceSum + "元。");
                    sn.close();
                    break;
                }
            } catch (Exception e) {
                System.out.println("输入有误,系统关闭!");
                sn.close();
                System.exit(0);
            }
        }
    }

    // 累计载客量
    public static int getPassengerCapacity(Object o) {
        if (o instanceof IPassengerCapacity) {
            int passengerCapacity = 0;
            passengerCapacity += ((IPassengerCapacity) o).passengerCapacity();
            return passengerCapacity;
        } else {
            return 0;
        }
    }

    // 累计载货量
    public static double getCargoCapacity(Object o) {
        if (o instanceof ICargoCapacity) {
            double cargoCapacity = 0;
            cargoCapacity += ((ICargoCapacity) o).cargoCapacity();
            return cargoCapacity;
        } else {
            return 0;
        }
    }
}
点击查看更多内容
52人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消