[英]Accessing an enum type to create an object through inheritance
My assignment is to use take an old lab we created an wrap a GUI around it, simple enough.. I have no problem creating the GUI or inheriting variables to create different objects but one object requires an Enum type in its parameters and i seem to be having a tough time assigning an enum type to the object even though the compiler doesn't complain so maybe you guys can help me out. 我的任务是使用一个旧的实验室,我们创建了一个环绕它的GUI,这很简单。我创建GUI或继承变量来创建不同的对象没有问题,但是一个对象的参数需要Enum类型,我似乎即使编译器没有抱怨,也很难分配一个枚举类型给对象,所以也许你们可以帮帮我。
Manager class containing the enum class : 包含枚举类的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)];
}
};
there is a mutator method setDepartment that can be utilized but i am not sure how to instantiate it. 有一个变种方法setDepartment可以使用,但我不确定如何实例化它。
public void setDepartment(Department d) {
department = d;
}
heres my Add manager method in my GUI class : 这是我的GUI类中的我的Add manager方法:
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);
}
}
theres obviously something wrong with my method because when i input the department through the GUI it doesn't recognize the department i added, any thoughts on what my next step should be? 我的方法显然存在问题,因为当我通过GUI输入部门时,它无法识别我添加的部门,对下一步应该做什么有任何想法吗?
Enums always provide a valueOf(String)
static factory which could help you I think. 枚举始终提供一个
valueOf(String)
静态工厂,可以为您提供帮助。
ie 即
Manager.Department.valueOf("PAYROLL")
will return a Manager.Department.PAYROLL
Manager.Department.valueOf("PAYROLL")
将返回Manager.Department.PAYROLL
I seemed to have fixed the problem and like always its something simple but thats coding for you lol, 我似乎已经解决了这个问题,并且喜欢它总是很简单,但是多数民众赞成在为您编码,哈哈,
i forgot to instantiate a string variable to act as a holder for the GUI so when i was inputting the department the string was going nowhere so it wasn't recognizing the department. 我忘了实例化一个字符串变量来充当GUI的持有者,所以当我输入部门时,字符串无处可去,因此它无法识别部门。
the working code is now : 现在的工作代码是:
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);
}
}
thanks for any input that was provided 感谢您提供的任何输入
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.