java--汽车租赁系统:第三天完成VIP用户类

//实现用户的租车、换车等功能
class VIPUser {

    public VIPUser() {

    }
    public static double revenue = 0;
    public static double rentalFee = 0;
    public static double set = 0;
    private List<Vehicle> availableVehicles;

    public VIPUser(List<Vehicle> availableVehicles) {
        this.availableVehicles = availableVehicles;
    }

    // 查看可租车辆
    public void viewAvailableVehicles() {
        System.out.println("可租车辆:");
        for (Vehicle vehicle : availableVehicles) {
            if (vehicle instanceof Car) {
                Car car = (Car) vehicle;
                System.out.println("轿车(品牌:" + car.getBrand() + ",每日租金:" + car.getRentalPrice() +
                        ",载客量:" + car.getPassengerCount()+")");
            } else if (vehicle instanceof Bus) {
                Bus bus = (Bus) vehicle;
                System.out.println("公共汽车(品牌:" + bus.getBrand() + ",每日租金:" + bus.getRentalPrice() +
                        ",座位数:" + bus.getSeatCount()+")");
            } else if (vehicle instanceof Truck) {
                Truck truck = (Truck) vehicle;
                System.out.println("卡车(品牌:" + truck.getBrand() + ",每日租金:" + truck.getRentalPrice() +
                        ",载货量:" + truck.getCargoCapacity()+")");
            }
        }
    }

    // 租车
    public void rentVehicle() throws aException{
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要租车的品牌:");
        String brand = scanner.nextLine();
        for (Vehicle vehicle : availableVehicles) {
            if (vehicle.getBrand().equals(brand)) {
                System.out.println("请输入租车天数:");
                int days = scanner.nextInt();
                rentalFee = calculateRentalFee(vehicle.getRentalPrice(), days);
                this.revenue += rentalFee;
                set+=rentalFee;
                System.out.println("租赁车辆成功!租金金额为:" + rentalFee+",租金总额为:" + set);
                return;
            }
        }
        try {
            throw new aException("未找到该车辆品牌!");
        }
        catch(aException e) {
            System.out.println("未找到该车辆品牌!");
        }
    }

    // 换车
    public void changeVehicle() throws aException{
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要更换的车辆品牌:");
        String brand = scanner.nextLine();
        for (Vehicle vehicle : availableVehicles) {
            if (vehicle.getBrand().equals(brand)) {
                System.out.println("请输入租车天数:");
                int days = scanner.nextInt();
                revenue-=rentalFee;
                set-=rentalFee;
                rentalFee = calculateRentalFee(vehicle.getRentalPrice(), days);
                revenue+=rentalFee;
                set+=rentalFee;
                System.out.println("更换车辆成功!租金金额为:" + rentalFee+"租金总额为:" + set);
                return;
            }
        }

        try {
            throw new aException("未找到该车辆品牌!");
        }
        catch(aException e) {
            System.out.println("未找到该车辆品牌!");
        }
    }

    // 计算租车费用
    private double calculateRentalFee(double rentalPrice, int days) {
        return rentalPrice * days;
    }
    public double getRevenue() {
        return this.revenue;
    }
    public void makePayment() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("当前需要支付的金额为:" + set + "元");

        System.out.print("请输入付款金额:");
        double payment = scanner.nextDouble();

        if (payment >set) {
            double change = payment - set;
            System.out.println("付款成功!找零:" + change + "元");
        } else if(payment<set){
            System.out.println("付款失败!付款金额不足。");
        }
        else
            System.out.println("付款成功!");

    }
}
  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值