3 changed files with 197 additions and 0 deletions
@ -0,0 +1,107 @@ |
|||
package com.rental; |
|||
|
|||
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; |
|||
this.dailyRent = dailyRent; |
|||
this.isRented = false; // 初始化为未租出
|
|||
totalCars++; // 创建车辆时总数加1
|
|||
} |
|||
|
|||
// 三参构造方法(日租金使用默认值300)
|
|||
public Car(String licensePlate, String brand, String model) { |
|||
this(licensePlate, brand, model, 300.0); // 调用全参构造
|
|||
} |
|||
|
|||
// Getter方法
|
|||
public String getLicensePlate() { |
|||
return licensePlate; |
|||
} |
|||
|
|||
public String getBrand() { |
|||
return brand; |
|||
} |
|||
|
|||
public String getModel() { |
|||
return model; |
|||
} |
|||
|
|||
public double getDailyRent() { |
|||
return dailyRent; |
|||
} |
|||
|
|||
public boolean isRented() { |
|||
return isRented; |
|||
} |
|||
|
|||
// Setter方法
|
|||
public void setBrand(String brand) { |
|||
this.brand = brand; |
|||
} |
|||
|
|||
public void setModel(String model) { |
|||
this.model = model; |
|||
} |
|||
|
|||
public void setDailyRent(double dailyRent) { |
|||
if (dailyRent > 0) { |
|||
this.dailyRent = dailyRent; |
|||
} else { |
|||
System.out.println("错误:日租金必须大于0,保持原值:" + this.dailyRent); |
|||
} |
|||
} |
|||
|
|||
// 业务方法:租车
|
|||
public void rentCar() { |
|||
if (isRented) { |
|||
System.out.println("车辆已租出,无法再次租用"); |
|||
} else { |
|||
isRented = true; |
|||
System.out.println("车辆 " + licensePlate + " 租用成功"); |
|||
} |
|||
} |
|||
|
|||
// 业务方法:还车
|
|||
public void returnCar() { |
|||
if (!isRented) { |
|||
System.out.println("车辆未被租用,无需归还"); |
|||
} else { |
|||
isRented = false; |
|||
System.out.println("车辆 " + licensePlate + " 归还成功"); |
|||
} |
|||
} |
|||
|
|||
// 业务方法:计算租金
|
|||
public double calculateRent(int days) { |
|||
return dailyRent * days; |
|||
} |
|||
|
|||
// 显示车辆信息的方法
|
|||
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("================================"); |
|||
} |
|||
|
|||
// 静态方法
|
|||
public static int getTotalCars() { |
|||
return totalCars; |
|||
} |
|||
} |
|||
@ -0,0 +1,61 @@ |
|||
package com.rental; |
|||
|
|||
public class TestCar { |
|||
public static void main(String[] args) { |
|||
System.out.println("========== 车辆管理系统测试 ==========\n"); |
|||
|
|||
// 1. 创建3个Car对象(使用不同构造方法)
|
|||
System.out.println("--- 创建车辆对象 ---"); |
|||
Car car1 = new Car("京A12345", "宝马", "X5", 500.0); |
|||
Car car2 = new Car("沪B67890", "奔驰", "E300", 600.0); |
|||
Car car3 = new Car("粤C34567", "奥迪", "A6"); // 使用默认租金300
|
|||
|
|||
// 2. 输出所有车辆信息
|
|||
System.out.println("\n--- 所有车辆信息 ---"); |
|||
car1.displayInfo(); |
|||
car2.displayInfo(); |
|||
car3.displayInfo(); |
|||
|
|||
// 3. 测试租车功能
|
|||
System.out.println("\n--- 测试租车功能 ---"); |
|||
System.out.println("第一次租车:"); |
|||
car1.rentCar(); // 第一次租车
|
|||
|
|||
System.out.println("\n第二次租车(同一辆车):"); |
|||
car1.rentCar(); // 第二次租车,应该提示已租出
|
|||
|
|||
System.out.println("\n第三次租车(同一辆车):"); |
|||
car1.rentCar(); // 第三次租车,应该提示已租出
|
|||
|
|||
// 4. 测试计算租金
|
|||
System.out.println("\n--- 测试租金计算 ---"); |
|||
int days = 5; |
|||
double rent = car1.calculateRent(days); |
|||
System.out.println("车辆 " + car1.getLicensePlate() + " 租用 " + days + " 天的租金为:" + rent + "元"); |
|||
|
|||
// 5. 测试还车功能
|
|||
System.out.println("\n--- 测试还车功能 ---"); |
|||
System.out.println("第一次还车:"); |
|||
car1.returnCar(); // 还车
|
|||
|
|||
System.out.println("\n第二次还车(同一辆车):"); |
|||
car1.returnCar(); // 再次还车,应该提示未租出
|
|||
|
|||
// 6. 测试setter方法
|
|||
System.out.println("\n--- 测试setter方法 ---"); |
|||
System.out.println("原日租金:" + car2.getDailyRent()); |
|||
System.out.println("尝试设置日租金为-100:"); |
|||
car2.setDailyRent(-100); |
|||
System.out.println("修改后日租金:" + car2.getDailyRent()); |
|||
|
|||
System.out.println("\n尝试设置日租金为800:"); |
|||
car2.setDailyRent(800); |
|||
System.out.println("修改后日租金:" + car2.getDailyRent()); |
|||
|
|||
// 7. 测试静态变量
|
|||
System.out.println("\n--- 统计信息 ---"); |
|||
System.out.println("总共创建的车辆数:" + Car.getTotalCars()); |
|||
|
|||
System.out.println("\n========== 测试完成 =========="); |
|||
} |
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
# Java实验报告:车辆管理系统中的Car类 |
|||
|
|||
## 一、实验目的 |
|||
1. 巩固封装的基本实现:私有属性 + 公有getter/setter |
|||
2. 练习构造方法的重载以及this关键字的作用 |
|||
3. 掌握在业务方法中加入数据校验,保护对象状态的合法性 |
|||
4. 能够编写测试类验证类的功能 |
|||
|
|||
## 二、类图 |
|||
|
|||
## 三、核心代码 |
|||
|
|||
### 1. 构造方法重载 |
|||
```java |
|||
// 全参构造 |
|||
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++; |
|||
} |
|||
|
|||
// 三参构造(使用this()调用全参构造) |
|||
public Car(String licensePlate, String brand, String model) { |
|||
this(licensePlate, brand, model, 300.0); |
|||
} |
|||
|
|||
Loading…
Reference in new issue