当存在多个类并帮助修复代码时,ArrayList会给Java带来麻烦

当存在多个类并帮助修复代码时,ArrayList会给Java带来麻烦,java,arraylist,enums,jpanel,jradiobutton,Java,Arraylist,Enums,Jpanel,Jradiobutton,我的结论是我有三个问题 如何在Employee类中使用company类的变量和arrayList 我应该如何使actionListeners正确运行 Company类中的方法正确吗?如果没有,我应该如何制作它们 检查上面段落中给出的链接,以获得对我的问题的提示。这里有一个链接到我的教授提供的提示,这样你们就可以看到我想要达到的目的。我通常会从导师那里得到帮助,但他们现在不在。非常非常感谢你的帮助。我必须在今晚之前完成这件事。我知道这对这个网站来说有点太多了,但这是你能很好地了解正在发生的事情的唯

我的结论是我有三个问题

  • 如何在Employee类中使用company类的变量和arrayList
  • 我应该如何使actionListeners正确运行
  • Company类中的方法正确吗?如果没有,我应该如何制作它们
  • 检查上面段落中给出的链接,以获得对我的问题的提示。这里有一个链接到我的教授提供的提示,这样你们就可以看到我想要达到的目的。我通常会从导师那里得到帮助,但他们现在不在。非常非常感谢你的帮助。我必须在今晚之前完成这件事。我知道这对这个网站来说有点太多了,但这是你能很好地了解正在发生的事情的唯一方法。我已经开始了一篇关于这个问题的帖子,并且更进一步了,但是没有人再回答我在那个帖子上的问题,所以我正在做另一篇

    这是GUI,也就是驱动程序

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    
    public class GUI extends JFrame {
    
       private JPanel employeePanel;
       private JPanel buttonPanel2;
       private JPanel positionPanel;
       private JPanel namePanel2;
       private JPanel buttonPanel1;
       private JPanel upperLine;
       private JPanel lowerLine;
       private JPanel companyAndPresidentPanel;
       private JPanel companyPanel;
       private JRadioButton designButton;
       private JRadioButton salesButton;
       private JRadioButton manuButton;
       private JTextField firstField;
       private JTextField lastField;
       private JLabel firstLabel;
       private JLabel lastLabel;
       private JLabel cNameLabel;
       private JLabel presidentLabel;
       private JLabel logo;
       private ButtonGroup bGroup;
       private static final long serialVersionUID = 1L;
    
       Company c;
       Employee e;
       private JButton addButton;
       private JButton clearButton;
       private JButton printButton;
       private JButton newButton;
       private JButton exitButton;
    
       String companyName;
    
       public GUI() {
    
          companyName = JOptionPane.showInputDialog(
                "What is the name of this company", companyName);
          setTitle("Company Employees");
          setSize(425, 450);
          setLayout(new BorderLayout());
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          main();
          subPanels();
          add(companyAndPresidentPanel, BorderLayout.NORTH);
          add(employeePanel, BorderLayout.CENTER);
          add(buttonPanel1, BorderLayout.SOUTH);
    
          setVisible(true);
    
          // pack();
    
       }
    
       public void subPanels() {
    
          positionPanel = new JPanel();
          buttonPanel1 = new JPanel();
          buttonPanel2 = new JPanel();
          employeePanel = new JPanel();
          namePanel2 = new JPanel();
          upperLine = new JPanel();
          lowerLine = new JPanel();
          employeePanel.setSize(new Dimension(100, 100));
          designButton = new JRadioButton("Design");
          salesButton = new JRadioButton("Sales");
          manuButton = new JRadioButton("Manufacturing");
    
          addButton = new JButton("Add Employee");
          clearButton = new JButton("Clear Button");
          printButton = new JButton("Print Company Employees");
          newButton = new JButton("New Company");
          exitButton = new JButton("Exit");
    
          firstField = new JTextField(10);
          lastField = new JTextField(10);
    
          firstLabel = new JLabel("First Name:");
          lastLabel = new JLabel("Last Name:");
    
          bGroup = new ButtonGroup();
          bGroup.add(designButton);
          bGroup.add(salesButton);
          bGroup.add(manuButton);
    
          positionPanel.setLayout(new FlowLayout());
          positionPanel.add(designButton);
          positionPanel.add(salesButton);
          positionPanel.add(manuButton);
          positionPanel.setBorder(BorderFactory.createTitledBorder("Position"));
    
          upperLine.add(printButton);
          lowerLine.add(newButton);
          lowerLine.add(exitButton);
          buttonPanel1.add(upperLine, BorderLayout.NORTH);
          buttonPanel1.add(lowerLine, BorderLayout.SOUTH);
    
          buttonPanel2.add(addButton);
          buttonPanel2.add(clearButton);
    
          namePanel2.setLayout(new GridLayout(2, 2));
          namePanel2.add(firstLabel);
          namePanel2.add(firstField);
          namePanel2.add(lastLabel);
          namePanel2.add(lastField);
    
          employeePanel.setBorder(BorderFactory.createTitledBorder("Add Employee"));
          employeePanel.add(namePanel2, BorderLayout.NORTH);
          employeePanel.add(positionPanel, BorderLayout.CENTER);
          employeePanel.add(buttonPanel2, BorderLayout.SOUTH);
          employeePanel.setBorder(BorderFactory.createTitledBorder("Add Employee"));
    
          printButton.addActionListener(new aListener());
          addButton.addActionListener(new aListener());
          clearButton.addActionListener(new aListener());
          newButton.addActionListener(new aListener());
          exitButton.addActionListener(new aListener());
    
       }
    
       /*
        * if data manager.add employee.equals("too many design") joption pane too
        * many
        */
       public void main() {
    
          companyAndPresidentPanel = new JPanel();
          companyPanel = new JPanel();
          presidentLabel = new JLabel("President:Amin Oskoui");
          cNameLabel = new JLabel("");
          logo = new JLabel("");
          cNameLabel.setText(companyName);
    
          ImageIcon myImage = new ImageIcon("src/company.png");
          logo.setIcon(myImage);
    
          companyPanel.add(logo);
          companyPanel.add(cNameLabel);
    
          companyAndPresidentPanel.setLayout(new GridLayout(2, 1));
          companyAndPresidentPanel.add(companyPanel);
          companyAndPresidentPanel.add(presidentLabel);
    
       }
    
       private class aListener implements ActionListener {
          String fName;
          String lName, position;
          int nEmployees, nCompanies, nDesign, nSales, nManu;
    
          public void actionPerformed(ActionEvent e) {
             // TODO Auto-generated method stub
    
             if (e.getSource() == addButton) {
                fName = firstField.getText();
                lName = lastField.getText();
                if (e.getSource() instanceof JRadioButton) {
                   if (manuButton.isSelected()) {
                      position = (manuButton.getText());
                   } else if (designButton.isSelected()) {
                      position = (designButton.getText());
                   } else if (salesButton.isSelected()) {
                      position = (salesButton.getText());
                   }
                   c = new Company(nEmployees, nCompanies, nDesign, nSales, nManu);
                   c.addEmployee(fName, lName, position);
                   c.printCompany();
    
                }
             } else if (e.getSource() == clearButton) {
                firstField.setText("");
                lastField.setText("");
                bGroup.clearSelection();
    
             } else if (e.getSource() == printButton) {
    
             } else if (e.getSource() == newButton) {
    
             } else if (e.getSource() == exitButton) {
                System.exit(0);
    
             }
    
          }
    
       }
    
       public static void main(String[] args) {
          new GUI();
       }
    }
    
    这是数据管理器

    import javax.swing.*;
    
    import java.util.*;
    public class Company {
    private static final long serialVersionUID = 1L;//ensuring that the class corresponds with a serialized object
    
    
    Employee a;
    
    private String companyName;//name of company
    private String employeeName;
    private String position;
    
    final int maxCompanies = 2, maxEmployees = 7, maxSales = 1, maxDesign = 2, maxManufacturing = 4;
    
    private static int numberOfCompanies;//the number of companies
    private int numEmployees;//the number of employees
    public int numDesign;//the number of employees in design
    private int numManufacturing;// the number of employees in manufacturing
    private int numSales;//the number of employees in sales
    
    private ArrayList<Employee> employeeList;
    
    
    public Company(String cn){
        numEmployees = 0;
        numSales = 0;
        numDesign = 0;
        numManufacturing = 0;
        employeeList = new ArrayList<Employee>();
    }
    public Company(int ec, int nc, int nd, int ns,int nm) {
        numEmployees = ec;
        numberOfCompanies = nc;
        numDesign = nd;
        numSales = ns;
        numManufacturing = nm;
    }
    public String addEmployee(String fName, String lName, Position p) {
           String errorMessage;
           errorMessage = "It is one of the errors";
    
        switch (p) {
            case SALES:
                //if there's less than 1 salesman, add them to the list
    
                if (numSales < maxSales && numEmployees< maxEmployees) {
                    Employee employee = new Employee(fName, lName, p);
                    employeeList.add(employee);
                    numSales++;
                    numEmployees++;
                }
                else {
                    JOptionPane.showMessageDialog(null, "There is already a Sales representative.");
    
                    }
    
    
            case DESIGN:
                if (numDesign < maxEmployees && numEmployees< maxEmployees) {
                    Employee employee = new Employee(fName, lName, p);
                    employeeList.add(employee);
                    numDesign++;
                    numEmployees++;
                }
                else {
                    JOptionPane.showMessageDialog(null, "There are already two design employees.");            
                    }
    
    
            case MANUFACTURING:
                if (numManufacturing < maxManufacturing && numEmployees< maxEmployees){
                    Employee employee = new Employee(fName, lName, p);
                    employeeList.add(employee);
                    numManufacturing++;
                    numEmployees++;
                }
                else {
                    JOptionPane.showMessageDialog(null, "There are already four manufacturers.");              
                    }
            default:
    
    
        }
        return errorMessage;
        }
    public static int getNumCompanies(){//return the number of companies 
        return numberOfCompanies;
    }
    public int getNumEmployees(){//get the number of employees
        return numEmployees;
    }
    public String printCompany(){//print the company with all of the positions
        String companyPrint = companyName + "\n";
    
        return companyName;
    }
    @Override
    public String toString() {//converts everything to a string
        return "Company [position="  + ", companyName=" + companyName
                + ", employees=" + employeeList + ", numEmployees=" + numEmployees
                + ", numDesign=" + numDesign + ", numManufacturing="
                + numManufacturing + ", numSales=" + numSales + "]";
    }
    
    }
    

    这是很多代码。关于ArrayList,我不太清楚你在问什么。通常,这是将数据放入ArrayList的方式

    List<String> aList = new ArrayList<String>();
    aList.add("Hello World");
    
    List aList=new ArrayList();
    aList.add(“Hello World”);
    
    如果您不熟悉泛型,并且不认识
    部分,可以将其关闭。
    ArrayList a列表仅在创建它的方法或类中具有作用域。因此,如果在方法中声明它,则只能在该方法中向其添加内容/从中获取内容。

    枚举用作一种伪类型。您不需要枚举类,因为1。这是你作业的一部分。它使任何阅读您的代码(包括您自己)的人都更清楚某些值的含义

    如果您有一些枚举日和一些变量day1,那么在编写
    If(day1.toString.equals(“星期一”)
    时很容易看到您在做什么,但是如果您使用幻数/预定义常量,它将看起来像
    If(day1==0)
    ,这对于没有编写代码的人来说毫无意义(将来当您再次查看此代码时,您可能也会这样做)

    除此之外,使用
    enum
    不允许垃圾值。如果不使用enum,可以将
    day1
    定义为10000,这将是有效的Java。如果一周只有7天,这是一个毫无意义的值。enum提供了一个抽象层,可以避免垃圾值,并为您提供对垃圾值的显式控制如何处理枚举

    位置枚举的实现 因此,在您的
    Employee
    类中,您声明了一名
    Employee

    Employee employee = new Employee("John", "Deer", DESIGN);
    
    由于您希望跟踪每个职位的员工数量,因此需要在
    公司
    中创建一个属性来存储给定类型的员工数量

    因此,在
    公司
    的属性声明中,您将创建3个整数:

    private int numOfDesigners;
    private int numOfManufacturers;
    private int numOfSalesmen;
    
    无论何时添加员工,都要确保没有太多员工,因此将
    employee
    类的创建封装在
    public boolean addEmployee(String fName,String lName,Position p)
    方法中,如果职位
    p
    太多,该方法将返回false

    public boolean addEmployee(String fName, String lName, Position p) {
        //since this method will be in the Company class, we can directly
        //refer to the number of a specific position type
        //I'm going to assume you know how a switch statement works.
        switch (p) {
            case SALES:
                //if there's less than 1 salesman, add them to the list
                if (numOfSalesmen < 1) {
                    Employee employee = new Employee(fName, lName, p);
                    //assuming you name your ArrayList<Employee> 'staffList'
                    staffList.add(employee);
                }
                else {
                    //give some error message or some functionality with GUI
                }
                break;
    
            case DESIGN:
                if (numOfDesigners < 2) {
                    ...
                }
                break;
                
            ...
    
            default:
                //some error handling
                break;
        }
    }
    
    此外,OOP最有价值的一个方面是,它允许您更改功能,而无需记住在何处使用此代码。如果出于某种原因,您的应用程序现在只打印出第一个名称,该怎么办?您必须找到此for循环的每个实例,并从m中的语句中删除
    +e.lastName
    iddle.如果您已经正确封装了
    listOfEmployees()
    函数,您只需在该函数中进行更改,就不必担心更改每个用法


    这就是如何让您的类以清晰和可预测的方式彼此交互。

    enum Position需要在哪里?您所说的需要在哪里是什么意思?如果您指的是公司中的实际职位,即销售、设计和制造。我不太了解enum positions,这就是我的问题的一部分。但我相信你可以从提示中看到一切。谢谢你在回答我的问题时考虑。我应该用更好的措辞,你将枚举包括在哪一类?我相信员工,因为这就是职位的用途。
    “由于这学期修了太多学分,我没有太多时间,还有很多其他工作要做。。。"
    --这与问题本身完全无关,应该删除。我知道这就是如何添加到arraylist的方法。这并不困难。我基本上需要有人来调试它并使其正常运行。事实上,其中有太多的因素,例如文本字段和从一个类到另一个类的转换,我遇到了麻烦r与方法。如果您仍然能够提供帮助,那就太好了。@user3482560:不,您应该是调试它并隔离问题的人。这个站点作为调试服务确实不太好。您的问题是关于hiw将数据放入ArrayList。StackOverflow是询问特定问题,如果您特别地谢谢你的回答。我的时间有点短,你能不能把我的代码展示出来,让大家看清楚一点。这太棒了。哈哈,我会整理一下,然后写一些代码,但是如果我写的代码不止一点的话,我最终会完成这个任务ole项目,因为它的实现非常简单。我编译了您提供的代码,其中大部分已经为您完成。人类,我敦促您不要给OP,@user3482560太多的代码。确实有一点来说明您的想法,但不是直接的代码回答。他似乎做得更多
    Employee employee = new Employee("John", "Deer", DESIGN);
    
    private int numOfDesigners;
    private int numOfManufacturers;
    private int numOfSalesmen;
    
    public boolean addEmployee(String fName, String lName, Position p) {
        //since this method will be in the Company class, we can directly
        //refer to the number of a specific position type
        //I'm going to assume you know how a switch statement works.
        switch (p) {
            case SALES:
                //if there's less than 1 salesman, add them to the list
                if (numOfSalesmen < 1) {
                    Employee employee = new Employee(fName, lName, p);
                    //assuming you name your ArrayList<Employee> 'staffList'
                    staffList.add(employee);
                }
                else {
                    //give some error message or some functionality with GUI
                }
                break;
    
            case DESIGN:
                if (numOfDesigners < 2) {
                    ...
                }
                break;
                
            ...
    
            default:
                //some error handling
                break;
        }
    }
    
    String currentList = "";
    for (Employee e : employeeList) {
        currentList += e.firstName + e.lastName;
    }