這篇文章主要為大家介紹了利用Java實現一個達達租車系統的步驟,文中給出了詳細的實現思路和示例代碼,並在文末給出了完整的源碼供大家學習下載,需要的朋友可以參考借鑒,下面來一起看看吧。
本文介紹的是利用java編寫一個控制台版的“達達租車系統”,下面話不多說了,來看看詳細實現方法吧。
實現目標
java寫一個控制台版的「達達租車系統」
實現功能
1.展示所有可租借車輛
## 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()+"\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("****欢迎使用达达租车系统!****"); 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日租金(元/天)"); //使用循环方式将各类车输出 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("****感谢您的惠顾,欢迎再次光临!****"); } } }收穫
思路決定編碼。
程式設計要注重自頂而下、逐步求精的設計方法。
1.
Java免費影片教學
#2. 全面解析Java註解
FastJson教學手冊以上是Java實現租車系統的詳細過程的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

禪工作室 13.0.1
強大的PHP整合開發環境

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver CS6
視覺化網頁開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。