一、項目功能
1、展示所有可租車輛
2、選擇車型、租車輛、租車天數
3、展示租車清單,包括總金額、總載貨量及車型、總載客量及車型
二、項目分析
1、數據模型:通過對現實世界的事與物的主要特征的分析、抽象,提取數據結構及相應的約束。其中,數據結構的組成是:操作(方法)、屬性,如將現實的車寫成代碼。本項目的數據模型是🚗模型。
| 貨車(載貨量) | 客車(載客量) | 皮卡車(載貨、載客量) |
| 輕型(5t)、重型(5-20t) |
小客車(4人)、大客車(>10人) | 載貨、載客 |
2、業務模型:設計程序之前,應該明確程序執行的任務,確認業務需求的目的在於創建一個能同時滿足零售商和消費者需要的解決方案。本項目中➡️消費者。
用戶
選車 租車天數 統計金額 載貨、載客量
3、顯示和流程
顯示:用戶可以看到的信息提示界面
流程:顯示信息的執行過程、步驟
二、項目實現
1、模型層:在答答租車系統中,我們的數據模型是汽車,所以我們首先創建汽車模型類。
(1)創建汽車模型類
1 package com.companyname.model; 2 3 /** 4 * 汽車模型類 5 * 6 * @author wangna 7 */ 8 public class Car { 9 private int carId;// 車序號 10 private String carName; 11 private double priceForAday;// 一天的租金 12 private int personAmount;// 容量 13 private int carAmount;// 容量 14 15 public int getCarId() { 16 return carId; 17 } 18 19 public void setCarId(int carId) { 20 this.carId = carId; 21 } 22 23 public String getCarName() { 24 return carName; 25 } 26 27 public void setCarName(String carName) { 28 this.carName = carName; 29 } 30 31 public double getPriceForAday() { 32 return priceForAday; 33 } 34 35 public void setPriceForAday(double priceForAday) { 36 this.priceForAday = priceForAday; 37 } 38 39 public int getPersonAmount() { 40 return personAmount; 41 } 42 43 public void setPersonAmount(int personAmount) { 44 this.personAmount = personAmount; 45 } 46 47 public int getCarAmount() { 48 return carAmount; 49 } 50 51 public void setCarAmount(int carAmount) { 52 this.carAmount = carAmount; 53 } 54 55 }
(2)創建租車信息模型類
1 package com.companyname.model; 2 3 public class RentInfo { 4 private Car car; 5 private int day;// 租的天數 6 private int nums;// 租幾輛 7 private double sumPrize; 8 9 public Car getCar() { 10 return car; 11 } 12 13 public void setCar(Car car) { 14 this.car = car; 15 } 16 17 public int getDay() { 18 return day; 19 } 20 21 public void setDay(int day) { 22 this.day = day; 23 } 24 25 public int getNums() { 26 return nums; 27 } 28 29 public void setNums(int nums) { 30 this.nums = nums; 31 } 32 33 public double getSumPrize() { 34 return sumPrize; 35 } 36 37 public void setSumPrize(double sumPrize) { 38 this.sumPrize = sumPrize; 39 } 40 41 }
2、服務層:
(1)顯示車信息
1 package com.companyname.service; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.companyname.model.Car; 7 8 public class CarInfoService { 9 10 private List<Car> carList = new ArrayList<>();//車的集合(看作長度可變化的數組) 11 12 /** 13 * 添加car到carlist 14 * 注意: 15 * 所添加的car是在頁面(客戶端,即main())傳入的 16 */ 17 public void addCar(Car car){ 18 carList.add(car); 19 } 20 21 /** 22 * 獲取所有的car信息 23 */ 24 public List<Car> getCars() { 25 return carList; 26 } 27 28 /** 29 * 根據carId從carlist中找出相應car 30 */ 31 public Car getCarById(int carId) { 32 for (Car car : carList) { 33 if (car.getCarId() == carId) { 34 return car; 35 } 36 } 37 return null; 38 } 39 }
(2)租車服務
1 package com.companyname.service; 2 3 import com.companyname.model.Car; 4 5 public class RentService { 6 7 /** 8 * 客戶租車 9 * @param carId 10 * @param day 租幾天 11 * @param amount 租幾輛 12 * @return 總金額 13 */ 14 public double rentCar(int carId, int day, int amount, CarInfoService sc){ 15 double sumPrize = 0; 16 Car car = sc.getCarById(carId); 17 double prizeForCar = car.getPriceForAday(); 18 sumPrize = prizeForCar*day*amount; 19 return sumPrize; 20 } 21 22 }
3、創建測試類
1 package com.companyname.test; 2 3 import com.companyname.model.Car; 4 import com.companyname.model.RentInfo; 5 import com.companyname.service.CarInfoService; 6 import com.companyname.service.RentService; 7 8 /** 9 * test class 10 * 11 * @author wangna 12 */ 13 public class TestCar { 14 public static void main(String[] args) { 15 CarInfoService sc = new CarInfoService(); 16 17 /****************** 1 添加車輛到carList *********************/ 18 // audi 19 Car audi = new Car(); 20 audi.setCarId(1); 21 audi.setCarName("奧迪A4"); 22 audi.setPriceForAday(500); 23 audi.setPersonAmount(4); 24 sc.addCar(audi); 25 26 // pika 27 Car pika = new Car(); 28 pika.setCarId(3); 29 pika.setCarName("pika"); 30 pika.setPriceForAday(440); 31 pika.setPersonAmount(4); 32 pika.setCarAmount(2); 33 sc.addCar(pika); 34 35 /****************** 2 租車 *********************/ 36 RentService rentService = new RentService(); 37 Car car = sc.getCarById(1); 38 double prize = rentService.rentCar(car, 2, 2); 39 RentInfo rentInfo = new RentInfo(); 40 rentInfo.setCar(car); 41 rentInfo.setDay(2); 42 rentInfo.setNums(2); 43 rentInfo.setSumPrize(prize); 44 45 System.out.println("總金額:" + rentInfo.getSumPrize() + "-->"+"車名:"+rentInfo.getCar().getCarName()+"-->"+"租幾天:"+rentInfo.getDay()+"-->"+"租幾輛:"+rentInfo.getNums()+"-->"+"總金額:"+rentInfo.getSumPrize()); 46 } 47 }
運行結果:
總金額:2000.0-->車名:奧迪A4-->租幾天:2-->租幾輛:2-->總金額:2000.0
