diff --git a/W3/Car/Car.java b/W3/Car/Car.java new file mode 100644 index 0000000..af3400a --- /dev/null +++ b/W3/Car/Car.java @@ -0,0 +1,101 @@ +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); // 通过setter赋值,自动校验 + this.isRented = false; // 初始状态为未租出 + totalCars++; // 每创建一个对象,总数+1 + } + + // 三参构造方法(日租金默认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; + } + + 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 static int getTotalCars() { + return totalCars; + } + + // 打印车辆信息 + public void displayInfo() { + System.out.println("车牌号:" + licensePlate + + ",品牌:" + brand + + ",型号:" + model + + ",日租金:" + dailyRent + + "元/天,状态:" + (isRented ? "已租出" : "可租")); + } +} diff --git a/W3/Car/CarTest.java b/W3/Car/CarTest.java new file mode 100644 index 0000000..92bed46 --- /dev/null +++ b/W3/Car/CarTest.java @@ -0,0 +1,35 @@ +public class CarTest { + public static void main(String[] args) { + // 创建3个Car对象(使用不同构造方法) + Car car1 = new Car("京A12345", "丰田", "凯美瑞", 280.0); + Car car2 = new Car("沪B67890", "本田", "雅阁"); + Car car3 = new Car("粤C13579", "大众", "帕萨特", 320.0); + + // 打印所有车辆信息 + System.out.println("=== 初始车辆信息 ==="); + car1.displayInfo(); + car2.displayInfo(); + car3.displayInfo(); + + // 测试租车业务 + System.out.println("\n=== 测试租车业务 ==="); + car1.rentCar(); // 正常租出 + car1.rentCar(); // 重复租出,提示错误 + car1.returnCar(); // 正常还车 + car1.returnCar(); // 重复还车,提示错误 + + // 测试计算租金 + System.out.println("\n=== 测试租金计算 ==="); + double rent = car2.calculateRent(5); + System.out.println("车辆" + car2.getLicensePlate() + "租赁5天,总租金:" + rent + "元"); + + // 测试日租金非法值 + System.out.println("\n=== 测试日租金非法值 ==="); + car3.setDailyRent(-100); // 输入负数,提示错误 + System.out.println("修改后日租金:" + car3.getDailyRent() + "元/天"); + + // 测试静态变量 + System.out.println("\n=== 车辆总数统计 ==="); + System.out.println("当前创建的车辆总数:" + Car.getTotalCars() + " 辆"); + } +} diff --git a/W3/Car/ai使用记录.txt b/W3/Car/ai使用记录.txt new file mode 100644 index 0000000..ef1ce7c --- /dev/null +++ b/W3/Car/ai使用记录.txt @@ -0,0 +1 @@ +本次Java车辆管理系统Car类实验中,AI辅助完成需求拆解与代码规范校验。根据实验要求,AI协助梳理Car类私有属性、构造方法重载、getter/setter数据校验、业务方法逻辑等核心要点,排查this()调用、isRented状态控制等易错点,优化静态变量统计逻辑,同时生成测试用例覆盖合法/非法操作场景,提升代码完整性与可测试性。AI还辅助规范实验报告结构,明确类图绘制、代码注释、运行结果分析等要求,提高实验效率与代码质量,全程遵循实验评分标准,确保功能符合封装、数据校验等核心要求。 \ No newline at end of file diff --git a/W3/Car/运行截图.png b/W3/Car/运行截图.png new file mode 100644 index 0000000..c10cdcc Binary files /dev/null and b/W3/Car/运行截图.png differ diff --git a/W3/README2.md b/W3/README2.md new file mode 100644 index 0000000..028543a --- /dev/null +++ b/W3/README2.md @@ -0,0 +1,25 @@ +1. 编译: javac com/rental/Car.java com/rental/TestCar.java  + +2. 运行: java com.rental.TestCar  + +3. 测试覆盖: + +- 不同构造方法创建车辆 + +- 重复租车/重复还车的异常提示 + +- 非法日租金修改的校验 + +- 租金计算功能 + +- 车辆总数统计 + + 实验要点 + +- 封装:私有属性+受控访问,保护对象状态合法性 + +- 构造方法重载与 this() 调用 + +- 数据校验:在setter和业务方法中做合法性检查 + +- 状态控制:通过业务方法而非直接修改属性,避免非法状态 \ No newline at end of file