访问枚举类型以通过继承创建对象

时间:2015-04-06 16:56:44

标签: java user-interface inheritance enums

我的任务是使用一个旧的实验室,我们创建了一个围绕它的GUI包装,很简单。我创建GUI或继承变量来创建不同的对象没有问题但是一个对象在其参数中需要一个Enum类型即使编译器没有抱怨,我似乎也很难为对象分配枚举类型,所以也许你们可以帮助我。

包含枚举类的Manager类:

public enum Department {

    PAYROLL, PRODUCTION, ACCOUNTING, RESEARCH, MARKETING;

    public static Department getRandomDepartment() {
        Department[] d = Department.values();
        int size = Department.values().length;

        return d[r.nextInt(size)];
    }
};

有一个可以使用的mutator方法setDepartment,但我不确定如何实例化它。

public void setDepartment(Department d) {
    department = d;
}

继承我的GUI类中的添加管理器方法:

if(command.equals("Add Manager")){
        m_name = inputName.getText();
        m_num = inputNum.getText();
        m_year = Integer.parseInt(inputHire.getText());
        m_yearlyPay = Double.parseDouble(inputYearly.getText());


        if(inputDepartment.equals("Payroll") || inputDepartment.equals("payroll")){

             Manager temp = new Manager(m_name,m_num,m_year,m_yearlyPay,Manager.Department.PAYROLL);
             System.out.println(temp);
        }
        if(inputDepartment.equals("Production") || inputDepartment.equals("production")){
            Manager temp = new Manager(m_name,m_num,m_year,m_yearlyPay,Manager.Department.PRODUCTION);
            System.out.println(temp);
        }
        if(inputDepartment.equals("Accounting") || inputDepartment.equals("accounting")){
            Manager temp = new Manager(m_name,m_num,m_year,m_yearlyPay,Manager.Department.ACCOUNTING);
            System.out.println(temp);
        }
        if(inputDepartment.equals("Research") || inputDepartment.equals("research")){
             Manager temp = new Manager(m_name,m_num,m_year,m_yearlyPay,Manager.Department.RESEARCH);
             System.out.println(temp);
        }
        if(inputDepartment.equals("Marketing") || inputDepartment.equals("marketing")){
             Manager temp = new   Manager(m_name,m_num,m_year,m_yearlyPay,Manager.Department.MARKETING);
             System.out.println(temp);
        }



    }
显然,我的方法有问题,因为当我通过GUI输入部门时,它不会识别我添加的部门,对我的下一步应该是什么的任何想法?

2 个答案:

答案 0 :(得分:1)

Enums总是提供valueOf(String)静态工厂,可以帮助您。

Manager.Department.valueOf("PAYROLL")将返回Manager.Department.PAYROLL

答案 1 :(得分:0)

我似乎已经修复了这个问题,并且总是喜欢它的简单东西,但那就是为你编写代码,

我忘了实例化一个字符串变量来充当GUI的持有者,所以当我输入部门时,字符串无处可去,因此它无法识别部门。

现在的工作代码是:

if(command.equals("Add Manager")){
        m_name = inputName.getText();
        m_num = inputNum.getText();
        m_year = Integer.parseInt(inputHire.getText());
        m_yearlyPay = Double.parseDouble(inputYearly.getText());
        department = inputDepartment.getText(); 


        if(department.equalsIgnoreCase("Payroll")){

             Manager temp = new Manager(m_name,m_num,m_year,m_yearlyPay,Manager.Department.PAYROLL);
             System.out.println(temp);
        }
        if(department.equalsIgnoreCase("Production")){
            Manager temp = new Manager(m_name,m_num,m_year,m_yearlyPay,Manager.Department.PRODUCTION);
            System.out.println(temp);
        }
        if(department.equalsIgnoreCase("Accounting")){
            Manager temp = new Manager(m_name,m_num,m_year,m_yearlyPay,Manager.Department.ACCOUNTING);
            System.out.println(temp);
        }
        if(department.equalsIgnoreCase("Research")){
             Manager temp = new Manager(m_name,m_num,m_year,m_yearlyPay,Manager.Department.RESEARCH);
             System.out.println(temp);
        }
        if(department.equalsIgnoreCase("Marketing")){
             Manager temp = new Manager(m_name,m_num,m_year,m_yearlyPay,Manager.Department.MARKETING);
             System.out.println(temp);
        }



    }

感谢您提供的任何输入