首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java -开关:案例2和3不起作用

Java -开关:案例2和3不起作用
EN

Stack Overflow用户
提问于 2016-03-19 20:52:00
回答 1查看 472关注 0票数 0

我想知道是否有人能解释一下为什么我的代码中的案例2和3似乎什么都不做,也许还能提供一些建议。我会提供任何我认为相关的信息,如果你要求更多的细节,我会增加更多。

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

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

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

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

然而,案例2和3似乎没有做任何事情。下面是开关的全部内容,它位于包含主要方法的驱动程序类中:

代码语言:javascript
复制
        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()方法:

代码语言:javascript
复制
    public String toString()
{
    return firstName + " " + lastName + " from " + company +
            ". The worker's pay this week was $" + pay + ".";
}

和determineBenefits()方法:

代码语言:javascript
复制
    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;
}

和雇员建设者,如果这是相关的:

代码语言:javascript
复制
public Employee()
{

}

public Employee(String com, String first, String last)
{
    setCompany(com);
    setFirstName(first);
    setLastName(last);
}

那么,如何传递位于ArrayList中的Employee对象呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-19 21:18:24

正如注释中指出的那样,我的问题是我忘记将Employee对象实际添加到ArrayList中,这是一个非常简单的错误。感谢所有回答我的人。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36107001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档