Implement the supermarket simulation by using the following algorithm:
For every value in the (for) loop:
Check to see if the departure time of a customer has been reached. If yes, check to see if the queue is empty.
Print the line after every value in the (for) loop to display the customers standing in the line.
Since the arrival and departure rates are random, the arrival and departure for the same customer will not be properly synchronized. That is, at times, the time value for departure may be less than or equal to the time value for arrival. In our simulation, this will probably happen for about 3-5 customers (as the random values for the arrival times have a lower range compared to the random values of departure times). We will deal with synchronization in more detail when we discuss Multithreading in Java later in the course. For this problem, we will ignore this scenario.
Supermarket Simulation
Arrival Time of Customer#1 = 2
Departure Time of Customer#1 = 7
The Clock = 0 seconds
No Customers
The Clock = 1 seconds
No Customers
The Clock = 2 seconds
Customer 1 has arrived
Arrival Time of Customer#2 = 6
Line: 1
The Clock = 3 seconds
Line: 1
The Clock = 4 seconds
Line: 1
The Clock = 5 seconds
Line: 1
The Clock = 6 seconds
Customer 2 has arrived
Arrival Time of Customer#3 = 10
Line: 1 --> 2
The Clock = 7 seconds
Customer 1 departs
Departure Time of Customer#2 = 9
Line: 2
The Clock = 8 seconds
Line: 2
The Clock = 9 seconds
Customer 2 departs
Departure Time of Customer#3 = 14
No Customers
The Clock = 10 seconds
Customer 3 has arrived
Arrival Time of Customer#4 = 11
Line: 3
The Clock = 11 seconds
Customer 4 has arrived
Arrival Time of Customer#5 = 15
Line: 3 --> 4
...
The Clock = 100 seconds
Customer 37 has arrived
Arrival Time of Customer#38 = 103
Line: 29 --> 30 --> 31 --> 32 --> 33 --> 34 --> 35 --> 36 --> 37
...
The Clock = 117 seconds
Customer 45 has arrived
Arrival Time of Customer#46 = 120
Line: 36 --> 37 --> 38 --> 39 --> 40 --> 41 --> 42 --> 43 --> 44 --> 45
The Clock = 118 seconds
Line: 36 --> 37 --> 38 --> 39 --> 40 --> 41 --> 42 --> 43 --> 44 --> 45
The Clock = 119 seconds
Line: 36 --> 37 --> 38 --> 39 --> 40 --> 41 --> 42 --> 43 --> 44 --> 45
The Clock = 120 seconds
Customer 46 has arrived
Arrival Time of Customer#47 = 123
Customer 36 departs
Departure Time of Customer#37 = 124
Line: 37 --> 38 --> 39 --> 40 --> 41 --> 42 --> 43 --> 44 --> 45 --> 46
import java.util.Random;
public class SupermarketLine
{
public static void main(String[] args)
{
System.out.println("Supermarket Simulation");
Random random = new Random();
Queue queue = new Queue();
int arr_time = 0, dep_time = 0, id1 = 1, id2 = 1;
arr_time = random.nextInt(4) + 1;
dep_time = arr_time + random.nextInt(6) + 1;
System.out.println("Arrival Time of Customer #" + id1 + " = " + arr_time);
System.out.println("Departure Time of Customer #" + id2 + " = " + dep_time);
for (int i = 0; i <= 120; i++) {
System.out.println("The Clock = " + i + " seconds ");
if (i == arr_time) {
queue.enqueue(id1);
System.out.println("Customer " + id1 + " has arrived ");
id1++;
arr_time = i + random.nextInt(4) + 1;
System.out.println("Arrival Time of Customer#" + id1 + " = " + arr_time);
}
if (i == dep_time && !queue.isEmpty()) {
System.out.println ("Customer# " + id2 + " departs");
queue.dequeue();
id2++;
dep_time = i + random.nextInt(5) + 1;
System.out.println ("Departure Time of Customer#" + id2 + " = " + dep_time);
}
}
System.out.println("Line: " + queue.toString());
}
}
A priority queue is a queue in which elements are assigned a priority, and only dequeued according to that priority. Therefore, elements with higher priority are dequeued first. Write a class PriorityQueue
that implements this data structure.
One implementation is to use an array of Queue
structures, with the array index being the priority. Elements of priority 0 are stored in array[0]
, and so on. En- and dequeuing are slightly different from a normal queue, however.
Enqueued items are added to the end of the
Queue
located atarray[priority]
When dequeue is called, the array ofQueue
structures is traversed backwards (from highest to lowest priority), and the first non-emptyQueue
has an element dequeued.
Make life easy, and use 0-9 as your priorities.