Question
Instructions: Write a program that simulates customers waiting in line at a grocery store. Your program must use a Queue to represent the customer objects waiting in line.
A Customer class is provided for you. You must use that class, without alternations, for the creation of your Customer objects. You must analyze the class and use the provided methods to achieve the desired functionality of the program.
The program should simulate 60 minutes of activity at the store. Each iteration of your program should represent one minute. At each iteration (minute), your program should do the following:
--> Check to see if new customers are added to the queue. There is a 25% chance that new customers show up (need to be added to the queue) every minute. This does not mean you should add a customer every four iterations, but rather each iteration should have its own 25% chance.
--> Update the customer object currently being serviced (if one exists). This will be the customer object at the front of the queue. If the customer has been completely serviced, remove them from the queue.
During execution, your program should output the following information:
--> When a new customer is added to the queue, output,
Explanation / Answer
import java.util.*; public class GrocerySim { private Queue checkLine; private ArrayList customers; private int numCustomers; private Customer currCust; private CheckOut cashier; private Random r; public GrocerySim(int n) { FEL = new PriorityQueue(); // Keeping the Future Event List as a PQ allows // it to be maintained by the next closest event // (or smallest into the future). checkLine = new LinkedList(); // The customers waiting to be // checked out are in a simple queue (FIFO). numCustomers = n; customers = new ArrayList(); cashier = new CheckOut(); r = new Random((new Date()).getTime()); // Seed the random number // generator so output differs from run to run. } // Iterate as long as there are events left in the FEL. Since events are // both arrival events and completion events, we must distinguish between // the two and take different actions accordingly. See classes SimEvent, // ArrivalEvent and CompletionEvent for details on how they are set up. public void runSimulation() throws ClassNotFoundException { int custNum = 0; int clock = 0; FEL.add(new ArrivalEvent(0)); // Initialize FEL with first arrival // Simulation should run until no more events are left while (!FEL.isEmpty()) { SimEvent e = FEL.remove(); clock = e.get_e_time(); if (Class.forName("ArrivalEvent").isInstance(e)) { custNum++; currCust = new Customer(custNum, e.get_e_time()); currCust.serviceT = serviceTime(); if (!cashier.isBusy()) { currCust.startServiceT = clock; cashier.addCust(currCust); FEL.add(new CompletionEvent(clock + currCust.serviceT)); } else { checkLine.add(currCust); } if (custNum