Question
The main objective of this assignment is checking students ability to implement membership functions. After completing this assignment, students will be able to: implement member
The main objective of this assignment is checking students’ ability to implement membership functions. After completing this assignment, students will be able to:
implement member functions
use standalone functions and member functions
call member functions
implement constructors
Problem description: You have been provided with an incomplete program whose purpose is to simulate the arrival, seating process, and departure of customers in a restaurant. Based on data collected from the past months, it has been determined that the probability of a party arriving depends on the time of day.
Time Interval | Probability of Customer Arrival |
7:00 AM – 11:59 AM (Breakfast) | 11 % |
12: 00 PM – 1:59 PM (Lunch) | 40 % |
2:00 PM – 9:00 PM (Dinner) | 20 % |
When customers arrive, the party size ranges from one to eight with equal probability. As the restaurant includes limited seating with a total of 30 tables, the customers enter a queue upon arrival. The hostess then checks if there are any free tables that can accommodate the party, since 20 tables can fit four customers, and only ten can fit parties of larger size. The hostess will only attempt to seat customers at the head of the queue (the customers who have been waiting the longest). Regardless of party size, customers will eat for time ranging from 40 to 70 minutes with equal probability.
Note: If the head of the queue is a party larger than four, and the next in queue is a party smaller or equal to four, the smaller party will not be seated before the larger one, even if there is a smaller table available.
The program simulates the hostess’ actions while maintaining the total number of customers currently at the restaurant, and the total number of customers that have already departed. Time advances by the minute, and within each minute a party is likely to arrive. In addition, at each minute, the program displays the time along with the queue (displaying the position of each party in the queue and its size), the tables, the capacity of each table (F if it can seat more than four people, H otherwise), and how many customers are currently sitting at each table. The information displayed at each minute is illustrated in the figure below.
Use of global variables will incur a deduction of 10 points from your total points.
Part A:
Task 1:
Implement the constructor for the Hostess struct. It must initialize all member variables, including any arrays. There is another function that performs initialization. You must change the program so that the object’s initialization is done automatically, i.e. no explicit call is needed for an initialization function.
Task 2:
Implement the member function AdvanceTime, ShiftLeft, and Seat.
AdvanceTime simply advances the current time (given as hours and minutes) by one minute. You need not include a call to this function anywhere else in the code, as it is already being called wherever necessary. You must convert it to a member function though as it is currently a standalone function in main.cpp.
ShiftLeft is called when a party from the head of the queue is being seated. This means that the party must be removed from the head of the queue, and all other parties must advance in the queue by one spot.
Seat is an important function of the program that must encompass the process of the hostess seating a table. The table that is to be used must be updated accordingly, as does the queue, and attributes that refer to the number of customers seated at the restaurant, and the number of customers that have eaten and departed.
Hint: This function can call other functions that have been defined in the program or that you may have implemented.
Codes:
Main.cpp
#include <iostream> #include "Hostess.h" rnusing namespace std; rnint GetRandomBetween(int floor, int ceiling){ int temp = floor + (rand() % static_cast<int>(ceiling - floor + 1)); return temp; }rn /* Whenever a party arrives, this function is called to determine the size of the PartySize()*/ int PartySize(){ return GetRandomBetween(1,8); }rn /* Returns the number of customers that have arrived at the current time The arrival depends on the time and utilizes a random number generator */ int GetNumArrivals(int hour){ bool arrived = false; if((hour < 5 && GetRandomBetween(0,100) < 12) || (hour >=5 && hour < 7 && GetRandomBetween(0,100) < 40) || (hour >=7 && GetRandomBetween(0,100) < 20)) arrived = true; if(arrived == true) return PartySize(); else return 0; }rn /* Advances the current time by one minute */ void AdvanceTime(int &hour, int &minute){ //FILL THIS FUNCTION }rn /* Wrapper function that calls the method CalcTimeLeft for each table, which decrements the variable that corresponds to the time for which the customers will remain at the table. If that time goes to 0, the function automatically frees the table and updates the variables keeping tracking of the total number of customers that have departed and the number that is currently at the restaurant */ void CalculateTimeLeft(Table tables[], int numTables, int &totalCustomers, int ¤tCustomers){ for(int i=0; i<numTables; i++){ int customersLeaving = tables[i].CalcTimeLeft(); totalCustomers += customersLeaving; currentCustomers -= customersLeaving; } }rn void SetRandGeneratorSeed(){ srand (time(NULL)); }rn void ClearScreen(){ system("CLS"); }rn/* This is the main function of the program, everything is called from here and the function operates for 14 hours starting from 7:00 AM */ void Operate(Hostess &h){ int hour = 0; int minute = 0; int hours = 14; int arrivals = -1; int table = -1; for(int i = 0; i<hours*60; i++){ ClearScreen(); AdvanceTime(hour, minute); arrivals = GetNumArrivals(hour); CalculateTimeLeft(h.tables, h.GetNumTables(), h.totalServed, h.currentCustomers); h.PlaceArrivalInQueue(arrivals); h.Print(hour, minute, arrivals); table = h.CheckAvail(); h.Seat(table, GetRandomBetween(40,70)); } }rn int main(){ SetRandGeneratorSeed(); Hostess h; h.Init(); Operate(h); return 0; }rn ============================ table.cpp
#include "Table.h" rnTable::Table(){ vacant = true; numSeated = 0; }rn void Table::Free(){ vacant = true; numSeated = 0; timeToSpend = 0; }rn /* Advance Time left for customer at the table If the customers vacate the table, return their number so that we can increment the number of total customers served if customers have not left the table yet, return zero */ int Table::CalcTimeLeft(){ if(vacant == false && timeToSpend > 0){ timeToSpend--; return 0; } else if(vacant == false && timeToSpend == 0){ int numPeople = numSeated; Free(); return numPeople; } return 0; }
===================
Table h Code:
#ifndef TABLE_H #define TABLE_H rn#include <stdlib.h> #include <time.h> rnstruct Table{ Table(); void Free(); void SetCustomers(int num){numSeated = num;} void SetVacant(bool param){vacant = param;} void SetCapacity(int c){capacity = c;} void SetEatingTime(int t){timeToSpend = t;} int CalcTimeLeft(); bool IsVacant(){return vacant;} bool vacant; int capacity; int numSeated; int timeToSpend; rn}; rn#endif // TABLE_H
==========
Table.cpp
#include "Table.h" rnTable::Table(){ vacant = true; numSeated = 0; }rn void Table::Free(){ vacant = true; numSeated = 0; timeToSpend = 0; }rn /* Advance Time left for customer at the table If the customers vacate the table, return their number so that we can increment the number of total customers served if customers have not left the table yet, return zero */ int Table::CalcTimeLeft(){ if(vacant == false && timeToSpend > 0){ timeToSpend--; return 0; } else if(vacant == false && timeToSpend == 0){ int numPeople = numSeated; Free(); return numPeople; } return 0; }
============
Hostess.Cpp
#include "Hostess.h" rn/* takes the number of customers that have just arrived and places them in the queue */ void Hostess::PlaceArrivalInQueue(int arrivals){ if(queue_size<15 && arrivals!=0){ queue[queue_size] = arrivals; queue_size++; } }rn /* This function is called when a party from the head of the queue is being seated. This means that it must be removed from the head of the queue, and all other parties must move one position to the left */ void Hostess::ShiftLeft(){ /* FILL IN THIS FUNCTION }rn /* This function performs the seating process of seating customers from the head of the queue at table "int table" which will be occupied by that party for "int time" minutes If "int table" is -1, it indicates that no table is available and the function must then return -1 If there is a table available, the queue and the table attributes must be updated accordingly and the function must return 0. Look for any other functions from the table struct or hostess struct that can be utilized here */ rnint Hostess::Seat(int table, int time){ /* FILL THIS FUNCTION COMMENT OUT THE RETURN STATEMENTS THEY HAVE BEEN INCLUDED SO THE CODE WILL COMPILE BUT DO NOT SERVE A SPECIFIC PURPOSE THE WAY THEY ARE WRITTEN */ return 0; }rn /* Checks if any tables are free, and whether all customers can fit in it If such a table exists, return its ID(index), otherwise return -1 */ int Hostess::CheckAvail(){ if(queue_size == 0) return -1; int numPeople = queue[0]; for(int i=0; i<30; i++){ if(tables[i].IsVacant() == true && tables[i].capacity >= numPeople) return i; } return -1; }rn /* INITIALIZES THE HOSTESS OBJECT ACCORDINGLY */ void Hostess::Init(){ numTables = 30; queue_size = 0; totalServed = 0; currentCustomers = 0; for(int i=0; i<30; i++){ if(i<20) tables[i].SetCapacity(4); else tables[i].SetCapacity(8); tables[i].SetVacant(true); } for(int i=0; i<15; i++) queue[i] = 0; }rn void Hostess::Print(int hour, int minute, int arrivals){ printf("Table Queuen"); for(int i=0; i<15; i++){ printf("%i|", queue[i]); } printf("n"); printf("n============================================================n"); printf("Table INFOn"); for(int i=0; i<30; i++){ if(i<20) printf("H|"); else printf("F|"); } printf("n============================================================n"); for(int i=0; i<30; i++){ printf("%i|", tables[i].numSeated); } printf("nn"); printf(" Time:%i:%in", hour+7, minute); printf("# Current customers:%in", currentCustomers); printf("# Total customers served:%in", totalServed); }rn==================
Hostess.H
#ifndef HOSTESS_H #define HOSTESS_H #include "Table.h" #include <iostream> rnstruct Hostess{ void Init(); void PlaceArrivalInQueue(int arrivals); void ShiftLeft(); void Print(int hour, int minute, int arrivals); int GetNumTables(){return numTables;} int Seat(int table, int time); int CheckAvail(); int totalServed; int currentCustomers; int queue_size; int numTables; Table tables[30]; int queue[15]; }; #endif // HOSTESS_H
==========================
Table Queue Table INF0 H|H|H|H|H|H|H|H|H|H|H|H|H|H|H|H|H|H|H|H|F|F|F|F|F|F|F|F|F|F| e|e|e[e]e]e]e]ele|e|0|e|0|0|ele|e|e|e|0|0|0|0|0|0|0|0|0|e|e| Time:7:1 customers :0 # Total customers served:0 # Current
Step by Step Solution
3.43 Rating (150 Votes )
There are 3 Steps involved in it
Step: 1
As reqeusted by you the entire program has not be rewriiten instead the missi...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started