Write a simulation program to simulate lines at a grocery store for a typical da
ID: 3605198 • Letter: W
Question
Write a simulation program to simulate lines at a grocery store for a typical day's business. You will assume there are three cashier lines at the grocery store. Customers will enter randomly to check out (this is a time stamp in the input file) and will enter the shortest line. If the lines are equal, then the first line in the queue will be selected. Each transaction takes a random amount of time to complete (this also is a number from the input file). The grocery store operates on a ten hour business day and only 6 customers per queue will be able to wait for service at any time. If a customer arrives and there are 6 in all the queues, the customer will be turned away. To perform the simulation, a number, a time stamp, and a service time (randomly generated between 1 and 20 minutes) is given for each customer. This information will be in an input file. The first item in the input file will be the number of customers in the file. To keep the simulation simple, this time stamp will be a count of the number of minutes elapsed from the start of the business day. The time stamps will be in order in the file. So the time could be any values from 0 to 570 (no new business will be allowed 30 minutes before closing, so the waiting queue can be cleared out, hopefully, before the end of the business day.) There will be a time counter which will start at 0 when the simulation starts. No two customers can arrive at the same minute. Customers should be taken out of the queue when the cashier starts serving them. This simulation will inform the owner how many customers were serviced, how many were turned away, the average time any customer in the queue spent waiting, and the total time customers spent waiting. You will generate your own input file to test your program that will have a number a time stamp from 0 to 570 in order, and a service time. Those will be your customers. The grader will use a standard file to test all the projects To simulate the time, you will use a loop that increments the "timer" by 1 each time through the loopExplanation / Answer
MAIN.CPP
#include <iostream>
#include <queue>
#include <fstream>
#include <stdlib.h>
using namespace std;
#include "cashier.hpp"
#include "simulation.hpp"
int main (void)
{
//setting 3 lines
Cashier cashier1;
Cashier cashier2;
Cashier cashier3;
Customer cust;
queue<Customer> custInput;
Simulation sim;
ifstream in_file;
string line;
string substring;
//opening file
in_file.open("output.dat");
if( in_file.fail() )
{
cout<<"Could not open file. ";
return 0;
}
else
{
while( !in_file.eof() )
{
//gets input until new line
getline( in_file, line );
if( line.length() > 0 )
{
substring = line.substr(0, 2);
cust.set_number( atoi(substring.c_str()) );
substring = line.substr(4, 4);
cust.set_at( atoi(substring.c_str()) );
substring = line.substr(6, 3);
cust.set_serv_time( atoi(substring.c_str()) );
custInput.push( cust );
}
}
//closing file
in_file.close();
}
while( sim.get_cnt() < 600 )
{
sim.inc_cnt();
cashier1.inc_timer();
cashier2.inc_timer();
cashier3.inc_timer();
if( sim.get_count() < 570 )
{
cust = custInput.front();
while( cust.get_at() == sim.get_count() )
{
/
if(
cashier1.get_status() == 'y' && cashier2.get_status() == 'n' ||cashier1.get_in_line() <= cashier2.get_in_line()) &&
cashier3.get_status() == 'y' ||
cashier1.get_in_line() <= cashier3.get_in_line())
)
{
/
cashier1.add_customer( cust.get_serv_time() );
custInput.pop(); //removing customer
cust = custInput.front(); //setting customer to 1st line to be serviced
}
else if(
cashier2.get_status() == 'y' &&
(cashier1.get_status() == 'n' ||
cashier2.get_in_line() <= cashier1.get_in_line()) &&
(cashier3.get_status() == 'n' ||
cashier2.get_in_line() <= cashier3.get_in_line()) &&
)
{
cashier2.add_customer( cust.get_serv_time() );
custInput.pop();
cust = custInput.front();
}
else if(
cashier3.get_status() == 'a' &&
(cashier2.get_status() == 'i' ||
cashier3.get_in_line() <= cashier2.get_in_line()) &&
(cashier1.get_status() == 'i' ||
cashier3.get_in_line() <= cashier1.get_in_line()) &&
(cashier4.get_status() == 'i' ||
cashier3.get_in_line() <= cashier4.get_in_line()) &&
(cashier5.get_status() == 'i' ||
cashier3.get_in_line() <= cashier5.get_in_line())
)
{
cashier3.add_customer( cust.get_serv_time() );
custInput.pop();
cust = custInput.front();
}
else
{
sim.inc_num_turned_away(); //turning customer away
}
}
}
if( cashier1.get_time() == cashier1.get_curr_cust_time_limit() )
{
sim.add_twt( cashier1.get_curr_cust_time_limit() );
cashier1.rmv_cust();
sim.inc_num_serviced();
}
else
if( cashier2.get_time() == cashier2.get_curr_cust_time_limit() )
{
sim.add_twt( cashier2.get_curr_cust_time_limit() );
cashier2.rmv_cust();
sim.inc_num_serviced();
}
else
if( cashier3.get_time() == cashier3.get_curr_cust_time_limit() )
{
sim.add_twt( cashier3.get_curr_cust_time_limit() );
cashier3.rmv_cust();
sim.inc_num_serviced();
}
}
cout<<"Total number of customers serviced: "<<sim.get_num_serviced();
cout<<endl;
cout<<"Total number of customers turned away: "
<<sim.get_num_turned_away();
cout<<endl<<"Average waiting time: "<<sim.get_avg_wait_time()<<endl;
return 0;
}
cashier.hpp
#include "node.hpp"
class Cash
{
int timer;
char status;
queue<int> limit;
int line;
public:
Cash()
{
timer = 0;
status = 'y';
line = 0;
}
char get_status()
{
return status;
}
void increment()
{
timer++;
}
int get_time()
{
return timer;
}
int in_line()
{
return line;
}
void add_customer( int servtime )
{
//sets timer to 0 for first input, since i reset it on pop its
//needed for first entry.
if( time_limit.empty() )
{
timer = 0;
}
//puts the serv_time on the queue of time_limits
time_limit.push( servtime );
//add customer to line
line++;
if( line == 6 )
{
status = 'n';
}
}
int get_curr_cust_time_limit()
{
return limit.front();
}
void rmv_cust()
{
limit.pop();
timer = 0;
line--;
status = 'y';
}
};
customer.hpp/
class Customer
{
/
int no;
int at;
int service;
public:
Customer()
{
no = 0;
at = 0;
service = 0;
}
void set_number(int x)
{
no = x;
}
void set_at(int x)
{
at = x;
}
int get_at()
{
return at;
}
void set_serv_time(int x)
{
service = x;
}
int get_serv_time()
{
return service;
}
};
simulation.hpp
class Simulation
{
int count; // ranges in values from 0 to 600
int service; // number of customers serviced so far
long twt;
int turned_away; // count of customers turned away
public:
Simulation()
{
count = 0;
service = 0;
twt = 0;
turned_away = 0;
}
void inc_cnt()
{
count++;
}
int get_count()
{
return count;
}
void inc_num_turned_away()
{
turned_away++;
}
int get_num_turned_away()
{
return turned_away;
}
void inc_num_serviced()
{
service++;
}
int get_num_serviced()
{
return service;
}
int get_avg_wait_time()
{
return twt / service;
}
void add_twt ( int x )
{
twt = twt + x;
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.