需求中的名词
汽车租赁公司、汽车、轿车、客车、别克、宝马、金杯、金龙、X6、550i、GL8、林荫大道、座位数、日租金、折扣、车牌号(京NY28588、京CNY3284、京NT37465、京NT96968、京6566754、京6566754、京9696996、京8696998)
类和类属性
根据已知名词找出需要使用的类和类属性
汽车类:车牌号、车的品牌、日租金
客车类:车牌号、车的品牌、日租金、座位数
轿车类:车牌号、车的品牌、日租金、车的型号
汽车业务类:
汽车租赁管理类(测试类)
分析:
客车和轿车都属于汽车,汽车是客车和汽车的父类,汽车和客车是汽车的子类。
客车和汽车都具有相同的属性(车牌号、车的品牌、日租金);那么客车类和轿车类可以继承汽车类的属性。
除去相同的属性还具有私有的属性,客车类具有座位数,轿车类具有车的型号。
还应有一个汽车业务类来完成汽车租赁功能。
汽车租赁管理类用来对汽车租赁系统进行测试。
需求中的动词
计算租金、租赁、程序入口是类中所需大方法。 完成优化设计代码:
创建 .Java文件:MotoVehicle类(汽车类)、Car类(轿车类)、Bus类(客车类)、MotoOperation类(汽车业务类)、Test类(测试类)
阶段划分
第一阶段:创建并完成汽车类
第二阶段:创建并完成轿车类
第三阶段:创建并完成客车类
第四阶段:创建并完成汽车业务类
第五阶段:创建并完成测试类
第一阶段:创建并完成汽车类
父类MotoVehicle
//汽车类
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;
}
//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;
}
//计算租金(抽象方法)
public abstract double calcRent(int days);
}
第二阶段:创建并完成轿车类
子类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;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public double calcRent(int days) {
double price = this.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;
}
return price;
}
}
第三阶段:创建并完成客车类
子类Bus继承父类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;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public double calcRent(int days) {
double price = this.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;
}
return price;
}
}
第四阶段:创建并完成汽车业务类
MotoOperation类
import java.util.ArrayList;
import java.util.Scanner;
//汽车业务类
public class MotoOperation {
Scanner in = new Scanner(System.in);
// 创建轿车类数组
Car[] car = new Car[4];
// 创建客车类
Bus[] bus = new Bus[4];
public void inti() {
// 将轿车信息保存到轿车类数组
car[0] = new Car("京NY28588", "宝马", 800, "X6");
car[1] = new Car("京CNY28588", "宝马", 600, "550i");
car[2] = new Car("京NT37465", "别克", 300, "林荫大道");
car[3] = new Car("京NT96968", "别克", 600, "GL8");
// 将客车信息保存到客车类数组
bus[0] = new Bus("京6566754", "金杯", 800, 16);
bus[1] = new Bus("京9696996", "金杯", 1500, 34);
bus[2] = new Bus("京8696997", "金龙", 800, 16);
bus[3] = new Bus("京8696998", "金龙", 1500, 34);
}
// 汽车租赁方法
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();
}
// 如果用户选择别克,则给轿车型号赋值为林荫大道;如果用户选择别克,则给轿车型号赋值为GL8;
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();
}
// 如果用户选择宝马,则给轿车型号赋值为X6;如果用户选择宝马,则给轿车型号赋值为550i;
type = chooseType == 1 ? "X6" : "550i";
}
// 输出提车信息
for (int i = 0; i < car.length; i++) {
if (car[i].getBrand().equals(brand)
&& car[i].getType().equals(type)) {
// 计算轿车租金
System.out.println("请输入您要租的天数:");
int days = in.nextInt();
double sumPrice = calcRent(days, type);
System.out.println("租车成功,请按照如下车牌号提车:" + car[i].getVehicleId());
System.out.println("您需要支付:" + sumPrice + "元");
}
}
}
// 计算轿车租金的方法
public double calcRent(int days, String type) {
double price = 0;
for (int i = 0; i < car.length; i++) {
if (car[i].getType().equals(type)) {
price = car[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;
}
}
}
return 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();
// 如果用户输入1和2以外的数子,则重新输入
while (chooseCount < 1 && chooseCount > 2) {
System.out.println("输入错误,请重新选择您要租赁的客车座位数:1-16座\t2-34座");
chooseCount = in.nextInt();
}
// 如果用户选择16座,则给客车座位数赋值为16;如果用户选择34座,则给客车座位数赋值为34;
count = chooseCount == 1 ? 16 : 34;
// 输出提车信息
for (int i = 0; i < bus.length; i++) {
if (bus[i].getBrand().equals(brand) && bus[i].getSeatCount() == count) {
// 计算轿车租金
System.out.println("请输入您要租的天数:");
int days = in.nextInt();
double sumPrice = calcRent(days, count);
System.out.println("租车成功,请按照如下车牌号提车:" + bus[i].getVehicleId());
System.out.println("您需要支付:" + sumPrice + "元");
}
}
}
// 计算客车租金的方法
public double calcRent(int days, int count) {
double price = 0;
for (int i = 0; i < bus.length; i++) {
if (bus[i].getSeatCount() == count) {
price = bus[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;
}
}
}
return price;
}
}
第五阶段:创建并完成测试类
Test类
//汽车租赁管理类:测试类
public class Text {
public static void main(String[] args) {
//实例化汽车业务类
MotoOperation moto = new MotoOperation();
//调用汽车业务类中的初始化汽车信息方法
moto.inti();
//调用汽车业务类中的汽车租赁方法
moto.start();
}
}