I am fairly new to programming and I am writing a GUI for an array. The program should read in an apartment number and then read in how many people occupy that apartment. Then it should store this information and update it as new information is inputted. It should return values for total occupancy for the building, avg occupancy per room, number of rooms above avg and number of rooms below avg.
My problem is that after the first input. When I put in the second input and update the GUI the values get all screwed up and I end up with way more people in the building then I entered. If you could take a look at my code and give me any tips I would appreciate it.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AppartmentGUI
{
static JButton store;
static JButton update;
static JTextField aptNoField;
static JTextField occNoField;
static JLabel totalOccLabel;
static JLabel avgOccLabel;
static JLabel aboveAvgOccLabel;
static JLabel belowAvgOccLabel;
static JLabel title1;
static JLabel title2;
static JFrame outFrame;
static Container outPane;
static ActionHandler storeAction;
static ActionHandler updateAction;
static int occNo = 0;
static int aptNo = 0;
static double totalOcc = 0;
static double avgOcc = 0;
static double aboveAvgOcc = 0;
static double belowAvgOcc = 0;
static final int BUILDING_SIZE = 10;
static int[] aptArray = new int[BUILDING_SIZE];
static class ActionHandler implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
String command;
command = event.getActionCommand();
for(aptNo = 0; aptNo < aptArray.length; aptNo++)
totalOcc = totalOcc + aptArray[aptNo];
if(command.equals("Store"))
{
aptNo = Integer.parseInt(aptNoField.getText());
occNo = Integer.parseInt(occNoField.getText());
if(aptNo >= 1 && aptNo < BUILDING_SIZE)
{
aptArray[aptNo - 1] = Integer.parseInt(occNoField.getText());
totalOcc = totalOcc + aptArray[aptNo];
}
else
{
aptNoField.setText("# is out of range. Must be between 1-10");
}
}
else if(command.equals("Update"))
{
avgOcc = totalOcc/BUILDING_SIZE;
aboveAvgOcc = 0;
belowAvgOcc = 0;
for(aptNo = 0; aptNo < BUILDING_SIZE; aptNo++)
if (aptArray[aptNo] > avgOcc)
{
aboveAvgOcc++;
}
else if(aptArray[aptNo] < avgOcc)
{
belowAvgOcc++;
}
totalOccLabel.setText("Total number of Occupants: " + totalOcc);
avgOccLabel.setText("Average number of occupants per apartment: " + avgOcc);
aboveAvgOccLabel.setText("Number of apartments above average occupancy: " + aboveAvgOcc);
belowAvgOccLabel.setText("Number of apartments below average occupancy: " + belowAvgOcc);
}
}
}
public static void main(String[] args)
{
storeAction = new ActionHandler();
updateAction = new ActionHandler();
aptNoField = new JTextField();
occNoField = new JTextField();
totalOccLabel = new JLabel("Total number of occupants: " + totalOcc);
avgOccLabel = new JLabel("Average number of occupants per apartment: " + avgOcc);
aboveAvgOccLabel = new JLabel("Number of apartments above average occupancy: " + aboveAvgOcc);
belowAvgOccLabel = new JLabel("Number of apartments below average occupancy: " + belowAvgOcc);
title1 = new JLabel("Enter the apartment number below:");
title2 = new JLabel("Enter the number of occupents in the room below:");
store = new JButton("Store");
update = new JButton("Update");
store.addActionListener(storeAction);
update.addActionListener(updateAction);
outFrame = new JFrame();
outFrame.setSize(750, 400);
outFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
outPane = outFrame.getContentPane();
outPane.setLayout(new GridLayout(5, 2));
outPane.add(totalOccLabel);
outPane.add(avgOccLabel);
outPane.add(aboveAvgOccLabel);
outPane.add(belowAvgOccLabel);
outPane.add(title1);
outPane.add(title2);
outPane.add(aptNoField);
outPane.add(occNoField);
outPane.add(store);
outPane.add(update);
outFrame.setVisible(true);
}
}

New Topic/Question
Reply



MultiQuote



|