Java: Accessibility problems with two methods in ArrayList (three classes)

Viewed 49

I have three classes: Main,ReusaxCorp and Employee. Let's get straight to the point: In the ReusaxCorp class I want to implement two methods: retrieveEmployee, which iterates through the Array List and prints out all employees information. So I tried this:

public void retrieveEmployee() {
    for (int i = 0; i < employees.size(); i++) {
        System.out.println("ID: " + employee.ID + END_OF_LINE + "Name: " + employee.name + END_OF_LINE + "salary: " + employee.grossSalary);
    }
}

But that does not work, because I can't access - for instance - employee.ID or employee.name. In the second method. updateEmployee I would have liked to change the information, but that doesn't work either because of the accessibility. I appreciate any kind of help. Here are my three classes:

public class Employee {

protected String ID;
protected String name;
protected double grossSalary;
final String END_OF_LINE = System.lineSeparator();

   public Employee (String ID, String name, double grossSalary){
       this.ID = ID;
       this.name = name;
       this.grossSalary = grossSalary;
   }

   public double getGrossSalary() {
       return grossSalary;
   }

   public void setGrossSalary(double grosssalary) {
       this.grossSalary = grossSalary;
   }

   public String getName(){
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }
}

Here's my Main class:

public class Main {
private static String END_LINE;
private Scanner sc;
public String name;
public String ID;
public double salary;
private int GPA;
private ReusaxCorp reusaxcorp; 

public Main(){
    sc = new Scanner(System.in);
    END_LINE = System.lineSeparator();
}


   public void presentoptions(){
       while (true){
           System.out.println("=== Welcome === ");
           System.out.println("Choose an option below: ");
           System.out.println(" ");
           System.out.println("1. Register an employee. ");
           System.out.println("2. Remove an employee. ");
           System.out.println("3. Retrieve an employees information. ");

           int option = sc.nextInt();

           switch (option) {
               case 1:
                   System.out.println("What type of employee? " + END_LINE
                        + " - Intern. " + END_LINE
                        + " - Employee. " + END_LINE
                        + " - Manager. " + END_LINE
                        + " - Director." + END_LINE);
                   String type = sc.nextLine();

                   createEmployee();
                   break;

               case 2:
                   break;
               case 3:
                   reusaxcorp.retrieveEmployee();
                   break;
               default:
                   System.out.println("Error. Please try again.");
                   break;
           }
       }
   }

   public void createEmployee(){
       String typeofemployee = sc.nextLine();

           System.out.println("What's the ID of the new " + typeofemployee + "?");
           ID = sc.nextLine();
           System.out.println("What's the name of the new " + typeofemployee + "?");
           name = sc.nextLine();
           System.out.println("What's the salary of the new " + typeofemployee + "?");
           salary = sc.nextDouble();

           Employee employee = new Employee(ID, name, salary);

           switch (typeofemployee) {
               case "Intern":
                   System.out.println("What's the new Interns GPA? ");
                   GPA = sc.nextInt();                   
               case "Employee":
                   break;

               case "Manager":
                   break;

               case "Director":
                   break;

               default:
                   System.out.println("Error");
                   break;
           }
   }

   public static void main(String[] args) {
       Main runcode = new Main();
       runcode.presentoptions();
   }
}

And here at last the ReusaxCorp class.

public class ReusaxCorp extends Main {

   ArrayList<Employee> employees = new ArrayList<Employee>();
   final String END_OF_LINE = System.lineSeparator();

   public void registerEmployee(){
       employees.add(new Employee(ID, name, salary));
   }

   public void retrieveEmployee() {
       for (int i = 0; i < employees.size(); i++) {
           System.out.println("ID: " + employee.ID + END_OF_LINE + "Name: " + employee.name + END_OF_LINE + "salary: " + employee.grossSalary);
       }
   }

   public void updateEmployee(){
   }
}
2 Answers

You are trying to access the variable employee which does not exist anywhere int the class.

public void retrieveEmployee() {
   for (int i = 0; i < employees.size(); i++) {

       //add this line
       Employee employee = employees.get(i);

       System.out.println("ID: " + employee.ID + END_OF_LINE + "Name: " + employee.name + END_OF_LINE + "salary: " + employee.salary);
   }
}

You can also use a for-each loop:

for(Employee employee: employees){
    System.out.println("ID: " + employee.ID + END_OF_LINE + "Name: " + employee.name + END_OF_LINE + "salary: " + employee.salary);
}

Also note that accessing the fields such as ID using employee.ID is only applicable if the classes are in the same package (being protected). Otherwise you need to create a getter and access them using getter such as employee.getID()

Use employee.getId() instead of employee.ID while iterating through the list of employees, you already have those public getter methods implemented while the class members are protected. Do so for the other class members (name and so on) as well… You can do that in your solution by getting the employee from the list by index like

System.out.println(employees.get(i).getId());

or you just take a "for each" loop like

for (Employee employee : employees) { 
    System.out.println(employee.getId()); 
}
Related