汽车租赁系统
author:luckyboy!
version:1.2
知识储备:变量、数据类型、选择结构、循环结构、数组 、面向对象、集合
系统概述:某汽车租赁公司出租多种轿车和客车,出租费用以日为单位计算。出租车型及信息如下表所示。
车型 | 具体信息 | 日租金 | 折扣 |
轿车 | 宝马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) |
面向对象设计步骤
首先读懂项目需求,分析项目需求找出隐藏在其中的名词和动词,这些名词可能是所能用到的类和属性,动词可能是需要用到的方法。根据已知的类、属性、方法进一步优化设计,最后梳理项目运行过程。
需求中的名词
汽车租赁公司、汽车、轿车、客车、别克、宝马、金杯、金龙、X6、550i、GL8、林荫大道、座位数、日租金、折扣、车牌号(京NY28588、京CNY3284、京NT37465、京NT96968、京6566754、京6566754、京9696996、京8696998)
类和类属性
根据已知名词找出需要使用的类和类属性
汽车类:车牌号、车的品牌、日租金
客车类:车牌号、车的品牌、日租金、座位数
轿车类:车牌号、车的品牌、日租金、车的型号
汽车业务类:
汽车租赁管理类(测试类)
分析:
- 客车和轿车都属于汽车,汽车是客车和汽车的父类,汽车和客车是汽车的子类。
- 客车和汽车都具有相同的属性(车牌号、车的品牌、日租金);那么客车类和轿车类可以继承汽车类的属性。
- 除去相同的属性还具有私有的属性,客车类具有座位数,轿车类具有车的型号。
- 还应有一个汽车业务类来完成汽车租赁功能。
- 汽车租赁管理类用来对汽车租赁系统进行测试。
需求中的动词
计算租金、租赁、程序入口是类中所需大方法。
优化设计
完成优化设计代码:
创建 .Java文件:MotoVehicle类(汽车类)、Car类(轿车类)、Bus类(客车类)、MotoOperation类(汽车业务类)、Test类(测试类)
阶段划分
- 第一阶段:创建并完成汽车类
- 第二阶段:创建并完成轿车类
- 第三阶段:创建并完成客车类
- 第四阶段:创建并完成汽车业务类
- 第五阶段:创建并完成测试类
第一阶段:创建并完成汽车类
父类MotoVehicle
//汽车类
public class MotoVehicle {
// 车牌号
private String vehicleId;
// 品牌
private String brand;
// 日租金
private int perRent;
//无参构造方法
public MotoVehicle() {
super();
}
//有参构造方法
public MotoVehicle(String vehicleId, String brand, int perRent) {
super();
this.vehicleId = vehicleId;
this.brand = brand;
this.perRent = perRent;
}
//get和set方法
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;
}
}
第二阶段:创建并完成轿车类
子类Car继承父类MotoVehicle
//轿车类
public class Car extends MotoVehicle{
// 型号
private String type;
// 无参构造方法
public Car() {
super();
}
// 有参构造方法
public Car(String vehicleId, String brand, int perRent, String type) {
super(vehicleId, brand, perRent);
this.type = type;
}
//get和set方法
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
第三阶段:创建并完成客车类
子类Bus继承父类MotoVehicle
//客车类
public class Bus extends MotoVehicle{
// 座位数
private int seatCount;
// 无参构造方法
public Bus() {
super();
}
// 有参构造方法
public Bus(String vehicleId, String brand, int perRent, int seatCount) {
super(vehicleId, brand, perRent);
this.seatCount = seatCount;
}
public int getSeatCount() {
return seatCount;
}
public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
}
第四阶段:创建并完成汽车业务类
MotoOperation类
import java.util.ArrayList;
import java.util.Scanner;
public class MotoOperation {
Scanner in = new Scanner(System.in);
// 创建集合保存轿车信息
ArrayList<Car> carList = new ArrayList<Car>();
// 创建集合保存客车信息
ArrayList<Bus> busList = new ArrayList<Bus>();
// 初始化汽车信息
public void inti() {
// 实例化Car类
Car car1 = new Car("京NY28588", "宝马", 800, "X6");
Car car2 = new Car("京CNY28588", "宝马", 600, "550i");
Car car3 = new Car("京NT37465", "别克", 300, "林萌大道");
Car car4 = new Car("京NT96968", "别克", 600, "GL8");
// 将轿车信息添加到集合
carList.add(car1);
carList.add(car2);
carList.add(car3);
carList.add(car4);
// 实例化Bus类
Bus bus1 = new Bus("京6566754", "金杯", 800, 16);
Bus bus2 = new Bus("京9696996", "金杯", 1500, 34);
Bus bus3 = new Bus("京8696997", "金龙", 800, 16);
Bus bus4 = new Bus("京8696998", "金龙", 1500, 34);
// 将客车信息添加到集合
busList.add(bus1);
busList.add(bus2);
busList.add(bus3);
busList.add(bus4);
}
// 汽车租赁方法
public void start() {
System.out.println("**********欢迎光临租赁公司**********");
System.out.println("1-轿车\t2-客车");
System.out.println("请选择您要租赁的汽车类型:");
int type = in.nextInt();
// 如果输入1和2以外的数字,则重新输入
while (type < 1 || type > 2) {
System.out.println("输入错误,请重新选择您要租赁的汽车类型:");
type = in.nextInt();
}
if (type == 1) {// 用户选择租轿车
// 租轿车的方法
car();
} else if (type == 2) {// 用户选择租客车
// 租客车的方法
bus();
}
}
// 租轿车的方法
public void car() {
String brand = "";
String type = "";
System.out.println("请选择您要租赁的轿车品牌:1-别克\t2-宝马");
int chooseBrand = in.nextInt();
// 如果输入1和2以外的数字,则重新输入
while (chooseBrand < 1 || chooseBrand > 2) {
System.out.println("输入错误,请重新选择您要租赁的轿车品牌:");
chooseBrand = in.nextInt();
}
if (chooseBrand == 1) {
brand = "别克";
System.out.println("请选择您要租赁的汽车型号:1-林萌大道\t2-GL8");
int chooseType = in.nextInt();
// 如果输入1和2以外的数字,则重新输入
while (chooseType < 1 || chooseType > 2) {
System.out.println("输入错误,请重新选择您要租赁的轿车品牌:");
chooseType = in.nextInt();
}
type = chooseType == 1 ? "林萌大道" : "GL8";
} else if (chooseBrand == 2) {
brand = "宝马";
System.out.println("请选择您要租赁的汽车类型:1-X6\t2-50i");
int chooseType = in.nextInt();
// 如果输入1和2以外的数字,则重新输入
while (chooseType < 1 || chooseType > 2) {
System.out.println("输入错误,请重新选择您要租赁的轿车品牌:");
chooseType = in.nextInt();
}
type = chooseType == 1 ? "X6" : "550i";
}
// 计算租金并输出租车成功的信息
for (int i = 0; i < carList.size(); i++) {
// 如果存储轿车信息的集合中的信息与用户选择的轿车品牌和型号相同,输出车牌号提车
if (carList.get(i).getBrand().equals(brand)
&& carList.get(i).getType().equals(type)) {
// 计算租金
System.out.println("请输入您要租的天数:");
int days = in.nextInt();
double price = carList.get(i).getPerRent() * days;
if (days > 7 && days <= 30) {
price = price * 0.9;
} else if (days > 30 && days <= 150) {
price = price * 0.8;
} else if (days > 150) {
price = price * 0.7;
}
System.out.println("租车成功,请按照如下车牌号提车:"
+ carList.get(i).getVehicleId());
System.out.println("您需要支付:" + price + "元");
}
}
}
// 租客车的方法
public void bus() {
String brand = "";
int count = 0;
System.out.println("请选择您要租赁的客车品牌:1-金杯\t2-金龙");
int chooseBrand = in.nextInt();
// 如果用户输入1和2以外的数子,则重新输入
while (chooseBrand < 1 && chooseBrand > 2) {
System.out.println("输入错误,请重新选择您要租赁的轿车品牌:");
chooseBrand = in.nextInt();
}
brand = chooseBrand == 1 ? "金杯" : "金龙";
System.out.println("请选择您要租赁的客车座位数:1-16座\t2-34座");
int chooseCount = in.nextInt();
while (chooseCount < 1 && chooseCount > 2) {
System.out.println("输入错误,请重新选择您要租赁的客车座位数:1-16座\t2-34座");
chooseCount = in.nextInt();
}
count = chooseCount == 1 ? 16 : 34;
// 计算租金并输出租车成功的信息
for (int i = 0; i < busList.size(); i++) {
// 如果存储轿车信息的集合中的信息与用户选择的轿车品牌和型号相同,输出车牌号提车
if (busList.get(i).getBrand().equals(brand)
&& busList.get(i).getSeatCount() == count) {
// 计算租金
System.out.println("请输入您要租的天数:");
int days = in.nextInt();
double price = busList.get(i).getPerRent() * days;
if(days >= 3 && days < 7){
price = price * 0.9;
}else if(days >= 7 && days < 30){
price = price * 0.8;
}else if(days >= 30 && days < 150){
price = price * 0.7;
}else if(days >= 150){
price = price * 0.6;
}
System.out.println("租车成功,请按照如下车牌号提车:"
+ busList.get(i).getVehicleId());
System.out.println("您需要支付:" + price + "元");
}
}
}
}
第五阶段:创建并完成测试类
Test类
public class Test {
public static void main(String[] args) {
//实例化汽车业务类
MotoOperation moto = new MotoOperation();
//调用汽车业务类中的初始化汽车信息方法
moto.inti();
//调用汽车业务类中的汽车租赁方法
moto.start();
}
}