车型 | 具体信息 | 日租金 | 折扣 |
轿车 | 宝马X6(京NY28588) | 800 | days>7天9折 days>30天8折 days>150天7折 |
宝马550i(京CNY3284) | 600 | ||
别克林荫大道(京NT37465) | 300 | ||
别克GL8(京NT96968) | 600 | ||
客车 | 金杯,16座(京6566754) | 800 | days>=3天9折 days>=7天8折 days>=30天7折 days>=150天6折 |
金龙,16座(京8696997) | |||
金杯,34座(京9696996) | 1500 | ||
金龙,34座(京8696998) |
汽车类:车牌号、品牌、日租金
客车类:车牌号、品牌、日租金、座位数
轿车类:车牌号、品牌、日租金、型号
汽车业务类:忽略
汽车租赁管理类:忽略
父类:MotoVehicle类
public abstract class MotoVehicle {
private String vehicleId;//车牌号
private String brand;//品牌
private double perRent;//日租金
public MotoVehicle() {
super();
}
public MotoVehicle(String vehicleId, String brand, double perRent) {
super();
this.vehicleId = vehicleId;
this.brand = brand;
this.perRent = perRent;
}
public String getVehicleId() {
return vehicleId;
}
public void setVehicleId(String vehicleId) {
this.vehicleId = vehicleId;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPerRent() {
return perRent;
}
public void setPerRent(double perRent) {
this.perRent = perRent;
}
public abstract double calRent(int days);//计算租金
}
子类:Car类
public class Car extends MotoVehicle {
private String type;//型号
public Car() {
super();
}
public Car(String vehicleId, String brand, double perRent, String type) {
super(vehicleId, brand, perRent);
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public double calRent(int days) {//重写租金
double price = this.getPerRent()*days;
if (days>150) {
price*=0.7;
}else if (days>30) {
price*=0.8;
}else if (days>7) {
price*=0.9;
}
return price;
}
}
子类:Bus类
public class Bus extends MotoVehicle {
private int seatCount;//座位数
public Bus() {
super();
}
public Bus(String vehicleId, String brand, double perRent, int seatCount) {
super(vehicleId, brand, perRent);
this.seatCount = seatCount;
}
public int getSeatCount() {
return seatCount;
}
public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
@Override
public double calRent(int days) {//重写租金
double price = this.getPerRent()*days;
if (days>=150) {
price*=0.6;
}else if (days>=30) {
price*=0.7;
}else if (days>=7) {
price*=0.8;
}else if (days>=3) {
price*=0.9;
}
return price;
}
}
业务类:MotoOperation 类
public class MotoOperation {
public MotoVehicle[] motos=new MotoVehicle[8];
public void init(){//初始化
motos[0]=new Car("京NY28588", "宝马", 800, "X6");
motos[1]=new Car("京CNY3284", "宝马", 600, "550i");
motos[2]=new Car("京NT37465", "别克", 300, "林荫大道");
motos[3]=new Car("京NT96968", "别克", 600, "GLB");
motos[4]=new Bus("京6566754", "金杯", 800, 16);
motos[5]=new Bus("京8696997", "金龙", 800, 16);
motos[6]=new Bus("京9696996", "金杯", 1500, 34);
motos[7]=new Bus("京8696998", "金龙", 1500, 34);
}
//租赁汽车
public MotoVehicle motoLeaseOut(String brand,String type,int seat){
MotoVehicle moto=null;
for (MotoVehicle mymoto : motos) {
if (mymoto instanceof Car) {//判断类型
Car car=(Car) mymoto;
if (car.getBrand().equals(brand)&&car.getType().equals(type)) {
moto=car;
break;
}
}else if (mymoto instanceof Bus) {
Bus bus=(Bus) mymoto;
if (bus.getBrand().equals(brand)&&bus.getSeatCount()==seat) {
moto=bus;
break;
}
}
}
return moto;
}
}
测试类:RentMgrSys 类
import java.util.Scanner;
public class RentMgrSys {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
MotoOperation motoMgr = new MotoOperation();
motoMgr.init();
MotoVehicle moto=null;
System.out.println("****欢迎光临腾飞汽车租赁有限公司****");
System.out.println("1、轿车 \t2、客车");
System.out.println("请输入您要租赁的汽车类型:");
int choose = sc.nextInt();
String brand = "";
String type = "";
int seat = 0;
if (choose == 1) {//选择汽车
System.out.println("请输入您要租赁的汽车品牌:1、别克 \t2、宝马");
int choose2 = sc.nextInt();
if (choose2 == 1) {
brand = "别克";
System.out.println("请输入您要租赁的汽车类型:1、林荫大道\t2、GLB");
int choose3 = sc.nextInt();
type = (choose3 == 1) ? "林荫大道" : "GLB";
} else if (choose2 == 2) {
brand = "宝马";
System.out.println("请输入您要租赁的汽车类型:1、X6 \t2、550i");
int choose3 = sc.nextInt();
type = (choose3 == 1) ? "X6" : "550i";
}
}else if (choose == 2) {
System.out.println("请输入您要租赁的客车品牌:1、金杯 \t2、金龙");
int choose2 = sc.nextInt();
if (choose2 == 1) {
brand = "金杯";
System.out.println("请输入您要租赁的客车座位:1、16座 \t2、34座");
int choose3 = sc.nextInt();
seat = (choose3 == 1) ? 16 : 34;
} else if (choose2 == 2) {
brand = "金龙";
System.out.println("请输入您要租赁的汽车类型:1、16座 \t2、34座");
int choose3 = sc.nextInt();
seat = (choose3 == 1) ? 16 : 34;
}
}
moto=motoMgr.motoLeaseOut(brand, type, seat);//获得汽车
System.out.println("请输入您要租赁的天数:");
int days=sc.nextInt();
double sumprice=moto.calRent(days);//费用
System.out.println("分配给你的汽车牌号是:"+moto.getVehicleId());
System.out.println("您需要租赁的汽车费用是:"+sumprice+"元");
sc.close();
}
}
结果: