1 changed files with 83 additions and 0 deletions
@ -0,0 +1,83 @@ |
|||
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 = brand; |
|||
setDailyRent(dailyRent); |
|||
this.isRented = false; |
|||
totalCars++; |
|||
} |
|||
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 String getModel(){ |
|||
return model; |
|||
} |
|||
public double getDailyRent(){ |
|||
return this.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("日租金必须大于0,设置失败,保持原值。"); |
|||
} |
|||
} |
|||
//业务方法
|
|||
public void rentCar() { |
|||
if (this.isRented) { |
|||
System.out.println("车辆已租出,无法再次租用。"); |
|||
} else { |
|||
this.isRented = true; |
|||
System.out.println("车辆" + this.licensePlate + "租用成功。"); |
|||
} |
|||
} |
|||
public void returnCar() { |
|||
if (!this.isRented) { |
|||
System.out.println("车辆未被租用,无需归还。"); |
|||
} else { |
|||
this.isRented = false; |
|||
System.out.println("车辆" + this.licensePlate + "归还成功。"); |
|||
} |
|||
} |
|||
public double calculateRent(int days) { |
|||
return this.dailyRent * days; |
|||
} |
|||
//静态方法
|
|||
public static int getTotalCars() { |
|||
return totalCars; |
|||
} |
|||
//displayInfo方法
|
|||
public void displayInfo() { |
|||
String status = this.isRented ? "已租出" : "可租"; |
|||
System.out.println("车牌: " + this.licensePlate + ", 品牌: " + this.brand + |
|||
", 型号: " + this.model + ", 日租金: " + this.dailyRent + |
|||
"元/天, 状态: " + status); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue