//抽象类 父类 汽车类
package 对象和集合.汽车租赁系统.vehicle; //汽车类 public abstract class MotoVehicle { //车牌号 品牌 日租金 private String vehicleId; private String brand; private int perRent; public MotoVehicle() { } public MotoVehicle(String vehicleId, String brand, int perRent) { 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 int getPerRent() { return perRent; } public void setPerRent(int perRent) { this.perRent = perRent; } public String toString() { return "MotoVehicle{vehicleId = " + vehicleId + ", brand = " + brand + ", perRent = " + perRent + "}"; } //计算租金 抽象方法 public abstract float caleRent(int days); }
//子类 bus
package 对象和集合.汽车租赁系统.vehicle; public class Bus extends MotoVehicle { //座位数 private int seatCount ; public Bus() { } public Bus(String vehicleId, String brand, int perRent, int seatCount) { super(vehicleId, brand, perRent); this.seatCount = seatCount; } /** * 获取 * @return seatCount */ public int getSeatCount() { return seatCount; } /** * 设置 * @param seatCount */ public void setSeatCount(int seatCount) { this.seatCount = seatCount; } //计算客车类的租金 @Override public float caleRent(int days) { float price = this.getPerRent()*days; if (days>=3&&days<7){ price*=0.9f; }else if (days>=7&&days<30){ price*=0.8f; }else if (days>=30&&days<150){ price*=0.7f; }else if (days>150){ price*=0.6f; } return price; } }
//子类 car
package 对象和集合.汽车租赁系统.vehicle; public class Car extends MotoVehicle { //型号 private String type; public Car() { } public Car(String vehicleId, String brand, int 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 float caleRent(int days) { float price = this.getPerRent()*days; if (days>7&&days<=30){ price*=0.9f; }else if (days>30&&days<=150){ price*=0.8f; }else if (days>150){ price*=0.7f; } return price; } }
//子类 truck
package 对象和集合.汽车租赁系统.vehicle; public class Truck extends MotoVehicle { //吨 private double tonnage ; public Truck() { } public Truck(String vehicleId, String brand, int perRent, double tonnage) { super(vehicleId, brand, perRent); this.tonnage = tonnage; } /** * 获取 * @return tonnage */ public double getTonnage() { return tonnage; } /** * 设置 * @param tonnage */ public void setTonnage(double tonnage) { this.tonnage = tonnage; } @Override public float caleRent(int days) { float price = this.getPerRent()*days; if (days>=3&&days<7){ price*=0.9f; }else if (days>=7&&days<30){ price*=0.8f; }else if (days>=30&&days<150){ price*=0.7f; }else if (days>150){ price*=0.6f; } return price; } }
//初始化 及一些功能需求
package 对象和集合.汽车租赁系统.mgr; import 对象和集合.汽车租赁系统.vehicle.Bus; import 对象和集合.汽车租赁系统.vehicle.Car; import 对象和集合.汽车租赁系统.vehicle.MotoVehicle; import 对象和集合.汽车租赁系统.vehicle.Truck; import java.util.ArrayList; import java.util.List; //汽车业务类 public class MotoOperation { public static final List<MotoVehicle> motos =new ArrayList<>(); //初始化且车信息 public void init(){ motos.add(new Car("皖M78614","宝马",800,"x6")); motos.add(new Car("皖S66666","宝马",600,"550i")) ; motos.add(new Car("皖D55555","别克",300,"林荫大道")) ; motos.add(new Car("皖S44444","别克",600,"GL8")); motos.add(new Car("皖A88888","劳斯莱斯",2000,"库里南")) ; motos.add(new Car("皖A99999","劳斯莱斯",3000,"幻影")) ; motos.add(new Bus("皖I78614","金杯",800,16)); motos.add( new Bus("皖B64516","金杯",1500,34)); motos.add( new Bus("皖B66461","金龙",800,16)); motos.add(new Bus("皖F88954","金龙",1500,34)) ; motos.add(new Bus("皖P66461","金龟",800,16)) ; motos.add( new Bus("皖O88954","金龟",1500,34)); motos.add( new Truck("皖U66461","奔驰",1500,20.0)); motos.add(new Truck("皖Y88954","飞利",3000,55.5)) ; } //租车 根据用户需求去查找数组中的相应的车辆并返回 public MotoVehicle motoLeaseOut(String brand, String type, int seat, double tonnage){ MotoVehicle moto =null; for (MotoVehicle motoVehicle : motos) { if (motoVehicle instanceof Car){ Car car= (Car) motoVehicle; if (car.getBrand().equals(brand)&&car.getType().equals(type)){ moto=car; break; } } else if (motoVehicle instanceof Bus) { Bus bus = (Bus) motoVehicle; if (bus.getBrand().equals(brand)&&bus.getSeatCount()==seat){ moto =bus; break; } } else if (motoVehicle instanceof Truck) { Truck truck = (Truck) motoVehicle; if (truck.getBrand().equals(brand)&&truck.getTonnage()==tonnage){ moto=truck; break; } } } return moto; } }
//主类
package 对象和集合.汽车租赁系统.mgr; import 对象和集合.汽车租赁系统.vehicle.MotoVehicle; import java.util.Scanner; //汽车租赁管理类 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("------------欢迎光临租赁豪车公司-------------"); String barnd= null; //品牌 String type= null; //型号 int seat = 0; //座位数 double tonnage= 0;//吨位 String num="n"; b:while (true) { System.out.println("1、轿车\t2、客车\t3、卡车"); System.out.println("请选择你要租的车:"); int motoType =SC.nextInt(); barnd = ""; type = " "; seat = 0; tonnage = 0; if (motoType==1) { //租赁轿车 do{ System.out.println("请选择你要租赁的品牌: 1、宝马\t2、别克\t3、劳斯莱斯"); int choose = SC.nextInt(); switch (choose){ case 1: barnd="宝马"; System.out.println("请输入你的汽车型号:1、x6\t2、550i"); type=(SC.nextInt()==1)?"x6":"550i"; break b; case 2: barnd="别克"; System.out.println("请输入你的汽车型号:1、林荫大道\t2、GL8"); type=(SC.nextInt()==1)?"林荫大道":"GL8"; break b; case 3: barnd="劳斯莱斯"; System.out.println("请输入你的汽车型号:1、库里南\t2、幻影"); type=(SC.nextInt()==1)?"库里南":"幻影"; break b; default: System.out.println("输入错误!"); System.out.println("是否要继续y/n"); num=SC.next(); if (num.equals("n")){ continue b; } } }while (true); } else if (motoType==2) { type=""; do { System.out.println("请选择你要租赁的品牌: 1、金杯\t2、金龙\t3、金龟"); int choose = SC.nextInt(); switch (choose){ case 1: barnd="金杯"; System.out.println("请输入你要选择的座位数:1、16\t2、34"); seat=(SC.nextInt()==1)?16:34; break b; case 2: barnd="金龙"; System.out.println("请输入你要选择的座位数:1、16\t2、34"); seat=(SC.nextInt()==1)?16:34; break b; case 3: barnd="金龟"; System.out.println("请输入你要选择的座位数:1、16\t2、34"); seat=(SC.nextInt()==1)?16:34; break b; default: System.out.println("输入错误!"); System.out.println("是否要继续y/n"); num=SC.next(); if (num.equals("n")){ continue b; } } } while (true); }else if (motoType==3) { type=""; do { System.out.println("请选择你要租赁的品牌: 1、奔驰\t2、飞利"); int choose = SC.nextInt(); switch (choose){ case 1: barnd="奔驰"; System.out.println("请输入你要选择的吨位数:1、20.0\t2、55.5"); tonnage=(SC.nextInt()==1)?20.0:55.5; break b; case 2: barnd="飞利"; System.out.println("请输入你要选择的吨位数:1、20.0\t2、55.5"); tonnage=(SC.nextInt()==1)?20.0:55.5; break b; default: System.out.println("输入错误!"); System.out.println("是否要继续y/n"); num=SC.next(); if (num.equals("n")){ continue b; } } } while (true); }else { System.out.println("输入错误请重新输入!"); } } //租车 MotoVehicle moto = motomgr.motoLeaseOut(barnd, type, seat, tonnage); System.out.println("请输入您的租赁天数"); int days = SC.nextInt(); float momey = moto.caleRent(days); System.out.println("租车成功,车牌号是:" + moto.getVehicleId()); System.out.println("您需要支付:" + momey + "元"); } }
如有需要可自行进行扩展!!!!