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.
138 lines
4.2 KiB
138 lines
4.2 KiB
package com.rental;
|
|
|
|
/**
|
|
* Car 类:车辆管理系统核心类
|
|
* 实现封装(私有属性 + 公有 getter/setter)、构造方法重载、this() 调用、
|
|
* 数据校验(日租金 > 0)、业务方法状态保护
|
|
* 包含可选静态成员 totalCars 统计车辆总数
|
|
*/
|
|
public class Car {
|
|
// 全部私有属性
|
|
private String licensePlate; // 车牌号(不可变)
|
|
private String brand; // 品牌
|
|
private String model; // 型号
|
|
private double dailyRent; // 日租金
|
|
private boolean isRented; // 是否已租出
|
|
|
|
// 静态成员(加分项)
|
|
private static int totalCars = 0;
|
|
|
|
/**
|
|
* 全参构造方法
|
|
* @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;
|
|
this.dailyRent = dailyRent; // 构造方法中直接赋值(按要求,不做校验)
|
|
this.isRented = false; // 初始化为未租出
|
|
totalCars++; // 每创建一个对象,计数加1
|
|
}
|
|
|
|
/**
|
|
* 重载构造方法:只传入车牌、品牌、型号
|
|
* 日租金默认 300.0,使用 this() 调用全参构造
|
|
*/
|
|
public Car(String licensePlate, String brand, String model) {
|
|
this(licensePlate, brand, model, 300.0); // this() 必须是第一条语句
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
/**
|
|
* 日租金 setter:必须校验 > 0,否则输出提示并保持原值
|
|
*/
|
|
public void setDailyRent(double dailyRent) {
|
|
if (dailyRent > 0) {
|
|
this.dailyRent = dailyRent;
|
|
} else {
|
|
System.out.println("【提示】日租金必须大于0,设置无效,保持原值!");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 租出状态只提供 isRented() 查询方法(boolean 特殊命名规范)
|
|
* 无 setter —— 状态必须通过业务方法 rentCar()/returnCar() 改变,防止外部直接破坏业务逻辑
|
|
*/
|
|
public boolean isRented() {
|
|
return isRented;
|
|
}
|
|
|
|
// 业务方法
|
|
/**
|
|
* 租车:只有未租出才能租出,否则提示
|
|
*/
|
|
public void rentCar() {
|
|
if (isRented) {
|
|
System.out.println("车辆已租出,无法再次租用");
|
|
} else {
|
|
isRented = true;
|
|
// 成功时不输出(按实验要求仅在非法情况输出提示)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 还车:只有已租出才能归还,否则提示
|
|
*/
|
|
public void returnCar() {
|
|
if (!isRented) {
|
|
System.out.println("车辆未被租用,无需归还");
|
|
} else {
|
|
isRented = false;
|
|
// 成功时不输出(按实验要求仅在非法情况输出提示)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 计算租金:仅计算,不修改任何属性
|
|
* @param days 租用天数
|
|
* @return 总租金
|
|
*/
|
|
public double calculateRent(int days) {
|
|
return dailyRent * days;
|
|
}
|
|
|
|
/**
|
|
* 辅助方法:打印车辆完整信息(便于测试类调用)
|
|
*/
|
|
public void displayInfo() {
|
|
System.out.println("车牌号: " + licensePlate);
|
|
System.out.println("品牌: " + brand);
|
|
System.out.println("型号: " + model);
|
|
System.out.println("日租金: " + dailyRent + " 元/天");
|
|
System.out.println("是否已租出: " + (isRented ? "是" : "否"));
|
|
}
|
|
|
|
// 静态方法
|
|
public static int getTotalCars() {
|
|
return totalCars;
|
|
}
|
|
}
|