问答详情
源自:12-1 综合练习

小白,自己写的,渴望得到指导

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-16 00:34

个回答

  • 电力皮卡
    2020-04-17 15:01:19

    scanner那个用完好像需要在最后加一个close,在那个visual code里不结束会提示保存,在eclipse和idea里好像不会