2017-10-06 78 views
1

我有三個類:Main,ReusaxCorpEmployee。讓我們直接看點:在ReusaxCorp類中,我想實現兩種方法:retrieveEmployee,它遍歷數組列表,並且列印出所有員工信息。所以,我想這一點:Java:ArrayList中兩種方法的可訪問性問題(三類)

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

但是,這並不工作,因爲我不能訪問 - 例如 - employee.ID或employee.name。在第二種方法中。updateEmployee我本來希望更改信息,但由於無障礙原因,這不起作用。我很欣賞任何形式的幫助。這裏是我的三類:

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

這裏是我的主類:

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

這裏最後的ReusaxCorp類。

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

回答

1

您正試圖訪問變量employee,該變量在類的任何位置都不存在。

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

您也可以使用for-each循環:

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

另外請注意,訪問諸如ID使用employee.ID領域只適用,如果類是在同一包(受保護) 。否則,您需要創建一個吸氣劑並使用吸氣劑訪問它們,例如employee.getID()

+0

@azurefrog注意到並註釋添加。 –

+0

感謝它的運行,但我得到一個NullPointerException。我不知道爲什麼。 – JavaTeachMe2018

+0

你在哪裏得到NPE? –

0

使用employee.getId(),而不是employee.ID同時通過員工列表進行迭代,你已經擁有了實現這些public getter方法,而類成員protected。這樣做對於其他類成員(名稱等),以及... 你可以在你的解決方案通過讓員工從列表索引像

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

或者你只是採取了「每個」像

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