項目需求: 基本界面需求:
and:
最后是把賬單打印出來:
個人代碼實現
基本思路:考慮到車輛之間的共性,設置一個父類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; }}系統: CarSystem.java:
package Car;import java.util.Scanner;import java.util.ArrayList;public class CarSystem { private static String goodByeInfo = "歡迎再次使用本系統,再見!"; 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("歡迎使用答答租車系統:"); 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("請輸入您要租汽車的數量:"); 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("請輸入租車天數:"); 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(); }}運行結果:
新聞熱點
疑難解答