本文主要是介绍第三次项目《汽车租赁》,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/*** 父类车(抽象类)*/
public abstract class Car {/**品牌*/private String brand;/**牌照*/private String licenseTag;/**日租金*/private int dayRant;public Car() {}public Car(String brand, String licenseTag, int dayRant) {this.brand = brand;this.licenseTag = licenseTag;this.dayRant = dayRant;}/**汽车信息*/public void Information() {System.out.println("品牌是:"+brand);}/**租金*/public abstract float money(int day);/**设置品牌*/public void setBrand(String brand) {this.brand = brand;}/**得到品牌*/public String getBrand() {return brand;}/**得到牌照*/public String getLicenseTag() {return licenseTag;}/**设置牌照*/public void setLicenseTag(String licenseTag) {this.licenseTag = licenseTag;}/**得到日租金*/public int getDayRant() {return dayRant;}/**设置日租金*/public void setDayRant(int dayRant) {this.dayRant = dayRant;}
}
/**客车(具体产品类)*/
public class PassengerCar extends Car{/**座位*/private String seat;public PassengerCar() {}// 构造方法public PassengerCar(String brand, String licenseTag, int dayRant, String seat) {super(brand, licenseTag, dayRant);this.seat = seat;}/**轿车租赁信息*/@Overridepublic void Information() {super.Information();System.out.println("座位数:"+seat);System.out.println("牌照是:"+getLicenseTag());System.out.println("日租金:"+getDayRant());}public String getSeat() {return seat;}public void setSeat(String seat) {this.seat = seat;}/**客车租金计算*/@Overridepublic float money(int days) {float price = this.getDayRant() * 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;}
}
/**轿车(子类) */
public class SaloonCar extends Car {/**型号*/private String type;public SaloonCar() {}// 构造方法public SaloonCar(String brand, String licenseTag, int dayRant, String type) {super(brand, licenseTag, dayRant);this.type = type;}/**轿车租赁信息 */@Overridepublic void Information() {super.Information();System.out.println("型号是:"+type);System.out.println("牌照是:"+getLicenseTag());System.out.println("日租金:"+getDayRant());}public String getType() {return type;}public void setType(String type) {this.type = type;}/**客车租金计算*/@Overridepublic float money(int days) {float price = this.getDayRant() * days;if (days > 150) {price *= 0.7; }else if(days > 30) {price *= 0.8;}else if(days > 7) {price *= 0.9;}return price;}
}
/** 用户租车 (客户类)*/
public class Client {public static void main(String[] args) {// 创建工厂类对象Factory factory = new Factory();Scanner input = new Scanner(System.in);do {System.out.println("=======欢迎来到太阳载具公司=======");System.out.println("请选择你的车型:1、汽车 2、其他");int choise = input.nextInt();// 品牌String brand = "";// 型号String type = "";// 座位数String seat = "";switch (choise) {case 1:System.out.println("请选择汽车品牌: 1、保时捷 2、布加迪");int ScBrand = input.nextInt();brand = ScBrand == 1 ? "保时捷" : "布加迪";if (ScBrand == 1) {System.out.println("请选择型号:1、911 2、新能源 3、卡宴");int ScType = input.nextInt();if (ScType == 1) {type = "911";} else if(ScType == 2) {type = "新能源";} else if(ScType == 3) {type = "卡宴";}} else {System.out.println("请选择型号:1、威龙 2、威航");type = input.nextInt() == 1 ? "威龙" : "威航";}break;case 2:System.out.println("请选择其他载具: 1、UFO 2、和谐号");int select = input.nextInt();brand = select == 1 ? "UFO" : "和谐号";System.out.println("请选择座位类型: 1、100座 2、300座");int pcSeat = input.nextInt();if (pcSeat == 1) {seat = "100座";}else {seat = "300座";}break;default:System.out.println("输入错误!");break;}// 存车到父类引用Car car = factory.rantCar(brand, type, seat);// 输出车辆信息System.out.println("恭喜您!系统选车成功!车辆信息如下:");car.Information();// 判断子类对象(动态绑定)if(car instanceof SaloonCar) {// 调用子类特有方法要强转SaloonCar sc = (SaloonCar)car;System.out.println("您租的是"+sc.getBrand()+sc.getType()+",现有租赁活动: 租车大于7天打9折,租车大于30天打8折,租车大于150天打7折.");// 判断子类对象(动态绑定)}else if(car instanceof PassengerCar){// 调用子类特有方法要强转PassengerCar pc = (PassengerCar)car;System.out.println("您租的是"+pc.getBrand()+pc.getSeat()+",现有租赁活动: 租车3天打9折,租车7天打8折,租车30天打7折,租车150天6折.");}System.out.print("请输入您租赁的天数:");// 租赁天数int rentDay = input.nextInt();// 调用子类租金计算double rant = car.money(rentDay);System.out.println("租金为:"+rant);}while(isContinue());System.out.println("后期会有更多黑科技,欢迎下次光临呦~!");}/**是否继续方法*/public static boolean isContinue() {Scanner input = new Scanner(System.in);System.out.println("是否继续(y/n)?");String select = input.next();if(select.equals("y")) {return true;}else {return false;}}
}
/**载具工厂(工厂类)*/
public class Factory {public static Car[] car = new Car[8];/**初始化载具信息*/static{car[0] = new SaloonCar("保时捷", "京11111", 1800, "911");car[1] = new SaloonCar("保时捷", "京22222", 1500, "新能源");car[2] = new SaloonCar("保时捷", "京33333", 1200, "卡宴");car[3] = new SaloonCar("布加迪", "京44444", 2000, "威航");car[4] = new PassengerCar("UFO", "宇55555", 2200, "100座");car[5] = new PassengerCar("和谐号", "GYYYY", 3200, "300座");car[6] = new PassengerCar("UFO", "$$$$$$", 10000, "300座");car[7] = new PassengerCar("和谐号", "FY0000", 4200, "100座");}/*** 判断租哪辆载具* @param brand 品牌* @param type 型号* @param seat 座位* @return 返回Car*/public Car rantCar(String brand, String type, String seat) {for (Car car1 : this.car) {// 租轿车if(car1 instanceof SaloonCar) {// 调用子类特有方法 SaloonCar sc = (SaloonCar)car1;if (sc.getBrand().equals(brand) && sc.getType().equals(type)) {return sc;}// 租其他载具}else if(car1 instanceof PassengerCar) {// 调用子类特有方法PassengerCar pc = (PassengerCar)car1;if (pc.getBrand().equals(brand) && pc.getSeat().equals(seat)) {return pc;}}}return null;}
}
这篇关于第三次项目《汽车租赁》的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!