如何从用户输入数组中调用某些信息

How to call certain information from a user input array

我正在为 class 完成我的最终项目,我们正在制作一个 class 来存储员工、ID# 和薪水范围,然后创建方法和数组添加到 class。到目前为止,我已将其设置为添加新员工并打印所有员工,但我有点困惑如何获得这两个提示:

  1. 检索特定员工的数据 - 提示用户输入员工 ID 并显示相应员工的数据:ID、姓名和薪水
  2. 根据范围检索薪水员工 - 提示用户输入最低和最高薪水,并显示薪水在该范围内的所有员工。在单独的行中显示每个员工的所有信息 - 姓名、身份证和薪水

我如何将其合并到以下代码中?

import java.util.ArrayList;

import java.util.Scanner;

public class FP {



   public static void main(String[] args) {

      Scanner in = new Scanner(System.in);

      ArrayList<EmployeeData> companyData = new ArrayList<>();

// Boolean loop and while loop to restart the program in all cases but 3
      boolean loopAgain = true;

      while (loopAgain) {

          System.out.println("Main Menu:");

          System.out.println("1: Load New Employee Data");

          System.out.println("2: Print Employee Data");

          System.out.println("3: Exit");

          int answer = in.nextInt();


          switch (answer) {
// Case 1 to be called on when user selects "1"
              case 1: {

                  System.out.println("Load New Employee Data:");
                  System.out.println("Please enter the number of new Employees to be added to 
the sytem.");
                  System.out.println();
                  

                  int loop = in.nextInt();
//For loop to add the number to class EmployeeData
                  for (int i =0; i < loop; i++) {

                      companyData.add(new EmployeeData());

                  }

              } break;

           // Case 2 to be called on when user selects "2"
              case 2: {
// for loop to display the employees information after being entered by the user
                  for (EmployeeData x : companyData) {

                      x.printData();

                  }

              } break;

// Case 2 to be called on when user selects "3" breaking the loop and ending the program
              case 3: loopAgain = false; break;


          }

      }



   }

}

// Class to store information for user input of employee data
class EmployeeData {
    
// String variable for names
   String name;
// Int variables for ID and Salary
   int id, salary;


   public EmployeeData() {
// scan options and prompts for user input to be stored in class Employee Data
       Scanner in = new Scanner(System.in);
       System.out.println();
       System.out.println("Enter Employee Name: ");
       System.out.println();
       String name = in.nextLine();
       System.out.println();
       System.out.println("Enter Employee ID:");
       System.out.println();
       int id = in.nextInt();
       System.out.println();
       System.out.println("Enter Employee Salary: ");
       System.out.println();
       int salary = in.nextInt();
       
       // callable and modified variables stored for case 2
       this.name = name;

       this.id = id;

       this.salary = salary;

    
   }

// print section to be shown to user when case 2 is selected
   public void printData() {
       System.out.println();
       System.out.println("Employee: " + this.name);
       System.out.println();
       System.out.println("ID: " + this.id);
       System.out.println();
       System.out.println("Salary: " + this.salary);
       System.out.println();
   }

}

对于案例 4:

                    System.out.println("Please enter the id.");
                    System.out.println();
                    int id = in.nextInt();
                    boolean find = false;
                    for (EmployeeData employeeData : companyData) {
                        if (employeeData.id == id) {
                            find = true;
                            // todo print info

                            break;
                        }

                    }
                    if (!find) {
                        // todo print something
                    }

对于案例 5:

                    System.out.println("Please enter the min salary.");
                    System.out.println();
                    int min = in.nextInt();
                    System.out.println("Please enter the max salary.");
                    System.out.println();
                    int max = in.nextInt();
                    for (EmployeeData employeeData : companyData) {
                        if (employeeData.salary <= max && employeeData.salary >= min) {
                            // todo print info on one line
                        }

                    }

此外,加载数据时在构造函数中进行扫描操作不是一个好习惯,将其移到外部:

case 1:
                   for (int i =0; i < loop; i++) {
                        EmployeeData employeeData = new EmployeeData();
                        // todo scan and set value  

                        companyData.add(employeeData);

                    }

并且为了让程序更完整,你应该做一些额外的检查,比如加载数据时ID不能重复。