Question

Hello, I am having some trouble with a supermarket checkout simulation program in C++. What I...

Hello, I am having some trouble with a supermarket checkout simulation program in C++. What I have so far just basically adds customers to the queue and prints when they arrive. I am struggling with how to implement a way of keeping track of when a given customer finishes(I will attach what I have so far). I had already created queue and node classes (with headers and cpp files) that I modified in my attempt. I would be very grateful if you could give me some help with this assignment, my teacher was not very clear on how to do this and it is far beyond what we have done so far. I have found one solution but it uses friend functions (which we have not gone over yet and I cannot use) and does not work well. Here are the requirements for the project:

(Please do NOT use trees, friend functions or templates as we have not gone over those in class, we're only supposed to use a node and queue class, which can instead be titled customer and lane and also use ALOT of comments so I can understand everything and write my own code)

Write a program that simulates a checkout line at a supermarket. Customers arrive in random intervals from 1 - 4 minutes. Each customer is also serviced from 1 - 4 minutes. The store starts the day with 3 lanes open. Run the simulation for a 12-hour day (720 minutes) using the following to help you.

1. Get a random integer between 1 and 4 (customer arrival time)

2. Determine that customer's service time (another random integer!)

3. Begin servicing that customer (all lines are empty!)

4. Don't forget to account for all of the following throughout the "day"! a. calculate when customers arrive b. When they do, output a message (include customer ID and time of arrival) c. Enqueue them into a lane (think of logic to prevent 1 lane always being chosen!) d. When a customer is done being serviced, output a message (include customer ID and total time they spend in the queue) e. Dequeue the customer when they are finished Answer the following questions after your simulation is complete.

1. What was the maximum number of customers in line (across all lines) at any time? How many were in each line?

2. What was the longest wait time for a customer?

3. Change the arrival time to 2-5 minutes, how does this affect the outcome? Re-answer problems 1 & 2 with these settings.

4. What are the minimum number of lanes that must be open to keep all customer's waiting times < 5 minutes using both of the arrival time intervals? How about for < 10 minutes?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <cstdlib>
#include <ctime>

#include <fstream>
#include <iostream>

#include "supermarket.h"

using namespace std;

/***
* Customer
*/
Customer::Customer(int nCustID, int nStartTime, Random *randor)
{
   nCustomerID = nCustID;
   nArrivalTime = nStartTime + randor->GetArrivalInterval();
   nServiceTime = randor->GetServiceTime();

   /** presumed time **/
   nDequeueTime = nArrivalTime;
}

/***
* Random
*/
Random::Random()
{
   nServiceTime = SERVTIME;
   nArrivalInterval = ARRIINTV;

   srand(time(0));
}

int Random::GetServiceTime()
{
   return rand()%nServiceTime + 1;
}

int Random::GetArrivalInterval()
{
   return rand()%nArrivalInterval + 1;
}

/***
* Cashier
*/

/*
Cashier::Cashier()
{
   ptrCurrentCustomer = NULL;
   nServiceTime = 0;
   bIsBusy = false;
}
*/

void Cashier::ServiceCustomer(Customer *cust)
{
   ptrCurrentCustomer = cust;
   nServiceTime += cust->GetServiceTime();

   nEndTime = cust->GetDequeueTime() + cust->GetServiceTime();
   bIsBusy = true;
}

/***
* Queue
*/
   template <typename T>
void Queue<T>::Enqueue(T *item)
{
   if (IsFull()) {
       cerr << "Queue Is Full." <<endl;
       return;
   }

   cout<<"Customer "<<item->GetCustomerID()<<" arrived at "
       << item->GetArrivalTime() <<" mins"<<endl;

   ptrArray[tail++] = item;
   nItemCount++;
   tail %= MAXTIME;

   GetMaxCount();
}

   template <typename T>
T* Queue<T>::Dequeue()
{
   if (IsEmpty()) {
       cerr << "Queue Is Empty." << endl;
       return NULL;
   }

   T *item = ptrArray[head++];
   nItemCount--;
   head %= MAXTIME;

   return item;
}

   template <typename T>
Queue<T>::~Queue()
{
   /***
   * Delete all items
   */
   while (!IsEmpty()) {
       T *p = Dequeue();
       delete p;
   }
}

int main()
{

   int cnum=1;
   int served=0;
   int t=0;
   int twait=0;
   int maxwt=0;
   int tserv=0;

   Random *myRandor = new Random();
   Cashier *myCashier = new Cashier();
   Customer *cur = new Customer(cnum, t, myRandor);
   Customer *next;
   /***
   * Change The Random Time Seed Here
   */
   myRandor->RandomReset();

   cout<<"Simulation started"<<endl;
   t = cur->GetArrivalTime();
   cout<<"Customer 1 arrived at "<< t <<" mins"<<endl;

   myCashier->ServiceCustomer(cur);
   cout<<"Customer 1 began service at "<<t<<" mins"<<endl;

   next = new Customer(++cnum, t, myRandor);
   Queue<Customer> myQueue;

   /* SIMULATION */
   while (t <= MAXTIME)
   {
       if(t == next->GetArrivalTime())
       {
           myQueue.Enqueue(next);
           cnum++;

           next = new Customer(cnum, t, myRandor);
       }

       /***
       * Get A Cutomer to service
       */
       if(!myCashier->IsBusy())
       {
           cur = myQueue.Dequeue();

           if(NULL != cur)
           {
               /* Update Dequeue Time */
               cur->SetDequeueTime(t);

               myCashier->ServiceCustomer(cur);
               cout<<"Customer "<< cur->GetCustomerID()<<" began service at "<<t<<" mins"<<endl;
           }
       }

       if(t == myCashier->GetEndTime())
       {
           served++;

           cout<<"Customer "<<cur->GetCustomerID()<<" ended service at "<<t<<" mins"<<endl;

           /** Need destroy */
           delete cur;

           cur = myQueue.Dequeue();
           if(NULL != cur)
           {
               cur->SetDequeueTime(t);
               twait += (t - cur->GetArrivalTime());
               if(maxwt < cur->GetWaitingTime())
                   maxwt = cur->GetWaitingTime();

               myCashier->ServiceCustomer(cur);
               cout<<"Customer "<<cur->GetCustomerID()<<" began service at "<<t<<" mins"<<endl;
           }
           else
           {
               myCashier->SetBusyState(false);
           }
       }

       t++;
   }


   cout<<"Simulation ended"<<endl;
   cout<<"Served "<<served<<" customers"<<endl;
   cout<<"Left in line "<<myQueue.GetItemCount()<<" customers"<<endl;
   cout<<"Total wait time: "<<twait<<" mins"<<endl;
   cout<<"Average wait time: "<<(double)twait/served<<" mins"<<endl;
   cout<<"Max wait time for any customer: "<<maxwt<<" mins"<<endl;
   tserv = myCashier->GetServiceTime();
   cout<<"Total service time: "<<tserv<<" mins"<<endl;
   cout<<"Average service time: "<<(double)tserv/served<<" mins"<<endl;
   cout<<"Max number of customers in line: "<<myQueue.GetMaxCount()<<endl;

   delete cur;
   delete next;
   delete myRandor;
   delete myCashier;

   return 0;
}

supermarket.h

#ifndef _SUPERMARKET_H

#define MAXTIME   720
#define SERVTIME 4
#define ARRIINTV 4

class Random {
private:
   int nServiceTime;
   int nArrivalInterval;

public:
   Random();

   void RandomReset(int nST = 4, int nAI = 4)
   {
       nServiceTime = nST;
       nArrivalInterval = nAI;
   }

   ~Random()
   {
   }

   int GetServiceTime();
   int GetArrivalInterval();
};

class Customer {
private:
   int nCustomerID;
   int nArrivalTime;
   int nServiceTime;
   int nDequeueTime;

public:
   Customer(int nCustID, int nStartTime, Random *randor);

   int GetCustomerID()
   {
       return nCustomerID;
   }

   int GetArrivalTime()
   {
       return nArrivalTime;
   }

   int GetServiceTime()
   {
       return nServiceTime;
   }

   int GetDequeueTime()
   {
       return nDequeueTime;
   }

   int GetWaitingTime()
   {
       return nDequeueTime - nArrivalTime;
   }

   void SetDequeueTime(int nDT)
   {
       nDequeueTime = nDT;
   }

   ~Customer(){}
};

template <typename T>
class Queue {
private:
   T *ptrArray[MAXTIME];
   int head, tail;

   int nItemCount;
   int nMaxCount;
private:
   bool IsFull()
   {
       return nItemCount == MAXTIME;
   }

   bool IsEmpty()
   {
       return nItemCount == 0;
   }

public:
   Queue():head(0),tail(0),nItemCount(0),nMaxCount(0)
   {
   }

   ~Queue();

   void Enqueue(T* item);
   T* Dequeue();

   int GetItemCount()
   {
       return nItemCount;
   }

   int GetMaxCount()
   {
       if (nItemCount > nMaxCount)
           nMaxCount = nItemCount;

       return nMaxCount;
   }
};

class Cashier {
private:
   Customer *ptrCurrentCustomer;

   /** Total service Time */
   int nServiceTime;

   /** Service One Customer End */
   int nEndTime;
   bool bIsBusy;

public:
   Cashier()
   {
       nServiceTime = 0;
       bIsBusy = false;
   }

   ~Cashier()
   {
   }

   void SetBusyState(bool s)
   {
       bIsBusy = s;
   }

   bool IsBusy()
   {
       return bIsBusy;
   }

   int GetEndTime()
   {
       return nEndTime;
   }

   int GetServiceTime()
   {
       return nServiceTime;
   }

   void ServiceCustomer(Customer *cust);

};

#endif

Customer 283 ended service at 716 mins Customer 284 began service at 716 mins Customer 287 arrived at 718 mins Customer 284 e

Add a comment
Know the answer?
Add Answer to:
Hello, I am having some trouble with a supermarket checkout simulation program in C++. What I...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Sample run might look like this: How many minutes should the simulation run? 10 Running...

    C++ Sample run might look like this: How many minutes should the simulation run? 10 Running simulation for 10 minutes. minute 1: nothing happens minute 2: customer 1 arrives and will need 3 minutes to check out minute 3: nothing happens minute 4: customer 2 arrives and will need 1 minute to check out minute 5: customer 3 arrives and will need 1 minute to check out minute 5: customer 1 has checked out minute 6: customer 2 has checked...

  • Needs Help with Java programming language For this assignment, you need to write a simulation program...

    Needs Help with Java programming language For this assignment, you need to write a simulation program to determine the average waiting time at a grocery store checkout while varying the number of customers and the number of checkout lanes. Classes needed: SortedLinked List: Implement a generic sorted singly-linked list which contains all of the elements included in the unsorted linked list developed in class, but modifies it in the following way: • delete the addfirst, addlast, and add(index) methods and...

  • Do you dislike waiting in line? A supermarket chain has used computer simulation and information technology...

    Do you dislike waiting in line? A supermarket chain has used computer simulation and information technology to reduce the average waiting time for customers at 2,300 stores. Using a new system, which allows the supermarket to better predict when shoppers will be checking out, the company was able to decrease average customer waiting time to just 19 seconds. (a) Assume that supermarket waiting times are exponentially distributed. Show the probability density function of waiting time at the supermarket. • X20...

  • Queues This programming exercise introduces the Queue data structure. The students must create all the necessary...

    Queues This programming exercise introduces the Queue data structure. The students must create all the necessary methods for the queue and use the queue in a Java program. Step 1 - Create a new project in Netbeans Use the following naming convention: “Module4_Lastname_Assignment1”. Step 2 - Build a solution You have been asked to create a customer service program for a new phone store that will be opening soon. Since this company anticipates being the agent for a rising new...

  • This is Python programming to develop a multithread program. Please follow the step below. 1. You...

    This is Python programming to develop a multithread program. Please follow the step below. 1. You need to develop a multithread program in Python to simulate a checkout in supermarket. The process of simulation will last for T1 seconds. You do not need to consider thread safety. 2. An integer sequence t is used to record the remaining waiting time for each customer. 3. One thread which runs every one second is used to simulate the processing of the checkout....

  • PLEASE DO NOT COPY CODE FROM OTHER QUESTIONS POSTED WITH THIS. THAT IS C++ NOT PYTHON...

    PLEASE DO NOT COPY CODE FROM OTHER QUESTIONS POSTED WITH THIS. THAT IS C++ NOT PYTHON Please follow the step below. 1. You need to develop a multithread program in PYTHON to simulate a checkout in supermarket. The process of simulation will last for T1 seconds. You do not need to consider thread safety. 2. An integer sequence t is used to record the remaining waiting time for each customer. 3. One thread which runs every one second is used...

  • In Java, complete the following: During the lunch hour, the ATM machine in a large office...

    In Java, complete the following: During the lunch hour, the ATM machine in a large office complex is in heavy demand. Customers complain that the waiting time is much too long. The local bank considering the addition of a second ATM machine. But first, the bank needs a few statistics to justify the cost of adding a second ATM machine. Using a Queue, simulate a waiting line at the ATM machine for a period of one hour. Make the following...

  • Consider a simple queuing system in which customers arrive randomly such that the time between successive...

    Consider a simple queuing system in which customers arrive randomly such that the time between successive arrivals is exponentially distributed with a rate parameter l = 2.8 per minute. The service time, that is the time it takes to serve each customer is also Exponentially distributed with a rate parameter m = 3 per minute. Create a Matlab simulation to model the above queuing system by randomly sampling time between arrivals and service times from the Exponential Distribution. If a...

  • 11. Leonard Chang runs a barbershop in Fredericton, NB. Due to his excellent marketing skills, the...

    11. Leonard Chang runs a barbershop in Fredericton, NB. Due to his excellent marketing skills, the sales have increased by 25% in the last quarter. The barbershop does 1200 haircuts per month. The labor productivity is about 2 haircuts per labor-hour. Each barber works 150 hours per month. How many barbers should Leonard add? The hourly pay will be $10 for barbers. Leonard is considering redesign the barbershop to increase the customer flows. The redesign entails a significant increase in...

  • Can someone help me get started on this question? I am not sure where to begin...

    Can someone help me get started on this question? I am not sure where to begin here. Thank you! Question B: Car traffic arrives at a large toll plaza at different rates depending on the time of day. The rate (in cars-per-hour) at time "t" is r(t) = 5000*(1 - 0.8cos(2 pit/24)) for t measured in hours between 0 and 24. i) Define A(T) = the # of cars that have arrived by time "T". Find a formula for A(T)....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT