gpt4 book ai didi

java - 如何在数组中存储新的客户端信息并能够在java中再次查看它?

转载 作者:行者123 更新时间:2023-12-01 18:44:12 25 4
gpt4 key购买 nike

这是我的任务。我必须创建一个应用程序来显示菜单并根据用户的选择执行相应的操作。这是我的菜单应包含的内容

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();

但是如何获取所有用户输入并将其存储到数组中?我不知道从哪里开始。

最佳答案

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Exercise {

// list to store your clients
static List<Client> clients = new ArrayList<>();

static class Client{
public String name;
public String email;
public String make;
public int year;
public String vin;

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;
}
}

public static void main_addNewClient() {
String name, email, vin, make;
int year;
Scanner input = new Scanner(System.in);
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.nextInt();

// create new client object with input you got
Client c = new Client(name, email, make, year, vin);
// add client to the list
clients.add(c);
}
}

这就是你想知道的吗?这个练习有很多内容。我认为您首先需要构建一个菜单,该菜单将询问用户他想做什么:添加新客户、查找客户等。

关于java - 如何在数组中存储新的客户端信息并能够在java中再次查看它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59869794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com