(30 points): Write a void function named updateUser
that either creates a new user object and inserts the object in sorted
alphabetical order based on the user's name into a list, or, if the
user already exists, adds the indicated amount to their balance.
insertUser takes three
arguements:
- userList: an STL list of pointers to User objects.
- name: the name of the user
- amount: the amount to add to the user's balance. If the user is
a new user, then this amount will be the initial balance for the user.
You are given the following
class declaration for User:
class User {
public:
string name;
double balance;
User(string n, double amount): name(n), balance(amount) {}
};
As an example, suppose that successive calls were made with the following
users and amounts:
charley 20
nancy 10
anne 25.5
anne 15
zack 75
nancy 25.15
Then when the final call returned, your list would contain pointers to the
following User objects and they would be in this exact order:
{anne, 40.5}, {charley, 20}, {nancy, 35.15}, {zack, 75}
(30 points): Write a C++
method named InsertReservation
to add a new reservation to a hotel's reservation system if
the room is not occupied for those dates. Each room will have a map
showing the dates it is occupied and the guest in the room on each date.
A hotel will have a map with a pair for each room number and the room's
occupancy map.
The function will take
four parameters: the guest name, the room number, the check in date, and
the check out date. For simplicity dates will
be simple integers starting at one and the guest will not be considered
to be staying in the room on the check out date. For example, suppose
you get the following calls to InsertReservation:
InsertReservation("rabbit", 2, 10, 12)
InsertReservation("hound", 2, 1, 6)
InsertReservation("hen", 2, 7, 11)
InsertReservation("chicken", 2, 1, 20)
The reservations will be processed as follows:
- rabbit is inserted into the map associated with room 2 with the two
pairs (10, "rabbit") and (11, "rabbit") to denote the two days rabbit
is staying in room 2.
- hound is inserted into the map associated with room 2 with the
pairs (1, "hound"), (2, "hound"), ..., (5, "hound") to denote the
five days hound will be staying in room 2.
- hare's reservation is rejected because hare's occupancy dates
overlap with a pre-existing reservation for rabbit.
- chicken's reservation is rejected because chicken's occupancy
date overlaps with a pre-existing reservation for hound.
When these 4 calls are complete, the map associated with room 2 will have
the following key/value pairs:
(1, "hound"), (2, "hound"), (3, "hound"), (4, "hound"),
(5, "hound"), (10, "rabbit"), (11, "rabbit")
You are given the following class definition for a Hotel:
class Hotel {
public:
void CreateRoom(int r); // adds the room to reservations with an empty map
void InsertReservation(string guest, int room,
int checkInDate, int checkOutDate);
protected:
map<int, map<int, string> * > reservations;
};
The key for the reservations map is a room number and the value is a pointer to a
map<int, string> that
holds day/guest name pairs where day is a day the room is occupied and
guestname is the name of the guest occupying the room on that day.
There will be one entry in the map for each day that
the guest is staying in the room, as illustrated in the above example.
Your function must perform the following tasks:
- If the room does not exist, then throw the string
"no such room".
- If the room is occupied during the requested days of
the reservation, then throw the string "room occupied". You
can use the map function lower_bound(key) to return an iterator
that points to the first date equal to or greater than the check in
date.
For example, hound's requested check-in date is 1 and lower_bound
will return an iterator that points to day 10. As another example,
hare's requested check-in date is 7 and lower_bound will again return
an iterator that points to day 10.
If there is no entry equal to or greater than the
date, then lower_bound returns an iterator to .end(). For example,
when you try to insert rabbit in the above example, lower_bound
will return .end() because there is no entry in the room 2 map
that is equal to or greater than 6. You should be able to figure
out how to use this returned date (e.g., 10) to
determine whether or not the room is occupied during the requested
dates.
- If the reservation is valid, then insert the guest into the room's map for the
specified days (remember to not include the check out day).