Java實(shí)現(xiàn)答答租車(chē)系統(tǒng)
本文實(shí)例為大家分享了Java實(shí)現(xiàn)答答租車(chē)系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
項(xiàng)目需求:
基本界面需求:
and:
最后是把賬單打印出來(lái):
個(gè)人代碼實(shí)現(xiàn)
基本思路:考慮到車(chē)輛之間的共性,設(shè)置一個(gè)父類(lèi)Car, 子類(lèi)MannedCar(載人), Truck(載貨),BothCary(既載人又載貨),三者繼承父類(lèi)Car的price, name屬性, getName()方法, 同時(shí)重寫(xiě)getPersonNum, getGoodsNum方法。
Car.java:
package Car; public abstract class Car { protected int price; protected String name; protected int getPrice() { return price; } protected String getName() { return name; } public int getPersonNum() { // TODO Auto-generated method stub return 0; } public int getGoodsNum() { // TODO Auto-generated method stub return 0; } }
MannedCar.java:
package Car; public class MannedCar extends Car { private int personNum; public MannedCar() { this.personNum = 0; this.price = 0; this.name = ""; } public MannedCar(int personNum, int price, String name) { this.personNum = personNum; this.price = price; this.name = name; } @Override public int getPersonNum() { return personNum; } }
Truck.java:
package Car; public class Truck extends Car{ private int goodsNum; public Truck() { this.price = 0; this.goodsNum = 0; this.name = ""; } public Truck(int price, int goodsNum, String name) { this.price = price; this.goodsNum = goodsNum; this.name = name; } @Override public int getGoodsNum() { return goodsNum; } }
BothCarry.java:
package Car; public class BothCarry extends Car { private int personNum; private int goodsNum; public BothCarry() { this.personNum = 0; this.goodsNum = 0; this.name = ""; this.price = 0; } public BothCarry(int price, int personNum, int goodsNum, String name) { this.personNum = personNum; this.goodsNum = goodsNum; this.price = price; this.name = name; } public int getPersonNum() { return personNum; } public int getGoodsNum() { return goodsNum; } }
系統(tǒng):
CarSystem.java:
package Car; import java.util.Scanner; import java.util.ArrayList; public class CarSystem { private static String goodByeInfo = "歡迎再次使用本系統(tǒng),再見(jiàn)!"; private static int dayBorrow; public static void beginSystem() { CarSystem.SystemWelcome(); Scanner scanner = new Scanner(System.in); String userCommand = scanner.next(); switch(userCommand){ case "1": CarSystem.getUserInput(); break; case "0": System.out.println(goodByeInfo); break; default: System.out.println("輸入錯(cuò)誤..End running.."); System.exit(0); break; } } public static void SystemWelcome() { System.out.println("歡迎使用答答租車(chē)系統(tǒng):"); System.out.println("您是否要租車(chē): 1-是 0-否"); } public static void getUserInput() { int numCarBorrow = 0; ArrayList<Car> carList = new ArrayList<Car>(6); carList.add(new MannedCar(4,500,"奧迪A4")); carList.add(new MannedCar(4,400,"馬自達(dá)6")); carList.add(new BothCarry(450,4,2,"皮卡雪6")); carList.add(new MannedCar(20,800,"金龍")); carList.add(new Truck(400,4,"松花江")); carList.add(new Truck(1000,20,"依維河")); System.out.println("請(qǐng)輸入您要租汽車(chē)的數(shù)量:"); Scanner sr = new Scanner(System.in); numCarBorrow = sr.nextInt(); int[] carNumList = new int[numCarBorrow]; for(int i=0;i<numCarBorrow;i++) { System.out.println("請(qǐng)輸入第" + (i+1) + "輛車(chē)的序號(hào):"); if (sr.hasNext()) { carNumList[i] = sr.nextInt(); } } System.out.println("請(qǐng)輸入租車(chē)天數(shù):"); if (sr.hasNext()) { dayBorrow = sr.nextInt(); } sr.close(); StringBuilder manned = new StringBuilder(); int numOfManned = 0; StringBuilder goods = new StringBuilder(); int numOfGoods = 0; int totalCost = 0; for(int i = 0;i < carNumList.length;i++) { if(carNumList[i]>0 && carNumList[i] < 3 || carNumList[i]==4) { manned.append(" "); manned.append(carList.get(carNumList[i]-1).getName()); numOfManned += carList.get(carNumList[i]-1).getPersonNum(); } else if(carNumList[i]==3) { manned.append(" "); manned.append(carList.get(carNumList[i]-1).getName()); goods.append(" "); goods.append(carList.get(carNumList[i]-1).getName()); numOfManned += carList.get(carNumList[i]-1).getPersonNum(); numOfGoods += carList.get(carNumList[i]-1).getGoodsNum(); } else { goods.append(" "); goods.append(carList.get(carNumList[i]-1).getName()); numOfGoods += carList.get(carNumList[i]-1).getGoodsNum(); } totalCost += carList.get(carNumList[i]-1).getPrice(); } //Output System.out.println("您的賬單:\n***可載人的車(chē)有:"); System.out.println(manned + " 共載人: " + numOfManned); System.out.println("***載貨的車(chē)有:\n" + goods + " 共載貨 : " + numOfGoods + "噸"); System.out.println("***租車(chē)總價(jià)格: " + totalCost * dayBorrow + "元"); } }
主程序:
package Car; public class CarSystemTest { public static void main(String[] args) { // TODO Auto-generated method stub CarSystem.beginSystem(); } }
運(yùn)行結(jié)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)簡(jiǎn)單的抽牌游戲
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單的抽牌游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04Android開(kāi)發(fā)Kotlin實(shí)現(xiàn)圓弧計(jì)步器示例詳解
這篇文章主要為大家介紹了Android開(kāi)發(fā)Kotlin繪制圓弧計(jì)步器示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Spring集成MyBatis和PageHelper分頁(yè)插件整合過(guò)程詳解
Spring?整合?MyBatis?是將?MyBatis?數(shù)據(jù)訪問(wèn)框架與?Spring?框架進(jìn)行集成,以實(shí)現(xiàn)更便捷的開(kāi)發(fā)和管理,在集成過(guò)程中,Spring?提供了許多特性和功能,如依賴(lài)注入、聲明式事務(wù)管理、AOP?等,這篇文章主要介紹了Spring集成MyBatis和PageHelper分頁(yè)插件整合,需要的朋友可以參考下2023-08-08java中自定義Spring Security權(quán)限控制管理示例(實(shí)戰(zhàn)篇)
本篇文章主要介紹了java中自定義Spring Security權(quán)限控制管理示例(實(shí)戰(zhàn)篇) ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02Sharding-JDBC自動(dòng)實(shí)現(xiàn)MySQL讀寫(xiě)分離的示例代碼
本文主要介紹了Sharding-JDBC自動(dòng)實(shí)現(xiàn)MySQL讀寫(xiě)分離,優(yōu)點(diǎn)在于數(shù)據(jù)源完全有Sharding-JDBC托管,寫(xiě)操作自動(dòng)執(zhí)行master庫(kù),讀操作自動(dòng)執(zhí)行slave庫(kù),感興趣的可以了解一下2021-11-11詳解Java線(xiàn)程同步器CountDownLatch
這篇文章主要介紹了Java線(xiàn)程同步器CountDownLatch的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下2020-09-09一篇文章帶你了解java泛型--泛型類(lèi),泛型方法,泛型接口
這篇文章主要介紹了java泛型基礎(chǔ)知識(shí)及通用方法,從以下幾個(gè)方面介紹一下java的泛型: 基礎(chǔ), 泛型關(guān)鍵字, 泛型方法, 泛型類(lèi)和接口,感興趣的可以了解一下2021-08-08詳解Spring系列之@ComponentScan批量注冊(cè)bean
本文介紹各種@ComponentScan批量掃描注冊(cè)bean的基本使用以及進(jìn)階用法和@Componet及其衍生注解使用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-02-02