diff --git a/CAR/Car.java b/CAR/Car.java new file mode 100644 index 0000000..0b12a7c --- /dev/null +++ b/CAR/Car.java @@ -0,0 +1,158 @@ +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 + + '}'; + } +} \ No newline at end of file diff --git a/CAR/TestCar.class b/CAR/TestCar.class new file mode 100644 index 0000000..549b154 Binary files /dev/null and b/CAR/TestCar.class differ diff --git a/CAR/TestCar.java b/CAR/TestCar.java new file mode 100644 index 0000000..611bd25 --- /dev/null +++ b/CAR/TestCar.java @@ -0,0 +1,66 @@ +import com.rental.Car; + +public class TestCar { + public static void main(String[] args) { + try { + // 测试1:创建车辆对象 + System.out.println("=== 测试1:创建车辆对象 ==="); + Car car1 = new Car("沪A12345", "奥迪", "A6", 500.0); + Car car2 = new Car("沪B67890", "宝马", "5系"); // 使用默认日租金 + + // 测试2:展示车辆信息 + System.out.println("=== 测试2:展示车辆信息 ==="); + car1.displayInfo(); + car2.displayInfo(); + + // 测试3:租车操作 + System.out.println("=== 测试3:租车操作 ==="); + boolean rentResult1 = car1.rentCar(); + System.out.println("租车结果: " + (rentResult1 ? "成功" : "失败")); + + // 测试4:重复租车 + System.out.println("=== 测试4:重复租车 ==="); + boolean rentResult2 = car1.rentCar(); + System.out.println("重复租车结果: " + (rentResult2 ? "成功" : "失败")); + + // 测试5:还车操作 + System.out.println("=== 测试5:还车操作 ==="); + boolean returnResult = car1.returnCar(); + System.out.println("还车结果: " + (returnResult ? "成功" : "失败")); + + // 测试6:计算租金 + System.out.println("=== 测试6:计算租金 ==="); + double rent = car1.calculateRent(7); + System.out.println("7天租金: " + rent + "元"); + + // 测试7:获取车辆总数 + System.out.println("=== 测试7:获取车辆总数 ==="); + System.out.println("车辆总数: " + Car.getTotalCars()); + + // 测试8:测试toString方法 + System.out.println("=== 测试8:测试toString方法 ==="); + System.out.println(car1.toString()); + System.out.println(car2.toString()); + + // 测试9:测试参数验证 + System.out.println("=== 测试9:测试参数验证 ==="); + try { + new Car(null, "奔驰", "C级"); + } catch (IllegalArgumentException e) { + System.out.println("空车牌号测试: " + e.getMessage()); + } + + try { + car1.calculateRent(0); + } catch (IllegalArgumentException e) { + System.out.println("天数为0测试: " + e.getMessage()); + } + + System.out.println("\n=== 所有测试完成 ==="); + + } catch (Exception e) { + System.out.println("测试过程中出现错误: " + e.getMessage()); + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/CAR/截图.docx b/CAR/截图.docx new file mode 100644 index 0000000..46d393d Binary files /dev/null and b/CAR/截图.docx differ