汽车租赁系统
2015-09-19 08:58
295 查看
新购置了卡车,根据吨位,租金每吨每天50
面向对象设计的步骤
1、分析需求
2、找名词(类、属性)
父类
汽车:品牌(brand)、日租金(perRent)、车牌号(vehicleId)
子类
轿车:型号(type)
客车:座位数(seatCout)
卡车:吨位(tonnage)
汽车业务类
汽车租赁管理类
3、找方法(方法)
汽车(计算租金)
汽车业务类(初始化车、提供租赁服务)
汽车租赁管理类(入口和系统界面)
4、优化设计
父子类的关系:汽车(父)–>轿车、客车(子类)
汽车(抽象类):计算租金–>抽象方法
5、梳理运行过程
汽车业务类(MotoOperation)
初始化车–>构造车(轿车、客车)
–>构造这些车怎么存储
–>对象数组(MotoVehicle[])
MotoVehicle moto1=new Car(…);
租车:
根据用户的租车 条件去查找相应车辆,并返回相应车辆,用户的租车条件:方法的参数
循环遍历汽车数组(MotoVehicle[])
拿到每一辆车(Car、Bus、Truck)
将车辆转换为自己实际的子类类型
instanceof
分别根据用户不同的条件进行比较
Car:brand、type
Bus:brand、seatCount
Truck:brand、tonnage
发现符合用户条件的幸运车辆,返回
首先编写父类:MotoVehicle
package cn.jbit.vehicle; /* * 汽车:父类 */ public abstract class MotoVehicle { // 品牌、车牌号、日租金 private String brand; private String vehicleId; private int perRent; public MotoVehicle() { } public MotoVehicle(String brand, String vehicleId, int perRent) { this.brand = brand; this.vehicleId = vehicleId; this.perRent = perRent; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getVehicleId() { return vehicleId; } public void setVehicleId(String vehicleId) { this.vehicleId = vehicleId; } public int getPerRent() { return perRent; } public void setPerRent(int perRent) { this.perRent = perRent; } // 计算租金:根据用户租车的天数(days)计算租金 public abstract float calcRent(int days); }
编写子类:Bus
package cn.jbit.vehicle; /* * 子类:客车类 */ public class Bus extends MotoVehicle { // 座位数 private int seatCount; public Bus() { } public Bus(String brand, String vehicleId, int perRent, int seatCount) { super(brand, vehicleId, perRent); this.seatCount = seatCount; } public int getSeatCount() { return seatCount; } public void setSeatCount(int seatCount) { this.seatCount = seatCount; } // 重写计算租金:根据用户租车的天数(days)计算租金 /* * days>=3天9折 * days>=7天8折 * days>=30天7折 * days>=150天6折 */ public float calcRent(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 cn.jbit.vehicle; /* * 子类:轿车类 */ public class Car extends MotoVehicle { // 型号 private String type; public Car() { } public Car(String brand, String vehicleId, int perRent, String type) { super(brand, vehicleId, perRent); this.type = type; } public String getType() { return type; } public void setType(String type) { this.type = type; } // 重写计算租金:根据用户租车的天数(days)计算租金 /* * days>7天9折 * days>30天8折 * days>150天7折 */ public float calcRent(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 cn.jbit.vehicle; public class Truck extends MotoVehicle { private int tonnage;// 吨位 public Truck bf17 () { } public Truck(String brand, String vehicleId, int perRent, int tonnage) { super(brand, vehicleId, perRent); this.tonnage = tonnage; } public int getTonnage() { return tonnage; } public void setTonnage(int tonnage) { this.tonnage = tonnage; } // 重写计算租金:每吨每天50 public float calcRent(int days) { float price = tonnage * this.getPerRent() * days; return price; } }
汽车业务类:
package cn.jbit.vehicle; /* * 汽车业务类 */ public class MotoOperation { MotoVehicle[] motos = new MotoVehicle[12]; // 初始化数据:汽车 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, "GL8"); // 客车:品牌、车牌号、日租金、座位数 motos[4] = new Bus("金龙", "京8696997", 800, 16); motos[5] = new Bus("金龙", "京8696998", 1500, 34); motos[6] = new Bus("金杯", "京6566754", 800, 16); motos[7] = new Bus("金杯", "京9696996", 1500, 34); // 货车:品牌、车牌号、租金、吨位 motos[8] = new Truck("一汽解放", "京MH98725", 50, 1); motos[9] = new Truck("一汽解放", "京NU98631", 50, 2); motos[10] = new Truck("重庆红岩", "京L593216", 50, 1); motos[11] = new Truck("重庆红岩", "京CY56312", 50, 2); } // 提供租赁服务 // 根据用户的租车条件去查找相应车辆,并返回相应车辆 // 用户的租车条件:方法的参数 public MotoVehicle rentVehicle(String brand, String type, int seatCount, int tonnage) { MotoVehicle rentMoto = null; for (MotoVehicle moto : motos) { if (moto instanceof Car) { Car car = (Car) moto; if (brand.equals(moto.getBrand()) && type.equals(car.getType())) { rentMoto = car; break; } } if (moto instanceof Bus) { Bus bus = (Bus) moto; if (brand.equals(moto.getBrand()) && (bus.getSeatCount() == seatCount)) { rentMoto = bus; break; } } if (moto instanceof Truck) { Truck truck = (Truck) moto; if (brand.equals(moto.getBrand()) && (truck.getTonnage() == tonnage)) { rentMoto = truck; break; } } } return rentMoto; } }
汽车租赁管理类(入口和系统界面)
package cn.jbit.vehicle; import java.util.Scanner; /* * 汽车租赁管理类(入口和系统界面) */ public class RentVehicleSys { public static void main(String[] args) { System.out.println("**********欢迎光临汽车租赁公司**********"); System.out.print("请选择粗车类型:1、轿车 2、客车 3、卡车"); Scanner input = new Scanner(System.in); int choose = input.nextInt(); // 品牌、轿车类型、客车座位数、吨位 String brand = ""; String type = ""; int seatCount = 0; int tonnage = 0; switch (choose) { case 1: // 租赁轿车 System.out.print("请选择品牌:1、宝马 2、别克"); if (input.nextInt() == 1) { brand = "宝马"; System.out.print("请选择类型:1、X6 2、550i"); type = (input.nextInt() == 1) ? "X6" : "550i"; } else { brand = "别克"; System.out.print("请选择类型:1、林荫大道 2、GL8"); type = (input.nextInt() == 1) ? "林荫大道" : "GL8"; } break; case 2: // 租赁客车 System.out.print("请选择品牌:1、金龙 2、金杯"); brand = (input.nextInt() == 1) ? "金龙" : "金杯"; System.out.print("请选择类型:1、16座 2、34座"); seatCount = (input.nextInt() == 1) ? 16 : 34; break; case 3: // 租赁卡车 System.out.print("请选择品牌:1、一汽解放 2、重庆红岩"); brand = (input.nextInt() == 1) ? "一汽解放" : "重庆红岩"; System.out.print("请选择吨位:1、1吨 2、2吨"); tonnage = (input.nextInt() == 1) ? 1 : 2; break; } // 租车 System.out.print("请输入要租赁的天数:"); int days = input.nextInt(); MotoOperation motoOpr = new MotoOperation(); motoOpr.init(); MotoVehicle moto = motoOpr.rentVehicle(brand, type, seatCount, tonnage); // 提示租车信息给用户 System.out.println("**************租车结果**************"); System.out.println("租车成功!" + "\n" + "分配给您的车牌号是:" + moto.getVehicleId()); System.out.println("您应付的租金(元):" + moto.calcRent(days)); } }
相关文章推荐
- 一个类型转换的讨论
- IT未来发展前景
- Android——单、多线程下载
- mysql 常用查询
- 一些变量的比较
- USACO 1.3 Combination Lock
- POJ——1323 Game Prediction
- "_OBJC_CLASS_$_MAMapServices", referenced from: 的问题修复
- TCP和UDP比较
- 架构设计:系统间通信(3)——IO通信模型和JAVA实践 上篇
- 自学安卓复习基础_之四(关于intent和bundle传值)
- 大道至简第一章感悟上
- 万能搜索
- 错误: Native library not found! Please copy libbdpush_V2_3.so into your project!
- 测试用例的基本概念
- C++对象模型系列集合
- zzuli OJ 1104: 求因子和(函数专题)
- 架构设计:系统间通信(1)——概述从“聊天”开始上篇
- C++ STL The compare function
- Object类