Hello I am working on a Java Project that simulates a checkout line at a superma
ID: 3762020 • Letter: H
Question
Hello I am working on a Java Project that simulates a checkout line at a supermarket.
Assignment:
Queue simulation. Write a program that simulates a checkout line in a
supermarket. A queue is FIFO, so enqueue at the tail of the queue, and
dequeue at the head of the queue. The line will be a queue object array.
Customer (objects) will arrive in random intervals from 1 to 4 minutes.
Customers remain in the queue until they are serviced – also determined by
another random interval from 1 to 4 minutes. The arrival times and the
service times are, then, integers between 1 and 4. Run the simulation for 720
minutes (a 12-hour day). Use an algorithm similar to:
a. Choose a random number between 1 and 4 to determine when the
b. At each customer’s arrival time: 1) determine the customer’s service
c. For each simulated minute of the day:
first customer arrives. When this time (in minutes) arrives, add a
customer to the queue (see also b below), and determine when the
next random customer is to arrive.
time (also between 1 and 4 minutes) when reaching the head of the
queue, 2) enqueue the customer, and 3) schedule the arrival of the
next customer (between 1 and 4 minutes).
i. When a customer arrives on the queue: 1) say so, 2) determine
the customer’s service time and display it, 3) enqueue the
arrival, and 4) schedule the next customer’s arrival time, and
display it.
6
CMPSC 221/IST 311 Assignments
ii. When service is completed for each customer: 1) say so,
2) determine the customer’s completion time (an integer
between 1 and 720 – also display this time when you say so),
3) determine the customer’s actual wait time (which is the
time now minus the customer’s arrival time), and 3) dequeue
the customer.
iii. Note: no butting in line! Only dequeue at the head of the
queue, even if there are customers with shorter service times
behind the head.
d. At the end of the simulation, display: 1) number of customers serviced
in the simulation, 2) the maximum number of customers in the queue
during the 720-minute simulation, 3) the average actual wait time for
all customers (a double), and 4) the number of customers left on the
queue when the simulation ends.
e. Place all output into a file (and post it to Angel when submitting the
assignment).
Implement the queue as a LinkedList:
Queue <Customer> q = new LinkedList<Customer>();
I am totally lost on how to do this, can someone please offer some guidance?
Explanation / Answer
Hope this helps you
customer.h
#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
{ ""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
#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> 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 << "Huff STORE IS CLOSED! " << endl;
cout << serviceQueue << " " << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.