Java - 开关:情况 2 和 3 不起作用

Java - switch: case 2 and 3 do not function

我想知道是否有人可以阐明为什么我的代码中的案例 2 和案例 3 似乎什么也没做,并且也许可以提供一些建议。我会提供我认为相关的任何信息,如果您需要更多详细信息,我会添加更多信息。

在发布我的代码之前让我提供一些细节:我的程序被设计成一个非常简单的员工数据库。它使用 switch 语句在命令行上为用户提供选项。开关的选项是:

  1. 将员工添加到数据库
  2. 列出数据库中的所有员工,并查看工资和工作时间
  3. 列出所有员工并显示公司福利(如果有)
  4. 终止程序

案例 1 将 Employee 对象添加到 Employee 类型的 ArrayList。员工 class 负责跟踪员工姓名、工资、工时和他们工作的公司。

案例 1 和案例 4 似乎运行正常。

然而,情况2和3似乎没有做任何事情。这是开关的全部,它位于包含主要方法的驱动程序 class 中:

        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 对象?

正如评论中所指出的,我的问题是我忘记了将 Employee 对象实际添加到 ArrayList,这是一个很容易修复的错误。感谢所有回答的人。