【发布时间】:2020-01-22 23:45:03
【问题描述】:
这是我的任务。 我必须创建一个显示菜单并根据用户选择执行相应操作的应用程序。这是我的菜单应该包含的内容
1. add a new client - keep it as the current client
2. find a client with a given name - keep it as the current client
3. add a service for the current client
4. print the current client's balance
5. print the current client's history of services
6. print all the clients' balances
7. exit [save all the data to a file option and retrieve it back when the program starts]
System.out.println("Welcome to Tracy'S Auto Mechanic Shop!");
do{
System.out.println("\nTracy's Auto Mechanic Shop Main Menu");
System.out.println("------------------------------------");
for(int i = 0; i < choices.length; i++){
System.out.println((i < choices.length-1? ? i+1 : 0) + ". " + choices[i]);
}
System.it.print("Make your choice: ");
choice = input.nextInt();
switch(choice){
case 1:
main_addNewClient();
break;
case 2:
main_SearchClient();
break;
case 3:
main_AddService();
break;
case 4:
main_ViewBalance();
break;
case 5:
main_ViewHistory();
break;
case 6:
main_ViewAll();
break;
case 0:
System.exit(0);
default:
System.out.println("Invalid choice. Try again.");
}
}while(choice != 0);
input.close();
}
到目前为止,我已经创建了菜单,但我一直在添加新客户端。如何将所有信息存储到数组中并保存?我创建了一个如下所示的类:
class Client{
public String name;
public String email;
public String make;
public int year;
public String vin;
public static void Client(String name, String email, String make, int year, String vin){
this.name = name;
this.email = email;
this.make = make;
this.year = year;
this.vin = vin;
}
}
对于添加新客户端的选项,我在 main 中创建了一个方法
public static void main_addNewClient() {
String name, email, vin, make;
int year;
input.nextLine();
System.out.print("Enter your first name: ");
name = input.nextLine();
System.out.print("Enter your email: ");
email = input.nextLine();
System.out.print("Enter vin: ");
vin = input.nextLine();
System.out.print("Enter make: ");
make = input.nextLine();
System.out.print("Enter year: ");
year = input.nextLine();
但是如何获取所有用户输入并将其存储到数组中?我不知道从哪里开始。
【问题讨论】:
-
使用
List<Client>(并通过它搜索以查找现有客户)或Map<String,Client>(可让您通过密钥请求特定客户)。
标签: java arrays class user-input