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();
}
}
運行結果:


以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
淺談Spring Data如何簡化數(shù)據(jù)操作的方法
這篇文章主要介紹了看Spring Data如何簡化數(shù)據(jù)操作的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
JavaEE組件commons-fileupload實現(xiàn)文件上傳、下載
這篇文章主要介紹了JavaEE組件commons-fileupload實現(xiàn)文件上傳、下載,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
TCP/IP協(xié)議中三次握手四次揮手的原理及流程分析
這篇文章主要介紹了TCP/IP協(xié)議中三次握手四次揮手的原理及流程分析,具有一定參考價值,需要的朋友可以了解下。2017-11-11

