本文介紹的是利用java編寫(xiě)一個(gè)控制臺(tái)版的“達(dá)達(dá)租車(chē)系統(tǒng)”,下面話(huà)不多說(shuō)了,來(lái)看看詳細(xì)實(shí)現(xiàn)方法吧。
實(shí)現(xiàn)目標(biāo)
java編寫(xiě)一個(gè)控制臺(tái)版的“達(dá)達(dá)租車(chē)系統(tǒng)”
實(shí)現(xiàn)功能
1.展示所有可租車(chē)輛
2.選擇車(chē)型、租車(chē)量
3.展示租車(chē)清單,包含:總金額、總載貨量及其車(chē)型、總載人量及其車(chē)型
三大分析
數(shù)據(jù)模型分析
業(yè)務(wù)模型分析
顯示和流程分析
實(shí)現(xiàn)效果
租車(chē)頁(yè)面
租車(chē)賬單
實(shí)現(xiàn)思路
首先定義一個(gè)car類(lèi),它包含基本功能:車(chē)名、載客數(shù)、載貨量、日租金。接著創(chuàng)建三個(gè)小類(lèi),分別是客車(chē)類(lèi)、貨車(chē)類(lèi)和皮卡類(lèi)(既能載客又能載貨),它們都繼承car類(lèi)。最后需要一個(gè)主類(lèi),用于開(kāi)啟整個(gè)系統(tǒng),調(diào)用每個(gè)小類(lèi)。
實(shí)現(xiàn)代碼
package com.jinger; public abstract class car { public int rent;//日租金 public int people;//載客人數(shù) public int loads;//載貨量 public string name;//車(chē)名 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; } }
客車(chē)類(lèi)
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(); } }
卡車(chē)類(lèi)
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(); } }
皮卡類(lèi)
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(); } }
主類(lèi)
package com.jinger; import java.util.*; public class initial { public static void main(string[] args) { //對(duì)各類(lèi)車(chē)實(shí)例化并保存到cars數(shù)組 car[] cars={ new passagecar("奧迪a4",4,500), new passagecar("馬自達(dá)6",4,400), new pickup("皮卡雪6",4,2,450), new passagecar("金龍",20,800), new truck("松花江",4,400), new truck("依維柯",20,1000)}; system.out.println("****歡迎使用達(dá)達(dá)租車(chē)系統(tǒng)!****"); system.out.println("****您確認(rèn)租車(chē)嗎?****"+" "+"是(請(qǐng)輸入1) 否(請(qǐng)輸入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("****您可租車(chē)的類(lèi)型及價(jià)目表****"); system.out.println("序號(hào)"+" 車(chē)名"+" 載客數(shù)(人)"+" 載貨量(噸)"+" 日租金(元/天)"); //使用循環(huán)方式將各類(lèi)車(chē)輸出 for(int i=0;i<cars.length;i++){ system.out.println((i+1)+" "+cars[i]); } system.out.println("****請(qǐng)輸入您的租車(chē)數(shù)量:****"); int num1=in1.nextint(); car[] rentcar=new car[num1]; int price=0;//總價(jià)格 int totalpeople=0;//總?cè)藬?shù) int totalloads=0;//總載貨量 for(int i=0;i<num1;i++){ system.out.println("****請(qǐng)輸入第"+(i+1)+"輛車(chē)的序號(hào):****"); int numx=in1.nextint(); rentcar[i]=cars[numx-1]; } system.out.println("****請(qǐng)輸入天數(shù):****"); int day=in1.nextint(); for(int i=0;i<num1;i++){ price=price+rentcar[i].rent *day; } system.out.println("****您的賬單:****"); system.out.println("已選載人車(chē):"); 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("已選載貨車(chē):"); 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("租車(chē)總價(jià)格:"+price+"元"); system.out.println(' '); system.out.println("****感謝您的惠顧,歡迎再次光臨!****"); } } }
收獲
思路決定編碼。
編程要注重自頂而下、逐步求精的設(shè)計(jì)方法。
源程序下載:
github:https://github.com/hubojing/car-rental-system
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家或者使用java能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。
原文鏈接:http://hubojing.me/2017/03/18/達(dá)達(dá)租車(chē)系統(tǒng)(Java實(shí)現(xiàn))/