//实现用户的租车、换车等功能 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("付款成功!"); } }