Java實(shí)現(xiàn)一個(gè)達(dá)達(dá)租車(chē)系統(tǒng)的步驟詳解
本文介紹的是利用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()+"\t"+this.getPeople()+"\t\t\t\t"+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()+"\t\t\t"+this.getLoads()+"\t\t"+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()+"\t"+this.getPeople()+"\t\t"+this.getLoads()+"\t\t"+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ē)嗎?****"+"\n"+"是(請(qǐng)輸入1) \t 否(請(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)"+"\t車(chē)名"+"\t載客數(shù)(人)"+"\t載貨量(噸)"+"\t日租金(元/天)");
//使用循環(huán)方式將各類(lèi)車(chē)輸出
for(int i=0;i<cars.length;i++){
System.out.println((i+1)+"\t"+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+"\t");
}
totalpeople=totalpeople+rentcar[i].people;
}
System.out.println('\n');
System.out.println("已選載貨車(chē):");
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("租車(chē)總價(jià)格:"+price+"元");
System.out.println('\n');
System.out.println("****感謝您的惠顧,歡迎再次光臨!****");
}
}
}
收獲
思路決定編碼。
編程要注重自頂而下、逐步求精的設(shè)計(jì)方法。
源程序下載:
github:https://github.com/hubojing/Car-rental-system
本地下載:http://xiazai.jb51.net/201704/yuanma/Car-rental-system-master(jb51.net).rar
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家或者使用java能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Spring如何通過(guò)注解引入外部資源(PropertySource?Value)
這篇文章主要為大家介紹了Spring通過(guò)注解@PropertySource和@Value引入外部資源的方法實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
淺談Spring Cloud中的API網(wǎng)關(guān)服務(wù)Zuul
這篇文章主要介紹了淺談Spring Cloud中的API網(wǎng)關(guān)服務(wù)Zuul,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
使用原生JDBC動(dòng)態(tài)解析并獲取表格列名和數(shù)據(jù)的方法
這篇文章主要介紹了使用原生JDBC動(dòng)態(tài)解析并獲取表格列名和數(shù)據(jù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
Java利用Redis實(shí)現(xiàn)高并發(fā)計(jì)數(shù)器的示例代碼
這篇文章主要介紹了Java利用Redis實(shí)現(xiàn)高并發(fā)計(jì)數(shù)器的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
淺談maven的jar包和war包區(qū)別 以及打包方法
下面小編就為大家分享一篇淺談maven的jar包和war包區(qū)別 以及打包方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2017-11-11
一文帶你掌握J(rèn)ava?ReentrantLock加解鎖原理
這篇文章將為大家詳細(xì)介紹一下Java中ReentrantLock?加鎖和釋放鎖的原理,以及和?Synchronized?的對(duì)比。文中的示例代碼講解詳細(xì),希望對(duì)大家有所幫助2022-12-12
JavaWeb實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
這篇文章主要為大家詳細(xì)介紹了JavaWeb中的文件上傳和下載功能的實(shí)現(xiàn),在Web應(yīng)用系統(tǒng)開(kāi)發(fā)中,文件上傳和下載功能是非常常用的功能,需要的朋友可以參考下2015-08-08
詳解SpringBoot中Controller接收對(duì)象列表實(shí)現(xiàn)
這篇文章主要介紹了詳解SpringBoot中Controller接收對(duì)象列表實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

