1 Replies - 963 Views - Last Post: 17 February 2012 - 10:11 AM Rate Topic: -----

#1 lytwynk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 22-November 11

GUI array for an apartment building. Can't get total output correc

Posted 17 February 2012 - 10:06 AM


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];
				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];
					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)
					else if(aptArray[aptNo] < avgOcc)
				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");
	outFrame = new JFrame();
	outFrame.setSize(750, 400);
	outPane = outFrame.getContentPane();
	outPane.setLayout(new GridLayout(5, 2)); 

Is This A Good Question/Topic? 0
  • +

Replies To: GUI array for an apartment building. Can't get total output correc

#2 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: GUI array for an apartment building. Can't get total output correc

Posted 17 February 2012 - 10:11 AM

I would consider creating an Apartment class. Which will hold the appartment number and the number of occupants and methods to retrieve that info. Then store it in an arraylist and when you need to do the calculations call the methods to get the numbers. They will enter the info for the apartment, click an "Add" button and it will create the apartment object and add it into the ArrayList and clear the entry fields to add another one in.

Also, take a look at this thread, Moving Away From Parallel Arrays. I know you aren't using 2 arrays but you are using an array and a variable to try and store it all collectively.

This post has been edited by Fuzzyness: 17 February 2012 - 10:13 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1