Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a simulation program to simulate lines at a grocery store for a typical da

ID: 3601916 • 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 loop

Explanation / Answer

//main.cpp

#include <iostream>

#include <queue>

#include <fstream>

#include <stdlib.h>

using namespace std;

#include "cashier.hpp"

#include "sim_data_type.hpp"

int main (void)

{

//setting up the 5 lines of cashiers

Cashier cashier1;

Cashier cashier2;

Cashier cashier3;

Cashier cashier4;

Cashier cashier5;

//creating Customer variable to temporarily hold input

Customer cust;

//creating queue to hold input from file

queue<Customer> custInput;

//creating variable to hold simulation data

Sim_data_type sim;

//file for getting input

ifstream in_file;

//used to temperarily hold the line obtained from the file

string line;

//used to temperarily hold the selected substring

string substring;

//opening file

in_file.open("output.dat");

if( in_file.fail() )

{

cout<<"Could not open file. ";

return 0;

}

else

{

//get data from file until end of file

while( !in_file.eof() )

{

//gets input until new line

getline( in_file, line );

//if extracted string isn't empty

if( line.length() > 0 )

{

//getting the first digits for (number)

substring = line.substr(0, 2);

  

cust.set_number( atoi(substring.c_str()) );

//getting the next digits for (arrival_time)

substring = line.substr(4, 4);

cust.set_arrival_time( atoi(substring.c_str()) );

//getting the next digits for (serv_time)

substring = line.substr(9, 3);

cust.set_serv_time( atoi(substring.c_str()) );

//adding newly created customer to the data queue

custInput.push( cust );

}

}

//closing file

in_file.close();

}

//while store not closed

while( sim.get_cnt() < 600 )

{

  

//increment minutes

sim.inc_cnt();

cashier1.inc_timer();

cashier2.inc_timer();

cashier3.inc_timer();

cashier4.inc_timer();

cashier5.inc_timer();

//while customers can still be served

if( sim.get_cnt() < 570 )

{

//set cust to temporarily hold front of custInput

cust = custInput.front();

//if the front of queue is ready to join line

while( cust.get_arrival_time() == sim.get_cnt() )

{

  

//set the customer to the first available/shortest

//lane; if none are open turn them away

if(

cashier1.get_status() == 'a' &&

(cashier2.get_status() == 'i' ||

cashier1.get_in_line() <= cashier2.get_in_line()) &&

(cashier3.get_status() == 'i' ||

cashier1.get_in_line() <= cashier3.get_in_line()) &&

(cashier4.get_status() == 'i' ||

cashier1.get_in_line() <= cashier4.get_in_line()) &&

(cashier5.get_status() == 'i' ||

cashier1.get_in_line() <= cashier5.get_in_line())

)

{

//adding customers serv time to cashiers queue

cashier1.add_customer( cust.get_serv_time() );

//removing customer from input we added to queue

custInput.pop();

//setting cust to new front so while checks correctly

cust = custInput.front();  

}

else if(

cashier2.get_status() == 'a' &&

(cashier1.get_status() == 'i' ||

cashier2.get_in_line() <= cashier1.get_in_line()) &&

(cashier3.get_status() == 'i' ||

cashier2.get_in_line() <= cashier3.get_in_line()) &&

(cashier4.get_status() == 'i' ||

cashier2.get_in_line() <= cashier4.get_in_line()) &&

(cashier5.get_status() == 'i' ||

cashier2.get_in_line() <= cashier5.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 if(

cashier4.get_status() == 'a' &&

(cashier2.get_status() == 'i' ||

cashier4.get_in_line() <= cashier2.get_in_line()) &&

(cashier3.get_status() == 'i' ||

cashier4.get_in_line() <= cashier3.get_in_line()) &&

(cashier1.get_status() == 'i' ||

cashier4.get_in_line() <= cashier1.get_in_line()) &&

(cashier5.get_status() == 'i' ||

cashier4.get_in_line() <= cashier5.get_in_line())

)

{

cashier4.add_customer( cust.get_serv_time() );

custInput.pop();

cust = custInput.front();

}

else if(

cashier5.get_status() == 'a' &&

(cashier2.get_status() == 'i' ||

cashier5.get_in_line() <= cashier2.get_in_line()) &&

(cashier3.get_status() == 'i' ||

cashier5.get_in_line() <= cashier3.get_in_line()) &&

(cashier4.get_status() == 'i' ||

cashier5.get_in_line() <= cashier4.get_in_line()) &&

(cashier1.get_status() == 'i' ||

cashier5.get_in_line() <= cashier1.get_in_line())

)

{

cashier5.add_customer( cust.get_serv_time() );

custInput.pop();

cust = custInput.front();

}

else

{

sim.inc_num_turned_away();

}

}

}

//increment sim variables for output later

//if cashiers timer = the serv time of cust

//then remove that cust

if( cashier1.get_timer() == cashier1.get_curr_cust_time_limit() )

{

//add the customer-to-be-removed's wait time to total wait

sim.add_tot_wait_time( cashier1.get_curr_cust_time_limit() );  

//remove it front queue

cashier1.rmv_frnt_cust();

//increment the total number serviced

sim.inc_num_serviced();

}

if( cashier2.get_timer() == cashier2.get_curr_cust_time_limit() )

{

sim.add_tot_wait_time( cashier2.get_curr_cust_time_limit() );

cashier2.rmv_frnt_cust();

sim.inc_num_serviced();

}

if( cashier3.get_timer() == cashier3.get_curr_cust_time_limit() )

{

sim.add_tot_wait_time( cashier3.get_curr_cust_time_limit() );

cashier3.rmv_frnt_cust();

sim.inc_num_serviced();

}

if( cashier4.get_timer() == cashier4.get_curr_cust_time_limit() )

{

sim.add_tot_wait_time( cashier4.get_curr_cust_time_limit() );

cashier4.rmv_frnt_cust();

sim.inc_num_serviced();

}

if( cashier5.get_timer() == cashier5.get_curr_cust_time_limit() )

{

sim.add_tot_wait_time( cashier5.get_curr_cust_time_limit() );

cashier5.rmv_frnt_cust();

sim.inc_num_serviced();

}

}

//final output

cout<<"Total number of customers serviced: "<<sim.get_num_serviced();

cout<<endl<<"Total number of customers turned away: "

<<sim.get_num_turned_away();

cout<<endl<<"Average wait time: "<<sim.get_avg_wait_time()<<endl;

//exit program

return 0;

}

==================================================================================

//cashier.hpp

#include "customer.hpp"

//Cashier class to act as 5 queues for customer info to be held in
class Cashier
{
int timer; //minutes in use
char status; // 'a' -available, 'i' - inactive
queue<int> time_limit; //depends on customer serv_time
int in_line;

public:
Cashier()
{
timer = 0;
status = 'a';
in_line = 0;
}

char get_status()
{
return status;
}

void inc_timer()
{
timer++;
}

int get_timer()
{
return timer;
}

int get_in_line()
{
return in_line;
}

void add_customer( int serv_time )
{

//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( serv_time );

//add customer to line
in_line++;

if( in_line == 6 )
{
status = 'i';
}

}

int get_curr_cust_time_limit()
{
return time_limit.front();
}

void rmv_frnt_cust()
{
time_limit.pop();
  
timer = 0;

in_line--;

status = 'a';

}

};

==================================================================================

//customer.hpp


//customer class to hold information pulled from notepad
class Customer
{  
//all information on the customer pulled from notepad
int number;
int arrival_time;
int serv_time;

public:
Customer()
{
number = 0;
arrival_time = 0;
serv_time = 0;
}

void set_number(int x)
{
number = x;
}

void set_arrival_time(int x)
{
arrival_time = x;
}

int get_arrival_time()
{
return arrival_time;
}

void set_serv_time(int x)
{
serv_time = x;
}

int get_serv_time()
{
return serv_time;
}

};

===============================================================================

//sim_data_type.hpp

class Sim_data_type
{
int time_cnt; // ranges in values from 0 to 600
int num_serviced; // number of customers serviced so far
long tot_wait_time; // of all customers in the queues
int num_turned_away; // count of customers turned away

public:
Sim_data_type()
{
time_cnt = 0;
num_serviced = 0;
tot_wait_time = 0;
num_turned_away = 0;
}

void inc_cnt()
{
time_cnt++;
}  

int get_cnt()
{
return time_cnt;
}

void inc_num_turned_away()
{
num_turned_away++;
}  

int get_num_turned_away()
{
return num_turned_away;
}

void inc_num_serviced()
{
num_serviced++;
}

int get_num_serviced()
{
return num_serviced;
}

int get_avg_wait_time()
{
return tot_wait_time / num_serviced;
}

void add_tot_wait_time ( int x )
{
tot_wait_time = tot_wait_time + x;
}

};

================================================================================

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote