2017-10-09 97 views
-1

我有兩個類(實際上更多,但這是不相關的)。在我的主類類中,我有一個名爲createEmployee()的方法,其中我使用用戶輸入創建了一個Employee。 這裏的主要類:Java:將一個對象添加到數組列表並檢索它打印出null

import java.util.*; 

public class Main { 

private static String END_LINE; 
private Scanner sc; 
public String name; 
public String ID; 
public double salary; 
private ReusaxCorp reusaxcorp; 

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

public void presentoptions(){ 
    reusaxcorp = new ReusaxCorp(); 
    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 all employees information. "); 
     System.out.println("4. Quit this program. "); 
     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; 
      case 4: 
       System.out.println("You've quitted the program."); 
       System.exit(0); 

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

String empID; 
String empName; 
double empSalary; 

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

     System.out.println("What's the ID of the new " + typeofemployee + "?"); 
     String ID = sc.nextLine(); 
     System.out.println("Wheat'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 "Employee": 
       reusaxcorp.registerEmployee(); 
       break; 

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

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

} 

在我ReusaxCorp類我有一個方法registerEmployee,這應該添加一個新員工數組列表和retrieveEmployee,應打印所有在冊職工。在這裏,他們是:

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() { 
     Main main = new Main(); 
     for(Employee employee: employees){ 
      System.out.println("ID: " + ID + END_OF_LINE + "Name: " + employee.name + END_OF_LINE + "salary: " + employee.grossSalary); 
    } 
} 

我的問題是,它總是打印出ID:空名稱:零薪水:0.0,我不知道那是什麼。我也想這樣做 - 例如 - 這樣的:

String empID; 
String empName; 
double empSalary; 

// in my Main class I wrote getters and setters and this in my createEmployee method: 

System.out.println("What's the ID of the new " + typeofemployee + "?"); 
String newID = sc.nextLine(); 
setEmpID(newID); 

在我retrieveEmployee()方法我然後寫getEmpID()代替ID,但它仍然打印出空。錯誤在哪裏?

+0

我不明白......你創建了一個'Employee'實例,其中包含你需要的所有數據,而不是將它傳遞給'registerEmployee',你只是忽略它。爲什麼?這有什麼意義?爲什麼你在'registerEmployee'中創建一個新的員工,其中包含你從未設置過的字段? – Tom

+0

這裏的零件沒有任何意義。您的註冊僱員沒有被給予任何信息。它似乎只是製作當前員工的副本。你也似乎甚至沒有初始化員工。沒有看到你所有的相關代碼(比如Main),我不認爲這是可以回答的。 – Carcigenicate

+0

謝謝,但我如何將具有數據的Employee實例傳遞給registerEmployee? – JavaTeachMe2018

回答

0

實際上,您將創建一個新的Employee對象,不做任何事情,然後在您的ReusaxCorp中調用一個創建一個沒有值的新Employee對象的方法,並將其添加到您的「employees」列表中。 當你調用你的「retrieveEmployees」方法時,它會打印出你的空僱員。在你的 「在CreateEmployee」 類

,做如下改變你的 「開關」 的聲明:

解決這個由

reusaxcorp.registerEmployee(employee); // Now you are actually passing your newly created Employee object 

,並在你的 「ReusaxCorp」 類,改變你的 「registerEmployee」方法:

registerEmployee(Employee employee){ // Here, you are receiving an Employee object 
    employees.add(employee);   // and here you are adding it to your list. 
} 

歸根結底,問題是你從未使用過任何你創建的對象,只是填充「空」 Employee對象名單。

希望這有助於。

+0

謝謝,名字和薪水現在顯示,但ID仍然爲空。這是爲什麼? – JavaTeachMe2018

+0

在您的retrieveEmployee方法中,您只需打印「ID」。它應該是「employee.ID」。 – Aaron

相關問題