PersonNode *x;
PersonNode *y;
[mystery: line = 3, n = 2] [mystery: line = 4, n = 4] [mystery: line = 4, n = 8] [mystery: line = 5, n = 11] [mystery: line = 4, n = 22] [mystery: line = 5, n = 25] [mystery: line = 4, n = 50] [mystery: line = 5, n = 53] [main: line = 1]
void updateUser(list<User *> &userList, const string &name, double amount) {
list<User *>::iterator lit;
User *newUser;
// iterate through the user list looking for the user. If we find the
// user then update the user's balance. Since the list is sorted we can
// break out of the loop as soon as the user's name is less than the name
// of a node in the list because we then know that the user cannot be in
// the list.
for (lit = userList.begin(); lit != userList.end(); lit++) {
if ((*lit)->name == name) {
(*lit)->balance += amount;
return;
}
else if (name < (*lit)->name)
break;
}
// if we get here we did not find the user in the list--notice that whether
// or not we reached the end of the list, lit points to the correct node.
// If we reached the end of the list then lit points to .end() and it is
// okay to insert before .end()
newUser = new User(name, amount);
userList.insert(lit, newUser);
}
void Hotel::InsertReservation(string guest, int room, int checkInDate,
int checkOutDate) {
map<int, string> *roomOccupancy;
map<int, map<int, string> *>::iterator reservationIter;
map<int, string>::iterator roomIter;
int date;
// doess the room exist?
reservationIter = reservations.find(room);
if (reservationIter == reservations.end())
throw (string) "no such room";
// get the room's occupancy map and then get the first date on or after
// the check in date when the room is occupied
roomOccupancy = reservationIter->second;
roomIter = roomOccupancy->lower_bound(checkInDate);
// if the room is occupied on or after the check in date and before the
// checkout date, then we cannot accommodate the reservation because someone
// else is in the room for a portion of the reservation
if (roomIter != roomOccupancy->end() && roomIter->first < checkOutDate) {
throw (string) "room occupied";
}
// if we get here then we know that the room is unoccupied during the
// requested dates and we should add the guest to the room's occupancy map
// for each of the requested days
for (date = checkInDate; date < checkOutDate; date++) {
(*reservations[room])[date] = guest;
// here's an alternative way to perform the assignment
// roomOccupancy->insert(make_pair(date, guest));
}
}