java 简单的Java停车场管理系统
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40031246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple Java Car Park Management System
提问by tomaszsvd
I got a bit rusty in Java, have not program anything like this for ages.I'am trying to create a simple car park manager program and implement following classes Vehicle and the sub classes Car, Van,Motorbike(those extend Vehicle Class. The Car class should hold information about the number of the doors of the car and the color. The Van class should include information about the cargo volume of the van. The class Motorbike should have information about the engine size. The program should allow to add Vehicle to the parking lot, delete the vehicle and print the list of currently parked vehicles and give information if parked vehicles are cars bikes or vans. I have designed all the classes. However i have a problem with the user input on this. User while adding vehicle should be allowed to input car bike or van model, registration plate, the color of the vehicle, also number of doors. And program should print the information when corresponding menu option is chosen. Can you please have a look at the code i have so far, any help will be much appreciated. The problem i have is to get the user to input all the requested information.
车辆的颜色,还有车门的数量。并且程序应在选择相应的菜单选项时打印信息。你能看看我到目前为止的代码吗,任何帮助将不胜感激。我的问题是让用户输入所有请求的信息。
public class Vehicle {
private String carBrand;
private String regPlate;
// default constructor
public Vehicle() {
}
// constructor
public Vehicle(String carBrand, String regPlate) {
this.carBrand = carBrand;
this.regPlate = regPlate;
}
//getters
public String getCarBrand() {
return carBrand;
}
public String getRegPlate() {
return regPlate;
}
//setters
public void setCarBrand(String carBrand) {
this.carBrand = carBrand;
}
public void setColor(String regPlate) {
this.regPlate = regPlate;
}
}
...
...
public class Main {
public static void main(String[] args) {
CarParkManager myCarPark = new CarParkManager();
Scanner input = new Scanner(System.in);
int menu;
String model;
do {
System.out.println("WELCOME TO PARKING MANAGEMENT");
System.out.println("1: To Park Vehicle");
System.out.println("2: To Departure");
System.out.println("3: Show All Perked Vehicles");
System.out.println("0: To Exit");
System.out.print("Enter your choice: ");
menu = input.nextInt();
System.out.println();
switch (menu) {
case 1: {
String vType;
System.out.println("Please choose The Vehicle type");
System.out.println("C = Car");
System.out.println("B = Motorbike");
System.out.println("V = VAN");
vType = input.next();
if (vType.equals("C")) {
System.out.println("Enter Model");
model = input.next();
System.out.println("Enter Colour");
String colour = input.next();
System.out.println("Enter Reg Plate");
String regPlate = input.next();
System.out.println("Door Number");
int doorNumber = input.nextInt();
} else if (vType.equals("B")) {
} else if (vType.equals("V")) {
}
break;
}
case 2: {
break;
}
case 3: {
System.out.println("List of All Parked Vehicles : ");
myCarPark.printParkedVehicleDetails();
break;
}
case 0: {
System.out.println("\nThank you!\n");
break;
}
default: {
System.out.println("Invalid option!\n");
break;
}
}
} while (menu != 0);
}
}
回答by John3136
- Ask user what type of Vehicle they want to create
- Create the given type of Vehcle
- Call
getDetailsFromUser
on the Vehicle
- 询问用户他们想要创建什么类型的车辆
- 创建给定类型的 Vehcle
- 呼叫
getDetailsFromUser
车辆
The specific subclass (Van etc) can ask the user what ever questions they need in order to populate themselves.
特定的子类(Van 等)可以询问用户他们需要什么问题来填充自己。