Question: Java Grocery Store Queue Simulation Write a program that simulates customers waiting in line at a grocery store. Your program must use a Queue to

Java Grocery Store Queue Simulation Write a program that simulates customers waiting in line at a grocery store. Your program must use a Queue to represent the customer objects waiting in line. A Customer class is provided below to be used with this program. You must use that class, without alterations, for the creation of your Customer objects. You must analyze the class and use the provided methods to achieve the desired functionality of the program. The program should simulate 60 minutes of activity at the store. Each iteration of your program should represent one minute. I'm having trouble figuring out how to dequeue a customer when there service time is 0.

Note: Successfully implement your own Linked Queue (not Javas) that holds Customer objects.

Given Customer Class:

import java.util.Random;

public class Customer {

private int serviceTime; // ServiceTime for this Customer

private Customer next;

/// Constructor

public Customer() {

serviceTime = new Random().nextInt(5) + 1; // Randomly assign required service time 1-5

next = null;

}

// Getter for next Customer in list

public Customer getNext(){

return next;

}

// Setter for next reference

public void setNext(Customer c){

next = c;

}

/// Getter for ServiceTime

public int getServiceTime() {

return serviceTime;

}

/// Decrement ServiceTime by 1

public void decServiceTime() {

serviceTime--;

}

}

My LinkedQueue Class:

public class LinkedQueue {

private Customer first, last;

int TotalNumberOfCustomers=0;

int currentSize=0;

public LinkedQueue () {

first = last = null;

}

public boolean isEmpty () {

return first == null;

}

public void enqueue (Customer c) {

//Adds Customer c to the back of the queue

//if the queue is empty, first should reference the new object

if ( isEmpty() ) {

first = c;

} else {

//before we change last to reference the new object,

//set the current last object's next reference to point

//to the new object

last.setNext (c);

}

//last should always reference the new object

last = c;

TotalNumberOfCustomers++;

currentSize++;

}

public Customer dequeue () {

//Removes and return the first Customer in the queue

if ( isEmpty() ) {

return null;

}

//Store a temp reference to the object we want to remove

Customer temp = first;

//Set first to reference the current first object's next reference

//(which is the current second object in the list)

first = first.getNext ();

//if the queue is now empty, set last to null

if ( isEmpty () ) {

last = null;

}

currentSize--;

return temp;

}

public int getTotalNumberOfCustomers() {

return TotalNumberOfCustomers;

}

public int getCurrentSize() {

return currentSize;

}

}

My Driver Class:

import java.util.Random;

public class Driver {

public static void main(String[] args) {

// Create a linked queue

LinkedQueue line = new LinkedQueue();

// Create a customer object

Customer myCustomer = new Customer();

Random randomNumber = new Random();

int customerWait;

int maxLength = 0;

for (int i = 0; i < 60; i++) {

int num = randomNumber.nextInt(4) + 1;

if (num == 1) {

//Add a new customer to the queue if newCustomer equals 1

line.enqueue(new Customer());

//Display a message to the user that a new customer has been added

System.out.println("New Customer added! Queue length is: " + line.getCurrentSize());

if (maxLength < line.getCurrentSize()) {

maxLength = line.getCurrentSize();

}

}

if (myCustomer != null) {

//Decrease the service time if the customer isn't null

myCustomer.decServiceTime();

if (myCustomer.getServiceTime() == 0) {

//Dequeue the line if the service time is 0

line.dequeue();

//Display a message to the user that a customer was serviced and removed

System.out.println("Customer serviced and removed form the queue. Queue length is now: "

+ line.getCurrentSize());

}

}

System.out.println("-------------------------------------");

}

System.out.println("The total number of customers is: " + line.getTotalNumberOfCustomers());

System.out.println("The total Maximum number of customers in the queue is:" + maxLength);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!