Hello I am having some trouble with a supermarket checkout s
Hello, I am having some trouble with a supermarket checkout simulation program. 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 or friend functions 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)
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?
Solution
package grocerystoresimulation;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.ArrayList;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class GroceryStoreSimulation {
private PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
private Random rand = new Random(); //instantiate new Random object
private Date date = new Date();
private DateFormat dateFormat = new SimpleDateFormat(\"yyyy/MM/dd - hh:mm:ss a\");
private ArrayList<String> timeStamp = new ArrayList<String>(); //store timestamps
private int totalCustomers; //# of customers served during simulation
private long startTime = System.nanoTime(); //time of initial build
private long simulationTime = 1; //desired time in minutes
private long firstWaitTime = generateWaitTime();
private long serviceCustomerAt;
public GroceryStoreSimulation(){
System.out.println(\"Instantiated new GroceryStoreSimulation [\"
+ dateFormat.format(date) + \"]\ \" + insertDivider());
}
public void run(){
try {
Thread.sleep(firstWaitTime
System.out.println(\"Delay until first customer: \" + firstWaitTime);
newCustomer(totalCustomers);
serveCustomer();
} catch (InterruptedException e)
while((System.nanoTime()-startTime)<=(simulationTime*60000000000L)-firstWaitTime){
try {
newCustomer(totalCustomers);
serveCustomer();
} catch(Exception e){
}
System.out.println(\"Exit\");
System.exit(0); //stop runtime
}
public String toString(){
return this.pq.toString();
}
private void serveCustomer(){
long elapsedTime = System.nanoTime()-startTime;
while((elapsedTime)<(serviceCustomerAt)){
elapsedTime += System.nanoTime()/10000000;
}
if(pq.size()!=0){
System.out.println(\"Dequeued customer @[\" + dateFormat.format(new Date())
+ \"]\");
pq.poll();
} else {
System.out.println(\"ERROR: Queue is empty!\");
}
}
private void newCustomer(int ID){
long elapsedTime = System.nanoTime()-startTime;
long waitTime = (long)generateWaitTime()*1000000;
long generateAt = elapsedTime+waitTime;
while((elapsedTime)<(generateAt)){
elapsedTime += System.nanoTime()/10000000;
}
serviceCustomerAt = 0;
System.out.println(\"Customer # \" + totalCustomers + \" added to queue. . .\");
totalCustomers++;
pq.offer(ID);
System.out.println(\"Queue size: \" + pq.size());
assignTimestamp(ID);
waitTime = (long)generateWaitTime()*1000000;
elapsedTime = System.nanoTime()-startTime;
serviceCustomerAt = elapsedTime + waitTime;
System.out.println(\"Service delay: \" + waitTime/1000000);
}
private void assignTimestamp(int ID){
timeStamp.add(ID + \": \" + dateFormat.format(new Date()));
System.out.println(timeStamp.get(totalCustomers-1));
}
private int generateWaitTime(){
int Low = 1000; //1000ms
int High = 4000; //4000ms
return rand.nextInt(High-Low) + Low;
}
private static String insertDivider(){
return (\"****\");
}
2. What was the longest wait time for a customer?
Ans=Customers arrive at intervals of 1 to 4 SECONDS.



