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

In the programming language of your choice, simulate the experience of a day\'s

ID: 3782842 • Letter: I

Question

In the programming language of your choice, simulate the experience of a day's business at a bank. Your implementation must make use of a min heap, and must fit the following criterion: At program launch, it will query the user for the number of tellers working that day. There must be at least four tellers, and no more than ten tellers. If a number is passed in outside of those bounds, have your program round up to four or down to ten as appropriate for the input. Your tellers will be represented by the min-heap of a size equal to the number of tellers. To start mo simulation generate 100-200 customers randomly, and place them in a queue. Each customer will have a total service time that it will take to conclude their business. Dequeue customers from the queue into your min-heap until it is full. The customer service time is the metric which will be used to balance the heap. Pop the top customer off of your heap, and decrement the service time for all customers remaining in the heap. If a customer which is still in the heap has their service time reach 0 in this process, remove that customer from the heap. Populate the heap back to maximum capacity through dequeueing from your queue Repeat steps 4 and 5 until both the heap and queue are empty Every 20 customers that pass out of the queue, print the current status of the queue (how many customers are left), and print the current composition of the heap.

Explanation / Answer

package com.simulation.bankteller;

import java.util.*;
import javax.swing.JOptionPane;

public class SimulateBank {
  
int noOfTellers;
int noOfCustomers;
GenQueue<Customer> cusQueue;
Heap<Customer> heapCustomers;
  
public SimulateBank(){
noOfTellers = 4;
noOfCustomers = 100;
cusQueue = new GenQueue<Customer>();
heapCustomers = new Heap<Customer>();
}
public SimulateBank(int noOfTellers, int noOfCustomers){
this.noOfTellers = noOfTellers;
this.noOfCustomers = noOfCustomers;
cusQueue = new GenQueue<Customer>();
heapCustomers = new Heap<Customer>();
}
  
public void addCustomersToQueue(){
for(int i = 0; i < noOfCustomers; i++){
Customer ci = new Customer(i+1);
//service time range from 1 minute to 10 minutes
int serviceTime = 1 + (int)(Math.random()*10);
ci.setServiceTime(serviceTime);
cusQueue.enqueue(ci);
}
  
}
  
public void reduceServiceTime(int minServiceTime){
int size = heapCustomers.size;
for(int i = 0; i < size; i++){
Customer customerInQueue = (Customer) heapCustomers.heap[i];
customerInQueue.setServiceTime(customerInQueue.getServiceTime() - minServiceTime);
}
}
  
public void displayCustomersInQueue(){
LinkedList <Customer> customerList = cusQueue.list;
System.out.println("Customers in Queue:");
for (Customer customer : customerList) {
System.out.print("Customer id: " + customer.getSerialNo());
System.out.println("ServiceTime:" + customer.getServiceTime());
}
System.out.println("------------------------------------------");
}
  
public void displayCustomersInHeap(){
int size = heapCustomers.size;
System.out.println("Customers before Tellers:");
for(int i = 0; i < size; i++){
Customer customerInHeap = heapCustomers.heap[i];
System.out.print("Customer id: " + customerInHeap.getSerialNo());
System.out.println("ServiceTime:" + customerInHeap.getServiceTime());
}
System.out.println("------------------------------------------");
}
  
public void simulateOperations(){
int customersProcessed = 0;
while(cusQueue.hasItems()){
for(int i = 0; i < noOfTellers; i++){
Customer nextCustomer = cusQueue.dequeue();
heapCustomers.insert(nextCustomer);
}
Customer topCustomer = heapCustomers.deleteMin();
int minServiceTime = topCustomer.getServiceTime();
reduceServiceTime(minServiceTime);
customersProcessed++;
if(customersProcessed%20 == 0){
displayCustomersInQueue();
displayCustomersInHeap();
}
}
  
}
  
public static void main(String args[]){
int noTellers, noCustomers;
String tellersNo, customersNo;
tellersNo = JOptionPane.showInputDialog("Enter the number of tellers as an integer(4 to 10)");
customersNo = JOptionPane.showInputDialog("Enter the number of customers as an integer (100 to 200)");
try{
noTellers = Integer.parseInt(tellersNo);
if(noTellers < 4){
noTellers = 4;
}
if(noTellers > 10){
noTellers = 10;
}
}
catch(Exception e){
noTellers = 4;
}
try{
noCustomers = Integer.parseInt(customersNo);
if(noCustomers < 100){
noCustomers = 100;
}
if(noCustomers > 200){
noCustomers = 200;
}
}
catch(Exception e){
noCustomers = 100;
}
  
  
SimulateBank simBank = new SimulateBank(noTellers, noCustomers);
simBank.addCustomersToQueue();
simBank.simulateOperations();
}
}

class Customer implements Comparable<Customer>{

private int serialNo;
private int serviceTime;
  
Customer(int slNo){
this.serialNo = slNo;
}
  
Customer(int slNo, int serviceTime){
this.serialNo = slNo;
this.serviceTime = serviceTime;
}
  
public int getSerialNo(){
return serialNo;
}
  
public void setServiceTime(int serviceTime){
this.serviceTime = serviceTime;
}
  
public int getServiceTime(){
return serviceTime;
}
  
@Override
public int compareTo(Customer o) {
if(serviceTime == o.serviceTime)
return 0;
else if(serviceTime > o.serviceTime)
return 1;
else
return -1;
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
  
}

@SuppressWarnings("unchecked")
class Heap<AnyType extends Comparable<AnyType>>
{
private static final int CAPACITY = 2;

public int size; // Number of elements in heap
public AnyType[] heap; // The heap array

public Heap()
{
size = 0;
heap = (AnyType[]) new Comparable[CAPACITY];
}

/**
* Construct the binary heap given an array of items.
*/
public Heap(AnyType[] array)
{
size = array.length;
heap = (AnyType[]) new Comparable[array.length+1];

System.arraycopy(array, 0, heap, 1, array.length);//we do not use 0 index

buildHeap();
}
/**
* runs at O(size)
*/
private void buildHeap()
{
for (int k = size/2; k > 0; k--)
{
percolatingDown(k);
}
}
private void percolatingDown(int k)
{
AnyType tmp = heap[k];
int child;

for(; 2*k <= size; k = child)
{
child = 2*k;

if(child != size &&
heap[child].compareTo(heap[child + 1]) > 0) child++;

if(tmp.compareTo(heap[child]) > 0) heap[k] = heap[child];
else
break;
}
heap[k] = tmp;
}

/**
* Sorts a given array of items.
*/
public void heapSort(AnyType[] array)
{
size = array.length;
heap = (AnyType[]) new Comparable[size+1];
System.arraycopy(array, 0, heap, 1, size);
buildHeap();

for (int i = size; i > 0; i--)
{
AnyType tmp = heap[i]; //move top item to the end of the heap array
heap[i] = heap[1];
heap[1] = tmp;
size--;
percolatingDown(1);
}
for(int k = 0; k < heap.length-1; k++)
array[k] = heap[heap.length - 1 - k];
}

/**
* Deletes the top item
*/
public AnyType deleteMin() throws RuntimeException
{
if (size == 0) throw new RuntimeException();
AnyType min = heap[1];
heap[1] = heap[size--];
percolatingDown(1);
return min;
   }

/**
* Inserts a new item
*/
public void insert(AnyType x)
{
if(size == heap.length - 1) doubleSize();

//Insert a new item to the end of the array
int pos = ++size;

//Percolate up
for(; pos > 1 && x.compareTo(heap[pos/2]) < 0; pos = pos/2 )
heap[pos] = heap[pos/2];

heap[pos] = x;
}
private void doubleSize()
{
AnyType [] old = heap;
heap = (AnyType []) new Comparable[heap.length * 2];
System.arraycopy(old, 1, heap, 1, size);
}

public String toString()
{
String out = "";
for(int k = 1; k <= size; k++) out += heap[k]+" ";
return out;
}

/* public static void main(String[] args)
{
Heap<String> h = new Heap<String>();

h.insert("p");
h.insert("r");
h.insert("i");
h.insert("o");
System.out.println(h);
h.deleteMin();
System.out.println(h);


Heap<Integer> tmp = new Heap<Integer>();
Integer[] a = {4,7,7,7,5,0,2,3,5,1};
tmp.heapSort(a);
System.out.println(Arrays.toString(a));
}*/
}

class GenQueue<C> {
public LinkedList<C> list = new LinkedList<C>();
public void enqueue(C item) {
list.addLast(item);
}
public C dequeue() {
return list.poll();
}
public boolean hasItems() {
return !list.isEmpty();
}
public int size() {
return list.size();
}
public void addItems(GenQueue<? extends C> q) {
while (q.hasItems()) list.addLast(q.dequeue());
}
}

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