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.
101 lines
2.7 KiB
101 lines
2.7 KiB
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++; // 每创建一个对象,总数+1
|
|
}
|
|
|
|
// 三参构造方法(日租金默认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;
|
|
}
|
|
|
|
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) {
|
|
System.out.println("车辆已租出,无法再次租出!");
|
|
} else {
|
|
isRented = true;
|
|
System.out.println("车辆租出成功!");
|
|
}
|
|
}
|
|
|
|
// 业务方法:还车
|
|
public void returnCar() {
|
|
if (!isRented) {
|
|
System.out.println("车辆未被租用,无需归还!");
|
|
} else {
|
|
isRented = false;
|
|
System.out.println("车辆归还成功!");
|
|
}
|
|
}
|
|
|
|
// 业务方法:计算租金
|
|
public double calculateRent(int days) {
|
|
return dailyRent * days;
|
|
}
|
|
|
|
// 静态方法:获取车辆总数
|
|
public static int getTotalCars() {
|
|
return totalCars;
|
|
}
|
|
|
|
// 打印车辆信息
|
|
public void displayInfo() {
|
|
System.out.println("车牌号:" + licensePlate +
|
|
",品牌:" + brand +
|
|
",型号:" + model +
|
|
",日租金:" + dailyRent +
|
|
"元/天,状态:" + (isRented ? "已租出" : "可租"));
|
|
}
|
|
}
|
|
|