Basic Oops problem
Hello
I have 3 classes, all small, that currently work in a basic way.
What I am confused about is how to turn this program into an array of offices. Should I put the details in the Office Class, OfficeDialog Class or in the main program? Additionally once it is in an array of objects how should I go about saving all the objects?
public class Office{
//all the Office variables
private int employeeNum;
private String officeName;
public void setEmployeeNum(int employeeNumInput ){
employeeNum = employeeNumInput;
}
public int getEmployeeNum(){
return employeeNum;
}
public int getPopLast(){
return popLast;
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.AbstractButton;
import javax.swing.*;
public class OfficeDialog extends JDialog implements ActionListener {
private int employeeNumInput;
JLabel employeeLabel, officeLabel;
JTextField employeeText;
JButton tradeBut;
String officeName = "Preston";
protected static final String employeeTextField = "employeeTextField";
public OfficeDialog(CompanyProgram parent, boolean modal) {
super(parent, modal);
// Labels
employeeLabel = new JLabel("The number of employees here are : " + employeeNumInput);
officeLabel = new JLabel(officeName);
// Buttons
tradeBut = new JButton("Trade");
// JTextFields
employeeText = new JTextField(10);
//Listen for actions on button Trade and 4 JTextFields.
tradeBut.addActionListener(this);
employeeText.addActionListener(this);
employeeText.setActionCommand(employeeTextField);
//tooltips for buttons
tradeBut.setToolTipText("Not activated yet.");
// Panels
JPanel panelHolder = new JPanel();
panelHolder.add(officeLabel);
panelHolder.add(employeeLabel);
panelHolder.add(employeeText);
panelHolder.add(tradeBut);
getContentPane().add(panelHolder);
setModal(true);
}
public void actionPerformed(java.awt.event.ActionEvent actionEvent) {
String stuff = actionEvent.getActionCommand();
if (stuff == "Trade"){
JOptionPane.showMessageDialog(tradeBut, "This is not implemented yet.");
}
else {
String text = employeeText.getText();
Integer employeeNumInput = Integer.valueOf(text);
employeeLabel.setText("The number of employees here are : " + text);
employeeText.selectAll();
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.AbstractButton;
import javax.swing.*;
public class CompanyProgram extends JFrame
implements ActionListener {
public JButton b1, b2, b3, b4, b5;
public JPanel panel, panel_top, panel_bottom;
public JFrame frame;
public CompanyProgram() {
frame = new JFrame("Company Program");
panel_bottom = new JPanel();
panel_top = new JPanel();
panel = new JPanel();
// Exit when the window is closed.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1 = new JButton("Edit Office");
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEFT);
b1.setMnemonic(KeyEvent.VK_D);
//b1.setActionCommand("disable");
b2 = new JButton("Edit Personal");
b2.setVerticalTextPosition(AbstractButton.BOTTOM);
b2.setHorizontalTextPosition(AbstractButton.CENTER);
b2.setMnemonic(KeyEvent.VK_E);
b3 = new JButton("New Personal");
//Use the default text position of CENTER, RIGHT.
b3.setMnemonic(KeyEvent.VK_N);
b3.setActionCommand("enable");
b4 = new JButton("New Office");
b4.setVerticalTextPosition(AbstractButton.CENTER);
b4.setHorizontalTextPosition(AbstractButton.LEFT);
b4.setMnemonic(KeyEvent.VK_S);
b4.setActionCommand("disable");
b5 = new JButton("Special Events");
b5.setVerticalTextPosition(AbstractButton.BOTTOM);
b5.setHorizontalTextPosition(AbstractButton.CENTER);
b5.setMnemonic(KeyEvent.VK_R);
//Listen for actions on buttons 1 to 5.
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
//tooltips for buttons 1 to 5.
b1.setToolTipText("Click to eidt or process an exsisting Office");
b2.setToolTipText("Click to edit or process an existing employee.");
//set layout of all panels and add them to frame.
panel_top.setLayout(new FlowLayout());
panel_bottom.setLayout(new FlowLayout());
panel_top.add(b1);
panel_top.add(b2);
panel_bottom.add(b3);
panel_bottom.add(b4);
panel_bottom.add(b5);
panel.setLayout(new GridLayout(2,0));
panel.add(panel_top);
panel.add(panel_bottom);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String buttonPressed = e.getActionCommand();
if (buttonPressed.equals("Edit Office"))
{
OfficeDialog s = new OfficeDialog(this, true);
s.pack();
s.show();
}
else {
JOptionPane.showMessageDialog(b4, "To be added soon.");
}
}
public static void main(String[] args) {
CompanyProgram company_program = new CompanyProgram();
}
}

