Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package lab4; Here is a sample output of what I am getting: Tick 21 *********************** New customer has arrived with the priority 2 and transaction

package lab4; Here is a sample output of what I am getting: Tick 21 *********************** New customer has arrived with the priority 2 and transaction time: 16 Tick 22 *********************** My desired output: image text in transcribed As you can see, I am appending customers to the heap and printing them to each tick, but I am unable to print the customer's departure. Please help. DRIVER CODE import javax.swing.Timer; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.*; public class BankSimulation { private static Heap customerHeap = new Heap(); static int counter = 0; static int numTellers; static int customersServed = 0; public static void main(String[] args) { Timer timer = new Timer(750, new TimerListener()); timer.start(); while(true){ } } private static class TimerListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { addCustomer(); processCustomers(); System.out.println("Tick "+counter); counter++; System.out.println("***********************"); } private void addCustomer() { if (Math.random()  0){ Customer serve = customerHeap.max(); int arrivalTime = serve.getArrivalTime(); int processTime = serve.getProcessTime(); int departureTime = arrivalTime + processTime; //calc departure if (counter == departureTime){ //check if customer needs to leave customerHeap.deleteMaximum(); numTellers++; customersServed++; System.out.println("Customer leaves, customer waited for " + serve.getWaitTime(counter) + " units of time." + " Customers serviced: " + customersServed + "... Average waiting time: " + serve.getWaitTime(counter)); } else{ break; //exit if no other customers can be served } } Iterator itr = customerHeap.iterator(); while (itr.hasNext()){ Customer temp = itr.next(); temp.incrementPriority(); } } } }

HEAP CODE

package lab4; import java.util.Iterator; public class Heap implements Iterable { private Key[] heap; private int size; public Heap(int initCapacity) { heap = (Key[]) new Object[initCapacity + 1]; size = 0; } public Heap() { this(1); } public boolean isEmpty() { return size == 0; } public Key max() { if (isEmpty()) { return null; } return heap[1]; } private void swapKeys(int i, int j) { Key exchange = heap[i]; heap[i] = heap[j]; heap[j] = exchange; } private void resize(int capacity) { assert capacity > size; Key[] temp = (Key[]) new Object[capacity]; for (int i = 1; i = heap.length - 1) resize(2 * heap.length); heap[++size] = x; swimUp(size); } public Key deleteMaximum() { if (isEmpty()) { return null; } Key max = heap[1]; swapKeys(1, size--); sinkDown(1); heap[size+1] = null; if ((size > 0) && (size == (heap.length - 1) / 4)) resize(heap.length / 2); return max; } private void swimUp(int i) { while (i > 1 && isLess(i/2, i)) { swapKeys(i, i/2); i = i/2; } } public int size() { return size; } private void sinkDown(int i) { while (2*i ) heap[i]).compareTo(heap[j])  iterator() { return new HeapIterator(); } private class HeapIterator implements Iterator { private Heap dup; public HeapIterator() { dup = new Heap(size()); for (int i = 1; i   Welcome to the Bank Sinulation! Welcome to the Bank simulation! Please enter a number of Tellers: Please enter a number of Tellers: 2 6 Tick  Tick  Tick 1 New customer has arrived with the priority 3 kkkkkkkkkkk and transaction time: 9 Tick 2 Tick 1 Tick 3 Tick 2 New customer has arrived with the priority 8 and transaction time: 8 Tick 4 Tick 3 New customer has arrived with the priority 4 and transaction time: 13 Tick 4 Tick 5 New customer has arrived with the priority 2 and transaction time: 5  Tick 5 Tick 6  New customer has arrived with the priority 6 Tick 7 Tick 6 kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk New customer has arrived with the priority 10 and transaction time: 16 1 customers waiting right now! Tick 442 Tilck 8 Tick 443 kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk Customer Leaves, customer waited for 47 units of time. Tick 1283 Tick 444 Tick 1284 Customer Leaves, customer waited for 4 units of time. *ick 1285 Customer's serviced: 200...Average waiting time: 27.44 Tick 445 Tick 1286 ************************************ Customer Leaves, customer waited for 881 units of time. Customer's serviced: 200.. Average waiting time: 834.15 Tick 1287

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Databases A Tour

Authors: Shashi Shekhar, Sanjay Chawla

1st Edition

0130174807, 978-0130174802

More Books

Students also viewed these Databases questions