How can I do this in JAVA Queue simulation Write a program t
How can I do this in JAVA??
Queue simulation. Write a program that simulates a checkout line in asupermarket. A queue is FIFO, so enqueue at the tail of the queue, anddequeue 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 reach the head, and then they stillremain until they are serviced – also determined by another random intervalfrom 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-hourday). Use an algorithm similar to:
a. Choose a random number between 1 and 4 to determine when thefirst customer arrives. When this time (in minutes) arrives, add acustomer to the queue (see also b below), and determine when the next random customer is to arrive.
b. At each customer’s arrival time: 1) determine the customer’s servicetime (also between 1 and 4 minutes) when reaching the head of thequeue, 2) enqueue the customer, and 3) schedule the arrival of thenext customer (between 1 and 4 minutes).
c. For each simulated minute of the day: i. When a customer arrives on the queue: 1) say so, 2) determinethe customer’s service time and display it, 3) enqueue thearrival, and 4) schedule the next customer’s arrival time, and display it. ii. When service is completed for each customer: 1) say so, 2)determine the customer’s completion time (an integer between1 and 720 – also display this time when you say so), 3)determine the customer’s actual wait time (which is the timenow minus the customer’s arrival time), and 3) dequeue thecustomer. iii. Note: no butting in line! Only dequeue at the head of the queue,even if there are customers with shorter service times behindthe head.
d. At the end of the simulation, display: 1) number of customers servicedin the simulation, 2) the maximum number of customers in the queueduring the 720-minute simulation, 3) the average actual wait time forall customers (a double), and 4) the number of customers left on thequeue when the simulation ends.
e. Place all output into a file. Implement the queue as a LinkedList:
Queue <Customer> q = new LinkedList<Customer>();
Solution
Solution:
Customer.java
//Define Customer class
public class Customer
{
int arr_time = 0;
int arr_count = 0;
public Customer(int arr_time, int arr_count)
{
this.arr_time = arr_time;
this.arr_count = arr_count;
}
}
Simulation.java
import java.util.*;
//Define Customer class
public class Simulation
{
//Define the main method
public static void main(String[] args)
{
int t = 0;
//Create object for queue
PriorityQueue<Customer> q = new LinkedList<Customer>();
int arr_count = 0;
Random generator = new Random();
int arrival_time = generator.nextInt(4) + 1;
generator = new Random();
int departure_time = arrival_time + generator.nextInt(4) + 1;
int max_wait = 0;
int max_size = 0;
int customer_departed;
int wait_time;
//Loop executes until the 720 minutes
while (t<720)
{
if (arrival_time == t)
{
arr_count ++;
q.add(new Customer(t, arr_count));
/*Display the customer arrival time */
System.out.println(\"Customer \" + arr_count + \" has arrived\");
generator = new Random();
arrival_time = generator.nextInt(4) + 1 + t;
}
if (departure_time == t)
{
if (q.size()>0)
{
customer_departed = q.peek().arr_count;
wait_time = t - q.peek().arr_time;
if (wait_time > max_wait)
{
max_wait = wait_time;
}
q.poll();
/*Display the customer service completion. */ System.out.println(\"Service was completed for customer \" + customer_departed);
generator = new Random();
departure_time = generator.nextInt(4) + t;
}
}
int queue_size = q.size();
if (max_size < queue_size)
{
max_size = queue_size;
}
t++;
}
//Display the number of customer service
System.out.println(\"Total Number of customers serviced: \" + q.peek().arr_count);
//Display the max number of customer in queue
System.out.println(\"Maximum number of customers in queue at any time: \" + max_size);
//Display the customer experience
System.out.println(\"Longest wait any one customer experienced: \" + max_wait);
}
}



