我想知道是否有人可以解释为什么我的代码中的情况2和3似乎没有做任何事情,并且可能提供一些建议。我会提供我认为相关的任何信息,如果您要求更多详细信息,我会添加更多信息。
在发布我的代码之前,让我提供一些细节:我的程序旨在成为一个非常简单的员工数据库。它使用switch语句在命令行上提供用户选项。交换机的选项是:
案例1将Employee对象添加到Employee类型的ArrayList中。 Employee类负责跟踪员工姓名,工资,工时和他们工作的公司。
案例1和案例4似乎正常运作。
然而,案例2和3似乎没有做任何事情。以下是整个交换机,它位于包含main方法的驱动程序类中:
ArrayList<Employee> employees = new ArrayList<Employee>();
int number = 0;
while(number != 4)
{
System.out.print("Please select an option: " +
"\n1) Add an Employee" +
"\n2) List Employees" +
"\n3) List Benefit Status" +
"\n4) Quit"+ "\n");
number = keyboard.nextInt();
keyboard.nextLine();
switch (number)
{
case 1:
System.out.println("Hourly, contract, or salary employee? ");
type = keyboard.nextLine();
if(type.equalsIgnoreCase("hourly"))
{
System.out.print("\nEnter the company: ");
comp = keyboard.nextLine();
System.out.print("\nEnter the first name: ");
fn = keyboard.nextLine();
System.out.print("\nEnter the last name: ");
ln = keyboard.nextLine();
System.out.print("\nEnter the hourly wage: ");
wage = keyboard.nextDouble();
System.out.print("\nEnter the hours worked: ");
hours = keyboard.nextInt();
Employee employee2 = new Employee(comp, fn, ln);
HourlyEmployee he = new HourlyEmployee(wage, hours);
}
else if(type.equalsIgnoreCase("contract"))
{
System.out.print("\nEnter the company: ");
comp = keyboard.nextLine();
System.out.print("\nEnter the first name: ");
fn = keyboard.nextLine();
System.out.print("\nEnter the last name: ");
ln = keyboard.nextLine();
System.out.print("\nEnter the hourly wage: ");
wage = keyboard.nextDouble();
System.out.print("\nEnter the hours worked: ");
hours = keyboard.nextInt();
Employee employee2 = new Employee(comp, fn, ln);
ContractEmployee ce = new ContractEmployee(wage, hours);
}
else if (type.equalsIgnoreCase("salary"))
{
System.out.print("\nEnter the company: ");
comp = keyboard.nextLine();
System.out.print("\nEnter the first name: ");
fn = keyboard.nextLine();
System.out.print("\nEnter the last name: ");
ln = keyboard.nextLine();
System.out.print("\nEnter the salary: ");
salary = keyboard.nextDouble();
Employee employee2 = new Employee(comp, fn, ln);
SalaryEmployee se = new SalaryEmployee(salary);
}
else
{
System.out.println("Invalid input.");
System.exit(0);
}
break;
case 2:
for(int i = 0; i < employees.size(); i++)
{
System.out.println(employees.get(i).toString());
}
break;
case 3:
for(int i = 0; i < employees.size(); i++)
{
System.out.println(employees.get(i).determineBenefits());
}
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Invalid input.");
System.exit(0);
}
}
}
}
在第2和第3种情况下,我试图将ArrayList的索引分别作为参数传递给toString()方法和determineBenefits()方法。当这些方法与交换机分开测试时,它们相同才能正常工作。这是toString()方法:
public String toString()
{
return firstName + " " + lastName + " from " + company +
". The worker's pay this week was $" + pay + ".";
}
还有determineBenefits()方法:
public String determineBenefits()
{
String benefits;
if(isSalaryEmployee == true)
{
benefits = "This employee has a standard company health " +
"insurance policy.";
}
else if(hhh >= 40)
{
benefits = "This worker gets benefits.";
}
else
{
benefits = "No benefits.";
}
return benefits;
}
和Employee构造函数,如果相关:
public Employee()
{
}
public Employee(String com, String first, String last)
{
setCompany(com);
setFirstName(first);
setLastName(last);
}
那么,我该如何传递位于ArrayList中的Employee对象?
答案 0 :(得分:0)
正如评论中指出的,我的问题是我忘了将Employee对象实际添加到ArrayList,这是一个非常简单的修复错误。感谢所有回答的人。