2 changed files with 138 additions and 0 deletions
@ -0,0 +1,138 @@ |
|||
package com.rental; |
|||
|
|||
public class TestCar { |
|||
public static void main(String[] args) { |
|||
// 创建3辆车
|
|||
Car car1 = new Car("京A12345", "丰田", "凯美瑞", 500.0); |
|||
Car car2 = new Car("沪B67890", "大众", "帕萨特"); |
|||
Car car3 = new Car("粤C54321", "宝马", "3系", 800.0); |
|||
|
|||
// 输出车辆信息
|
|||
System.out.println("=====车辆信息====="); |
|||
car1.displayInfo(); |
|||
car2.displayInfo(); |
|||
car3.displayInfo(); |
|||
|
|||
// 测试租车还车
|
|||
System.out.println("=====租赁测试====="); |
|||
car1.rentCar(); |
|||
car1.rentCar(); |
|||
car1.returnCar(); |
|||
car1.returnCar(); |
|||
|
|||
// 计算5天租金
|
|||
System.out.println("\n=====5天租金====="); |
|||
double money = car3.calculateRent(5); |
|||
System.out.println("5天总租金:" + money); |
|||
|
|||
// 测试非法租金
|
|||
System.out.println("\n=====测试非法日租金====="); |
|||
car2.setDailyRent(-100); |
|||
|
|||
// 车辆总数
|
|||
System.out.println("\n=====总车辆数====="); |
|||
System.out.println("总车辆数:" + Car.getTotalCars()); |
|||
} |
|||
} |
|||
|
|||
// Car 类写在同一个文件里
|
|||
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; |
|||
if (dailyRent > 0) { |
|||
this.dailyRent = dailyRent; |
|||
} else { |
|||
this.dailyRent = 300; |
|||
System.out.println("日租金非法,使用默认300元"); |
|||
} |
|||
this.isRented = false; |
|||
totalCars++; |
|||
} |
|||
|
|||
// 三参构造
|
|||
public Car(String licensePlate, String brand, String model) { |
|||
this(licensePlate, brand, model, 300.0); |
|||
} |
|||
|
|||
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 void displayInfo() { |
|||
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; |
|||
} |
|||
} |
|||
Binary file not shown.
Loading…
Reference in new issue