We want to realize a program to be used in a hotel to manage booking of hotel rooms. For each room, the following information is stored: room number (a positive integer), room category (a string), name of guest who occupies the room (a string), and number of days for which the guest occupies the room (a non-negative integer). If the name of the guest is missing, this means that the room is not occupied.
Part 1.
Write a class Room
that implements the following methods:
Room(int num, String cat)
: constructor that, given room
number and category, constructs a room that is initially not occupied;
int getNumber()
: that returns the room number;
String getCategory()
: that returns the category of the room;
String getGuest()
: that returns the name of the guest
who occupies the room, if the room is occupied, and null
otherwise;
int getDays()
: that returns the number of days for which
the room will be occupied;
void occupyRoom(String g, int d)
: that occupies the room
with the guest with name g
for d
days (with
d
a positive integer), if the room is not already occupied,
and does nothing otherwise;
void freeRoom()
: that sets the status of the room to
being not occupied (i.e., there is no guest who occupies the room); the
method should also set to 0 the number of days for which the room will be
occupied.
String toString()
: that returns a string with all the
information about the room.
Use the example program
TestRoom.Java
to test the class you have developed.
Part 2.
Write a class UseRoom
, client of Room
, that contains
the following public static method:
static int shortestOccupation(Room r1, Room r2)
: that,
given two rooms r1
and r2
, returns the number
of the room among r1
and r2
that will be
occupied for the least number of days . If both r1
and
r2
will be occupied for the same number of days, the method
should return the number of room r1
. If both
r1
and r2
are not occupied, the method should
return -1
.
Use the example program
TestUseRoom.java
to test the method you have developed.
Solutions:
Room.java
,
UseRoom.java