国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

慕課網(wǎng) - 答答租車系統(tǒng)(Java)

2019-11-06 06:55:42
字體:
供稿:網(wǎng)友

項目需求: 這里寫圖片描述 基本界面需求: 這里寫圖片描述 and: 這里寫圖片描述 最后是把賬單打印出來: 這里寫圖片描述


個人代碼實現(xiàn)

基本思路:考慮到車輛之間的共性,設(shè)置一個父類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,"馬自達(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("請輸入您要租汽車的數(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(); }}

運(yùn)行結(jié)果: 這里寫圖片描述 這里寫圖片描述


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 阳东县| 屏山县| 电白县| 四川省| 龙门县| 乌拉特中旗| 海伦市| 郁南县| 嘉义市| 宁化县| 元阳县| 广河县| 黄山市| 万荣县| 荆州市| 赤壁市| 马公市| 湘潭市| 鄯善县| 上高县| 青河县| 开封市| 嘉定区| 保康县| 河源市| 洪湖市| 威信县| 卓资县| 德安县| 东兰县| 江油市| 岑溪市| 龙川县| 灌云县| 富蕴县| 海兴县| 炎陵县| 漯河市| 织金县| 两当县| 五峰|