Reputation: 4312
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
Upvotes: 0
Views: 490
Reputation: 4312
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 hostel
object in your MainGUI
class,
private Hostel hostel;
this will allow previously entered information to be referenced
Upvotes: 0
Reputation: 20323
Yes you are right that everytime action is performed a new Hostel is created and so is the list of rooms associated with hostel.
On click of button you would know which hostel you want to show (May be reading your application database or something), in case this is the first time your hostel will have empty room, else once you have read the hostel information you would also know about rooms which belong to the hostel, which can then be passed to your hostel object either through constructor or setter method.
Code snippet:
createHostel(String hostelName) {
//read from database
//No hostel with hostelname found create a new hostel else if hostel is found send the same (by this time hostel object would have room information also
}
Your action
public void actionPerformed(ActionEvent actionEvent) {
//MyFactory.getHostel(String hostelName)
//Once you have hostel object call showAvail on it, if its new you will get nothing else you will get all the rooms available
}
Hope this gives you some insight.
Upvotes: 0
Reputation: 56069
Unless you omitted code between the Hostel host = new Hostel();
and host.showAvail();
, you never add any rooms to the hostel so there are no available rooms (or any at all) to show. You need to either add rooms to host
after you create it and before you showAvail
, or create a Hostel
instance variable, fill it somewhere, and then call showAvail
on that.
Upvotes: 1
Reputation: 4093
Since you are creating new Hostel each time. I guess there will be to Room
s in the ArrayList.
You need to create the Hostel Object outside the actionPerformed. And in your case it should be created only one time. And on this created Hostel object you will be adding the Room
object.
If the question is where to do it.. Its left to you. Its upon your design.
For eg it can be.
You can create a class called ABC
inside which you can create the hostel object. Write a static method called getHostel(). Then call ABC.getHostel()
Upvotes: 0
Reputation: 3091
public void actionPerformed(ActionEvent e)
{
Hostel host = new Hostel();
host.showAvail();
}
In the previous code, the object is created and then destroyed once the method is done. The variable host is a local variable and consequently lives only during the execution of the method.
Depending on what you want to do, you should declare your host variable inside the main method or declare an array of hostel inside the main method again.
Upvotes: 0
Reputation: 361
Would be good to see how you initialize your rooms inside Hostel. And if you like to initialize Hostel once only, do it outside of Listener. In this case it must not be a field inside MainGUI.
Upvotes: 0