2017-10-09 99 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

相关问题