【问题标题】:How do I store new client information in array and be able to view it again in java?如何将新的客户信息存储在数组中并能够在 java 中再次查看?
【发布时间】: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&lt;Client&gt;(并通过它搜索以查找现有客户)或Map&lt;String,Client&gt;(可让您通过密钥请求特定客户)。

标签: java arrays class user-input


【解决方案1】:
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);
}
}

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

【讨论】:

  • 是的,我已经建立了菜单(我已将其添加到帖子中)。它只是存储我遇到问题的客户的基本信息。
【解决方案2】:

首先,在你的 main 中声明一个包含所有客户端的 ArrayList

 ArrayList clients = new ArrayList<Client>();   

其次,在您的方法中创建一个新的 client 对象并填充您从输入中获得的数据,然后将此客户端数据添加到您创建的 ArrayList 中

   Client client = new Client(name,email,vin,year,make);
   clients.add(client);

【讨论】:

  • 我不认为它特定于 Android。看起来只是大学/学校的常规 Java 练习。
猜你喜欢
  • 2011-01-14
  • 2021-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多