简体   繁体   English

我想通过用户输入添加到arrayList

[英]I want to add to an arrayList through user input

I have been trying to create a small program that prompts the user for input that takes an employee name and salary adds it to an arrayList, then displays options on the screen(eg 0: quit, 1: add, 2: display), reads the input then proceeds based on the input.Displaying would just be(eg Last Name: Smith Salary: £14000. Just need some help to point me in the right direction. I currently have 3 classes Employee, Employee List and Employee Test. 我一直在尝试创建一个小程序,提示用户输入一个雇员姓名和工资将其添加到arrayList,然后在屏幕上显示选项(例如0:退出,1:添加,2:显示),读取输入然后根据输入进行。显示只是(例如姓氏:史密斯薪水:14000英镑。只需要一些帮助指出我正确的方向。我目前有3个班级员工,员工名单和员工测试。

This class prompts the user input. 该类提示用户输入。

import java.util.Scanner;
public class Employee {

private String Last_Name;
private int Salary;

public Employee(){
    Scanner inputValues = new Scanner(System.in);

    System.out.print("Enter employee last name: ");
    Last_Name = inputValues.next();

    System.out.print("Enter Employee Salary: " + "£");
    Salary = inputValues.nextInt();
}

public void Display(){

    System.out.printf("Name: " + Last_Name + " " + "Salary: " + Salary);
    System.out.println("\n");
}
}

This class is supposed to be adding the employees to an arraylist but im not sure if im doing it correctly. 这个类应该是将员工添加到arraylist但我不确定我是否正确地做了。

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

public class EmployeeList extends Employee{

private List <Employee> employee =  new ArrayList<Employee>();


 public EmployeeList(Employee person) {
     employee.add(person);
}

 public void DisplayEmployees(){
     System.out.println("Employee:"  + employee.size()); 
     displayList(employee);
 }

 public static void displayList(List employee) {

      } 
}

This is where the main method is 这是主要方法的地方

import java.util.Scanner;

public class EmployeeTest {


public static void main(String[] args) {

    Employee employee = new Employee();
    employee.Display();

    EmployeeList empList =  new EmployeeList(employee);
    empList.DisplayEmployees();

    Scanner scanner = new Scanner(System.in); 
    System.out.println("0: quit, 1: add, 2: display");
    String employees = scanner.next(); 

   /* if (employees.equals("1")){
     //not sure how to go back to displaying the user prompts

        break;
    } */

}

}

Few tips I can think of: 我能想到的一些提示:

  1. EmployeeList should not extend Employee . EmployeeList不应该扩展Employee The main rule of OOP is that class A extends class B if B is a A. This is clearly not the case here - employeeList is not an employee, it's a list of employees (In my mind you don't need a class for this, just List<Employee> ) OOP的主要规则是,如果B A,则A类扩展B类。这里显然不是这种情况 - employeeList不是员工,它是员工列表(在我看来,你不需要一个类这个,只List<Employee>
  2. I'd separate the logic from the data. 我将逻辑与数据分开。 meaning - Employee class should only hold the employee's data, not deal with scanning and getting the input from the user. 意思 - Employee类应该只保存员工的数据,而不是处理扫描和获取用户的输入。 the constructor should be simple in my mind, something like: 构造函数在我的脑海里应该很简单,例如:

     public Employee(String name, int salary) { this.name = name; this.salary = salary; } 

    The logic of getting the data should be outside of this class, either in an EmployeeHandler or in the main itself. 获取数据的逻辑应该在此类之外,可以在EmployeeHandler ,也可以在main本身中。 Since you put it inside the employee, you are having troubles continuing when some of the logic is in the employee and some in the main. 既然你把它放在员工里面,那么当你的一些逻辑存在于员工中而某些逻辑存在于主要人员中时,你就会遇到麻烦。

  3. the high-level code should be something like (I'll leave the details to you): 高级代码应该是这样的(我会留下细节给你):
    • show the menu options to the user 向用户显示菜单选项
    • if he wants to add user, get input for both variables, create the employee object and add it to the list 如果他想添加用户,获取两个变量的输入,创建员工对象并将其添加到列表中
    • if he wants to display, go over the list and print (the printing can be done overriding toString in Employee class) 如果他想显示,请查看列表并打印(打印可以在Employee类中覆盖toString
    • if he wants to quit, finish 如果他想退出,完成
    • continue this loop until he wants to quit 继续这个循环,直到他想要退出
public class Employee {

    private String Last_Name;
    private int Salary;

    public Employee(){

      public String getLName(){
           return Last_Name;
      }

       public void setLName(){
           this.Last_Name = Last_Name;
      }

       public int getSalary(){
           return salary;
      }

       public void setSalary(){
           this.salary = salary;
      }
   }
}

then in your main method you can create the employee object. 然后在main方法中,您可以创建employee对象。

public static void main(String[] args){

    Employee employee = new Employee();
    Scanner scanner = new Scanner(System.in); 
    employee.setLName = scanner.next();
    employee.setSalary = scanner.nextInt();

}

If i were you I would just make an arraylist to hold all employees. 如果我是你,我会让一个arraylist来容纳所有员工。 I would prompt the input option for x amount of times and add to the end of the arraylist. 我会提示x次输入选项并添加到arraylist的末尾。 the arraylist would be created as so arraylist将如此创建

ArrayList<Employee> employeeList = new ArrayList<Employee>();

to add to it use add 添加到它使用添加

employeeList.add(employee);

This should be able to get you started 这应该可以帮助你入门

EDIT: 编辑:

OOPS, made several mistakes. OOPS犯了几个错误。 edit with the following. 使用以下内容编辑。 Note that it is employee.setLastName(value) because the method setLastName is part of the employee class and it must be passed a value because we have defined that in the employee class. 请注意,它是employee.setLastName(value),因为方法setLastName是employee类的一部分,并且必须传递一个值,因为我们已在employee类中定义了该值。

    Employee employee = new Employee();
    Scanner scanner = new Scanner(System.in); 
    String tempName = scanner.next();
    int tempSalary = scanner.nextInt();
    employee.setLastName(tempName);
    employee.setSalary(tempSalary); 

EDIT 2: 编辑2:

try to print arraylists as follows. 尝试按如下方式打印arraylists。 didnt test it. 没有测试它。 let me know how it works. 让我知道它是如何工作的。

 for (int i = 0; i< employeelist.size(); i++){
         Employee temp = values.get(i);
         System.out.println("Last Name: " + temp.getLname() + "Salary: " + temp.getSalary());
 }

I modified my employee class to look like this: 我将我的员工类修改为如下所示:

public class Employee {

public String lastName;
private int salary;

public Employee(){
}
public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public int getSalary() {
    return salary;
}

public void setSalary(int salary) {
    this.salary = salary;
}

@Override
public String toString(){
    return "Employee Last Name: " + lastName + "\n" + "Employee Salary: " + salary;
}

}

I have modified my EmployeeTest class: 我修改了EmployeeTest类:

This is my EmployeeTest class: 这是我的EmployeeTest类:

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

public class EmployeeTest {

static ArrayList<Employee> employeeList = new ArrayList<Employee>();

public static void main(String[] args) {

    for(int i=0;i<5;i++){
        addEmployees(employeeList); 
    } 
   System.out.println(employeeList.toArray());

}

public static void addEmployees(ArrayList<Employee> employeeList){
    Scanner scanner = new Scanner(System.in); 
    System.out.println("0: quit, 1: add, 2: display");
    String options =  scanner.next();
    if(options.equals("1")){

    System.out.print("Enter employee last name: ");
    String lastname = scanner.next();

    System.out.print("Enter Employee Salary: " + "£");
    int salary = scanner.nextInt();

    Employee employee = new Employee();
    EmployeeList.add(employee);
    }

    else if(options.equals("2")){
        for (Employee employee : employeeList){
        /*System.out.println("Name: " + employee.getLastName() +  ", " + "Salary: " + employee.getSalary());*/
            System.out.println(employee);
        }
    }
    else{
        System.exit(0);
    }




}

} }

However when i press 2 as my options it doesn't display what is in my arraylist. 但是,当我按2作为我的选项时,它不显示我的arraylist中的内容。

I had read that a toString method is used to get those details so i could print them to the screen. 我读过一个toString方法用于获取这些细节,以便我可以将它们打印到屏幕上。 And that using this for loop gets each item in the list to display them. 并且使用此for循环获取列表中的每个项目以显示它们。 Am i missing something here?. 我在这里错过了什么吗? Apologies for this dragging on a bit, im just wanting this to work. 为此拖延道歉,我只想让它起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM