Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

How do I populate an ArrayList of complex objects which is an attribute of another complex object?

I have an object Hotel with a "has-many" relationship with Object Room.
I thought the best way to implement this, was to make Room a private ArrayList and include it in the Hotel class.
(If there is a more efficient data structure that I should be using, please let me know).

However, I am trying to populate my ArrayList of Room, but I'm not quite sure how the syntax would work.

package hotel;

import java.io.*;
import java.util.ArrayList;

@SuppressWarnings("unused")
public class Hotel {
    
	private ArrayList<Room> rooms;
	String location;
	
	public Hotel() {
		String location = "Paris";
	}
	
	public Hotel(String loc) {
		location = loc;
	}
	
		public static void main(String[] args) {
		Hotel paris = new Hotel();
		Hotel sBeach = new Hotel("South Beach Miami");
	}
}

Open in new window


package hotel;

public class Room {
	int rmNo;
	String type;
	int sqFootage;
	boolean isSmoking;
	boolean isClean;
	boolean isOccupied;
	
	public Room(){
		type = "Standard";
		sqFootage = 500;
		isSmoking = false;
		isClean = true;
		isOccupied = false;
	}
	
	public Room(int number){
		rmNo = number;
	}
	
	public Room(String typ, int sqFt, boolean smoke){
		type = typ;
		sqFt = sqFootage;
		isSmoking = smoke;
	}
}

Open in new window

0
--TripWire--
Asked:
--TripWire--
  • 3
  • 3
3 Solutions
 
CEHJCommented:
Yes, that's fine. You just need to create instances of Room to add to the collection
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
(If there is a more efficient data structure that I should be using, please let me know).
You haven't shown your true intentions here but I guess at some point that you will want to look up rooms, and more than likely this lookup would mainly be via the "room number"? If so, then it is probably worth using a data structure now that would allow that in the future. So rather than an ArrayList I would suggest a Map (HashMap in particular), like so...
private Map<Integer, Room> rooms = new HashMap<Integer, Room>();

Open in new window

And then when you need to populate this structure with the Room objects, something like this would be needed...
rooms.put(101, new Room(101));
rooms.put(102, new Room(102));
// etc...

// And then somewhere else in the code you can access the Room objects like so...
Room aRoom = rooms.get(102);

Open in new window

Note: you still need to do a bit of work on the constructors of your Room class as there is no way to create a Room object with all the fields properly initialized.
0
 
CEHJCommented:
Why not just use the index into the List?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Why not just use the index into the List?
Many hotels that I know of don't have consecutively numbered rooms starting from 0. Like in the example that I gave, many number the rooms as 101, 102 ... 120,   201, 202 ... 220,   301, etc. Yes, you could probably try and make it work but (unless I am missing something) it would be prone to errors and somewhat wasteful of memory.
0
 
CEHJCommented:
I doubt this is a model of a real hotel so i think the gaps should be minimal. It will use less memory than a Map of course
0
 
--TripWire--Author Commented:
If I were to change my structure to HashMap, would I still be able to iterate through the integer key values?

You haven't shown your true intentions here but I guess at some point that you will want to look up rooms, and more than likely this lookup would mainly be via the "room number"?

I would like to search on many things.  Such as, show me all of the non-smoking, standard rooms that are available today.

The main idea is that I would like to store all of the Objects/variables into a comma-delimited text file.  Once the program is closed, all of the Objects would be destroyed, and the next day the program would read the same text file and restore all of the values.

Ideally, I would like the program to create a new csv file every day, which shows the current status of all of the Rooms in the Hotel.
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
If I were to change my structure to HashMap, would I still be able to iterate through the integer key values?
Yes, you can do that like this...
for (Integer roomNumber : rooms.keySet()) {
    System.out.println("Room number: " + roomNumber);
    // You could then do this... (but see below)
    System.out.println("Room: " + rooms.get(roomNumber));
}

// Although if you want to do the above, ie. iterate through the Map to get BOTH the keys and their values, you would do this as it is more efficient...
for (Map.Entry<Integer, Room> entry : rooms.entrySet()) {
    System.out.println("Room number: " + entry.getKey());
    System.out.println("Room: " + entry.getValue());
}

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now