package com.rental; /** * 车辆管理系统的Car类,封装车辆属性和业务行为 */ import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; public class Car { // 私有属性 private final String licensePlate; // 车牌号不可变,加final private String brand; private String model; private double dailyRent; private boolean isRented; // 静态变量:统计车辆总数(线程安全) private static final AtomicInteger totalCars = new AtomicInteger(0); // 全参构造方法:车牌号、品牌、型号、日租金 public Car(String licensePlate, String brand, String model, double dailyRent) { if (licensePlate == null || licensePlate.trim().isEmpty()) { throw new IllegalArgumentException("车牌号不能为空"); } if (brand == null || brand.trim().isEmpty()) { throw new IllegalArgumentException("品牌不能为空"); } if (model == null || model.trim().isEmpty()) { throw new IllegalArgumentException("型号不能为空"); } this.licensePlate = licensePlate.trim(); this.brand = brand.trim(); this.model = model.trim(); this.dailyRent = validateDailyRent(dailyRent); this.isRented = false; totalCars.incrementAndGet(); } // 三参构造方法:调用全参构造,日租金默认300 public Car(String licensePlate, String brand, String model) { this(licensePlate, brand, model, 300.0); // this()必须是构造方法第一行 } // Getter:车牌号(仅get,无set) public String getLicensePlate() { return licensePlate; } // 品牌:get+set public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } // 型号:get+set public String getModel() { return model; } public void setModel(String model) { this.model = model; } // 日租金:get+set(含合法性校验) public double getDailyRent() { return dailyRent; } public void setDailyRent(double dailyRent) { this.dailyRent = validateDailyRent(dailyRent); } // 私有方法:验证日租金合法性 private double validateDailyRent(double dailyRent) { if (dailyRent > 0) { return dailyRent; } else { System.out.println("日租金必须大于0,使用默认值300元/天"); return 300.0; } } // 租出状态:仅查询(isXXX命名规范),无set public boolean isRented() { return isRented; } // 静态方法:获取车辆总数 public static int getTotalCars() { return totalCars.get(); } // 业务方法:租车 public boolean rentCar() { if (isRented) { System.out.println("车牌号【" + licensePlate + "】:车辆已租出,无法再次租用"); return false; } else { isRented = true; System.out.println("车牌号【" + licensePlate + "】:租车成功,车辆已租出"); return true; } } // 业务方法:还车 public boolean returnCar() { if (!isRented) { System.out.println("车牌号【" + licensePlate + "】:车辆未被租用,无需归还"); return false; } else { isRented = false; System.out.println("车牌号【" + licensePlate + "】:还车成功,车辆已归还"); return true; } } // 业务方法:计算总租金(日租金×天数) public double calculateRent(int days) { if (days <= 0) { throw new IllegalArgumentException("租车天数必须大于0"); } return dailyRent * days; } // 自定义方法:展示车辆完整信息(方便测试) public void displayInfo() { System.out.println("====================="); System.out.println("车牌号:" + licensePlate); System.out.println("品牌型号:" + brand + "-" + model); System.out.println("日租金:" + dailyRent + "元/天"); System.out.println("车辆状态:" + (isRented ? "已租出" : "可租用")); System.out.println("=====================\n"); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Car car = (Car) o; return Objects.equals(licensePlate, car.licensePlate); } @Override public int hashCode() { return Objects.hash(licensePlate); } @Override public String toString() { return "Car{" + "licensePlate='" + licensePlate + '\'' + ", brand='" + brand + '\'' + ", model='" + model + '\'' + ", dailyRent=" + dailyRent + ", isRented=" + isRented + '}'; } }