Java實現(xiàn)一個達達租車系統(tǒng)的步驟詳解
本文介紹的是利用java編寫一個控制臺版的“達達租車系統(tǒng)”,下面話不多說了,來看看詳細實現(xiàn)方法吧。
實現(xiàn)目標
java編寫一個控制臺版的“達達租車系統(tǒng)”
實現(xiàn)功能
1.展示所有可租車輛
2.選擇車型、租車量
3.展示租車清單,包含:總金額、總載貨量及其車型、總載人量及其車型
三大分析
數(shù)據(jù)模型分析

業(yè)務模型分析

顯示和流程分析

實現(xiàn)效果
租車頁面

租車賬單

實現(xiàn)思路
首先定義一個Car類,它包含基本功能:車名、載客數(shù)、載貨量、日租金。接著創(chuàng)建三個小類,分別是客車類、貨車類和皮卡類(既能載客又能載貨),它們都繼承Car類。最后需要一個主類,用于開啟整個系統(tǒng),調(diào)用每個小類。
實現(xiàn)代碼
package com.jinger;
public abstract class Car {
public int rent;//日租金
public int people;//載客人數(shù)
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數(shù)組
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載客數(shù)(人)"+"\t載貨量(噸)"+"\t日租金(元/天)");
//使用循環(huán)方式將各類車輸出
for(int i=0;i<cars.length;i++){
System.out.println((i+1)+"\t"+cars[i]);
}
System.out.println("****請輸入您的租車數(shù)量:****");
int num1=in1.nextInt();
Car[] rentcar=new Car[num1];
int price=0;//總價格
int totalpeople=0;//總?cè)藬?shù)
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("****請輸入天數(shù):****");
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
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家或者使用java能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Spring如何通過注解引入外部資源(PropertySource?Value)
這篇文章主要為大家介紹了Spring通過注解@PropertySource和@Value引入外部資源的方法實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
淺談Spring Cloud中的API網(wǎng)關(guān)服務Zuul
這篇文章主要介紹了淺談Spring Cloud中的API網(wǎng)關(guān)服務Zuul,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
使用原生JDBC動態(tài)解析并獲取表格列名和數(shù)據(jù)的方法
這篇文章主要介紹了使用原生JDBC動態(tài)解析并獲取表格列名和數(shù)據(jù),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08
Java利用Redis實現(xiàn)高并發(fā)計數(shù)器的示例代碼
這篇文章主要介紹了Java利用Redis實現(xiàn)高并發(fā)計數(shù)器的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02
淺談maven的jar包和war包區(qū)別 以及打包方法
下面小編就為大家分享一篇淺談maven的jar包和war包區(qū)別 以及打包方法,具有很好的參考價值,希望對大家有所幫助2017-11-11
詳解SpringBoot中Controller接收對象列表實現(xiàn)
這篇文章主要介紹了詳解SpringBoot中Controller接收對象列表實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05

