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

I have a program that uses a queue in a linkedlist to simulate store going into

ID: 3567047 • Letter: I

Question

I have a program that uses a queue in a linkedlist to simulate store going into a store. This time i need to modify this program to use a priority queue using a heap in an array. Here are details of what i need:

An updated PriorityCustomer class is provided for you (download from Moodle). You must use that class, without alternations, for the creation of your PriorityCustomer objects. You must analyze the class and use the provided methods to achieve the desired functionality of the program. You will also need to create two additional classes. The first will be a PriorityQueue class that will represent the data structure for holding your Customer objects. In your PriorityQueue class, you will create the actual heap that will act as the priority queue. You must use an array representation of a heap. No ArrayLists or linked structures! The second class you will need to create is a driver where your store simulation will take place.

Customers with a priority value higher than other existing customers should be placed in front of them. This is simulated by utilizing a Max Heap to implement your priority queue. The only exception to this is for the customer in the front of the line (the one currently being serviced). If a new customer is added to the line with a higher priority than the front customer, the new customer should not be put in front of the customer being serviced

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()
output:

run:
Instantiated new GroceryStoreSimulation @ [2012/03/13 - 01:55:23 AM]

Delay until first customer: 1263
Customer # 0 added to queue. . .
Queue size: 1
0: 2012/03/13 - 01:55:24 AM
Service delay: 1373
Dequeued customer @[2012/03/13 - 01:55:24 AM]
Customer # 1 added to queue. . .
Queue size: 1
1: 2012/03/13 - 01:55:24 AM
Service delay: 2188
Dequeued customer @[2012/03/13 - 01:55:24 AM]
Customer # 2 added to queue. .
.
.
.
Service delay: 3379
Dequeued customer @[2012/03/13 - 01:55:24 AM]
Customer # 927 added to queue. . .
Queue size: 1
927: 2012/03/13 - 01:55:24 AM
Service delay: 2300
Service delay: 2300BUILD STOPPED (total time: 1 second)