文章目录
RENT_CAR_DEMO
- 掌握面向对象的设计流程
- 能够根据设计文档实现项目功能
第一章 需求
出租车公司提供轿车和客车两种类型:
-
1)轿车如下:
品牌 型号 车牌 日租金 奔驰 C 200 L 时尚型运动轿车 赣BTK001 500 宝马 C 200 L 时尚型运动轿车 赣BTK002 400 奥迪 A5 Sportback 40TFSI 时尚型 赣BTK003 400 优惠活动:租车超过7天九折优惠,超过30天八折优惠,超过150天五折优惠。
-
2)客车如下:
品牌 座位 车牌 日租金 申沃客车 50 沪BTK666 1500 优惠活动:租车超过3天九折,超过7天八折,超过30天七折,超过150天五折。
需求:
- 写程序实现租车流程并计算租赁费用。
- 写程序实现还车流程。
第二章 设计
2.1 界面原型
我们的程序还是比较简单的程序,直接在控制台中实现交互,效果如下:
1)租车
2)还车
2.2 面向对象设计步骤
步骤概览
1)类的实现
2)类的继承
把共性的内容抽取到父类(基类)
3)多态
1、计算租金的功能,不管是客车还是轿车都有,故抽取到基类中。
2、因为客车和轿车的计算租金的逻辑是不同的,不能在基类中写死逻辑,定义抽象方法。
3、用多态的方式,在子类中各自重写实现具体的计算租金的逻辑。
4)逻辑设计
1)租车业务流程
2)还车业务流程
2.3、总结
- 面向对象的设计
封装、继承、多态 (接口实现类)
- 业务的开发
租车
计算车的租金
还车
第三章 实现
3.1 项目技术概览
要实现本项目,需要包括但不限于以下核心技术点:
- 面向对象的项目设计流程
- 面向对象设计包含技术点
- 类,接口设计
- 面向对象的封装,继承,多态
- 数据的维护
- 集合的使用
- 集合的嵌套
- JDK中相关API的使用
3.2 项目实现步骤
1、创建模
2、按照面向对象思想及步骤把对应的数据模型类设计
3.3 代码演示
1、程序的主入口 RentCarSys
package cn.ahlin.rent.main;
import cn.ahlin.rent.bean.Bus;
import cn.ahlin.rent.bean.Car;
import cn.ahlin.rent.bean.MotorCar;
import cn.ahlin.rent.service.RentCheServer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class RentCarSys {
// 1. 存储【已出租】的车 (包含轿车、客车)
public static ArrayList<MotorCar> yetRentCheList = new ArrayList<>();
// 2. 用来存储【轿车数据】
// key: 轿车品牌名 , value: 当前品牌下的所有轿车
public static Map<String,ArrayList<MotorCar>> carDate = new HashMap<>();
// 3. 用来存储【客车数据】
// key: 客车品牌名 , value: 当前品牌下的所有客车
public static Map<String,ArrayList<MotorCar>> busDate = new HashMap<>();
// 4. Map<类别, Map< 品牌,List<车> >
static {
// 4.1 初始化轿车数据
// 大众轿车
ArrayList<MotorCar> daZongList = new ArrayList<>();
daZongList.add(new Car("大众", "赣BTK001", "200", "探歌140T"));
daZongList.add(new Car("大众", "赣BTK002", "300", "探歌280T"));
// 奔驰轿车
ArrayList<MotorCar> benChiList = new ArrayList<>();
benChiList.add(new Car("奔驰", "沪BTK003", "400", "2021 C 200"));
benChiList.add(new Car("奔驰", "沪BTK004", "500", "2021 C 260"));
// 把各种品牌轿车,存储到轿车的Map集合中
carDate.put("大众",daZongList);
carDate.put("奔驰",benChiList);
// 4.2 初始化客车数据
//申沃客车
ArrayList<MotorCar> shenWoList = new ArrayList<>();
shenWoList.add(new Bus("申沃", "沪BTK666", "1500", 50));
shenWoList.add(new Bus("申沃", "沪BTK777", "1500", 50));
// 把各种品牌客车,存储到客车的Map集合中
busDate.put("申沃",shenWoList);
}
public static void main(String[] args) {
System.out.println("**************************欢迎来到易租车***************************");
Scanner sc = new Scanner(System.in);
// 租车业务逻辑对象
RentCheServer rentCheServer = new RentCheServer();
while (true) {
System.out.println("\n请选择你的服务: 1) 租车服务 2) 还车服务 3) 退出系统");
System.out.println("******************************************************************");
String choose = sc.nextLine();
if ("1".equals(choose)) {
rentCheServer.rentChe(); // 调用租车业务对象中的租车功能
} else if ("2".equals(choose)) {
rentCheServer.returnChe(); // 调用租车业务对象中的还车功能
} else if ("3".equals(choose)) {
System.out.println("退出系统");
break;
}
}
}
}
2、功能接口RentCheServerImpl
package cn.ahlin.rent.service;
public interface RentCheServerImpl {
public abstract void rentChe(); // 租车业务
public abstract void returnChe(); // 还车业务
}
3、抽象父类MotorCar
package cn.ahlin.rent.bean;
// 机动车类
public abstract class MotorCar {
private String brand; // 品牌
private String carId; // 车牌号
private String rent; // 租金
// 根据天数计算租金 设为 public 便于访问
public abstract double calculateRent(int days);
public MotorCar() {
}
public MotorCar(String brand, String carId, String rent) {
this.brand = brand;
this.carId = carId;
this.rent = rent;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getCarId() {
return carId;
}
public void setCarId(String carId) {
this.carId = carId;
}
public String getRent() {
return rent;
}
public void setRent(String rent) {
this.rent = rent;
}
}
4、轿车类Car
package cn.ahlin.rent.bean;
public class Car extends MotorCar{
private String carModel; // 车型
public final String msg = "租车超过7天九折优惠,超过30天八折优惠,超过150天五折优惠。";
@Override // 一般都写public
public double calculateRent(int days) {
//折扣优惠:租车超过7天九折优惠,超过30天八折优惠,超过150天五折优惠。
int money = Integer.parseInt(getRent()) * days;
if (days < 0) {
throw new RuntimeException("出错");
} else if (days <= 7) {
return money;
} else if (days <= 30) {
return money * 0.9;
} else if (days <= 150) {
return money * 0.8;
} else {
return money * 0.5;
}
}
@Override
public String toString() {
return getBrand() + ":" + carModel +
", 车牌号为:" + getCarId() +
", 租金每天" + getRent() + "元, " + msg;
}
public Car(String carModel) {
this.carModel = carModel;
}
public Car(String brand, String carId, String rent, String carModel) {
super(brand, carId, rent);
this.carModel = carModel;
}
public String getCarModel() {
return carModel;
}
public void setCarModel(String carModel) {
this.carModel = carModel;
}
}
5、客车类Bus
package cn.ahlin.rent.bean;
public class Bus extends MotorCar{
private int seatNum; // 座位
public final String msg = "租车超过3天九折,超过7天八折,超过30天七折,超过150天五折。";
@Override
public double calculateRent(int days) {
int money = Integer.parseInt( getRent() ) * days;
if (days < 3) {
return money;
} else if (days < 7) {
return money * 0.9;
} else if (days < 30) {
return money * 0.8;
} else if (days < 150) {
return money * 0.7;
} else {
return money * 0.5;
}
}
@Override
public String toString() {
return getBrand() + " " + seatNum + "座" +
", 车牌号为:" + getCarId() +
", 租金每天" + getRent() + "元, " + msg;
}
public Bus(int seatNum) {
this.seatNum = seatNum;
}
public Bus(String brand, String carId, String rent, int seatNum) {
super(brand, carId, rent);
this.seatNum = seatNum;
}
public int getSeatNum() {
return seatNum;
}
public void setSeatNum(int seatNum) {
this.seatNum = seatNum;
}
}
6、接口实现类RentCheServer
package cn.ahlin.rent.service;
import cn.ahlin.rent.bean.MotorCar;
import cn.ahlin.rent.main.RentCarSys;
import cn.ahlin.rent.bean.Bus;
import cn.ahlin.rent.bean.Car;
import java.util.ArrayList;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class RentCheServer implements RentCheServerImpl {
@Override
public void rentChe() {
System.out.println("请选择你要租车的类别:");
System.out.println("当前有汽车类型:[轿车, 客车]");
// 选择的租车类型
String rentCheType = new Scanner(System.in).next();
// 判断租的汽车是哪种类型
if (rentCheType.equals("轿车")) {
// 选择了租借"轿车"
//传递的参数:轿车--> 存储所有轿车信息的集合(静态参数:直接类名.变量名)
queryAndRent(rentCheType, RentCarSys.carDate);
} else if (rentCheType.equals("客车")) {
// 选择了租借"客车"
//传递的参数:客车 --> 存储所有客车信息的集合
queryAndRent(rentCheType, RentCarSys.busDate);
}
}
public void queryAndRent(String cheType, Map<String, ArrayList<MotorCar>> motorCar) {
Scanner sc = new Scanner(System.in);
// 选择租借汽车的品牌
System.out.println(cheType + "类型汽车,有如下品牌,请选择:");
Set<String> cheBrandAll = motorCar.keySet();
System.out.println(cheBrandAll);
String brandSingle = sc.next();
// 显示品牌下所有的汽车信息
System.out.println(brandSingle + "有以下汽车:");
// 根据汽车品牌名(Key) --> 获取Map集合中所有的Value,
// [value是一个ArrayList集合](品牌下所有的汽车信息)
ArrayList<MotorCar> cheInfoList = motorCar.get(brandSingle);
for (int i = 0; i < cheInfoList.size(); i++) {
System.out.print("[" + (i + 1) + "] ");
System.out.println(cheInfoList.get(i));
}
// 选择具体的车型
System.out.println("请输入您需要的车型对应的序号:");
//显示所要租借汽车的信息
int num = sc.nextInt();
System.out.print("您要租的汽车信息如下:");
MotorCar che = cheInfoList.get(num-1);
System.out.println(che);
// 租车的天数
System.out.println("请输入要租借的天数:");
int days = sc.nextInt();
// 计算租金
double money = che.calculateRent(days);
//已出租的汽车,需要从原有集合中删除
cheInfoList.remove(che);
//已出租的汽车,需要记录到一个特定的集合中
RentCarSys.yetRentCheList.add(che);
//打印租借信息
System.out.println("尊敬的顾客,您此次租借的车是" +
che.getBrand() +
"车牌为" + che.getCarId() +
"租借的时间为" + days + "天, 租金为" + money + "元。祝用车愉快,一路平安!");
// 让控制台内容更美观些
System.out.println("******************************************************************");
}
@Override
public void returnChe() {
Scanner sc = new Scanner(System.in);
// 获取已租车信息列表
ArrayList<MotorCar> yetRentCheList = RentCarSys.yetRentCheList;
// 初始还车设置为null (对象)
MotorCar returnChe = null;
// 判断用户是否租车
if(yetRentCheList.isEmpty()){
System.out.println("您当前没有租借汽车");
System.out.println();
return;
}
// 打印所租车的全部信息
System.out.println("尊敬的用户你好,租借的车有如下:");
for (int i = 0; i < yetRentCheList.size(); i++) {
System.out.println("[" + i + "] " + yetRentCheList.get(i).toString());
}
// 选择当前要归还的车
System.out.println("请选择您要归还的汽车:");
int index = sc.nextInt();
if (index < 0 || index > yetRentCheList.size()) {
System.out.println("输入错误,请重新输入");
} else {
//从存储已出租车的集合中,删除归还的车
returnChe = yetRentCheList.remove(index);
}
// 初始设置 (某一种车品牌下车信息的集合)cheList 还车为空
ArrayList<MotorCar> cheList = null;
//将汽车放回到集合中 判断是哪种品牌的汽车
if (returnChe instanceof Car) {
// 根据key值(品牌)--》 得到 此品牌车的信息列表 --》赋值给新定义的cheList
cheList = RentCarSys.carDate.get(returnChe.getBrand());
} else if (returnChe instanceof Bus) {
cheList = RentCarSys.busDate.get(returnChe.getBrand());
}
// 对车列表进行添加操作 --》把归还的车放回原有的车数据中
cheList.add(returnChe);
System.out.println("您已还车成功。欢迎下次使用,祝您生活愉快!");
System.out.println("******************************************************************");
}
}