简体   繁体   中英

have a Jbutton print content from another class

My addEmployee method is supposed to check that there is only 1 salesperson, 2 designers, and 4 manufacturing persons at a time and will check that there are no more than 7 employees of the company, and method will return a String indicating what the error is (ie “There is already a sales person in this company”) or a null if the employee was added, but my if statements are not working and for some reason it doesn't let me use a message dialog by saying "canot return a void result" but forces me to use an inputdialog.

My print company method seems to have a problem but i don't know what, as it is only returning null instead of printing out the arraylist of names,and position.

My question is how do i fix this so that whatever I input in my gui panel shows the print in the joptionpane as of now the joption pane i get is empty.

here is the code:

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);
}

and this the gui class where i am trying to print the arraylist from the printcompany method:

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);
                }







    }


}

Your instructions are not to show JOptionPanes in the Company class, but rather to have the addEmployee(...) method return a String. Understand that this class's reason for existing is not to communicate with the user, that's for other classes to do (namely your GUI), but rather it is supposed to communicate with another class. I would:

  • First and foremost, get all JOptionPanes out of the Company class. They don't belong there.
  • Instead have the method just return Strings. Your GUI will use those Strings to interact with the user.
  • Check the employee position counts first before adding a new employee. If the max is reached, return your String.
  • Otherwise if all is good, add your new Employee and increment your count.
  • Now your GUI code should capture any Strings returned by the addEmployee(...) method. I would put it into a String variable, say errorMessage . ie, String errorMessage = check.addEmployee(...); (your code won't have the ... in it of course).
  • If the String returned is null , ie, if (errorMessage == null) then all is good, but if not, then this class is where you notify the user, using the errorMessage String, that something is amiss.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM