我的addEmployee方法应该检查一次只有1名销售人员,2名设计师和4名制造人员,并检查公司的员工人数不超过7人,方法将返回一个字符串,表明什么错误是(即“这家公司已经有销售人员”)或者如果添加了员工,则为null,但是我的if语句不起作用,并且由于某种原因它不允许我使用消息对话框说“ canot返回一个void结果“但强迫我使用inputdialog。
我的打印公司方法似乎有问题,但我不知道是什么,因为它只返回null而不是打印名称和位置的arraylist。
我的问题是我如何解决这个问题,以便我在gui面板中输入的内容显示joptionpane中的打印截至目前我得到的joption窗格是空的。
这是代码:
public String addEmployee(String fName, String lName, String pos) {
String message = null;
// if (pos.equals("DESIGN")&&pos.equals("SALES")&&pos.equals("MANUFACTURING")&& numDesign==0 &&numSales==0&&numManufacturing==0)
if (pos.equals("DESIGN")&&pos.equals("SALES")&&pos.equals("MANUFACTURING")&& numDesign==0 &&numSales==0&&numManufacturing==0)
{
return JOptionPane.showInputDialog(null,"woow ",JOptionPane.ERROR_MESSAGE);
}
if (pos.equals("DESIGN")&& numDesign==1&&numSales!=2&&numManufacturing!=4)
{
p2=Position.DESIGN;
addEmp = new Employees(fName, lName, pos);
list.add(addEmp);
numDesign++;
// message="There are already"+ (numDesign++) +" design persons\nEmployee not added";
}//else return message;
else if (pos.equals("SALES")&&numDesign<1&&numSales<2&&numManufacturing<4)
{
//String error;
p2=Position.SALES;
addEmp = new Employees(fName, lName,pos);
list.add(addEmp);
numSales++;
}//else return JOptionPane.showInputDialog(null," There is already a sales person\nEmployee not added");
else if (pos.equals("MANUFACTURING")&& numDesign<1&&numSales<2&&numManufacturing<4)
{
p2=Position.MANUFACTURING;
addEmp = new Employees(fName, lName, pos);
list.add(addEmp);
numManufacturing++;
//p2.MANUFACTURING++;
}//else return JOptionPane.showInputDialog(null," There are already four manufacturing persons \nEmployee not added ","Empolyees", JOptionPane.ERROR_MESSAGE);
if (numSales<1)
{
return JOptionPane.showInputDialog(null," There is already a sales person\nEmployee not added");
}
if (numDesign<2)
{
return JOptionPane.showInputDialog(null, "There are already"+ (numDesign++) +" design persons\nEmployee not added");
}
if (numberOfCompanies<3)
{
return JOptionPane.showInputDialog(null,"There are already two companies Company not added","Empolyees", JOptionPane.ERROR_MESSAGE);
}
if (numManufacturing<4)
{
return JOptionPane.showInputDialog(null," There are already four manufacturing persons \nEmployee not added ","Empolyees", JOptionPane.ERROR_MESSAGE);
}
if (numEmployees<7)
{
return JOptionPane.showInputDialog(null,"There are already 7 employees\nEmployee not added","Empolyees", JOptionPane.ERROR_MESSAGE);//check.toString();//look
// System.out.println(check.printCompany());
}
return null;
// TODO Auto-generated method stub
}
/*public Company(String str) {
// TODO Auto-generated constructor stub
//
}*/
public String printCompany( ) {
// TODO Auto-generated method stub
String str = null;
for (int index=0; index<list.size();index++)
{
str+=list.get(index).getFName()+" "+list.get(index).getLName()+" "+"Position:"+list.get(index).getPosition();
System.out.println(str);
}
return str;
}
public String toString()
{
int index=0;
String str =list.get(index).getFName()+" "+list.get(index).getLName()+" "+"Position:"+list.get(index).getPosition();
System.out.println(str);;
return JOptionPane.showInputDialog(null,str);
}
这是gui类,我试图从printcompany方法打印arraylist:
private class ButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String lnameInput;
String fNameInput;
lnameInput= lNameText.getText();
fNameInput=fNameText.getText();
if(e.getSource() == addEmpButton){
if(design.isSelected())
{
check.addEmployee(fNameInput, lnameInput, "Design");
}
if(sales.isSelected())
{
check.addEmployee(fNameInput, lnameInput, "Sales");
}
if(manufacturing.isSelected())
{
check.addEmployee(fNameInput, lnameInput, "Manufacturing");
}
}
if(e.getSource() == clearButton)
{
lNameText.setText("");
fNameText.setText("");
}
if (e.getSource() == printEmpsButton) {
JOptionPane.showMessageDialog(null,check.printCompany());//look
System.out.println(check.printCompany());
}
if (e.getSource() == exitButton) {
System.exit(0);
}
if(e.getSource() == newCompButton)
{
// check = null;
check = new Company(companyN);
message=JOptionPane.showInputDialog(null, "What is the name of this Company?","Company Name", JOptionPane.PLAIN_MESSAGE);
}
}
}
答案 0 :(得分:0)
您的说明不是在Company类中显示JOptionPanes,而是让addEmployee(...)
方法返回String。要明白这个类的存在理由不是与用户沟通,而是要与其他类进行通信(即你的GUI),而是应该与另一个类进行通信。我会:
addEmployee(...)
方法返回的任何字符串。我会把它放到一个String变量中,比如errorMessage
。即,String errorMessage = check.addEmployee(...);
(你的代码当然不会......在其中)。null
,即if (errorMessage == null)
那么一切都很好,但如果没有,那么这个类就是你使用errorMessage字符串通知用户的地方,这是错误的。