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



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

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

如何将其合并到下面的代码中?

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

For case 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
}
}

另外,当你加载数据时,在构造函数中做scan操作并不是一个好的做法,把它移到外面:

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

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

最新更新