This is one of the projects I started last year with BlueJ, but ran into problems and just didnt finish it. Now I've come back to it, and really need help.
This is my GUI class:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
*
* @author TANVEER AHMED
*/
public class HouseAdmin
{
private JFrame frame = new JFrame();
private JMenuBar menuBar = new JMenuBar();
private JMenu fileMenu = new JMenu("Menu");
private JMenuItem listAllAppliances;
private JMenuItem turnOnSingleAppliance;
private JMenuItem switchOnAllLights;
private BorderEventListener listener;//this class the inner class to handle the events
private JTextArea textArea;
private String printHouse;
private House myHome;
/**
* Constructor for objects of class HouseAdmin
*/
public HouseAdmin()
{
menuBar = new JMenuBar();
listAllAppliances = new JMenuItem("List All appliances...");
turnOnSingleAppliance = new JMenuItem("Turn on a single appliance...");
switchOnAllLights = new JMenuItem("Switch On All Lights...");
fileMenu.add(listAllAppliances);
fileMenu.add(turnOnSingleAppliance);
fileMenu.add(switchOnAllLights);
menuBar.add(fileMenu);
textArea = new JTextArea(30,70);
frame.add(textArea, BorderLayout.CENTER);
frame.setJMenuBar(menuBar);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible (true);
frame.pack();
listener = new BorderEventListener();
listAllAppliances.addActionListener(listener);
turnOnSingleAppliance.addActionListener(listener);
switchOnAllLights.addActionListener(listener);
setupHouse();
}
private void setupHouse(){
//Sets up a house
House myHome = new House("Hatfield");
Light bulb1 = new Light(1,"Bed Light",50,"H");
Light bulb2 = new Light(2,"Kitchen Light",5,"L");
Light bulb3 = new Light(3,"Garden Light",30,"E");
Light bulb4 = new Light(4,"Bathroom Light",50,"H");
Light bulb5 = new Light(5,"Shower Light",5,"L");
Light bulb6 = new Light(6,"Solar Light",30,"E");
Light bulb7 = new Light(7,"Passage Light",5,"L");
Light bulb8 = new Light(8,"Storage Light",50,"H");
Light bulb9 = new Light(9,"Garage Light",30,"H");
Light bulb10 = new Light(10,"Living room Light",30,"E");
myHome.addAppliance(bulb1);
myHome.addAppliance(bulb2);
myHome.addAppliance(bulb3);
myHome.addAppliance(bulb4);
myHome.addAppliance(bulb5);
myHome.addAppliance(bulb6);
myHome.addAppliance(bulb7);
myHome.addAppliance(bulb8);
myHome.addAppliance(bulb9);
myHome.addAppliance(bulb10);
printHouse = myHome.toString();
//textArea.setText(myHome.toString());
}
class BorderEventListener implements ActionListener{
public void actionPerformed (ActionEvent e){
if(e.getSource() == listAllAppliances){
textArea.setText(printHouse);
}
if (e.getSource () == turnOnSingleAppliance){
String code = JOptionPane.showInputDialog("Please Enter Application Code");
int code2 = Integer.parseInt(code);
myHome.turnOnASingleAppliance(code2);
textArea.append("Appliance " + code2 + " Has been turned on!");
textArea.setText(printHouse);
}
if (e.getSource () == switchOnAllLights){
myHome.turnAllLightsOn();
textArea.setText(printHouse);
textArea.append("All lights have been turned on!");
}
}
}
}
You can see I call a method called "turnOnSingleAppliance" on line 105. This now crashes and throws the terminal window at me.
This is the code for the method:
import java.util.*;
/**
*
* @author TANVEER AHMED
*/
public class House {
private String address;
private int powerConsumption;
private ArrayList <ElectricalAppliance> HouseAppliances;
public House(String address) {
// initialise instance variables
this.address = address;
HouseAppliances = new ArrayList <ElectricalAppliance>();
}
public ElectricalAppliance turnOnASingleAppliance(int code){
for (int i = 0; i < HouseAppliances.size(); i++){
ElectricalAppliance app = HouseAppliances.get(i);
if (app.getCode() == code ){
app.lightSwitchOn();
}
}
return null;
}
}
perhaps you guys need to see more source? if so I can upload the whole bluej project.
My gut is telling me the loop is wrong. What do you guys think?