For my class, I am making a program that manages a Hotel. My ‘Test’ method creates random rooms and random customers. Once it has does this, it then takes the random customers it created, and checks them into the random rooms it created.
My goal is for the Test to check-in only HALF of the rooms with customers. And leave the other half of the rooms empty. And also, I can only have 1 customer per room.
The method compiles, but their are some flaws:
- The ‘Test’ fills ALL the rooms with customers (instead of only half of them).
- The ‘Test’ will often assign a single room to multiple customers( For example, Room 301 will now belong to 3 different customers instead of just 1)
Can anyone help me figure out how to fill only half of the rooms that are created.
And also help me figure out how to not let a single room be applied to more than 1 customer? I am pretty sure that the last FOR loop in the method should be the only thing that I need to adjust.
string Hotel::test(int numRooms, int numCustomers)
{
string result;
for(int i=0;i<numRooms;i++) // **********CREATES RANDOM ROOMS
{
Room iRoom(randString(8),
randInt(0,1000),
randInt(0,1000),
randInt(0,1000),
randInt(0,1000));
listofrooms.add(iRoom);
}
for(int i=0;i<numCustomers;i++) // ***********CREATES RANDOM CUSTOMERS
{
Customer cus(randString(8),randNumberString(10),randNumberString(16));
listofcustomers.add(cus);
}
for(int i=0;i<numCustomers+numRooms;i++) // **FILLS RANDOM ROOMS WITH RANDOM CUSTOMERS
{ // **I KNOW THIS IS THE ONLY LOOP I NEED TO ADJUST
checkIn(listofrooms.getRandID(),listofcustomers.getRandID());
}
return result;
}
Part 1
You need half the rooms to be filled. Since only 1 guest can be in any room, you need to fill the rooms
numRoomstimes. So, you just need to adjust your loop to:Part 2
There are many ways to ensure that. The naive approach would be to retry in a loop
Of course, you wouldn’t want the same customer in two rooms, so you need to do the same thing with customers also.
Another method could be to take a random room, and if it was full iterate to next rooms (wrapping around to first room if overflow) until you find an empty room.
Another method could be to
random_shufflea copy of the rooms and take the first half.Like I said, whatever you do with the rooms, you need to do with the customers also.
recommendation: Perhaps the best distribution is achieved with the last method (the one with
random_shuffle. Note that the naive approach has a non-deterministic (and possibly bad) performance.