本文介紹的是利用java編寫一個控制臺版的“達達租車系統”,下面話不多說了,來看看詳細實現方法吧。
實現目標
java編寫一個控制臺版的“達達租車系統”
實現功能
1.展示所有可租車輛
2.選擇車型、租車量
3.展示租車清單,包含:總金額、總載貨量及其車型、總載人量及其車型
三大分析
數據模型分析
業務模型分析
顯示和流程分析
實現效果
租車頁面
租車賬單
實現思路
首先定義一個car類,它包含基本功能:車名、載客數、載貨量、日租金。接著創建三個小類,分別是客車類、貨車類和皮卡類(既能載客又能載貨),它們都繼承car類。最后需要一個主類,用于開啟整個系統,調用每個小類。
實現代碼
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()+" "+this.getpeople()+" "+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()+" "+this.getloads()+" "+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()+" "+this.getpeople()+" "+this.getloads()+" "+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("****歡迎使用達達租車系統!****"); system.out.println("****您確認租車嗎?****"+" "+"是(請輸入1) 否(請輸入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("序號"+" 車名"+" 載客數(人)"+" 載貨量(噸)"+" 日租金(元/天)"); //使用循環方式將各類車輸出 for(int i=0;i<cars.length;i++){ system.out.println((i+1)+" "+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+" "); } totalpeople=totalpeople+rentcar[i].people; } system.out.println(' '); system.out.println("已選載貨車:"); for(int i=0;i<num1;i++){ if(rentcar[i].loads!=0){ system.out.println(rentcar[i].name+" "); } totalloads=totalloads+rentcar[i].loads; } system.out.println(' '); system.out.println("共載客:"+totalpeople+"人"); system.out.println("共載貨:"+totalloads+"噸"); system.out.println("租車總價格:"+price+"元"); system.out.println(' '); system.out.println("****感謝您的惠顧,歡迎再次光臨!****"); } } }
收獲
思路決定編碼。
編程要注重自頂而下、逐步求精的設計方法。
源程序下載:
github:https://github.com/hubojing/car-rental-system
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家或者使用java能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://hubojing.me/2017/03/18/達達租車系統(Java實現)/