Java小項目之租車系統
一:項目背景介紹;
根據所學知識,編寫一個控制台版的“呱呱租車系統”
功能:
1、展示所有可租車輛;
2、選擇車型、租車量;
3、展示租車清單,包含:總金額、總載貨量以及其車型號、總載人量及其車型;
二:項目分析;
- 數據模型分析;
- 業務模型分析;
- 顯示和流程分析;
三:數據模型分析;
1.通過對現實世界的事與物主要特征的分析、抽象,為信息系統的實施提供數據存取的數據結構以及相應的約束;
2.數據結構組成:操作(方法)、屬性;
如現實世界中的汽車,最終會轉化為一段代碼:
那么這個過程就是我們數據分析的過程。
四:業務模型分析;
1.在設計應用程序之前,應該明確該應用程序必須執行哪些任務。
分析業務需求是應用程序開發中最重要的步驟之一。
確認業務需求的目的在於創建一個能同時滿足零售商和消費者需要的解決方案。
2.呱呱租車系統中,只需要考慮消費者業務需求即可。
五:顯示和流程分析;
1.顯示:用戶可以看到的信息提升界面;
2.流程:顯示信息的執行過程、步驟;
3.呱呱租車系統中,要以命令行的方式顯示提示信息和輸出結果信息,要考慮其樣式,用戶輸入的數據不同,信息該如何處理、如何處理並顯示出結果,這部分知識囊括了顯示與流程的內容;
例如:請選擇車輛->請輸入序號->輸出總金額...;
六:代碼示例;
RentCar主類:
package com.test; import java.util.Arrays; import java.util.Scanner; public class RentCar { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("歡迎使用呱呱租車系統!:\n你是否要進行租車:1是 0否"); Scanner s = new Scanner(System.in); int i=s.nextInt(); if(i==1){ System.out.println("您可租車的類型及其目表:"); System.out.println("序號"+"\t"+"汽車名稱"+"\t"+"租金"+"\t\t"+"載人量"+"\t"+"載重量"); Car car1=new PassengerCar(1,"奧迪A4",500,4); Car car2=new PassengerCar(2,"馬自達6",400,4); Car car3=new Pika(3,"皮卡雪6",450,4,2); Car car4=new PassengerCar(4,"金龍",800,20); Car car5=new Truck(5,"松花江",400,4); Car car6=new Truck(6, "依維柯", 900, 20); System.out.println(car1+"\n"+car2+"\n"+car3+"\n"+car4+"\n"+car5+"\n"+car6); Car[] rentcar={car1,car2,car3,car4,car5,car6}; System.out.println("請輸入您要租汽車的數量:"); int n = s.nextInt(); float money1=0; int num=0; int allcargo=0; String [] hrentcar = new String[n]; for(int k=0;k<n;k++){ System.out.println("請輸入第"+(k+1)+"輛車的序號"); int t =s.nextInt(); if(t>0&&t<=6){ hrentcar[k]=rentcar[t-1].name; float money2=rentcar[t-1].price; int person=rentcar[t-1].capacity; int wg=rentcar[t-1].cargo; allcargo+=wg; num+=person; money1+=money2; }else{ System.out.println("您輸入的信息無效!"); k--; if(k==0)k=0; } } Arrays.sort(hrentcar); System.out.println("請輸入租車天數:"); int in = s.nextInt(); float sum =in*money1; System.out.println("您的租車完成,已租車"+n+"輛"); System.out.println("您的賬單:"); System.out.println("您租的車有:"+Arrays.toString(hrentcar)); System.out.println("共可載"+num+"人"); System.out.println("共可載貨"+allcargo+"噸"); System.out.println("一共需要支付"+sum+"元"); }else if(i==0){ System.out.println("已退出系統,歡迎下次光臨"); }else{ System.out.println("您輸入的信息無效!請重啟"); } } }
Car父類:
package com.test; public abstract class Car { public int carNumber; public String name; public float price; public int capacity; public int cargo; public int getCarNumber() { return carNumber; } public void setCarNumber(int carNumber) { this.carNumber = carNumber; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public int getCapacity() { return capacity; } public void setCapacity(int capacity) { this.capacity = capacity; } public int getCargo() { return cargo; } public void setCargo(int cargo) { this.cargo = cargo; } }
PassengerCar類:
package com.test; public class PassengerCar extends Car { public PassengerCar(int carNumber,String name,float price,int capacity) { // TODO Auto-generated constructor stub this.setCarNumber(carNumber); this.setName(name); this.setPrice(price); this.setCapacity(capacity); } @Override public String toString() { return getCarNumber()+"."+"\t"+getName()+ "\t" +getPrice()+"/天"+"\t\t"+"載人:"+getCapacity()+"人"+"\t"+"載貨:-" ; }}
Pika類:
package com.test; public class Pika extends Car { public Pika(int carNumber,String name,float price,int capacity,int cargo) { // TODO Auto-generated constructor stub this.setCarNumber(carNumber); this.setName(name); this.setPrice(price); this.setCapacity(capacity); this.setCargo(cargo); } @Override public String toString() { return getCarNumber()+"."+"\t"+getName()+ "\t" +getPrice()+"/天"+"\t\t"+"載人:"+getCapacity()+"人"+"\t"+"載貨:"+getCargo()+"噸" ; } }
Truck類:
package com.test; public class Truck extends Car { public Truck(int carNumber,String name,float price,int cargo) { // TODO Auto-generated constructor stub this.setCarNumber(carNumber); this.setName(name); this.setPrice(price); this.setCargo(cargo); } @Override public String toString() { return getCarNumber()+"."+"\t"+getName()+ "\t" +getPrice()+"/天"+"\t\t"+"載人:-"+"\t"+"載貨:"+getCargo()+"噸" ; }}
代碼僅供參考