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

}

}

[5552 byte] By [kingarou] at [2007-9-27 17:29:43]
# 1

As one possible suggestion:

Create another class object which holds an array of offices and methods for accessing the offices. It would be helpful to give each office an int id, from which each office could be identified and returned from a Offices object method.

Your main program would then hold the Offices object and pass it off to which ever class is in need of it.

Hope that helps.

tzman at 2007-7-6 12:35:46 > top of Java-index,Archived Forums,Java Programming...
# 2

You could have

1. Offices : Containing methods to add / delete / search for an Office.

2. An Office class to contain information about a particular office.

Remember the main application should just be a driver programme. You Dialog technially should contain only enough information to use the interface(public methods) provided by these classes. As previous solution suggested you could use an ID for each Office so a to get a reference .

nishuteddy at 2007-7-6 12:35:46 > top of Java-index,Archived Forums,Java Programming...
# 3

Cheers for the suggestions. I have just come across a problem involving the following section of my code: -

else {

String text = employeeText.getText();

Integer employeeNumInput = Integer.valueOf(text);

employeeLabel.setText("The number of employees here are : " + text);

employeeText.selectAll();

}

What I am trying to do is assign the numbers entered to the int variable employeeNumInput. The way I have written it does not work, and I am stumped on this.

Cheers so far

Joseph :)

kingarou at 2007-7-6 12:35:46 > top of Java-index,Archived Forums,Java Programming...
# 4

> Integer employeeNumInput = Integer.valueOf(text);

> What I am trying to do is assign the numbers entered to the int variable employeeNumInput.

Integer and int are not the same. One's an object, and the other's a primitive.

When you say it doesn't work, do you mean an exception is thrown or that you can't access it somewhere else? If the latter, the problem is that the scope of the variable ends with the } after employeeText.selectAll(). You need to move it into a larger scope to access it elsewhere.*******************************************************************************

Answer provided by Friends of the Water Cooler. Please inform forum admin via the

'Discuss the JDC Web Site' forum that off-topic threads should be supported.

YATArchivist at 2007-7-6 12:35:46 > top of Java-index,Archived Forums,Java Programming...
# 5

the code should have looked like

employeeNumInput = Integer.valueOf(text);

and NOT

Integer employeeNumInput = Integer.valueOf(text);

You have already declared employeeNumInput to be an int in your class.

If you want to use this one there is no need to declare it again.Also

the type is int (primitive) and not Integer (Object!).

nishuteddy at 2007-7-6 12:35:46 > top of Java-index,Archived Forums,Java Programming...
# 6

Arg!!

I have read through the API sections I can find to do with casting from the JTextField entry to the int employeeNumInput but none of it works.

I have used: -

employeeNumInput.valueOf(text); // like what is in the main Sun Tutorial, but the only example I found creates a Integer object instead of a primitive type.

employeeNumInput.parseInt(text);

employeeNumInput.intValue(text); //which according to the 1.4API returns the value of the Integer as an int.

I was told Java was rubbish due to its inability to typecase, I still believe that this is not the case, but now appreciate why other programmers say this when I cannot find how to do it myself. :(

Joseph

kingarou at 2007-7-6 12:35:46 > top of Java-index,Archived Forums,Java Programming...
# 7
Oops, yep and turning the String text into an Integer and then trying to turn the Integer into an int. e.gInteger employeeNumInput2 = Integer.valueOf(text);employeeNumInput.intValue(employeeNumInput2);etc, sorry for not mentiong that on previous
kingarou at 2007-7-6 12:35:47 > top of Java-index,Archived Forums,Java Programming...
# 8
Hi,Well I have fixed this problem, Integer employeeNumInput2 = new Integer(text); employeeNumInput = employeeNumInput2.parseInt(text);and that worked :-)Now to finally get on with the process of turning this program into a multi-object
kingarou at 2007-7-6 12:35:47 > top of Java-index,Archived Forums,Java Programming...
# 9
Hi,Well I have fixed this problem, Integer employeeNumInput2 = new Integer(text); employeeNumInput = employeeNumInput2.parseInt(text);and that worked :-)Now to finally get on with the process of turning this program into a multi-object
kingarou at 2007-7-6 12:35:47 > top of Java-index,Archived Forums,Java Programming...
# 10
Hi,Well I have fixed this problem, Integer employeeNumInput2 = new Integer(text); employeeNumInput = employeeNumInput2.parseInt(text);and that worked :-)Now to finally get on with the process of turning this program into a multi-object
kingarou at 2007-7-6 12:35:47 > top of Java-index,Archived Forums,Java Programming...