diff --git a/w3/Car.java b/w3/Car.java new file mode 100644 index 0000000..049bb92 --- /dev/null +++ b/w3/Car.java @@ -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; + 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) { + 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("=====车辆信息====="); + System.out.println("车牌号:" + licensePlate); + System.out.println("品牌:" + brand); + System.out.println("型号:" + model); + System.out.println("日租金:" + dailyRent); + System.out.println("是否租出:" + (isRented ? "是" : "否")); + System.out.println("==================\n"); + } +} \ No newline at end of file diff --git a/w3/TestCar.java b/w3/TestCar.java new file mode 100644 index 0000000..291c3e2 --- /dev/null +++ b/w3/TestCar.java @@ -0,0 +1,34 @@ +package com.rental; + +public class TestCar { + public static void main(String[] args) { + // 创建3辆车,使用不同构造 + Car car1 = new Car("京A12345", "丰田", "Camry", 260); + Car car2 = new Car("沪B67890", "大众", "Magotan"); + Car car3 = new Car("粤C98765", "比亚迪", "Han", 350); + + // 打印信息 + car1.displayInfo(); + car2.displayInfo(); + car3.displayInfo(); + + // 测试租车/还车重复操作 + System.out.println("===测试车辆1租车==="); + car1.rentCar(); + car1.rentCar(); + + System.out.println("===测试车辆1还车==="); + car1.returnCar(); + car1.returnCar(); + + // 计算5天租金 + System.out.println("\n车辆2租用5天费用:" + car2.calculateRent(5)); + + // 测试非法租金 + System.out.println("\n===尝试设置非法租金==="); + car3.setDailyRent(-100); + + // 静态变量:总车辆数 + System.out.println("\n系统总车辆数:" + Car.getTotalCars()); + } +} \ No newline at end of file diff --git a/w3/com/rental/Car.class b/w3/com/rental/Car.class new file mode 100644 index 0000000..fb9c12f Binary files /dev/null and b/w3/com/rental/Car.class differ diff --git a/w3/com/rental/TestCar.class b/w3/com/rental/TestCar.class new file mode 100644 index 0000000..db7a5d9 Binary files /dev/null and b/w3/com/rental/TestCar.class differ diff --git a/w3/租车管理系统Car类设计实验报告.docx b/w3/租车管理系统Car类设计实验报告.docx new file mode 100644 index 0000000..bd6fb3f Binary files /dev/null and b/w3/租车管理系统Car类设计实验报告.docx differ diff --git a/w3/运行输出1.jpg b/w3/运行输出1.jpg new file mode 100644 index 0000000..6fc798a Binary files /dev/null and b/w3/运行输出1.jpg differ diff --git a/w3/运行输出2.jpg b/w3/运行输出2.jpg new file mode 100644 index 0000000..47a0e9d Binary files /dev/null and b/w3/运行输出2.jpg differ