I want to add a list to the array through user input

I try to create a small program that asks the user for input that accepts the name of the employee, and the salary adds it to the array list, then displays the parameters on the screen (for example, 0: quit, 1: add, 2: display), reads the input, then goes to the entrance. Playback will be simple (eg Last Name: Smith Salary: £ 14,000. I just need help to point me in the right direction. Currently I have 3 classes Employee, Employee List and Employee Test.

This class requests 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 add employees to arraylist, but I'm not sure im doing it right.

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) {

      } 
}

Here is the main method

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

}

}
+4
3

, :

  • EmployeeList Employee. , , . : List , ( , , List<Employee>)
  • . - Employee , . , - :

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

    , EmployeeHandler, . , , - , .

  • - ( ):
    • , , employee
    • , ( toString Employee)
    • ,
    • ,
+3
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;
      }
   }
}

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

}

, arraylist, . x arraylist.

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

,

employeeList.add(employee);

EDIT:

OOPS, . . , employee.setLastName(), 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); 

2:

arraylists . . , .

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

:

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

}

EmployeeTest:

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




}

}

, 2 , , arraylist.

I read that the toString method is used to get these details, so I can print them on the screen. And, using this for loop, each item in the list displays them. Did I miss something? An apology for this will be pulled a little, they just need it to work.

0
source

Source: https://habr.com/ru/post/1658031/


All Articles