小白,自己寫(xiě)的,渴望得到指導(dǎo)
1.輸出是英文,因?yàn)椴恢罏樯?我的eclipse輸入中文總出現(xiàn)問(wèn)題
2.租車(chē)類(lèi)型及價(jià)目表的那個(gè)表沒(méi)有具體畫(huà)
3.scanner不太會(huì)用,提示資源泄漏和未關(guān)閉
4.好像寫(xiě)麻煩了,但是秉持著再修改再改造的思路編的,請(qǐng)求指導(dǎo)
package cn.zjy.easyCar;
import java.util.Scanner;
public class easyCar {
//--------------------------------------------------------------------------------------------------------------------------------------------
public static void main(String[] args) {
System.out.println("Welcome to DADA Car Rental! ");
//判斷是否要租車(chē)
System.out.println("Do you want to rent cars: 1-->yes 0-->no");
Scanner input = new Scanner(System.in);
int i = input.nextInt();
if(i==0) {
System.out.println("eixt");
}else if(i==1) {
//開(kāi)啟進(jìn)程
new Thread(new TCarRent()).start();
}
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------
//租車(chē)線程
class TCarRent implements Runnable{
public void run() {
System.out.println("The car you can rent and the rent:");
CarList carlist = new CarList();
carlist.show();
//定義鍵盤(pán)輸入租車(chē)數(shù)目和存儲(chǔ)選擇車(chē)編號(hào)的數(shù)組
System.out.println("How many car you want to rent:");
Scanner input = new Scanner(System.in);
int rentNum = input.nextInt();
int rentList[]=new int[rentNum];
for(int i=0;i<rentNum;i++) {
System.out.println("Please input No."+(i+1)+" car's carNum:");
//鍵盤(pán)輸入租車(chē)編號(hào)
Scanner in =new Scanner(System.in);
rentList[i] = in.nextInt();
}
//輸入租車(chē)天數(shù)
System.out.println("Please input the day you want to rent:");
int rentDay;
Scanner day =new Scanner(System.in);
rentDay = day.nextInt();
//
carBill carbill = new carBill(rentNum,rentList);
System.out.println("Your bill:");
//統(tǒng)計(jì)載客的車(chē)
System.out.println("The car can carry passenger:");
int Stapass = carbill.StaPassenger(); //返回載客數(shù)目 同時(shí)會(huì)打印載客的車(chē)都有哪些
System.out.println("Passenger carrying capacity: "+Stapass);
//統(tǒng)計(jì)載貨的車(chē)
System.out.println("The car can carry cargo:");
int Stacargo = carbill.StaCargo(); //返回載客數(shù)目 同時(shí)會(huì)打印載客的車(chē)都有哪些
System.out.println("Cargo carrying capacity: "+Stacargo);
//計(jì)算總租金
System.out.println("Total Price: "+carbill.totalPrice(rentDay));
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------
//達(dá)達(dá)租車(chē)公司的租車(chē)類(lèi)型和價(jià)目表
abstract class Car{
int carNum;
String carName;
int carRent;
boolean carryPassenger;? //是否可以載人
boolean carryCargo;? //是否可以再貨
int carryNumP ;
int carryNumC ;
}
//具體是車(chē)類(lèi)類(lèi)型
//客車(chē)類(lèi) 只能載人
class Bus extends Car{
int carryNumC=0 ;
public Bus(int carNum, String carName, int carRent, boolean carryPassenger, boolean carryCargo, int carryNumP) {
super();
this.carNum = carNum;
this.carName = carName;
this.carRent = carRent;
this.carryPassenger = carryPassenger;?
this.carryCargo = carryCargo;?
this.carryNumP = carryNumP;
}
}
//皮卡車(chē)類(lèi) 可載人載貨
class Pickup extends Car{
public Pickup(int carNum, String carName, int carRent, boolean carryPassenger, boolean carryCargo, int carryNumP,int carryNumC ) {
super();
this.carNum = carNum;
this.carName = carName;
this.carRent = carRent;
this.carryPassenger = carryPassenger;
this.carryCargo = carryCargo;
this.carryNumP = carryNumP;
this.carryNumC = carryNumC;
}
}
//貨車(chē)類(lèi) 只能載貨
class Trunk extends Car{
int carryNumP =0;
public Trunk(int carNum, String carName, int carRent, boolean carryPassenger, boolean carryCargo, int carryNumC ) {
super();
this.carNum = carNum;
this.carName = carName;
this.carRent = carRent;
this.carryPassenger = carryPassenger;
this.carryCargo = carryCargo;
this.carryNumC = carryNumC;
}
}
//車(chē)目清單
class CarList{
Car AudiA4 = new Bus(1,"AudiA4",500,true,false,4);
Car Mazda6 = new Bus(2,"Mazda6",400,true,false,4);
Car Pickup6 = new Pickup(3,"Pickup6",450,true,true,4,2);
Car JinLong = new Bus(4,"JinLong",800,true,false,20);
Car SHJ = new Trunk(5,"SHJ",400,false,true,4);
Car? Iveco = new Trunk(6,"Iveco",1000,false,true,20);
public void show() {
System.out.println("Car List:?租車(chē)類(lèi)型及其價(jià)目表");
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------
//賬單類(lèi)
class carBill {
int rentNum;
int[] rentList;
Car[] CarArray;
int rentDay;
//構(gòu)造方法 進(jìn)行初始化
public carBill(int rentNum,int[] rentList) {
this.rentNum = rentNum;
this.rentList = rentList;
this.CarArray = new Car[rentNum];
CarList cl = new CarList();
for(int i=0;i<rentNum;i++) {
switch(this.rentList[i]){
case 1:
CarArray[i] = cl.AudiA4;
break;
case 2:
CarArray[i] = cl.Mazda6;
break;
case 3:
CarArray[i] = cl.Pickup6;
break;
case 4:
CarArray[i] = cl.JinLong;
break;
case 5:
CarArray[i] = cl.SHJ;
break;
case 6:
CarArray[i] = cl.Iveco;
break;
}
}
}
//判斷載客的車(chē)和可以載客的數(shù)目
public int StaPassenger() {
int sumPassenger = 0; //用來(lái)統(tǒng)計(jì)載客數(shù)
for(int i=0;i<rentNum;i++) {
if(CarArray[i].carryPassenger) {
System.out.println(CarArray[i].carName);
sumPassenger += CarArray[i].carryNumP;
}
}
return sumPassenger;
}
//判斷載貨的車(chē)和可以載貨的數(shù)目
public int StaCargo() {
int sumCargo = 0;
for(int i=0;i<rentNum;i++) {
if(CarArray[i].carryCargo) {
System.out.println(CarArray[i].carName);
sumCargo += CarArray[i].carryNumC;
}
}
return sumCargo;
}
//租車(chē)總價(jià)格計(jì)算方法
public int totalPrice(int rentDay) {
this.rentDay = rentDay;
int sum = 0;
for(Car i:CarArray) {
sum += i.carRent;
}
sum *= rentDay;
return sum;
}
}
2020-04-17
scanner那個(gè)用完好像需要在最后加一個(gè)close,在那個(gè)visual code里不結(jié)束會(huì)提示保存,在eclipse和idea里好像不會(huì)