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;
}
}
An enum is used as a sort of pseudo-type. You do need the enumerated class because 1. it's part of your assignment and 2. it makes it just clearer to anyone reading your code (including yourself) what certain values mean.
If you have some enum Day and some variable day1, it's easy to see what you're doing when you write
if (day1.toString.equals("MONDAY"))
but if you use magic numbers/pre-defined constants, it will look likeif (day1 == 0)
which is meaningless to anyone who didn't write the code (and possibly you in the future, when you're looking at this code again).In addition to that, using an
enum
disallows for garbage values. If you weren't using enums, you could defineday1
to be 10,000 and it would be valid Java. This is a nonsensical value if you only have 7 days of the week. Enums provide a layer of abstraction that avoid garbage values and gives you explicit control over how an Enum is handled.Implementation of the Position enum
Thus in your
Employee
class you declare anEmployee
with:Since you're going to want to keep track of how many of each position you have, you need to create an attribute in
Company
that stores the number of employees of a given type.So in
Company
in your attribute declaration you will create 3 integers:Whenever you add an employee, you want to make sure there aren't too many already, so you'll encapsulate the creation of the
Employee
class inside anpublic boolean addEmployee(String fName, String lName, Position p)
method that returns false if there are too many of the Positionp
already.Secondly, you need to declare some
ArrayList<Employee>
in yourCompany
class as an attribute. Write a function in theCompany
class that somehow checks whether or not there are too many employees of a particular type and returns a value accordingly.In Object-Oriented Programming you provide functions for handling attributes of a class so that other classes don't have to worry about how a function is implemented (this is called encapsulation).
That is, if you want to print all the Employee names from your GUI class, you define an object Company, add Employees to it's
ArrayList<Employee>
attribute and print out the returned value of a functionpublic String listOfEmployees()
that returns "Bob M., Stacy J., John A" or however you want to implement it.That way when you need the list of employees, you don't have to iterate over the list of
Employees
in theCompany
class and concatenate them in some arbitrary way to a single string. The functionlistOfEmployees()
does that for you and assuming you have aCompany
calledcompany1
you can simply write:String currentList = company.listOfEmployees();
rather than cluttering your code up with
Also, one of the most valuable aspects of OOP is that it allows you to change functionality without having to remember where you used this code. What if for some reason your application should only print out the first names now? You have to find every instance of this for loop and remove the
+ e.lastName
from the statement in the middle. If you have properly encapsulated thelistOfEmployees()
function, you simply make the change in this function and never have to worry about changing every usage.This is how you can get your classes to interact with each other in a clear and predictable way.
That's a lot of code. I'm not very clear on what you are asking about ArrayLists. In general this is how you put data in an ArrayList
If you are not familiar with generics and don't recognize the
<String>
part, you can just leave it off. The ArrayList a list will only have scope in the method or class you create it in. So if you declare it inside a method you can only add stuff to it / get stuff out of it in that method.