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.
66 lines
2.7 KiB
66 lines
2.7 KiB
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();
|
|
}
|
|
}
|
|
}
|