From 42e2395cbb69a5f9d0df2c0a3b16764a85ff88c4 Mon Sep 17 00:00:00 2001 From: zhangsiyuan <3837703520@qq.com> Date: Tue, 24 Mar 2026 12:22:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=A0=E6=80=9D=E6=B8=8A-202401070104-w3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w3/TestCar.java | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 w3/TestCar.java diff --git a/w3/TestCar.java b/w3/TestCar.java new file mode 100644 index 0000000..100c495 --- /dev/null +++ b/w3/TestCar.java @@ -0,0 +1,41 @@ +import com.rental.Car; + +public class TestCar { + public static void main(String[] args) { + //创建至少3个Car对象 + Car car1 = new Car("京A88888", "奥迪", "A6L", 500.0); + Car car2 = new Car("沪B66666", "宝马", "5系"); + Car car3 = new Car("粤C12345", "奔驰", "E300L", 600.0); + + //输出所有车辆信息 + System.out.println("所有车辆信息如下"); + car1.displayInfo(); + car2.displayInfo(); + car3.displayInfo(); + System.out.println(); + + //对其中一辆车依次调用业务方法 + System.out.println("对其中一辆车依次调用业务方法如下"); + car1.rentCar(); // 第一次租用 + car1.rentCar(); // 再次租用,查看提示 + car1.returnCar(); // 第一次归还 + car1.returnCar(); // 再次归还,查看提示 + System.out.println(); + + //调用 calculateRent(5) 计算费用 + System.out.println("调用 calculateRent(5) 计算某辆车租用 5 天的费用"); + double rent5Days = car3.calculateRent(5); + System.out.println("车辆 " + car3.getLicensePlate() + " 租用5天的费用为: " + rent5Days + " 元"); + System.out.println(); + + //尝试通过 setter 修改日租金为非法值 + System.out.println("测试日租金合法性校验"); + System.out.println("修改前 car2 的日租金: " + car2.getDailyRent()); + car2.setDailyRent(-100); // 设置为非法值 + System.out.println("修改后 car2 的日租金: " + car2.getDailyRent()); + System.out.println(); + + //输出总车辆数 + System.out.println("本次运行共创建了 " + Car.getTotalCars() + " 辆车。"); + } +} \ No newline at end of file