汽车租赁系统
author:luckyboy!
version:1.1
知识储备:变量、数据类型、选择结构、循环结构、数组 、面向对象
系统概述:某汽车租赁公司出租多种轿车和客车,出租费用以日为单位计算。出租车型及信息如下表所示。
车型 | 具体信息 | 日租金 | 折扣 |
轿车 | 宝马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 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 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;
}
@Override
public double calcRent(int days) {
double price = this.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;
}
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;
}
}
第五阶段:创建并完成测试类文章来源:https://www.toymoban.com/news/detail-409994.html
Test类文章来源地址https://www.toymoban.com/news/detail-409994.html
//汽车租赁管理类:测试类
public class Text {
public static void main(String[] args) {
//实例化汽车业务类
MotoOperation moto = new MotoOperation();
//调用汽车业务类中的初始化汽车信息方法
moto.inti();
//调用汽车业务类中的汽车租赁方法
moto.start();
}
}
到了这里,关于Java汽车租赁系统1.2-面向对象+数组的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!