3 changed files with 127 additions and 0 deletions
@ -0,0 +1,87 @@ |
|||||
|
package com.rental; |
||||
|
|
||||
|
public class Car { |
||||
|
private String licensePlate; |
||||
|
private String brand; |
||||
|
private String model; |
||||
|
private double DailyRent; |
||||
|
private boolean isRented; |
||||
|
//构造静态变量totalcars
|
||||
|
public 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(){ |
||||
|
this("FV 6666","Toyota","Nissan",500.0); |
||||
|
} |
||||
|
|
||||
|
public Car(String licensePlate,String brand,String model){ |
||||
|
this(licensePlate,brand,model,300.0); |
||||
|
} |
||||
|
|
||||
|
//构造静态getTotalCars()方法
|
||||
|
public static int getTotalcars(){ |
||||
|
return totalcars; |
||||
|
} |
||||
|
//建立getter licensePlate brand model dailyRent isRented()method
|
||||
|
public String getLicensePlate(){ |
||||
|
return licensePlate; |
||||
|
} |
||||
|
public String getBrand(){ |
||||
|
return brand; |
||||
|
} |
||||
|
public String getModel(){ |
||||
|
return model; |
||||
|
} |
||||
|
|
||||
|
public double getDailyRent() { |
||||
|
return DailyRent; |
||||
|
} |
||||
|
public boolean isRented(){ |
||||
|
return isRented; |
||||
|
} |
||||
|
|
||||
|
//建立setter
|
||||
|
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==true){ |
||||
|
System.out.println("车辆已租出,无法再次租用"); |
||||
|
} |
||||
|
else{ |
||||
|
isRented=true; |
||||
|
} |
||||
|
} |
||||
|
public void returnCar(){ |
||||
|
if(isRented==false){ |
||||
|
System.out.println("车辆未被租用,无需归还"); |
||||
|
} |
||||
|
else{ |
||||
|
isRented=false; |
||||
|
} |
||||
|
} |
||||
|
public double calculateRent(int days){ |
||||
|
double money=days*DailyRent; |
||||
|
return money; |
||||
|
} |
||||
|
} |
||||
|
|
||||
@ -0,0 +1,40 @@ |
|||||
|
package com.rental; |
||||
|
|
||||
|
public class TestCar { |
||||
|
//编写静态方法displayInfo()打印车辆信息
|
||||
|
public static void displayInfo(Car car){ //static是声明静态
|
||||
|
System.out.println("车辆信息:车牌号"+car.getLicensePlate()+" 品牌"+car.getBrand()+" 型号"+car.getModel()+" 日租金(元/天)"+car.getDailyRent()); |
||||
|
} |
||||
|
public static void main(String[] args){ |
||||
|
Car mine1=new Car("FU 0001","Crown","Wilsaf",200.0); |
||||
|
Car mine2=new Car("FU 8888","Toyota","Alpha"); |
||||
|
Car mine3=new Car(); |
||||
|
displayInfo(mine1); |
||||
|
displayInfo(mine2); |
||||
|
displayInfo(mine3); |
||||
|
//调用两次rentCar()并查看提示
|
||||
|
System.out.println("--调用两次rentCar()并查看提示--"); |
||||
|
mine1.rentCar(); |
||||
|
System.out.println(mine1.isRented()); |
||||
|
mine1.rentCar(); |
||||
|
//调用两次returnCar()并查看提示
|
||||
|
System.out.println("--调用两次returnCar()并查看提示--"); |
||||
|
mine1.returnCar(); |
||||
|
System.out.println(mine1.isRented()); |
||||
|
mine1.returnCar(); |
||||
|
//计算5天的租金
|
||||
|
System.out.println("--计算5天的租金--"); |
||||
|
System.out.println("您的总租金为:"+mine1.calculateRent(5)+"元"); |
||||
|
//用setter修改租金为-100查看提示
|
||||
|
System.out.println("--用setter修改租金为-100查看提示--"); |
||||
|
System.out.println("当前日租金为:"+mine1.getDailyRent()+"元"); |
||||
|
mine1.setDailyRent(-100); |
||||
|
//查看日租金是否保持原值
|
||||
|
System.out.println("--查看日租金是否保持原值--"); |
||||
|
System.out.println("当前日租金为:"+mine1.getDailyRent()+"元"); |
||||
|
//输出车辆总数
|
||||
|
System.out.println("--输出车辆总数--"); |
||||
|
System.out.println("车辆总数为:"+Car.getTotalcars()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
Binary file not shown.
Loading…
Reference in new issue