Java實現(xiàn)答答租車系統(tǒng)
本文實例為大家分享了Java實現(xiàn)答答租車系統(tǒng)的具體代碼,供大家參考,具體內容如下
項目需求:
基本界面需求:
and:
最后是把賬單打印出來:
個人代碼實現(xiàn)
基本思路:考慮到車輛之間的共性,設置一個父類Car, 子類MannedCar(載人), Truck(載貨),BothCary(既載人又載貨),三者繼承父類Car的price, name屬性, getName()方法, 同時重寫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),再見!"; 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("輸入錯誤..End running.."); System.exit(0); break; } } public static void SystemWelcome() { System.out.println("歡迎使用答答租車系統(tǒng):"); System.out.println("您是否要租車: 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,"馬自達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("請輸入您要租汽車的數(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("請輸入第" + (i+1) + "輛車的序號:"); if (sr.hasNext()) { carNumList[i] = sr.nextInt(); } } System.out.println("請輸入租車天數(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***可載人的車有:"); System.out.println(manned + " 共載人: " + numOfManned); System.out.println("***載貨的車有:\n" + goods + " 共載貨 : " + numOfGoods + "噸"); System.out.println("***租車總價格: " + totalCost * dayBorrow + "元"); } }
主程序:
package Car; public class CarSystemTest { public static void main(String[] args) { // TODO Auto-generated method stub CarSystem.beginSystem(); } }
運行結果:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android開發(fā)Kotlin實現(xiàn)圓弧計步器示例詳解
這篇文章主要為大家介紹了Android開發(fā)Kotlin繪制圓弧計步器示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06Spring集成MyBatis和PageHelper分頁插件整合過程詳解
Spring?整合?MyBatis?是將?MyBatis?數(shù)據(jù)訪問框架與?Spring?框架進行集成,以實現(xiàn)更便捷的開發(fā)和管理,在集成過程中,Spring?提供了許多特性和功能,如依賴注入、聲明式事務管理、AOP?等,這篇文章主要介紹了Spring集成MyBatis和PageHelper分頁插件整合,需要的朋友可以參考下2023-08-08java中自定義Spring Security權限控制管理示例(實戰(zhàn)篇)
本篇文章主要介紹了java中自定義Spring Security權限控制管理示例(實戰(zhàn)篇) ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02Sharding-JDBC自動實現(xiàn)MySQL讀寫分離的示例代碼
本文主要介紹了Sharding-JDBC自動實現(xiàn)MySQL讀寫分離,優(yōu)點在于數(shù)據(jù)源完全有Sharding-JDBC托管,寫操作自動執(zhí)行master庫,讀操作自動執(zhí)行slave庫,感興趣的可以了解一下2021-11-11詳解Spring系列之@ComponentScan批量注冊bean
本文介紹各種@ComponentScan批量掃描注冊bean的基本使用以及進階用法和@Componet及其衍生注解使用,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2022-02-02