4 changed files with 191 additions and 0 deletions
@ -0,0 +1,142 @@ |
|||||
|
package com.rental; |
||||
|
|
||||
|
public class Car { |
||||
|
// 1. 私有属性
|
||||
|
private final String licensePlate; // 车牌号,使用 final 确保不可变
|
||||
|
private String brand; |
||||
|
private String model; |
||||
|
private double dailyRent; |
||||
|
private boolean isRented; |
||||
|
|
||||
|
// 5. 静态成员(加分项)
|
||||
|
private static int totalCars = 0; // 用于统计创建的车辆总数
|
||||
|
|
||||
|
// 2. 构造方法
|
||||
|
/** |
||||
|
* 全参构造方法 |
||||
|
* @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; |
||||
|
// 调用 setter 以进行租金校验
|
||||
|
this.setDailyRent(dailyRent); |
||||
|
this.isRented = false; // 初始化为未租出
|
||||
|
totalCars++; // 创建对象时,总数加1
|
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 部分参数构造方法(车牌、品牌、型号) |
||||
|
* 日租金默认为 300.0 |
||||
|
* @param licensePlate 车牌号 |
||||
|
* @param brand 品牌 |
||||
|
* @param model 型号 |
||||
|
*/ |
||||
|
public Car(String licensePlate, String brand, String model) { |
||||
|
// 使用 this() 调用全参构造方法
|
||||
|
this(licensePlate, brand, model, 300.0); |
||||
|
} |
||||
|
|
||||
|
// 3. 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; |
||||
|
} |
||||
|
|
||||
|
public void setDailyRent(double dailyRent) { |
||||
|
// 数据校验:日租金必须大于0
|
||||
|
if (dailyRent > 0) { |
||||
|
this.dailyRent = dailyRent; |
||||
|
} else { |
||||
|
System.out.println("[警告] 日租金必须大于0,修改失败。当前值保持为:" + this.dailyRent); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public boolean isRented() { |
||||
|
return isRented; |
||||
|
} |
||||
|
// isRented 没有 setter,状态必须通过业务方法改变
|
||||
|
|
||||
|
// 4. 业务方法
|
||||
|
/** |
||||
|
* 租车方法 |
||||
|
*/ |
||||
|
public void rentCar() { |
||||
|
if (!this.isRented) { |
||||
|
this.isRented = true; |
||||
|
System.out.println("车牌号 " + this.licensePlate + " 的车辆已成功租出。"); |
||||
|
} else { |
||||
|
System.out.println("车辆已租出,无法再次租用。"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 还车方法 |
||||
|
*/ |
||||
|
public void returnCar() { |
||||
|
if (this.isRented) { |
||||
|
this.isRented = false; |
||||
|
System.out.println("车牌号 " + this.licensePlate + " 的车辆已成功归还。"); |
||||
|
} else { |
||||
|
System.out.println("车辆未被租用,无需归还。"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 计算租金 |
||||
|
* @param days 租用天数 |
||||
|
* @return 总租金 |
||||
|
*/ |
||||
|
public double calculateRent(int days) { |
||||
|
// 假设 days 参数合法(>0),按作业要求暂不校验
|
||||
|
return this.dailyRent * days; |
||||
|
} |
||||
|
|
||||
|
// 5. 静态方法
|
||||
|
/** |
||||
|
* 获取创建的车辆总数 |
||||
|
* @return 车辆总数 |
||||
|
*/ |
||||
|
public static int getTotalCars() { |
||||
|
return totalCars; |
||||
|
} |
||||
|
|
||||
|
// 可选:一个用于打印车辆信息的方法,便于测试
|
||||
|
/** |
||||
|
* 显示车辆信息 |
||||
|
*/ |
||||
|
public void displayInfo() { |
||||
|
String status = this.isRented ? "已租出" : "可租"; |
||||
|
System.out.println("=== 车辆信息 ==="); |
||||
|
System.out.println("车牌号: " + this.licensePlate); |
||||
|
System.out.println("品牌型号: " + this.brand + " - " + this.model); |
||||
|
System.out.printf("日租金: %.2f 元/天\n", this.dailyRent); |
||||
|
System.out.println("状态: " + status); |
||||
|
System.out.println("================="); |
||||
|
} |
||||
|
} |
||||
|
|
||||
@ -0,0 +1,49 @@ |
|||||
|
package com.rental; |
||||
|
|
||||
|
public class TestCar { |
||||
|
public static void main(String[] args) { |
||||
|
System.out.println("===== 开始测试 Car 类 ====="); |
||||
|
|
||||
|
// 1. 创建至少3个Car对象(使用不同构造方法)
|
||||
|
System.out.println("\n1. 创建车辆对象:"); |
||||
|
Car car1 = new Car("京A12345", "丰田", "凯美瑞", 450.0); |
||||
|
Car car2 = new Car("沪B56789", "宝马", "X5"); // 使用默认租金300
|
||||
|
Car car3 = new Car("粤C99999", "特斯拉", "Model 3", 600.0); |
||||
|
|
||||
|
// 显示初始信息
|
||||
|
car1.displayInfo(); |
||||
|
car2.displayInfo(); |
||||
|
car3.displayInfo(); |
||||
|
|
||||
|
// 2. 测试租车、还车业务逻辑
|
||||
|
System.out.println("\n2. 测试租车与还车业务:"); |
||||
|
System.out.println("--- 测试 car1 (正常流程) ---"); |
||||
|
car1.rentCar(); // 第一次租,应成功
|
||||
|
car1.rentCar(); // 第二次租,应提示“已租出”
|
||||
|
car1.returnCar(); // 第一次还,应成功
|
||||
|
car1.returnCar(); // 第二次还,应提示“无需归还”
|
||||
|
|
||||
|
System.out.println("\n--- 测试 car2 (直接尝试归还) ---"); |
||||
|
car2.returnCar(); // 未租就还,应提示“无需归还”
|
||||
|
|
||||
|
// 3. 测试 calculateRent 方法
|
||||
|
System.out.println("\n3. 测试租金计算:"); |
||||
|
double rent = car3.calculateRent(5); |
||||
|
System.out.printf("车辆 %s 租用5天的费用为:%.2f 元\n", car3.getLicensePlate(), rent); |
||||
|
|
||||
|
// 4. 测试 setter 数据校验 (日租金)
|
||||
|
System.out.println("\n4. 测试日租金Setter的数据校验:"); |
||||
|
System.out.println("car2当前日租金:" + car2.getDailyRent()); |
||||
|
car2.setDailyRent(350.0); // 合法修改
|
||||
|
System.out.println("修改为350.0后,car2日租金:" + car2.getDailyRent()); |
||||
|
car2.setDailyRent(-100.0); // 非法修改,应提示警告并保持原值
|
||||
|
System.out.println("尝试修改为-100.0后,car2日租金:" + car2.getDailyRent()); |
||||
|
|
||||
|
// 5. 测试静态变量(车辆总数)
|
||||
|
System.out.println("\n5. 测试静态成员(车辆总数统计):"); |
||||
|
System.out.println("当前已创建的车辆总数为:" + Car.getTotalCars()); |
||||
|
|
||||
|
System.out.println("\n===== 测试结束 ====="); |
||||
|
} |
||||
|
} |
||||
|
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 278 KiB |
Loading…
Reference in new issue