5 changed files with 239 additions and 0 deletions
@ -0,0 +1 @@ |
|||
prompt:为car.java文件创建一份详细的README文档和一个程序输出文本文件。README文档应包含项目概述、功能特性、类结构说明、使用方法、编译运行步骤、依赖环境要求、示例代码以及注意事项等内容。程序输出文本文件需要展示car.java程序的实际运行结果,包括所有可能的输出场景、测试用例执行结果和程序状态信息,确保输出内容完整、格式规范且易于理解。 |
|||
@ -0,0 +1,89 @@ |
|||
package JavaLearningProject.w3; |
|||
|
|||
public class Car { |
|||
private 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) { |
|||
if (licensePlate == null || licensePlate.trim().isEmpty()) { |
|||
throw new IllegalArgumentException("licensePlate must not be empty"); |
|||
} |
|||
if (brand == null || brand.trim().isEmpty()) { |
|||
throw new IllegalArgumentException("brand must not be empty"); |
|||
} |
|||
if (model == null || model.trim().isEmpty()) { |
|||
throw new IllegalArgumentException("model must not be empty"); |
|||
} |
|||
if (dailyRent <= 0) { |
|||
throw new IllegalArgumentException("dailyRent must be positive"); |
|||
} |
|||
|
|||
this.licensePlate = licensePlate; |
|||
this.brand = brand; |
|||
this.model = model; |
|||
this.dailyRent = 300.0; |
|||
this.isRented = false; |
|||
totalCars++; |
|||
} |
|||
|
|||
public String getLicensePlate() { |
|||
return licensePlate; |
|||
} |
|||
|
|||
public String getBrand() { |
|||
return brand; |
|||
} |
|||
|
|||
public void setBrand(String brand) { |
|||
if (brand == null || brand.trim().isEmpty()) { |
|||
throw new IllegalArgumentException("brand must not be empty"); |
|||
} |
|||
this.brand = brand; |
|||
} |
|||
|
|||
public String getModel() { |
|||
return model; |
|||
} |
|||
|
|||
public void setModel(String model) { |
|||
if (model == null || model.trim().isEmpty()) { |
|||
throw new IllegalArgumentException("model must not be empty"); |
|||
} |
|||
this.model = model; |
|||
} |
|||
|
|||
public void setDailyRent(double dailyRent) { |
|||
if (dailyRent <= 0) { |
|||
throw new IllegalArgumentException("dailyRent must be positive"); |
|||
} |
|||
this.dailyRent = dailyRent; |
|||
} |
|||
|
|||
public boolean isRented() { |
|||
return isRented; |
|||
} |
|||
|
|||
public boolean rent() { |
|||
if (isRented) { |
|||
return false; |
|||
} |
|||
isRented = true; |
|||
return true; |
|||
} |
|||
|
|||
public double calculateRent(int days) { |
|||
if (days <= 0) { |
|||
throw new IllegalArgumentException("days must be positive"); |
|||
} |
|||
return dailyRent * days; |
|||
} |
|||
|
|||
public static int getTotalCars() { |
|||
return totalCars; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,59 @@ |
|||
package JavaLearningProject.w3; |
|||
|
|||
public class CarRunner { |
|||
public static void main(String[] args) { |
|||
System.out.println("=== 汽车租赁系统测试 ==="); |
|||
System.out.println("当前系统总车辆数: " + Car.getTotalCars()); |
|||
|
|||
System.out.println("\n--- 场景 1: 创建合法的 Car 对象 ---"); |
|||
Car car1 = new Car("京A88888", "Toyota", "Camry", 200.0); |
|||
System.out.println("创建车辆成功: " + car1.getBrand() + " " + car1.getModel() + " (车牌: " + car1.getLicensePlate() + ")"); |
|||
System.out.println("当前系统总车辆数: " + Car.getTotalCars()); |
|||
|
|||
System.out.println("\n--- 场景 2: 测试属性更新 (Setters) ---"); |
|||
car1.setBrand("Honda"); |
|||
car1.setModel("Accord"); |
|||
car1.setDailyRent(250.0); |
|||
System.out.println("更新后车辆信息: " + car1.getBrand() + " " + car1.getModel() + ", 日租金: " + car1.calculateRent(1)); |
|||
|
|||
System.out.println("\n--- 场景 3: 测试租赁逻辑 (Rent) ---"); |
|||
System.out.println("车辆当前是否已租出? " + car1.isRented()); |
|||
boolean rentResult = car1.rent(); |
|||
System.out.println("尝试租出车辆... 结果: " + (rentResult ? "成功" : "失败")); |
|||
System.out.println("车辆当前是否已租出? " + car1.isRented()); |
|||
|
|||
System.out.println("\n--- 场景 4: 重复租赁测试 ---"); |
|||
boolean rentResult2 = car1.rent(); |
|||
System.out.println("尝试再次租出该车辆... 结果: " + (rentResult2 ? "成功" : "失败")); |
|||
|
|||
System.out.println("\n--- 场景 5: 租金计算测试 ---"); |
|||
try { |
|||
double rentFor5Days = car1.calculateRent(5); |
|||
System.out.println("租用 5 天的总租金为: " + rentFor5Days); |
|||
} catch (Exception e) { |
|||
System.out.println("租金计算异常: " + e.getMessage()); |
|||
} |
|||
|
|||
System.out.println("\n--- 场景 6: 异常处理测试 (负数天数计算) ---"); |
|||
try { |
|||
car1.calculateRent(-2); |
|||
} catch (IllegalArgumentException e) { |
|||
System.out.println("捕获预期异常: " + e.getMessage()); |
|||
} |
|||
|
|||
System.out.println("\n--- 场景 7: 异常处理测试 (创建非法车辆) ---"); |
|||
try { |
|||
new Car("", "Ford", "Civic", 100.0); |
|||
} catch (IllegalArgumentException e) { |
|||
System.out.println("捕获预期异常(车牌为空): " + e.getMessage()); |
|||
} |
|||
|
|||
try { |
|||
new Car("沪B12345", "Ford", "Civic", -50.0); |
|||
} catch (IllegalArgumentException e) { |
|||
System.out.println("捕获预期异常(租金为负): " + e.getMessage()); |
|||
} |
|||
|
|||
System.out.println("\n=== 测试结束 ==="); |
|||
} |
|||
} |
|||
@ -0,0 +1,90 @@ |
|||
# 汽车租赁系统 - Car 类说明文档 |
|||
|
|||
## 项目概述 |
|||
本项目是一个基于 Java 的简单汽车租赁系统核心实体类 (`Car.java`)。该类用于表示一辆汽车的基本属性,并提供了汽车租赁、租金计算以及车辆信息管理等功能。本项目非常适合 Java 初学者学习面向对象编程(OOP)中的封装、构造方法、静态变量以及异常处理等核心概念。 |
|||
|
|||
## 功能特性 |
|||
- **车辆信息封装**:记录并管理车辆的车牌号、品牌、型号、日租金及租赁状态。 |
|||
- **全局统计功能**:使用静态变量(`static`)自动追踪系统内创建的车辆总数。 |
|||
- **数据有效性校验**:在创建对象和更新属性时,针对非法输入(如空字符串、负数租金等)抛出 `IllegalArgumentException`,保证数据的一致性和安全性。 |
|||
- **租赁状态管理**:提供了租车方法 `rent()`,可避免车辆被重复租赁。 |
|||
- **租金计算**:根据用户提供的租赁天数动态计算总租金。 |
|||
|
|||
## 类结构说明 |
|||
|
|||
### 核心类:`JavaLearningProject.w3.Car` |
|||
|
|||
#### 1. 成员变量 |
|||
- `private String licensePlate`: 车牌号(只读,创建后不可修改) |
|||
- `private String brand`: 汽车品牌 |
|||
- `private String model`: 汽车型号 |
|||
- `private double dailyRent`: 日租金(默认值为 300.0,可修改) |
|||
- `private boolean isRented`: 租赁状态(`true` 表示已租出,`false` 表示未租出) |
|||
- `private static int totalCars`: 系统中注册的总车辆数 |
|||
|
|||
#### 2. 构造方法 |
|||
- `public Car(String licensePlate, String brand, String model, double dailyRent)` |
|||
初始化一辆车,并验证参数的合法性,若合法则 `totalCars` 自增 1。 |
|||
|
|||
#### 3. 核心方法 |
|||
- **Getters / Setters**:提供对 `brand`、`model` 和 `dailyRent` 的安全访问与修改。 |
|||
- `public boolean rent()`:尝试租出车辆。若车辆未租出,则将状态改为已租出并返回 `true`;若已租出,返回 `false`。 |
|||
- `public double calculateRent(int days)`:计算给定天数的租金。天数必须为正数。 |
|||
- `public static int getTotalCars()`:获取系统内创建的总车辆数。 |
|||
|
|||
## 使用方法 & 示例代码 |
|||
|
|||
您可以参考同目录下的 `CarRunner.java` 进行测试。以下是一个快速使用的代码示例: |
|||
|
|||
```java |
|||
import JavaLearningProject.w3.Car; |
|||
|
|||
public class Main { |
|||
public static void main(String[] args) { |
|||
// 1. 创建车辆对象 |
|||
Car myCar = new Car("京A88888", "Toyota", "Camry", 200.0); |
|||
|
|||
// 2. 查看车辆状态 |
|||
System.out.println("车辆品牌: " + myCar.getBrand()); |
|||
System.out.println("是否已租出: " + myCar.isRented()); |
|||
|
|||
// 3. 租车操作 |
|||
boolean success = myCar.rent(); |
|||
System.out.println("租车结果: " + success); |
|||
|
|||
// 4. 计算 5 天的租金 |
|||
double totalCost = myCar.calculateRent(5); |
|||
System.out.println("5天租金: " + totalCost); |
|||
|
|||
// 5. 获取总车辆数 |
|||
System.out.println("总计车辆数: " + Car.getTotalCars()); |
|||
} |
|||
} |
|||
``` |
|||
|
|||
## 依赖环境要求 |
|||
- **JDK 版本**:Java 8 或以上版本。 |
|||
- **运行环境**:任何支持 Java 运行时的操作系统(Windows, macOS, Linux)。 |
|||
- **外部依赖**:纯 Java 原生实现,无需依赖任何第三方类库(如 Maven/Gradle)。 |
|||
|
|||
## 编译运行步骤 |
|||
|
|||
如果您使用命令行执行本程序,请确保当前处于项目根目录的上级目录(如包含 `JavaLearningProject` 的文件夹)。 |
|||
|
|||
**1. 编译代码** |
|||
```bash |
|||
javac JavaLearningProject/w3/Car.java JavaLearningProject/w3/CarRunner.java |
|||
``` |
|||
|
|||
**2. 运行测试程序** |
|||
```bash |
|||
java JavaLearningProject.w3.CarRunner |
|||
``` |
|||
|
|||
**3. 输出结果参考** |
|||
程序执行结果已保存至本目录下的 `output.txt` 文件中。您可以在其中查看所有可能的场景输出(包含正常情况和异常处理的捕获)。 |
|||
|
|||
## 注意事项 |
|||
1. **包名路径**:类文件使用了包声明 `package JavaLearningProject.w3;`,在编译和运行时必须严格遵循包目录结构的路径。 |
|||
2. **只读属性**:`licensePlate` (车牌号) 是车辆的唯一标识,因此在设计上仅提供了 `Getter` 方法,未提供 `Setter` 方法。 |
|||
3. **异常处理**:在传入空字符串或负数等非法参数时,程序会抛出 `IllegalArgumentException`。在实际开发中,调用方需做好 `try-catch` 处理,避免程序崩溃。 |
|||
Binary file not shown.
Loading…
Reference in new issue