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

Write a program that simulates waiting in line at the store. You must use a queu

ID: 3564345 • Letter: W

Question

Write a program that simulates waiting in line at the store. You must use a queue to represent the customer objects waiting in line. The customer class is provided and you must use this class to make the program functional. Along with the customer class you will need to create two new classes. The first class will be the linked list with the customer objects and the second class will be the driver where execution takes place.

The driver should simulate 60 minutes of activity at the store. Each iteration of the program should represent one minute. At each iteration or minute the program should do the following:

The program should output this information:

The customer class:

Explanation / Answer


//The java program that simulates the servicing the customers
//in a queue for particular period of time that is randomely
//allocated from Customer class for period of 1 hour
//Compile : javac Simulator.java
//Run: java Simulator
//Simulator.java
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
class Simulator
{
   public static void main(String[] args)
   {
       Random rand = new Random();
       int inQueue = 0, serviceCompleted = 0;

       //Create a store as a Queue specifying that it will hold customer objects
       Queue<Customer> store = new LinkedList<Customer>();
      

       System.out.println("Simulation of Adding and Removing Customers");
       System.out.println("From queue for 1 hour");
       System.out.println("------------------------");

       //Loops runs sixty time to simulate 60 minutes
       for (int minute = 1; minute <= 60; minute++)
       {          
           //Simulate adding new customers in the store with a 25% chance
           //generate number between [1,4]
           if ((rand.nextInt(4)+1) == 4)
           {
               Customer customer=new Customer();
               store.add(customer);
               inQueue += 1; //increment global variable, update how many customers entered store
               //notify that a new customer joined the line
               System.out.println("New customer added.Queue size : " + store.size());
           }

           //check whether the store has customer to serve or not
          
           if (!store.isEmpty())
           {
               //get the head of the customer from queue ,store
               Customer serviceCustomer=store.peek();
               //service the customer 1minute from allocated 5 minutes
               serviceCustomer.decServiceTime();

               if (serviceCustomer.getServiceTime() <= 0)
               {
                   store.remove(); //remove customer from store
                   serviceCompleted += 1; //
                   System.out.println("Customer sevice completed & removed! Queue size :" + store.size());
               }
           }
       System.out.println("--------");
       }
      
      
       System.out.println("Total customers come in queue : " + inQueue );
       System.out.println("customers serviced in Queue :"+ serviceCompleted);  
   }//end main
}//end class

-----------------------------------------------------------------------------------------------------------------------

//Customer.java
import java.util.Random;
class Customer
{
  
   private int serviceTime;               
  
   // Set random time to customer to serve in queueline
   public Customer()
   {                                      
       //generate a random number between 1 and 5 and
       //assign to servicetime.
       serviceTime = new Random().nextInt(5)+1;
   }
   // Returns serviceTime
   public int getServiceTime()
   {                          
       return serviceTime;
   }
  
   // Decrement ServiceTime by 1
   public void decServiceTime()
   {      
       serviceTime--;
   }
}

---------------------------------------------------------------------------------------------------------------------

Sample O/p:

Simulation of Adding and Removing Customers
From queue for 1 hour
------------------------
--------
New customer added.Queue size : 1
--------
Customer sevice completed & removed! Queue size :0
--------
--------
New customer added.Queue size : 1
--------
New customer added.Queue size : 2
--------
Customer sevice completed & removed! Queue size :1
--------
--------
New customer added.Queue size : 2
Customer sevice completed & removed! Queue size :1
--------
--------
--------
--------
New customer added.Queue size : 2
--------
Customer sevice completed & removed! Queue size :1
--------
Customer sevice completed & removed! Queue size :0
--------
--------
--------
--------
New customer added.Queue size : 1
--------
--------
New customer added.Queue size : 2
Customer sevice completed & removed! Queue size :1
--------
New customer added.Queue size : 2
--------
--------
Customer sevice completed & removed! Queue size :1
--------
New customer added.Queue size : 2
Customer sevice completed & removed! Queue size :1
--------
--------
Customer sevice completed & removed! Queue size :0
--------
New customer added.Queue size : 1
Customer sevice completed & removed! Queue size :0
--------
New customer added.Queue size : 1
Customer sevice completed & removed! Queue size :0
--------
--------
New customer added.Queue size : 1
--------
Customer sevice completed & removed! Queue size :0
--------
--------
--------
--------
--------
--------
--------
--------
--------
New customer added.Queue size : 1
Customer sevice completed & removed! Queue size :0
--------
New customer added.Queue size : 1
Customer sevice completed & removed! Queue size :0
--------
--------
--------
--------
--------
--------
New customer added.Queue size : 1
--------
--------
New customer added.Queue size : 2
Customer sevice completed & removed! Queue size :1
--------
--------
--------
Customer sevice completed & removed! Queue size :0
--------
--------
--------
--------
--------
--------
New customer added.Queue size : 1
--------
Customer sevice completed & removed! Queue size :0
--------
Total customers come in queue : 17
customers serviced in Queue :17


Hope this would be helpful.

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