继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

K_E_N
关注TA
已关注
手记 3
粉丝 15
获赞 242

车父类:

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;
        }
    }
}
打开App,阅读手记
52人推荐
发表评论
随时随地看视频慕课网APP

热门评论

CargoCapacity +=((ICargoCapacity)o).cargoCapacity();这个该怎么理解啊?

很好 你解决了我不能调用值得问题 用接口原来是这么做的!

为什么上面要重写tostring 这个方法?

查看全部评论