用Java编写租车项目核心业务
生活随笔
收集整理的這篇文章主要介紹了
用Java编写租车项目核心业务
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- RENT_CAR_DEMO
- 第一章 需求
- 第二章 設(shè)計(jì)
- 2.1 界面原型
- **1)租車**
- **2)還車**
- 2.2 面向?qū)ο笤O(shè)計(jì)步驟
- 步驟概覽
- **1)類的實(shí)現(xiàn)**
- 2)類的繼承
- 3)多態(tài)
- 4)邏輯設(shè)計(jì)
- 1)租車業(yè)務(wù)流程
- 2)還車業(yè)務(wù)流程
- 2.3、總結(jié)
- 第三章 實(shí)現(xiàn)
- 3.1 項(xiàng)目技術(shù)概覽
- 3.2 項(xiàng)目實(shí)現(xiàn)步驟
- 3.3 代碼演示
- 1、程序的主入口 RentCarSys
- 2、功能接口RentCheServerImpl
- 3、抽象父類MotorCar
- 4、轎車類Car
- 5、客車類Bus
- 6、接口實(shí)現(xiàn)類RentCheServer
RENT_CAR_DEMO
- 掌握面向?qū)ο蟮脑O(shè)計(jì)流程
- 能夠根據(jù)設(shè)計(jì)文檔實(shí)現(xiàn)項(xiàng)目功能
第一章 需求
出租車公司提供轎車和客車兩種類型:
-
1)轎車如下:
品牌型號(hào)車牌日租金 奔馳 C 200 L 時(shí)尚型運(yùn)動(dòng)轎車 贛BTK001 500 寶馬 C 200 L 時(shí)尚型運(yùn)動(dòng)轎車 贛BTK002 400 奧迪 A5 Sportback 40TFSI 時(shí)尚型 贛BTK003 400 優(yōu)惠活動(dòng):租車超過(guò)7天九折優(yōu)惠,超過(guò)30天八折優(yōu)惠,超過(guò)150天五折優(yōu)惠。
-
2)客車如下:
品牌座位車牌日租金 申沃客車 50 滬BTK666 1500 優(yōu)惠活動(dòng):租車超過(guò)3天九折,超過(guò)7天八折,超過(guò)30天七折,超過(guò)150天五折。
需求:
第二章 設(shè)計(jì)
2.1 界面原型
我們的程序還是比較簡(jiǎn)單的程序,直接在控制臺(tái)中實(shí)現(xiàn)交互,效果如下:
1)租車
2)還車
2.2 面向?qū)ο笤O(shè)計(jì)步驟
步驟概覽
1)類的實(shí)現(xiàn)
2)類的繼承
把共性的內(nèi)容抽取到父類(基類)
3)多態(tài)
1、計(jì)算租金的功能,不管是客車還是轎車都有,故抽取到基類中。
2、因?yàn)榭蛙嚭娃I車的計(jì)算租金的邏輯是不同的,不能在基類中寫死邏輯,定義抽象方法。
3、用多態(tài)的方式,在子類中各自重寫實(shí)現(xiàn)具體的計(jì)算租金的邏輯。
4)邏輯設(shè)計(jì)
1)租車業(yè)務(wù)流程
2)還車業(yè)務(wù)流程
2.3、總結(jié)
封裝、繼承、多態(tài) (接口實(shí)現(xiàn)類)
租車
計(jì)算車的租金
還車
第三章 實(shí)現(xiàn)
3.1 項(xiàng)目技術(shù)概覽
要實(shí)現(xiàn)本項(xiàng)目,需要包括但不限于以下核心技術(shù)點(diǎn):
- 面向?qū)ο蟮捻?xiàng)目設(shè)計(jì)流程
- 面向?qū)ο笤O(shè)計(jì)包含技術(shù)點(diǎn)
- 數(shù)據(jù)的維護(hù)
- JDK中相關(guān)API的使用
3.2 項(xiàng)目實(shí)現(xiàn)步驟
1、創(chuàng)建模
2、按照面向?qū)ο笏枷爰安襟E把對(duì)應(yīng)的數(shù)據(jù)模型類設(shè)計(jì)
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. 存儲(chǔ)【已出租】的車 (包含轎車、客車)public static ArrayList<MotorCar> yetRentCheList = new ArrayList<>();// 2. 用來(lái)存儲(chǔ)【轎車數(shù)據(jù)】// key: 轎車品牌名 , value: 當(dāng)前品牌下的所有轎車public static Map<String,ArrayList<MotorCar>> carDate = new HashMap<>();// 3. 用來(lái)存儲(chǔ)【客車數(shù)據(jù)】// key: 客車品牌名 , value: 當(dāng)前品牌下的所有客車public static Map<String,ArrayList<MotorCar>> busDate = new HashMap<>();// 4. Map<類別, Map< 品牌,List<車> >static {// 4.1 初始化轎車數(shù)據(jù)// 大眾轎車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"));// 把各種品牌轎車,存儲(chǔ)到轎車的Map集合中carDate.put("大眾",daZongList);carDate.put("奔馳",benChiList);// 4.2 初始化客車數(shù)據(jù)//申沃客車ArrayList<MotorCar> shenWoList = new ArrayList<>();shenWoList.add(new Bus("申沃", "滬BTK666", "1500", 50));shenWoList.add(new Bus("申沃", "滬BTK777", "1500", 50));// 把各種品牌客車,存儲(chǔ)到客車的Map集合中busDate.put("申沃",shenWoList);}public static void main(String[] args) {System.out.println("**************************歡迎來(lái)到易租車***************************");Scanner sc = new Scanner(System.in);// 租車業(yè)務(wù)邏輯對(duì)象RentCheServer rentCheServer = new RentCheServer();while (true) {System.out.println("\n請(qǐng)選擇你的服務(wù): 1) 租車服務(wù) 2) 還車服務(wù) 3) 退出系統(tǒng)");System.out.println("******************************************************************");String choose = sc.nextLine();if ("1".equals(choose)) {rentCheServer.rentChe(); // 調(diào)用租車業(yè)務(wù)對(duì)象中的租車功能} else if ("2".equals(choose)) {rentCheServer.returnChe(); // 調(diào)用租車業(yè)務(wù)對(duì)象中的還車功能} else if ("3".equals(choose)) {System.out.println("退出系統(tǒng)");break;}}} }2、功能接口RentCheServerImpl
package cn.ahlin.rent.service;public interface RentCheServerImpl {public abstract void rentChe(); // 租車業(yè)務(wù)public abstract void returnChe(); // 還車業(yè)務(wù) }3、抽象父類MotorCar
package cn.ahlin.rent.bean; // 機(jī)動(dòng)車類 public abstract class MotorCar {private String brand; // 品牌private String carId; // 車牌號(hào)private String rent; // 租金// 根據(jù)天數(shù)計(jì)算租金 設(shè)為 public 便于訪問(wèn)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 = "租車超過(guò)7天九折優(yōu)惠,超過(guò)30天八折優(yōu)惠,超過(guò)150天五折優(yōu)惠。";@Override // 一般都寫publicpublic double calculateRent(int days) {//折扣優(yōu)惠:租車超過(guò)7天九折優(yōu)惠,超過(guò)30天八折優(yōu)惠,超過(guò)150天五折優(yōu)惠。int money = Integer.parseInt(getRent()) * days;if (days < 0) {throw new RuntimeException("出錯(cuò)");} 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 +", 車牌號(hào)為:" + 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 = "租車超過(guò)3天九折,超過(guò)7天八折,超過(guò)30天七折,超過(guò)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 + "座" +", 車牌號(hào)為:" + 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、接口實(shí)現(xiàn)類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("請(qǐng)選擇你要租車的類別:");System.out.println("當(dāng)前有汽車類型:[轎車, 客車]");// 選擇的租車類型String rentCheType = new Scanner(System.in).next();// 判斷租的汽車是哪種類型if (rentCheType.equals("轎車")) { // 選擇了租借"轎車"//傳遞的參數(shù):轎車--> 存儲(chǔ)所有轎車信息的集合(靜態(tài)參數(shù):直接類名.變量名)queryAndRent(rentCheType, RentCarSys.carDate);} else if (rentCheType.equals("客車")) { // 選擇了租借"客車"//傳遞的參數(shù):客車 --> 存儲(chǔ)所有客車信息的集合queryAndRent(rentCheType, RentCarSys.busDate);}}public void queryAndRent(String cheType, Map<String, ArrayList<MotorCar>> motorCar) {Scanner sc = new Scanner(System.in);// 選擇租借汽車的品牌System.out.println(cheType + "類型汽車,有如下品牌,請(qǐng)選擇:");Set<String> cheBrandAll = motorCar.keySet();System.out.println(cheBrandAll);String brandSingle = sc.next();// 顯示品牌下所有的汽車信息System.out.println(brandSingle + "有以下汽車:");// 根據(jù)汽車品牌名(Key) --> 獲取Map集合中所有的Value,// [value是一個(gè)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("請(qǐng)輸入您需要的車型對(duì)應(yīng)的序號(hào):");//顯示所要租借汽車的信息int num = sc.nextInt();System.out.print("您要租的汽車信息如下:");MotorCar che = cheInfoList.get(num-1);System.out.println(che);// 租車的天數(shù)System.out.println("請(qǐng)輸入要租借的天數(shù):");int days = sc.nextInt();// 計(jì)算租金double money = che.calculateRent(days);//已出租的汽車,需要從原有集合中刪除cheInfoList.remove(che);//已出租的汽車,需要記錄到一個(gè)特定的集合中RentCarSys.yetRentCheList.add(che);//打印租借信息System.out.println("尊敬的顧客,您此次租借的車是" +che.getBrand() +"車牌為" + che.getCarId() +"租借的時(shí)間為" + days + "天, 租金為" + money + "元。祝用車愉快,一路平安!");// 讓控制臺(tái)內(nèi)容更美觀些System.out.println("******************************************************************");}@Overridepublic void returnChe() {Scanner sc = new Scanner(System.in);// 獲取已租車信息列表ArrayList<MotorCar> yetRentCheList = RentCarSys.yetRentCheList;// 初始還車設(shè)置為null (對(duì)象)MotorCar returnChe = null;// 判斷用戶是否租車if(yetRentCheList.isEmpty()){System.out.println("您當(dāng)前沒(méi)有租借汽車");System.out.println();return;}// 打印所租車的全部信息System.out.println("尊敬的用戶你好,租借的車有如下:");for (int i = 0; i < yetRentCheList.size(); i++) {System.out.println("[" + i + "] " + yetRentCheList.get(i).toString());}// 選擇當(dāng)前要?dú)w還的車System.out.println("請(qǐng)選擇您要?dú)w還的汽車:");int index = sc.nextInt();if (index < 0 || index > yetRentCheList.size()) {System.out.println("輸入錯(cuò)誤,請(qǐng)重新輸入");} else {//從存儲(chǔ)已出租車的集合中,刪除歸還的車returnChe = yetRentCheList.remove(index);}// 初始設(shè)置 (某一種車品牌下車信息的集合)cheList 還車為空ArrayList<MotorCar> cheList = null;//將汽車放回到集合中 判斷是哪種品牌的汽車if (returnChe instanceof Car) {// 根據(jù)key值(品牌)--》 得到 此品牌車的信息列表 --》賦值給新定義的cheListcheList = RentCarSys.carDate.get(returnChe.getBrand());} else if (returnChe instanceof Bus) {cheList = RentCarSys.busDate.get(returnChe.getBrand());}// 對(duì)車列表進(jìn)行添加操作 --》把歸還的車放回原有的車數(shù)據(jù)中cheList.add(returnChe);System.out.println("您已還車成功。歡迎下次使用,祝您生活愉快!");System.out.println("******************************************************************");} }總結(jié)
以上是生活随笔為你收集整理的用Java编写租车项目核心业务的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 这些旅行必备APP,你知道几个?
- 下一篇: java美元兑换,(Java实现) 美元