3 changed files with 150 additions and 0 deletions
@ -0,0 +1,105 @@ |
|||
public class Car { |
|||
// 私有属性
|
|||
private final String licensePlate; // 车牌号不可变
|
|||
private String brand; |
|||
private String model; |
|||
private double dailyRent; |
|||
private boolean isRented; |
|||
|
|||
// 静态变量:统计车辆总数
|
|||
private static int totalCars = 0; |
|||
|
|||
// 全参构造
|
|||
public Car(String licensePlate, String brand, String model, double dailyRent) { |
|||
this.licensePlate = licensePlate; |
|||
this.brand = brand; |
|||
this.model = model; |
|||
setDailyRent(dailyRent); // 借用setter做校验
|
|||
this.isRented = false; |
|||
totalCars++; |
|||
} |
|||
|
|||
// 重载构造:默认日租金300
|
|||
public Car(String licensePlate, String brand, String model) { |
|||
this(licensePlate, brand, model, 300.0); |
|||
} |
|||
|
|||
// Getter & Setter
|
|||
public String getLicensePlate() { |
|||
return licensePlate; |
|||
} |
|||
|
|||
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; |
|||
} |
|||
|
|||
// 日租金必须>0
|
|||
public void setDailyRent(double dailyRent) { |
|||
if (dailyRent > 0) { |
|||
this.dailyRent = dailyRent; |
|||
} else { |
|||
System.out.println("日租金必须大于0,设置失败!"); |
|||
} |
|||
} |
|||
|
|||
public boolean isRented() { |
|||
return isRented; |
|||
} |
|||
|
|||
// 业务方法:租车
|
|||
public void rentCar() { |
|||
if (!isRented) { |
|||
isRented = true; |
|||
System.out.println(licensePlate + " 租车成功!"); |
|||
} else { |
|||
System.out.println(licensePlate + " 车辆已租出,无法再次租用"); |
|||
} |
|||
} |
|||
|
|||
// 业务方法:还车
|
|||
public void returnCar() { |
|||
if (isRented) { |
|||
isRented = false; |
|||
System.out.println(licensePlate + " 还车成功!"); |
|||
} else { |
|||
System.out.println(licensePlate + " 车辆未被租用,无需归还"); |
|||
} |
|||
} |
|||
|
|||
// 计算租金
|
|||
public double calculateRent(int days) { |
|||
return dailyRent * days; |
|||
} |
|||
|
|||
// 静态方法:获取总车辆数
|
|||
public static int getTotalCars() { |
|||
return totalCars; |
|||
} |
|||
|
|||
// 打印车辆信息
|
|||
public void displayInfo() { |
|||
System.out.println("=====车辆信息====="); |
|||
System.out.println("车牌号:" + licensePlate); |
|||
System.out.println("品牌:" + brand); |
|||
System.out.println("型号:" + model); |
|||
System.out.println("日租金:" + dailyRent); |
|||
System.out.println("是否租出:" + (isRented ? "是" : "否")); |
|||
System.out.println("=================\n"); |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
实验目的 |
|||
|
|||
1.掌握 Java 封装思想,使用 private 修饰属性,通过 getter/setter 访问。 |
|||
|
|||
2.练习构造方法重载与 this() 调用。 |
|||
|
|||
3.学会在 setter 和业务方法中做数据合法性校验。 |
|||
|
|||
4.练习静态变量与静态方法实现全局统计。 |
|||
|
|||
5.编写测试类验证类功能。 |
|||
@ -0,0 +1,34 @@ |
|||
public class TestCar { |
|||
public static void main(String[] args) { |
|||
// 创建3辆车,使用不同构造
|
|||
Car car1 = new Car("京A12345", "丰田", "Camry", 280); |
|||
Car car2 = new Car("沪B67890", "大众", "Magotan"); |
|||
Car car3 = new Car("粤C54321", "比亚迪", "汉", 350); |
|||
|
|||
// 打印信息
|
|||
car1.displayInfo(); |
|||
car2.displayInfo(); |
|||
car3.displayInfo(); |
|||
|
|||
// 测试租车、重复租车、还车、重复还车
|
|||
System.out.println("===测试car1租车还车==="); |
|||
car1.rentCar(); |
|||
car1.rentCar(); |
|||
car1.returnCar(); |
|||
car1.returnCar(); |
|||
System.out.println(); |
|||
|
|||
// 计算5天租金
|
|||
System.out.println("car2 租赁5天费用:" + car2.calculateRent(5)); |
|||
System.out.println(); |
|||
|
|||
// 测试非法日租金
|
|||
System.out.println("===尝试设置非法日租金==="); |
|||
car3.setDailyRent(-100); |
|||
System.out.println("设置后car3日租金:" + car3.getDailyRent()); |
|||
System.out.println(); |
|||
|
|||
// 静态总数
|
|||
System.out.println("系统总车辆数:" + Car.getTotalCars()); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue