I have an assignment which can be found at http://www.cems.uwe.ac.uk/~lrlang/java_html/assignment2.html

i have coded all the required classes apart from Hotel and HotelGui classes

my problem is with the Hotel class

the assignment says
"The methods that you are required to implement are as follows:

* public Hotel ()
The constructor for this class. It initialises all the attributes, part of which involves creating an array of Booking objects containing at least 100 entries."

but i dont understand how to do this can someone please explain how, i dont want the code but an explaination of what is meant to be going on as i would like to work out the code for my self.

Thank you

Dani AI

Generated

A brief clarification and a small design sketch that complements what and already discussed.

The constructor should set up the Hotel's data structures and sensible defaults. In practice that means allocating a fixed-size container for bookings (an array of length 100 if the spec says "at least 100 entries") — that allocation creates 100 slots whose values are initially null. It is normal and preferable to create actual Booking instances only when a reservation is made; there is no requirement to manufacture 100 GuestData/Room pairs at startup.

Because Room is abstract, concrete room objects must be instances of SingleRoom or DoubleRoom. A typical pattern is to populate a separate rooms collection in the constructor (an array or a List) that represents the hotel inventory. When a booking is created later the Booking is constructed with a GuestData and a reference to one of those concrete room objects. That keeps responsibilities clear: constructor sets up containers and the room inventory; booking creation happens in the booking-handling methods.

Example workflow (illustrative, not copy of earlier snippets):

// inside Hotel()
bookings = new Booking[100];        // 100 slots, entries start null
rooms = new ArrayList<Room>();      // populate with concrete SingleRoom/DoubleRoom instances

// when making a reservation:
Room chosen = findAvailableRoom(...);
if (chosen != null) {
    Booking b = new Booking(guest, chosen, start, end);
    int slot = findFirstNull(bookings);
    if (slot >= 0) bookings[slot] = b;
}

Notes and cautions: always check for null booking slots before inserting; implement findAvailableRoom to check room-type and date conflicts; consider ArrayList<Booking> only if a variable-size collection is acceptable for the assignment. For Java details on arrays and abstract classes see the Java tutorials: Java arrays and abstract classes.

Recommended Answers

All 4 Replies

hi,
"public Hotel ()" is your default constructor, and in here must initialize objects. you need an array of Booking objects, or an ArrayList can be used. with ArrayList you can its size...
any other properties of Hotel Class also need to be initialize to its default value, for example if your hotel has a name, then (in your constructor)

name = "Default Name";

hope that answered your question,

thanks for the help, but i still dont understand how to initialize the booking array within the constructor as it needs a GuestData object and a Room object to initialize. Also the Room class is an abstract class with the two subclasses SingleRoom and DoubleRoom and so i can't initialize a Room object with the code:

Room newRoom = new Room (parameters);

so how do initialize a Room object if i dont know if it will be a single or double room ?

thank you

you don't need to know. what you need to know is SingleRoom and DoubleRoom are also a Room. so you can create an array of Room, then store SingleRoom and DoubleRoom in it, because they are Room. you simply typecast Room to Single(or Double)Room, when you are retriving from the array, an example

Room = new Room[100];

Room[5] = new SingleRoom();
Room[7] = new DoubleRoom();

((SingleRoom)Room[5]).someMethod();
((DoubleRoom)Room[7]).someMethod();

there is also another way with generics but i don't think i can you an example about it, because i'm new to them. :)

thanks very much thats a big help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.