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

Subject Java: Write a program that simulates a checkout line at a supermarket.Th

ID: 3829404 • Letter: S

Question

Subject Java:

Write a program that simulates a checkout line at a supermarket.The line is a queue object. Customers (i.e., customer objects)arrive in random integer intervals of 1–4 minutes. Also, eachcustomer is served in random integer intervals of 1–4minutes. Obviously, the rates need to be balanced. If the averagearrival rate is larger than the average service rate, the queuewill grow infinitely. Even with “balanced” rates,randomness can still cause long lines. Run the supermarketsimulation for a 12-hour day (720 minutes) using the following algorithm:


Choose a random integer between 1 and 4 to determine the minuteat which the first customer arrives.

At the first customer’s arrival time:
Determine customer’s service time (random integer from 1 to4);
Begin servicing the customer;
Schedule arrival time of next customer (random integer 1 to 4 addedto the current time).

For each minute of the day:
       If the next customerarrives,
               Say so,
               Enqueue the customer;
               Schedule the arrival time of the next customer;
       If service was completedfor the last customer;
               Say so
               Dequeue next customer to be serviced
               Determine customer’s service completion time
                      (randominteger from 1 to 4 added to the current time).

What is the maximum number of customers in the queue at any time?

What is the longest wait any one customer experiences?

What happens if the arrival interval is changed from 1–4minutes to 1–3 minutes?

-------------------------

Wanted to see if anyone had done this prior and could guide on how to complete this. Thank you!

Explanation / Answer

Customer.h
#pragma once
#include <string>
#include <vector>
class Customer
{
friend std::ostream& operator<<(std::ostream&, Customer&);
friend class SupermarketQueue;
public:
Customer();
~Customer();
const int getServiceTime() const;
std::string toString();
private:
int serviceTime;
int waitTimeStart;
std::string firstName;
std::string lastName;
static std::vector <std::string> firstNames;
static std::vector <std::string> lastNames;
};

Customer.cpp
#include "stdafx.h"
#include "Customer.h"
#include "Windows.h"
#include <string>
#include <random>
#include <ctime>
#include <iostream>

using namespace std;

vector <string> Customer::firstNames{ "Jim", "John", "Joe", "Erik", "Sophia", "Aiden", "Emma", "Jackson", "Olivia",
"Ethan", "Isabelle", "Lily", "Noah", "Zoe", "Mason", "Lucas", "Peter", "Jacob", "Chloe", "Emily", "Hannah", "Loki", "Thor", "Kaylee", "Riley", "Connor",
"Carter", "Daniel", "Nicholas", "Nathan", "Max", "Sam", "Kylie", "Zachary", "Jack", "Rick", "Bryce", "Colleen", "Linda", "Bailey", "Aria", "Bella", "Taylor",
"Jasmine", "Jace", "Christopher", "Nolan", "Carson", "Justin", "Jordan", "Collin", "Xavier", "Ian", "Cole", "Adam", "Daniel", "Monica", "Ruby", "Violet",
"Julia", "Lauren", "Alexa", "Brooke", "Nathaniel", "Sean", "Chase"};
vector <string> Customer::lastNames{ "Nordin", "Smith", "Auschwitz", "The Giant!", "Mungo", "The Ripper", "Johnson", "Williams", "Jones",
"Brown", "Davis", "Miller", "Wilson", "Moore", "Anderson", "White", "Harris", "Martin", "Thompson", "Garcia", "Robinson", "Clark",
"Walker", "Hall", "Casey", "Young", "Wright", "Hill", "Connell", "Lopez", "Green", "Adams", "Murphy", "Cox", "Watson", "Sanders", "Cook",
"Peterson", "Ford", "Freeman", "Boyd", "Kennedy", "Warren", "Hubler", "Grant", "Wagner", "Watkins", "Olson", "Snyder",
"Perkins", "Knight", "Stone", "Mastin"};

Customer::Customer()
:serviceTime(rand() % 4 + 1), firstName(firstNames[rand() % firstNames.size()]), lastName(lastNames[rand() % lastNames.size()])
{
waitTimeStart = GetTickCount();
}

const int Customer::getServiceTime() const
{
return serviceTime;
}

string Customer::toString()
{
return firstName + " " + lastName;
}

ostream& operator<<(ostream& output, Customer client)
{
output << client.toString();
return output;
}

Customer::~Customer()
{
}

SupermarketQueue.h
#pragma once
#include <queue>
#include <string>
#include "Customer.h"
#include <string>
class SupermarketQueue
{
friend std::ostream& operator<<(std::ostream&, SupermarketQueue&);
public:
SupermarketQueue(int);
~SupermarketQueue();
std::string enqueue();
std::string dequeue();
void setNextArrivalTime();
const int getNextArrivalTime() const;
bool isEmpty();
const int lineLength();
std::string toString();
Customer& front();
private:
std::queue <Customer> serviceLine;
int nextArrivalTime;
int recordQueueLength;
int customersServed;
int minuteLength;
double maxWaitTime;
};

SupermarketQueue.cpp
#include "stdafx.h"
#include "SupermarketQueue.h"
#include "Windows.h"
#include <queue>
#include <iostream>
#include <string>
#include <random>
#include <ctime>

using namespace std;

SupermarketQueue::SupermarketQueue(int minute)
:recordQueueLength(0), customersServed(0), maxWaitTime(0)
{
setNextArrivalTime();
minuteLength = minute;
}

string SupermarketQueue::enqueue()
{
serviceLine.push(Customer());
if (serviceLine.size() > recordQueueLength)
{
recordQueueLength = serviceLine.size();
}

return serviceLine.back().toString() + " has joined the queue.";
}

string SupermarketQueue::dequeue()
{
if ((GetTickCount() - serviceLine.front().waitTimeStart) > maxWaitTime)
{
maxWaitTime = static_cast <double> ((GetTickCount() - serviceLine.front().waitTimeStart)) / minuteLength;
}
string returnString = serviceLine.front().toString() + " has left the queue.";
serviceLine.pop();
customersServed++;
return returnString;
}

void SupermarketQueue::setNextArrivalTime()
{
nextArrivalTime = rand() % 3 + 1;
}

const int SupermarketQueue::getNextArrivalTime() const
{
return nextArrivalTime;
}

bool SupermarketQueue::isEmpty()
{
return serviceLine.empty();
}

Customer& SupermarketQueue::front()
{
return serviceLine.front();
}

const int SupermarketQueue::lineLength()
{
return serviceLine.size();
}

string SupermarketQueue::toString()
{
return "Customers Served Today: " + to_string(customersServed) + " Longest Line Length: " + to_string(recordQueueLength) +
" Longest Wait Time: " + to_string(maxWaitTime) + " Minutes";
}

ostream& operator<<(ostream& output, SupermarketQueue& serviceQueue)
{
output << serviceQueue.toString();
return output;
}

SupermarketQueue::~SupermarketQueue()
{
}

MAIN—Supermarket Simulation.cpp
// Supermarket Simulation.cpp : It tells us the entry point for the application.
//

#include "stdafx.h"
#include "SupermarketQueue.h"
#include "Customer.h"
#include "Windows.h"
#include <queue>
#include <iostream>
#include <string>
#include <random>
#include <ctime>
#include <iomanip>

using namespace std;

const int //Miliseconds

int _tmain(int argc, _TCHAR* argv[])
{
srand(static_cast <unsigned int> (time(0)));
SupermarketQueue serviceQueue(ONE_MINUTE);

int totalTimeReference;
int nextArrivalTimeReference;
int nextServiceTimeReference = 0;
totalTimeReference = nextArrivalTimeReference = GetTickCount();
  
while ((GetTickCount() - totalTimeReference) <= (720 * ONE_MINUTE))
{
if ((GetTickCount() - nextArrivalTimeReference) >= (serviceQueue.getNextArrivalTime() * ONE_MINUTE))
{
if (serviceQueue.isEmpty())
{
nextArrivalTimeReference = nextServiceTimeReference = GetTickCount();
cout << serviceQueue.enqueue() << endl;
cout << " Current Amount Of Customers In Line: " << serviceQueue.lineLength() << endl << endl;
}
else
{
nextArrivalTimeReference = GetTickCount();
cout << serviceQueue.enqueue() << endl;
cout << " Current Amount Of Customers In Line: " << serviceQueue.lineLength() << endl << endl;
}
serviceQueue.setNextArrivalTime();
}

if (!serviceQueue.isEmpty() && ((GetTickCount() - nextServiceTimeReference) >= (serviceQueue.front().getServiceTime() * ONE_MINUTE)))
{
cout << serviceQueue.dequeue() << endl;
cout << " Current Amount Of Customers In Line: " << serviceQueue.lineLength() << endl << endl;
nextServiceTimeReference = GetTickCount();
}
}

while (!serviceQueue.isEmpty())
{
if ((GetTickCount() - nextServiceTimeReference) >= (serviceQueue.front().getServiceTime() * ONE_MINUTE))
{
cout << serviceQueue.dequeue() << endl;
cout << " Current Amount Of Customers In Line: " << serviceQueue.lineLength() << endl << endl;
nextServiceTimeReference = GetTickCount();
}
}

cout << "STORE CLOSED! " << endl;
cout << serviceQueue << " " << endl;
return 0;
}