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.
39 lines
1.8 KiB
39 lines
1.8 KiB
|
|
public class TestCar {
|
|
public static void main(String[] args) {
|
|
System.out.println("=== 1. 创建车辆对象(两种构造方法) ===");
|
|
// 用全参构造创建车辆1
|
|
Car car1 = new Car("湘A12345", "特斯拉", "Model 3", 500.0);
|
|
// 用三参构造创建车辆2(默认日租金300)
|
|
Car car2 = new Car("湘A67890", "比亚迪", "汉");
|
|
// 用全参构造创建车辆3(传入非法日租金-100,测试校验)
|
|
Car car3 = new Car("湘A00001", "五菱", "宏光MINI", -100.0);
|
|
|
|
System.out.println("\n=== 2. 打印所有车辆初始信息 ===");
|
|
car1.displayInfo();
|
|
car2.displayInfo();
|
|
car3.displayInfo();
|
|
|
|
System.out.println("\n=== 3. 测试租车/还车业务(car1) ===");
|
|
car1.rentCar(); // 第一次租车:成功
|
|
car1.rentCar(); // 第二次租车:提示已租出
|
|
car1.displayInfo(); // 查看状态
|
|
|
|
car1.returnCar(); // 第一次还车:成功
|
|
car1.returnCar(); // 第二次还车:提示未租出
|
|
car1.displayInfo(); // 查看状态
|
|
|
|
System.out.println("\n=== 4. 测试租金计算(car2租用5天) ===");
|
|
double rent = car2.calculateRent(5);
|
|
System.out.printf("车辆%s租用5天的总租金为:%.2f元%n", car2.getLicensePlate(), rent);
|
|
|
|
System.out.println("\n=== 5. 测试日租金setter非法修改(car3) ===");
|
|
System.out.println("修改前日租金:" + car3.getDailyRent());
|
|
car3.setDailyRent(-200); // 非法值:提示错误,保持原值
|
|
car3.setDailyRent(200); // 合法值:修改成功
|
|
System.out.println("修改后日租金:" + car3.getDailyRent());
|
|
|
|
System.out.println("\n=== 6. 静态成员:车辆总数 ===");
|
|
System.out.println("当前创建的车辆总数:" + Car.getTotalCars());
|
|
}
|
|
}
|