1.汽车类(父类)
//汽车类 public abstract class Automobile { private String brand;//品牌 private String numberPlate;//车牌号 private double dailyRent;//日租金 public Automobile() { } public Automobile(String brand, String numberPlate, double dailyRent) { super(); this.brand = brand; this.numberPlate = numberPlate; this.dailyRent = dailyRent; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getNumberPlate() { return numberPlate; } public void setNumberPlate(String numberPlate) { this.numberPlate = numberPlate; } public double getDailyRent() { return dailyRent; } public void setDailyRent(double dailyRent) { this.dailyRent = dailyRent; } //日租金计算方法 public abstract double countRent(int days); }
2.轿车类(子类)
//轿车类 public class Car extends Automobile { private String model;// 汽车型号 public Car() { } public Car(String brand, String numberPlate, double dailyRent, String model) { super(brand, numberPlate, dailyRent); this.model = model; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } //重写日租金计算方法 @Override public double countRent(int days) { double price = this.getDailyRent() * days; if (days > 150) { price *= 0.7; }else if(days>30){ price *= 0.8; }else if(days>7){ price *= 0.9; } return price; } }
3.客车类(子类)
//客车类 public class Passenger extends Automobile { private int seatNum;//座位数 public Passenger() { } public int getSeatNum() { return seatNum; } public void setSeatNum(int seatNum) { this.seatNum = seatNum; } public Passenger(String brand, String numberPlate, double dailyRent, int seatNum) { super(brand, numberPlate, dailyRent); this.seatNum = seatNum; } //重写日租金计算方法 @Override public double countRent(int days) { double price = this.getDailyRent() * 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; } }
4.业务逻辑类
//汽车业务类 public class MotoOperation { // 创建汽车类数组 Automobile[] autos = new Automobile[8]; // 初始化汽车方法 public void init() { // 初始化轿车 Car car1 = new Car("宝马", "京NY28588", 800, "X6"); Car car2 = new Car("宝马", "京CNY3284", 600, "550i"); Car car3 = new Car("别克", "京NT37465", 300, "林荫大道"); Car car4 = new Car("别克", "京NT96968", 600, "GL8"); // 初始化客车 Passenger pg1 = new Passenger("金杯", "京6566754", 800, 16); Passenger pg2 = new Passenger("金龙", "京8696997", 800, 16); Passenger pg3 = new Passenger("金杯", "京9696996", 1500, 34); Passenger pg4 = new Passenger("金龙", "京8696998", 1500, 34); // 将汽车放入数组 autos[0] = car1; autos[1] = car2; autos[2] = car3; autos[3] = car4; autos[4] = pg1; autos[5] = pg2; autos[6] = pg3; autos[7] = pg4; } // 租车方法 public Automobile motoLeaseOut(String brand, String model, int seatNum) { Automobile auto = null; //查询整个数组,找到符合顾客要求的车 for (Automobile rentAuto : autos) { //如果是轿车,需要品牌和型号 if (rentAuto instanceof Car) { Car car = (Car) rentAuto; if (car.getBrand().equals(brand) && car.getModel().equals(model)) { auto = car; break; } } else { //如果是客车,需要品牌和座位数 Passenger pg = (Passenger) rentAuto; if(pg.getBrand().equals(brand)&&pg.getSeatNum()==seatNum){ auto = pg; break; } } } return auto; } }
5.测试类
import java.util.Scanner; import cn.bdqn.vehicle.Automobile; public class RentMgr { public static final Scanner sc = new Scanner(System.in); public static void main(String[] args) { MotoOperation motoMgr = new MotoOperation(); // 初始化汽车方法 motoMgr.init(); System.out.println("************欢迎光临汽车租赁系统************"); System.out.println("请选择您想租车的类型:(1.轿车 2.客车)"); // 顾客租车条件 String brand = "";// 汽车品牌 String model = "";// 汽车型号 int seatNum = 0;// 座位数 int index = judge(); if (index == 1) { // 租轿车 System.out.println("请选择您要租的汽车品牌:(1.宝马 2.别克)"); index = judge(); if (index == 1) { brand = "宝马"; System.out.println("请选择汽车型号:(1.X6 2.550i)"); index = judge(); if (index == 1) { model = "X6"; } else if (index == 2) { model = "550i"; } } else if (index == 2) { brand = "别克"; System.out.println("请选择汽车型号:(1.林荫大道 2.GL8)"); index = judge(); if (index == 1) { model = "林荫大道"; } else if (index == 2) { model = "GL8"; } } } else if (index == 2) { // 租客车 System.out.println("请选择您要租的汽车品牌:(1.金杯 2.金龙)"); index = judge(); if (index == 1) { brand = "金杯"; System.out.println("请选择汽车座位数:(1.16人 2.34人)"); index = judge(); if (index == 1) { seatNum = 16; } else if (index == 2) { seatNum = 34; } } else if (index == 2) { brand = "金龙"; System.out.println("请选择汽车座位数:(1.16人 2.34人)"); index = judge(); if (index == 1) { seatNum = 16; } else if (index == 2) { seatNum = 34; } } } Automobile auto = motoMgr.motoLeaseOut(brand, model, seatNum); System.out.println("请输入您要租赁天数:"); int days = sc.nextInt(); double money = auto.countRent(days); System.out.println("租车成功!请按照车牌号提车!车牌号为:"+auto.getNumberPlate()); System.out.println("您需要支付"+money+"元!"); } //输入数据效验 public static int judge(){ int index; do { index = sc.nextInt(); if (index > 2 || index < 1) { System.out.println("请选择正确品数据!"); } } while (index > 2 || index < 1); return index; } }