Part (a)
// if there are any empty rooms (rooms with no reservation),
// then create a reservation for an empty room for the
// specified guest and return the new Reservation;
// otherwise, add the guest to the end of waitList
// and return null
public Reservation requestRoom(String guestName)
{
for (int i = 0; i < rooms.length; i++)
{
if (rooms[i] == null)
{
Reservation res = new Reservation(guestName, i);
rooms[i] = res;
return res; 1
}
}
waitList.add(guestName);
return null;
} 1
Notes:
- Don't forget to quit once an available room is found.
-
Also possible but slightly longer:
Reservation res = null;
for (int i = 0; i < rooms.length && res == null; i++)
{
if (rooms[i] == null)
{
res = new Reservation(guestName, i);
rooms[i] = res;
}
}
if (res == null)
waitList.add(guestName);
return res;
Part (b)
// release the room associated with parameter res, effectively
// cancelling the reservation;
// if any names are stored in waitList, remove the first name
// and create a Reservation for this person in the room
// reserved by res; return that new Reservation;
// if waitList is empty, mark the room specified by res as empty and
// return null
// precondition: res is a valid Reservation for some room
// in this hotel
public Reservation cancelAndReassign(Reservation res)
{
Reservation newRes = null;
int i = res.getRoomNumber();
if (!waitList.isEmpty())
{
newRes = new Reservation((String)waitList.get(0), i);
waitList.remove(0); 1
}
rooms[i] = newRes;
return newRes;
}
Notes:
- Or:
newRes = new Reservation((String)waitList.remove(0), i);
|