1 changed files with 95 additions and 0 deletions
@ -0,0 +1,95 @@ |
|||||
|
package com.rental; |
||||
|
|
||||
|
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) { |
||||
|
this.licensePlate = licensePlate; |
||||
|
this.brand = brand; |
||||
|
this.model = model; |
||||
|
this.dailyRent = dailyRent; |
||||
|
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,当前值:" + dailyRent + ",保持原值:" + this.dailyRent); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
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; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue