我想知道是否有人能解释一下为什么我的代码中的案例2和3似乎什么都不做,也许还能提供一些建议。我会提供任何我认为相关的信息,如果你要求更多的细节,我会增加更多。
在发布代码之前,让我提供一些细节:我的程序被设计成一个非常简单的员工数据库。它使用switch语句在命令行上提供用户选项。开关的选项如下:
案例1将雇员对象添加到类型为Employee的ArrayList中。员工类负责跟踪他们工作的员工姓名、工资、工时和公司。
案例1和4似乎运作正常。
然而,案例2和3似乎没有做任何事情。下面是开关的全部内容,它位于包含主要方法的驱动程序类中:
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;
}和雇员建设者,如果这是相关的:
public Employee()
{
}
public Employee(String com, String first, String last)
{
setCompany(com);
setFirstName(first);
setLastName(last);
}那么,如何传递位于ArrayList中的Employee对象呢?
发布于 2016-03-19 21:18:24
正如注释中指出的那样,我的问题是我忘记将Employee对象实际添加到ArrayList中,这是一个非常简单的错误。感谢所有回答我的人。
https://stackoverflow.com/questions/36107001
复制相似问题