Java實現(xiàn)一個達達租車系統(tǒng)的步驟詳解
本文介紹的是利用java編寫一個控制臺版的“達達租車系統(tǒng)”,下面話不多說了,來看看詳細實現(xiàn)方法吧。
實現(xiàn)目標
java編寫一個控制臺版的“達達租車系統(tǒng)”
實現(xiàn)功能
1.展示所有可租車輛
2.選擇車型、租車量
3.展示租車清單,包含:總金額、總載貨量及其車型、總載人量及其車型
三大分析
數據模型分析
業(yè)務模型分析
顯示和流程分析
實現(xiàn)效果
租車頁面
租車賬單
實現(xiàn)思路
首先定義一個Car類,它包含基本功能:車名、載客數、載貨量、日租金。接著創(chuàng)建三個小類,分別是客車類、貨車類和皮卡類(既能載客又能載貨),它們都繼承Car類。最后需要一個主類,用于開啟整個系統(tǒng),調用每個小類。
實現(xiàn)代碼
package com.jinger; public abstract class Car { public int rent;//日租金 public int people;//載客人數 public int loads;//載貨量 public String name;//車名 public int getRent(){ return rent; } public void setRent(int rent){ this.rent=rent; } public int getPeople(){ return people; } public void setPeople(int people){ this.people=people; } public int getLoads(){ return loads; } public void setLoads(int loads){ this.loads=loads; } public String getName(){ return name; } public void setName(String name){ this.name=name; } }
客車類
package com.jinger; public class PassageCar extends Car{ public PassageCar(String name,int people,int rent){ this.setName(name); this.setPeople(people); this.setRent(rent); } public String toString(){ return this.getName()+"\t"+this.getPeople()+"\t\t\t\t"+this.getRent(); } }
卡車類
package com.jinger; public class Truck extends Car { public Truck(String name,int loads,int rent){ this.setName(name); this.setLoads(loads); this.setRent(rent); } public String toString(){ return this.getName()+"\t\t\t"+this.getLoads()+"\t\t"+this.getRent(); } }
皮卡類
package com.jinger; public class Pickup extends Car { public Pickup(String name,int people,int loads,int rent){ this.setName(name); this.setPeople(people); this.setLoads(loads); this.setRent(rent); } public String toString(){ return this.getName()+"\t"+this.getPeople()+"\t\t"+this.getLoads()+"\t\t"+this.getRent(); } }
主類
package com.jinger; import java.util.*; public class Initial { public static void main(String[] args) { //對各類車實例化并保存到cars數組 Car[] cars={ new PassageCar("奧迪A4",4,500), new PassageCar("馬自達6",4,400), new Pickup("皮卡雪6",4,2,450), new PassageCar("金龍",20,800), new Truck("松花江",4,400), new Truck("依維柯",20,1000)}; System.out.println("****歡迎使用達達租車系統(tǒng)!****"); System.out.println("****您確認租車嗎?****"+"\n"+"是(請輸入1) \t 否(請輸入2)"); Scanner in1=new Scanner(System.in); int is=in1.nextInt(); if(is!=1){ System.out.println("****歡迎下次光臨!****"); System.exit(0); } if(is==1){ System.out.println("****您可租車的類型及價目表****"); System.out.println("序號"+"\t車名"+"\t載客數(人)"+"\t載貨量(噸)"+"\t日租金(元/天)"); //使用循環(huán)方式將各類車輸出 for(int i=0;i<cars.length;i++){ System.out.println((i+1)+"\t"+cars[i]); } System.out.println("****請輸入您的租車數量:****"); int num1=in1.nextInt(); Car[] rentcar=new Car[num1]; int price=0;//總價格 int totalpeople=0;//總人數 int totalloads=0;//總載貨量 for(int i=0;i<num1;i++){ System.out.println("****請輸入第"+(i+1)+"輛車的序號:****"); int numx=in1.nextInt(); rentcar[i]=cars[numx-1]; } System.out.println("****請輸入天數:****"); int day=in1.nextInt(); for(int i=0;i<num1;i++){ price=price+rentcar[i].rent *day; } System.out.println("****您的賬單:****"); System.out.println("已選載人車:"); for(int i=0;i<num1;i++){ if(rentcar[i].people!=0){ System.out.println(rentcar[i].name+"\t"); } totalpeople=totalpeople+rentcar[i].people; } System.out.println('\n'); System.out.println("已選載貨車:"); for(int i=0;i<num1;i++){ if(rentcar[i].loads!=0){ System.out.println(rentcar[i].name+"\t"); } totalloads=totalloads+rentcar[i].loads; } System.out.println('\n'); System.out.println("共載客:"+totalpeople+"人"); System.out.println("共載貨:"+totalloads+"噸"); System.out.println("租車總價格:"+price+"元"); System.out.println('\n'); System.out.println("****感謝您的惠顧,歡迎再次光臨!****"); } } }
收獲
思路決定編碼。
編程要注重自頂而下、逐步求精的設計方法。
源程序下載:
github:https://github.com/hubojing/Car-rental-system
本地下載:http://xiazai.jb51.net/201704/yuanma/Car-rental-system-master(jb51.net).rar
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家或者使用java能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
SpringBoot集成Redis向量數據庫實現(xiàn)相似性搜索功能
Redis?是一個開源(BSD?許可)的內存數據結構存儲,用作數據庫、緩存、消息代理和流式處理引擎,向量檢索的核心原理是通過將文本或數據表示為高維向量,并在查詢時根據向量的相似度進行搜索,本文給大家介紹了SpringBoot集成Redis向量數據庫實現(xiàn)相似性搜索功能2024-09-09Java中List.contains(Object?object)方法使用
本文主要介紹了Java中List.contains(Object?object)方法,使用List.contains(Object?object)方法判斷ArrayList是否包含一個元素對象,感興趣的可以了解一下2022-04-04