用Java编写租车项目核心业务
生活随笔
收集整理的這篇文章主要介紹了
用Java编写租车项目核心业务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 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天五折。
需求:
第二章 設計
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 // 一般都寫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编写租车项目核心业务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 这些旅行必备APP,你知道几个?
- 下一篇: 用了十年竟然都不对,Java、Rust、