3 changed files with 115 additions and 0 deletions
@ -0,0 +1,74 @@ |
|||
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; |
|||
if (dailyRent > 0) { |
|||
this.dailyRent = dailyRent; |
|||
} else { |
|||
this.dailyRent = 300; |
|||
System.out.println("日租金不合法"); |
|||
} |
|||
this.isRented=false; |
|||
Car.totalCars+=1; |
|||
} |
|||
public String getLicensePlate(){ |
|||
return licensePlate; |
|||
} |
|||
public Car(String licensePlate, String brand, String model) { |
|||
this(licensePlate, brand, model, 300); |
|||
} |
|||
public String getBrand(){ |
|||
return brand; |
|||
} |
|||
public String getModel(){ |
|||
return model; |
|||
} |
|||
public double getDailyRent(){ |
|||
return dailyRent; |
|||
} |
|||
public boolean isRented() { |
|||
return isRented; |
|||
} |
|||
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("租金不合法"); |
|||
} |
|||
} |
|||
public void rentCar(){ |
|||
if(isRented){ |
|||
System.out.println("车辆已租出,无法再次租用"); |
|||
return; |
|||
} |
|||
this.isRented=true; |
|||
} |
|||
public void returnCar(){ |
|||
if (!isRented){ |
|||
System.out.println("车辆未被租用,无需归还"); |
|||
return; |
|||
} |
|||
this.isRented=false; |
|||
} |
|||
public double calculateRent(int days){ |
|||
return dailyRent*days; |
|||
} |
|||
public static int getTotalCars(){ |
|||
return totalCars; |
|||
} |
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
package com.rental; |
|||
public class TestCar { |
|||
public static void main(String[] args) { |
|||
// 构造方法
|
|||
Car car1 = new Car("A123", "Toyota", "Camry", 300); |
|||
Car car2 = new Car("B456", "Honda", "Civic"); // 默认租金
|
|||
Car car3 = new Car("C789", "BMW", "X5", -100); // 非法租金测试
|
|||
// 输出车辆信息
|
|||
System.out.println("所有车辆信息:"); |
|||
displayInfo(car1); |
|||
displayInfo(car2); |
|||
displayInfo(car3); |
|||
// 流程测试
|
|||
System.out.println("租车流程测试:"); |
|||
car1.rentCar(); // 第一次租
|
|||
car1.rentCar(); // 再租
|
|||
car1.returnCar(); // 第一次还
|
|||
car1.returnCar(); // 再还
|
|||
//计算租金
|
|||
System.out.println("租金计算:"); |
|||
double rent = car2.calculateRent(5); |
|||
System.out.println("car2 租用5天费用: " + rent); |
|||
// 非法租金
|
|||
System.out.println("非法租金测试:"); |
|||
car2.setdailyRent(-100); // 给一个非法值
|
|||
System.out.println("car2 当前日租金: " + car2.getDailyRent()); |
|||
|
|||
// 输出总车辆数
|
|||
System.out.println("总车辆数"); |
|||
System.out.println("Total Cars: " + Car.getTotalCars()); |
|||
} |
|||
//输出车辆信息
|
|||
public static void displayInfo(Car car) { |
|||
System.out.println("车牌号: " + car.getLicensePlate()); |
|||
System.out.println("品牌: " + car.getBrand()); |
|||
System.out.println("型号: " + car.getModel()); |
|||
System.out.println("日租金: " + car.getDailyRent()); |
|||
System.out.println("是否已租出: " + (car.isRented() ? "是" : "否")); |
|||
} |
|||
} |
|||
|
|||
|
After Width: | Height: | Size: 55 KiB |
Loading…
Reference in new issue