文章目录

  • RENT_CAR_DEMO
    • 第一章 需求
    • 第二章 设计
      • 2.1 界面原型
        • **1)租车**
        • **2)还车**
      • 2.2 面向对象设计步骤
      • 步骤概览
        • **1)类的实现**
        • 2)类的继承
        • 3)多态
        • 4)逻辑设计
          • 1)租车业务流程
          • 2)还车业务流程
      • 2.3、总结
    • 第三章 实现
      • 3.1 项目技术概览
      • 3.2 项目实现步骤
      • 3.3 代码演示
        • 1、程序的主入口 RentCarSys
        • 2、功能接口RentCheServerImpl
        • 3、抽象父类MotorCar
        • 4、轿车类Car
        • 5、客车类Bus
        • 6、接口实现类RentCheServer

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天五折。

需求:

  1. 写程序实现租车流程并计算租赁费用。
  2. 写程序实现还车流程。

第二章 设计

2.1 界面原型

我们的程序还是比较简单的程序,直接在控制台中实现交互,效果如下:

1)租车

2)还车

2.2 面向对象设计步骤

步骤概览

1)类的实现

2)类的继承

把共性的内容抽取到父类(基类)

3)多态

1、计算租金的功能,不管是客车还是轿车都有,故抽取到基类中。

2、因为客车和轿车的计算租金的逻辑是不同的,不能在基类中写死逻辑,定义抽象方法。

3、用多态的方式,在子类中各自重写实现具体的计算租金的逻辑。

4)逻辑设计

1)租车业务流程

2)还车业务流程

2.3、总结

  1. 面向对象的设计

封装、继承、多态 (接口实现类)

  1. 业务的开发

租车

计算车的租金

还车

第三章 实现

3.1 项目技术概览

要实现本项目,需要包括但不限于以下核心技术点:

  • 面向对象的项目设计流程
  • 面向对象设计包含技术点
  1. 类,接口设计
  2. 面向对象的封装,继承,多态
  • 数据的维护
  1. 集合的使用
  2. 集合的嵌套
  • 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   // 一般都写publicpublic 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;}}@Overridepublic 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天五折。";@Overridepublic 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;}}@Overridepublic 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 {@Overridepublic 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("******************************************************************");}@Overridepublic 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值(品牌)--》 得到 此品牌车的信息列表 --》赋值给新定义的cheListcheList = 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("******************************************************************");}
}

用Java编写租车项目核心业务相关推荐

  1. 用java编写租车系统代码_java实现租车系统

    今天用java编写了一个租车系统,过程中主要遇到的两个问题: 1.输出数组信息问题: 在得到cars[]数组后,要生成租车信息表,目前有两种思路:一是用循环输出:二是用arrays.tostring( ...

  2. java 二嗨租车项目_java实现简单租车系统

    本文实例为大家分享了java实现租车系统demo,供大家参考,具体内容如下 这也是参考了mooc上的一个基础项目,所以拿来写一下.不过我的demo肯定有不好或者错误的地方,欢迎指出 1.项目功能/需求 ...

  3. 【JAVAWEB开发】带你零基础学JAVA项目(二嗨租车项目篇)

    哈喽~大家好呀,时隔一个月,这次的一个小项目来喽,这次的 "二嗨租车项目" 使用的是的 oracle + JDBC + 集合 + 面向对象 + 分层思想(MVC),接下来就来看看吧 ...

  4. 基于Java毕业设计租车管理系统源码+系统+mysql+lw文档+部署软件

    基于Java毕业设计租车管理系统源码+系统+mysql+lw文档+部署软件 基于Java毕业设计租车管理系统源码+系统+mysql+lw文档+部署软件 本源码技术栈: 项目架构:B/S架构 开发语言: ...

  5. java简单租车系统 慕课手记_java小项目,租车系统

    这学期要学java,本以为暑假学windows程序设计的,没想到一些事情耽误了 ,只能回来再补了,因为学过c++,面向对象的三大特性什么的,c++比java难一点,所以学java感觉还比骄轻松,下面就 ...

  6. java二嗨租车项目_JAVA 第二季项目作业-租车系统

    第一次完成JAVA项目作业,花了近4个小时 弄懂了CMD下的运行和eclips下运行的不一样 父类CAR package car_demo; public class car { // 车的属性 St ...

  7. java二嗨租车项目_Java入门第二季6-1租车项目代码

    一共有5个类 Car类 //作为父类 package com.imooc; public class Car { public int ID; public String name; public i ...

  8. java二嗨租车项目_JAVA第二季项目实战———答答租车系统代码

    总共5个类: 第1个类:Car package DaDaRentCar; //父类 public class Car { String name; //车名 int money; //租金 int c ...

  9. java二嗨租车项目_Java入门第二季第六章项目实战(租车系统)

    新手,望指教. package test; public class CarInfo { private int pNum; private String type; private String n ...

最新文章

  1. FCC 成都社区·前端周刊 第 6 期
  2. unity获取电磁笔压感_1024级压感 原道W8S电磁笔的秘密
  3. PuTTY的下载、使用和设置,并推荐同类佳软——MobaXterm
  4. 20. C# -- Base, this 关键字
  5. 模板:半平面交(计算几何)
  6. 代码质量管理工具】——sonar
  7. 《徐徐道来话Java》:PriorityQueue和最小堆
  8. 【积少成多】vi的进阶使用
  9. java的set和get方法实例化_java反射机制 调用get set 方法 | 学步园
  10. 【优化算法】多目标花朵授粉算法(MOFPA)【含Matlab源码 1594期】
  11. 误差状态方程与雅可比矩阵
  12. 奇虎终于涉足生活搜索
  13. PGM-Explainer
  14. Strom整合Hbase
  15. java label 位置_java 怎样设置label的位置
  16. css cursor 鼠标指针样式总结
  17. G. Discarding Game(尺取)
  18. 电脑中找不到ie浏览器怎么办
  19. mixin(公共样式定义)
  20. java开发抢红包算法,抢红包算法的实现-java

热门文章

  1. 线性表的顺寻存储结构
  2. 服务器插无线usb网卡驱动,cubieboard安装USB无线网卡驱动及设置
  3. Spark系列之Spark体系架构
  4. 我眼中的《芳华》读后感作文2500字
  5. Cloud Programming Simplified: A Berkeley View on Serverless Computing
  6. 视频教程-用project做项目计划及总结报表-研发管理
  7. JAVA编写文件格式转换UTF-8
  8. 平步青云:Windows Azure(二)
  9. lower_bound 与 upper_bound
  10. 立志做文艺小清新的技术宅