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

An updated PriorityCustomer class is provided for you. You must use that class,

ID: 3568244 • Letter: A

Question

An updated PriorityCustomer class is provided for you. You must use that class, without alternations, for the creation of your PriorityCustomer 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 PriorityQueue class that will represent the data structure for holding your Customer objects. In your PriorityQueue class, you will create the actual heap that will act as the priority queue. You must use an array representation of a heap. No ArrayLists or linked structures! The second class you will need to create is a driver where your store simulation will take place.
Customers with a priority value higher than other existing customers should be placed in front of them. This is simulated by utilizing a Max Heap to implement your priority queue. The only exception to this is for the customer in the front of the line (the one currently being serviced). If a new customer is added to the line with a higher priority than the front customer, the new customer should not be put in front of the customer being serviced

Explanation / Answer

Customer.java

import java.util.Random;
public class Customer {
private int serviceTime; // ServiceTime for this Customer
/// Contructor
public Customer() {
serviceTime = new Random().nextInt(5)+1; // Randomly assign required service time 1-5
}
/// Getter for ServiceTime
public int getServiceTime() {
return serviceTime;
}
/// Decrement ServiceTime by 1
public void decServiceTime() {
serviceTime--;
}
}

CustomerQueue.java

public class CustomerQueue
{
private Node head;
private int length;
private Node tail;
public CustomerQueue()
{
head = tail = null;
length = 0;
}
public void add(Customer c)
{
if (length == 0)
{
head = new Node(c);
tail = head;
}
else
{
tail.setNext(new Node(c));
tail = tail.getNext();
}
}
public int size()
{
return length;
}
public void remove ()
{
head = head.getNext();
}
public Customer get()
{
return head.getData();
}
}

Driver.java

import java.util.*;
public class Driver
{
public static void main (String[] args)
{
CustomerQueue line = new CustomerQueue();
// Chance that a new customer arrives.
double newCustomer;
// Total number customer served.
int total = 0;
// Max number length.
int max = 0;
// Start the hour of work.
System.out.println("Hour of working has began. ");
for (int m = 0; m < 60; m++)
{
// If there customer in line subtract one minute from their service time.
if(line.size() > 0)
{
line.get().decServiceTime();
//System.out.println("Current customer time is " + line.get().getServiceTime());
if(line.get().getServiceTime() == 0)
{
line.remove();
System.out.println("Customer Served. Line length is " + line.size());
total++;
}
}
// Check to see if customer should be added.
newCustomer = Math.random();
if (newCustomer <= 0.25)
{
Customer c = new Customer();
line.add(c);
System.out.println("New customer arrives. The service time is " + c.getServiceTime() + ". Length is " + line.size());
// If the new line length is maximum, charge max
if(line.size() > max) max = line.size();
}
System.out.println("Time: " + (m+1) + "----------------");
}// End for loop
// Print total number of customers served and max
System.out.println("The number of customers served is: " + total);
System.out.println("The maximum line length is " + max);
}
}

Node.java

public class Node
{
private Customer data;
private Node next;
public Node (Customer c)
{
data = c;
next = null;
}
public Customer getData()
{
return this.data;
}
public void setNext(Node n)
{
this.next = n;
}
public Node getNext()
{
return this.next;
}
}

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