我有两个类(实际上更多,但这是不相关的)。在我的主类类中,我有一个名为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