小白,自己寫的,渴望得到指導
1.輸出是英文,因為不知道為啥?我的eclipse輸入中文總出現問題
2.租車類型及價目表的那個表沒有具體畫
3.scanner不太會用,提示資源泄漏和未關閉
4.好像寫麻煩了,但是秉持著再修改再改造的思路編的,請求指導
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! ");
//判斷是否要租車
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) {
//開啟進程
new Thread(new TCarRent()).start();
}
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------
//租車線程
class TCarRent implements Runnable{
public void run() {
System.out.println("The car you can rent and the rent:");
CarList carlist = new CarList();
carlist.show();
//定義鍵盤輸入租車數目和存儲選擇車編號的數組
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:");
//鍵盤輸入租車編號
Scanner in =new Scanner(System.in);
rentList[i] = in.nextInt();
}
//輸入租車天數
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:");
//統計載客的車
System.out.println("The car can carry passenger:");
int Stapass = carbill.StaPassenger(); //返回載客數目 同時會打印載客的車都有哪些
System.out.println("Passenger carrying capacity: "+Stapass);
//統計載貨的車
System.out.println("The car can carry cargo:");
int Stacargo = carbill.StaCargo(); //返回載客數目 同時會打印載客的車都有哪些
System.out.println("Cargo carrying capacity: "+Stacargo);
//計算總租金
System.out.println("Total Price: "+carbill.totalPrice(rentDay));
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------
//達達租車公司的租車類型和價目表
abstract class Car{
int carNum;
String carName;
int carRent;
boolean carryPassenger;? //是否可以載人
boolean carryCargo;? //是否可以再貨
int carryNumP ;
int carryNumC ;
}
//具體是車類類型
//客車類 只能載人
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;
}
}
//皮卡車類 可載人載貨
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;
}
}
//貨車類 只能載貨
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;
}
}
//車目清單
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:?租車類型及其價目表");
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------
//賬單類
class carBill {
int rentNum;
int[] rentList;
Car[] CarArray;
int rentDay;
//構造方法 進行初始化
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;
}
}
}
//判斷載客的車和可以載客的數目
public int StaPassenger() {
int sumPassenger = 0; //用來統計載客數
for(int i=0;i<rentNum;i++) {
if(CarArray[i].carryPassenger) {
System.out.println(CarArray[i].carName);
sumPassenger += CarArray[i].carryNumP;
}
}
return sumPassenger;
}
//判斷載貨的車和可以載貨的數目
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;
}
//租車總價格計算方法
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那個用完好像需要在最后加一個close,在那個visual code里不結束會提示保存,在eclipse和idea里好像不會