package com.rental; public class Car { // 私有属性 private final String licensePlate; // 车牌号不可变 private String brand; private String model; private double dailyRent; private boolean isRented; // 静态变量:统计车辆总数 private static int totalCars = 0; // 全参构造 public Car(String licensePlate, String brand, String model, double dailyRent) { this.licensePlate = licensePlate; this.brand = brand; this.model = model; setDailyRent(dailyRent); // 复用校验逻辑 this.isRented = false; totalCars++; } // 重载构造:默认日租金300 public Car(String licensePlate, String brand, String model) { this(licensePlate, brand, model, 300.0); } // Getter & Setter public String getLicensePlate() { return licensePlate; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public double getDailyRent() { return dailyRent; } // 租金必须 >0 public void setDailyRent(double dailyRent) { if (dailyRent > 0) { this.dailyRent = dailyRent; } else { System.out.println("日租金必须大于0,设置失败!"); } } public boolean isRented() { return isRented; } // 业务方法:租车 public void rentCar() { if (isRented) { System.out.println("车辆已租出,无法再次租用"); } else { isRented = true; System.out.println("租车成功!"); } } // 业务方法:还车 public void returnCar() { if (!isRented) { System.out.println("车辆未被租用,无需归还"); } else { isRented = false; System.out.println("还车成功!"); } } // 计算租金 public double calculateRent(int days) { return dailyRent * days; } // 显示车辆信息 public void displayInfo() { System.out.println("=====车辆信息====="); System.out.println("车牌号:" + licensePlate); System.out.println("品牌:" + brand); System.out.println("型号:" + model); System.out.println("日租金:" + dailyRent); System.out.println("是否租出:" + (isRented ? "是" : "否")); System.out.println("=================\n"); } // 静态方法:获取总车辆数 public static int getTotalCars() { return totalCars; } }