Program Assignment #1 Instructions Write a program that simulates customers wait
ID: 3869821 • Letter: P
Question
Program Assignment #1
Instructions
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 for you (download from Canvas). You must use that class, without alternations, 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. You will also need to create two additional classes. The first will be a Linked List Queue that will represent the data structure for holding your Customer objects. The second is a driver where your store simulation will take place. The program (driver) should simulate 60 minutes of activity at the store. Each iteration of your program should represent one minute. At each iteration (minute), your program should do the following:
• Check to see if new customers are added to the queue. There is a 25% chance that new customers show up (need to be added to the queue) every minute. This does not mean you should add a customer every four iterations, but rather each iteration should have its own 25% chance.
• Update the customer object currently being serviced (if one exists). This will be the customer object at the front of the queue. If the customer has been completely serviced, remove them from the queue. During execution, your program should output the following information:
• When a new customer is added to the queue, output, “New customer added! Queue length is now X” where X is the size of the queue after the new customer has been added. • When a customer has been completely serviced, output, “Customer serviced and removed from the queue. Queue length is now X” where X is the size of the queue after the customer has been removed.
• At the end of each iteration (minute), output, “---------------------------------------------------“ to visually identify the passing of time. When your simulation ends, your program should also output the following information:
• Total number of customers serviced
• Maximum line length during the simulation
PLEASE USE 3 CLASSES! IT IS A REQUIREMENT!
USE THE CUSTOMER CLASS PROVIDED AND DON'T CHANGE IT. YOU MUST USE IT THE WAY IT IS.
THEN CREATE A LINKED QUEUE CLASS AND A GROCERY STORE CLASS.
THANK YOU!
CUSTOMER CLASS (BELOW)
import java.util.Random;
public class Customer {
private int serviceTime;
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;
}
public void setNext(Customer c)
{
next = c;
}
// Getter for ServiceTime
public int getServiceTime()
{
return serviceTime;
}
//Decrement ServiceTime by 1
public void decServiceTime()
{
serviceTime--;
}
}
Explanation / Answer
Customer.java:
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--;
}
}
LinkedQueue.java:
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 Customer atFront() {
if (isEmpty()) {
return null;
}
return first;
}
public int getTotalNumberOfCustomers() {
return TotalNumberOfCustomers;
}
public int getCurrentSize() {
return currentSize;
}
}
Driver.java:
package oct02;
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 = null;
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) {
myCustomer = line.atFront();
System.out.println("Picking new customer: ");
}
}
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());
myCustomer = null;
if(line.getCurrentSize() > 0) {
myCustomer = line.atFront();
System.out.println("Picking new customer: ");
}
}
}
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);
}
}
Output:
New Customer added! Queue length is: 1
Picking new customer:
-------------------------------------
-------------------------------------
Customer serviced and removed form the queue. Queue length is now: 0
-------------------------------------
-------------------------------------
-------------------------------------
New Customer added! Queue length is: 1
Picking new customer:
-------------------------------------
-------------------------------------
-------------------------------------
Customer serviced and removed form the queue. Queue length is now: 0
-------------------------------------
-------------------------------------
New Customer added! Queue length is: 1
Picking new customer:
-------------------------------------
Customer serviced and removed form the queue. Queue length is now: 0
-------------------------------------
-------------------------------------
-------------------------------------
-------------------------------------
-------------------------------------
New Customer added! Queue length is: 1
Picking new customer:
-------------------------------------
-------------------------------------
-------------------------------------
New Customer added! Queue length is: 2
-------------------------------------
New Customer added! Queue length is: 3
Customer serviced and removed form the queue. Queue length is now: 2
Picking new customer:
-------------------------------------
-------------------------------------
-------------------------------------
Customer serviced and removed form the queue. Queue length is now: 1
Picking new customer:
-------------------------------------
New Customer added! Queue length is: 2
Customer serviced and removed form the queue. Queue length is now: 1
Picking new customer:
-------------------------------------
-------------------------------------
-------------------------------------
Customer serviced and removed form the queue. Queue length is now: 0
-------------------------------------
-------------------------------------
-------------------------------------
-------------------------------------
-------------------------------------
-------------------------------------
-------------------------------------
-------------------------------------
New Customer added! Queue length is: 1
Picking new customer:
-------------------------------------
New Customer added! Queue length is: 2
-------------------------------------
New Customer added! Queue length is: 3
-------------------------------------
Customer serviced and removed form the queue. Queue length is now: 2
Picking new customer:
-------------------------------------
New Customer added! Queue length is: 3
-------------------------------------
Customer serviced and removed form the queue. Queue length is now: 2
Picking new customer:
-------------------------------------
-------------------------------------
Customer serviced and removed form the queue. Queue length is now: 1
Picking new customer:
-------------------------------------
Customer serviced and removed form the queue. Queue length is now: 0
-------------------------------------
-------------------------------------
-------------------------------------
-------------------------------------
-------------------------------------
-------------------------------------
-------------------------------------
-------------------------------------
New Customer added! Queue length is: 1
Picking new customer:
-------------------------------------
Customer serviced and removed form the queue. Queue length is now: 0
-------------------------------------
-------------------------------------
-------------------------------------
New Customer added! Queue length is: 1
Picking new customer:
-------------------------------------
-------------------------------------
Customer serviced and removed form the queue. Queue length is now: 0
-------------------------------------
-------------------------------------
-------------------------------------
The total number of customers is: 13
The total Maximum number of customers in the queue is:3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.