diff --git a/w3/Car.java b/w3/Car.java new file mode 100644 index 0000000..64dd8af --- /dev/null +++ b/w3/Car.java @@ -0,0 +1,138 @@ +package com.rental; + +/** + * Car 类:车辆管理系统核心类 + * 实现封装(私有属性 + 公有 getter/setter)、构造方法重载、this() 调用、 + * 数据校验(日租金 > 0)、业务方法状态保护 + * 包含可选静态成员 totalCars 统计车辆总数 + */ +public class Car { + // 全部私有属性 + private String licensePlate; // 车牌号(不可变) + private String brand; // 品牌 + private String model; // 型号 + private double dailyRent; // 日租金 + private boolean isRented; // 是否已租出 + + // 静态成员(加分项) + private static int totalCars = 0; + + /** + * 全参构造方法 + * @param licensePlate 车牌号 + * @param brand 品牌 + * @param model 型号 + * @param dailyRent 日租金 + */ + public Car(String licensePlate, String brand, String model, double dailyRent) { + this.licensePlate = licensePlate; + this.brand = brand; + this.model = model; + this.dailyRent = dailyRent; // 构造方法中直接赋值(按要求,不做校验) + this.isRented = false; // 初始化为未租出 + totalCars++; // 每创建一个对象,计数加1 + } + + /** + * 重载构造方法:只传入车牌、品牌、型号 + * 日租金默认 300.0,使用 this() 调用全参构造 + */ + public Car(String licensePlate, String brand, String model) { + this(licensePlate, brand, model, 300.0); // this() 必须是第一条语句 + } + + // Getter/Setter(严格按要求实现) + public String getLicensePlate() { + return licensePlate; + } + // 车牌号无 setter(不可修改) + + 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; + } + + /** + * 日租金 setter:必须校验 > 0,否则输出提示并保持原值 + */ + public void setDailyRent(double dailyRent) { + if (dailyRent > 0) { + this.dailyRent = dailyRent; + } else { + System.out.println("【提示】日租金必须大于0,设置无效,保持原值!"); + } + } + + /** + * 租出状态只提供 isRented() 查询方法(boolean 特殊命名规范) + * 无 setter —— 状态必须通过业务方法 rentCar()/returnCar() 改变,防止外部直接破坏业务逻辑 + */ + public boolean isRented() { + return isRented; + } + + // 业务方法 + /** + * 租车:只有未租出才能租出,否则提示 + */ + public void rentCar() { + if (isRented) { + System.out.println("车辆已租出,无法再次租用"); + } else { + isRented = true; + // 成功时不输出(按实验要求仅在非法情况输出提示) + } + } + + /** + * 还车:只有已租出才能归还,否则提示 + */ + public void returnCar() { + if (!isRented) { + System.out.println("车辆未被租用,无需归还"); + } else { + isRented = false; + // 成功时不输出(按实验要求仅在非法情况输出提示) + } + } + + /** + * 计算租金:仅计算,不修改任何属性 + * @param days 租用天数 + * @return 总租金 + */ + public double calculateRent(int days) { + return dailyRent * days; + } + + /** + * 辅助方法:打印车辆完整信息(便于测试类调用) + */ + public void displayInfo() { + System.out.println("车牌号: " + licensePlate); + System.out.println("品牌: " + brand); + System.out.println("型号: " + model); + System.out.println("日租金: " + dailyRent + " 元/天"); + System.out.println("是否已租出: " + (isRented ? "是" : "否")); + } + + // 静态方法 + public static int getTotalCars() { + return totalCars; + } +} \ No newline at end of file diff --git a/w3/TestCar.java b/w3/TestCar.java new file mode 100644 index 0000000..9623cee --- /dev/null +++ b/w3/TestCar.java @@ -0,0 +1,47 @@ +package com.rental; + +/** + * 测试类 TestCar:验证 Car 类的所有功能 + * 包含:不同构造方法创建对象、显示信息、租车/还车状态切换及重复操作提示、 + * 租金计算、非法日租金设置、静态总数统计 + */ +public class TestCar { + public static void main(String[] args) { + System.out.println("=== 车辆管理系统测试开始 ==="); + + // 1. 使用不同构造方法创建至少 3 个 Car 对象 + Car car1 = new Car("京A·12345", "比亚迪", "汉EV", 500.0); + Car car2 = new Car("沪B·67890", "特斯拉", "Model 3"); // 默认日租金 300.0 + Car car3 = new Car("粤C·11111", "宝马", "X5", 800.0); + + // 2. 输出所有车辆信息 + System.out.println("\n【所有车辆信息】"); + car1.displayInfo(); + System.out.println("---------------------"); + car2.displayInfo(); + System.out.println("---------------------"); + car3.displayInfo(); + System.out.println("---------------------"); + + // 3. 对 car1 进行租车/还车测试(覆盖正常和重复操作) + System.out.println("【对 " + car1.getLicensePlate() + " 进行租车/还车测试】"); + car1.rentCar(); // 首次租车(成功,无输出) + car1.rentCar(); // 重复租车 → 输出提示 + car1.returnCar(); // 还车(成功,无输出) + car1.returnCar(); // 重复还车 → 输出提示 + + // 4. 计算租金 + double rentFee = car1.calculateRent(5); + System.out.println("租用 5 天总租金:" + rentFee + " 元"); + + // 5. 尝试设置非法日租金 + System.out.println("\n【测试非法日租金设置】"); + car1.setDailyRent(-100); // 应输出提示 + System.out.println("当前日租金仍为:" + car1.getDailyRent() + " 元"); + + // 6. 输出总车辆数(静态成员) + System.out.println("\n【系统总车辆数】:" + Car.getTotalCars() + " 辆"); + + System.out.println("\n=== 测试结束 ==="); + } +} \ No newline at end of file diff --git a/w3/实验报告.docx b/w3/实验报告.docx new file mode 100644 index 0000000..0acc478 Binary files /dev/null and b/w3/实验报告.docx differ