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

Hello I am working on a JAVA Project that simulates a checkout line at a superma

ID: 3765567 • 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

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

package grocerystoresimulation;

/*

* @import

*/

import java.util.PriorityQueue;

import java.util.Random;

import java.util.ArrayList;

import java.util.Date;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

/*

* @author: Tyler J Fisher

* Date: 2/27/2012

*/

public class GroceryStoreSimulation {

/*

* @fields

*/

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;

/*

* @constuctor

*/

public GroceryStoreSimulation(){

    System.out.println("Instantiated new GroceryStoreSimulation @ ["

            + dateFormat.format(date) + "] " + insertDivider());

} //GroceryStoreSimulation()

public void run(){

    //Main program body

    try {

        Thread.sleep(firstWaitTime); //generate wait time for first customer

        System.out.println("Delay until first customer: " + firstWaitTime);

        newCustomer(totalCustomers);

        serveCustomer();

    } catch (InterruptedException e){/*Catch 'em all*/}

    while((System.nanoTime()-startTime)<=(simulationTime*60000000000L)-firstWaitTime){

        try {

            newCustomer(totalCustomers); //enque customer

            serveCustomer();

        } catch(Exception e){/*Catch 'em all*/}

    }

    System.out.println("Exit");

    System.exit(0); //stop runtime

} //run()

/*

* @return String

*/

@Override

public String toString(){

    return this.pq.toString();

} //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(); //remove first element of queue

    } else {

        System.out.println("ERROR: Queue is empty!");

    }

} //serveCustomer()

/*

* @param String ID

*/

private void newCustomer(int ID){

    long elapsedTime = System.nanoTime()-startTime;

    long waitTime = (long)generateWaitTime()*1000000;

    long generateAt = elapsedTime+waitTime;

    while((elapsedTime)<(generateAt)){/*Wait*/   

        elapsedTime += System.nanoTime()/10000000; //increment elapsed time

    }     

    serviceCustomerAt = 0; //reset service wait time value

    System.out.println("Customer # " + totalCustomers + " added to queue. . .");

    totalCustomers++;

    pq.offer(ID); //insert element into PriorityQueue

    System.out.println("Queue size: " + pq.size()); //output linesize

    assignTimestamp(ID); //call assignArrivalTime() method

    //Calculate time until customer served

    waitTime = (long)generateWaitTime()*1000000;

    elapsedTime = System.nanoTime()-startTime;

    serviceCustomerAt = elapsedTime + waitTime;

    System.out.println("Service delay: " + waitTime/1000000);

} //newCustomer()

/*

* @param String ID

*/

private void assignTimestamp(int ID){

    timeStamp.add(ID + ": " + dateFormat.format(new Date()));

    System.out.println(timeStamp.get(totalCustomers-1));

} //assignArrivalTime()

* @return int

*/

private int generateWaitTime(){

    //Local variables

    int Low = 1000; //1000ms

    int High = 4000; //4000ms

    return rand.nextInt(High-Low) + Low;

}//generateWaitTime()

/*

* @return String

*/

private static String insertDivider(){

    return ("****");

}//insertDivider()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote