You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

59 lines
2.7 KiB

package JavaLearningProject.java.w3;
public class CarRunner {
public static void main(String[] args) {
System.out.println("=== 汽车租赁系统测试 ===");
System.out.println("当前系统总车辆数: " + Car.getTotalCars());
System.out.println("\n--- 场景 1: 创建合法的 Car 对象 ---");
Car car1 = new Car("京A88888", "Toyota", "Camry", 200.0);
System.out.println("创建车辆成功: " + car1.getBrand() + " " + car1.getModel() + " (车牌: " + car1.getLicensePlate() + ")");
System.out.println("当前系统总车辆数: " + Car.getTotalCars());
System.out.println("\n--- 场景 2: 测试属性更新 (Setters) ---");
car1.setBrand("Honda");
car1.setModel("Accord");
car1.setDailyRent(250.0);
System.out.println("更新后车辆信息: " + car1.getBrand() + " " + car1.getModel() + ", 日租金: " + car1.calculateRent(1));
System.out.println("\n--- 场景 3: 测试租赁逻辑 (Rent) ---");
System.out.println("车辆当前是否已租出? " + car1.isRented());
boolean rentResult = car1.rent();
System.out.println("尝试租出车辆... 结果: " + (rentResult ? "成功" : "失败"));
System.out.println("车辆当前是否已租出? " + car1.isRented());
System.out.println("\n--- 场景 4: 重复租赁测试 ---");
boolean rentResult2 = car1.rent();
System.out.println("尝试再次租出该车辆... 结果: " + (rentResult2 ? "成功" : "失败"));
System.out.println("\n--- 场景 5: 租金计算测试 ---");
try {
double rentFor5Days = car1.calculateRent(5);
System.out.println("租用 5 天的总租金为: " + rentFor5Days);
} catch (Exception e) {
System.out.println("租金计算异常: " + e.getMessage());
}
System.out.println("\n--- 场景 6: 异常处理测试 (负数天数计算) ---");
try {
car1.calculateRent(-2);
} catch (IllegalArgumentException e) {
System.out.println("捕获预期异常: " + e.getMessage());
}
System.out.println("\n--- 场景 7: 异常处理测试 (创建非法车辆) ---");
try {
new Car("", "Ford", "Civic", 100.0);
} catch (IllegalArgumentException e) {
System.out.println("捕获预期异常(车牌为空): " + e.getMessage());
}
try {
new Car("沪B12345", "Ford", "Civic", -50.0);
} catch (IllegalArgumentException e) {
System.out.println("捕获预期异常(租金为负): " + e.getMessage());
}
System.out.println("\n=== 测试结束 ===");
}
}