I am building an application with a Room class which is abstract and a Standard class which inherits from Room. I have then created a Hostel class. Within the Hostel class is ArrayList<Room> rooms to which rooms can be added. I have created a method in the Hostel class which shows all available rooms but when I try and instantiate this in another class (MainGUI) nothing is shown. As far as I can see this is because I am creating a new hostel each time I click the button but would like to know how to pass the data across instead of creating a new hostel each time. Below are the relevant snippets of code.
Hostel Class
public Hostel()
{
rooms = new ArrayList<Room>();
}
public void showAvail()
{
for (Room room : rooms)
{
if (room.available == true)
{
theString = room.getRoomData() + "\n";
//System.out.println("Available Rooms" + "\n" + theString);
JOptionPane.showMessageDialog(null,theString);
}
}
}
public void addRoom(Room theRoom)
{
rooms.add(theRoom);
}
MainGUI Class
roomsFreeB.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Hostel host = new Hostel();
host.showAvail();
}
});
Any help would be appreciated
Your problem is exactly what you thought, you are making a new ArrayList each time you click the button so you will never see the data. You should begin by creating a
hostelobject in yourMainGUIclass,private Hostel hostel;this will allow previously entered information to be referenced