我有兩個類(實際上更多,但這是不相關的)。在我的主類類中,我有一個名爲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
,但它仍然打印出空。錯誤在哪裏?
我不明白......你創建了一個'Employee'實例,其中包含你需要的所有數據,而不是將它傳遞給'registerEmployee',你只是忽略它。爲什麼?這有什麼意義?爲什麼你在'registerEmployee'中創建一個新的員工,其中包含你從未設置過的字段? – Tom
這裏的零件沒有任何意義。您的註冊僱員沒有被給予任何信息。它似乎只是製作當前員工的副本。你也似乎甚至沒有初始化員工。沒有看到你所有的相關代碼(比如Main),我不認爲這是可以回答的。 – Carcigenicate
謝謝,但我如何將具有數據的Employee實例傳遞給registerEmployee? – JavaTeachMe2018