I've come to the conclusion that I have 3 questions.
- How do I use the variables and arrayList of the company class in the Employee class.
- How should I make the actionListeners so the operate correctly.
- Would the methods in the Company class work correctly? If not, how should I make them?
Check the link given in the paragraph above for the prompt to my question. Here is a link to the prompt my professor has provided https://www.dropbox.com/s/omeg19u6ns2pot2/Work%205.doc so you guys can see what I am trying to reach. I would usually get help from the tutors but they aren't available at this time in particular. Help is really, really highly appreciated. I have to finish this by tonight. I know this is a bit too much for this website but it is the only way you can get a good grasp on what is happening. I have already started a post on this problem and moved a bit further but no one is replyting to my questions on that post anymore so I am doing another one.
Here is the GUI aka the driver
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();
}
}
Here is the Data Manager
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 + "]";
}
}
Here is the edited Employee class.
import javax.swing.JOptionPane;
public class Employee {
public Employee(String fName, String lName, Position p2) {
Employee employee = new Employee (fName, lName, p2);
public boolean addEmployee(String fName2, String lName2, Position p3) {
switch (p) {
case SALES:
//if there's less than 1 salesman, add them to the list
if (numSales < 1) {
Employee employee2 = new Employee(fName2, lName2, p3);
employeeList.add(employee);
}
else {
JOptionPane.showMessageDialog(null, "There is already a Sales representative.");
}
break;
case DESIGN:
if (numDesign < 2) {
Employee employee2 = new Employee(fName2, lName2, p3);
employeeList.add(employee2);
}
else {
JOptionPane.showMessageDialog(null, "There are already two design employees.");
}
break;
case MANUFACTURING:
if (numManu < 4){
Employee employee2 = new Employee(fName2, lName2, p3);
employeeList.add(employee2);
}
else {
JOptionPane.showMessageDialog(null, "There are already four manufacturers.");
}
default:
break;
}
}
}
String firstName;
String lastName;
Position p;
}
Here is the enum class and one of the reasons i'm asking for help.
//in Position.java
public enum Position {
DESIGN("Design"),
MANUFACTURING("Manufacturing"),
SALES("Sales");
private final String positionName;
private Position(String positionName) {
this.positionName= positionName;
}
@Override
public String toString() {
return positionName;
}
}