Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Bank Simulator program 2.1. Assumptions - At most one customer arrives per time unit - All numbers are positive integer numbers (>=0), except average values

Bank Simulator program

2.1. Assumptions

- At most one customer arrives per time unit - All numbers are positive integer numbers (>=0), except average values should be displayed to two decimal places - No time lost in between events: a customer arriving and entering waiting line a customer arriving and leaving without banking a customer completing transaction and departing a customer leaving the waiting line, advancing to a teller and beginning transaction

/////////////////////////////////////////////////////////////////////////////////////////

package PJ3;

import java.util.*; import java.io.*;

// You may add new functions or data in this class // You may modify any functions or data members here // You must use Customer, Teller and ServiceArea classes // to implement Bank simulator

class BankSimulator {

// input parameters private int numTellers, customerQLimit; private int simulationTime, dataSource; private int chancesOfArrival, maxTransactionTime;

// statistical data private int numGoaway, numServed, totalWaitingTime;

// internal data private int customerIDCounter; // customer ID counter private ServiceArea servicearea; // service area object private Scanner dataFile; // get customer data from file private Random dataRandom; // get customer data using random function

// most recent customer arrival info, see getCustomerData() private boolean anyNewArrival; private int transactionTime;

// initialize data fields private BankSimulator() { // add statements }

private void setupParameters() { // read input parameters // setup dataFile or dataRandom // add statements }

// Refer to step 1 in doSimulation() private void getCustomerData() { // get next customer data : from file or random number generator // set anyNewArrival and transactionTime // see Readme file for more info // add statements }

private void doSimulation() { // add statements

// Initialize ServiceArea

// Time driver simulation loop for (int currentTime = 0; currentTime < simulationTime; currentTime++) {

// Step 1: any new customer enters the bank? getCustomerData();

if (anyNewArrival) {

// Step 1.1: setup customer data // Step 1.2: check customer waiting queue too long? // if it is too long, update numGoaway // else enter customer queue } else { System.out.println("\tNo new customer!"); }

// Step 2: free busy tellers that are done at currentTime, add to free cashierQ // Step 3: get free tellers to serve waiting customers at currentTime } // end simulation loop

// clean-up - close scanner }

private void printStatistics() { // add statements into this method! // print out simulation results // see the given example in project statement // you need to display all free and busy gas pumps

// need to free up all customers in queue to get extra waiting time. // need to free up all tellers in free/busy queues to get extra free & busy time. }

// *** main method to run simulation ****

public static void main(String[] args) { BankSimulator runBankSimulator=new BankSimulator(); runBankSimulator.setupParameters(); runBankSimulator.doSimulation(); runBankSimulator.printStatistics(); }

}

//////////////////////////////////////////////////////////////////////////////////////////////////

package PJ3;

class Customer { private int customerID; private int transactionTime; private int arrivalTime; private int finishTime;

// default constructor Customer() { }

// constructor to set customerID, transactionTime, arrivalTime // and compute finishTime Customer(int customerid, int transactiontime, int arrivaltime) { customerID = customerid; transactionTime = transactiontime; arrivalTime = arrivaltime; }

int getTransactionTime() { return transactionTime; }

int getArrivalTime() { return arrivalTime; }

int getFinishTime() { return finishTime; }

int getCustomerID() { return customerID; }

void setFinishTime(int finishtime) { finishTime=finishtime; }

public String toString() { return "customerID="+customerID+":transactionTime="+ transactionTime+":arrivalTime="+arrivalTime+ ":finishTime="+finishTime; }

public static void main(String[] args) { // quick check! Customer mycustomer = new Customer(1,5,18); mycustomer.setFinishTime(28); System.out.println("Customer Info:"+mycustomer);

} }

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package PJ3;

import java.util.*;

//-------------------------------------------------------------------------- // // Define simulation queues in a service area. Queues hold references to Customer // and Teller objects // // Customer (FIFO) queue is used to hold waiting customers. If the queue is too long // (i.e. > customerQLimnit), customer goes away without entering customer queue // // There are several tellers in a service area. Use PriorityQueue to // hold BUSY tellers and FIFO queue to hold FREE tellers, // i.e. a teller that is FREE for the longest time should start be used first. // // To handle teller in PriorityQueue, we need to define comparator // for comparing 2 teller objects. Here is a constructor from Java API: // // PriorityQueue(int initialCapacity, Comparator comparator) // // For priority queue, the default compare function is "natural ordering" // i.e. for numbers, minimum value is returned first // // User can define own comparator class for PriorityQueue. // For teller objects, we like to have smallest end busy interval time first. // i.e. use Teller's getEndBusyIntervalTime() // // The following class define compare() for two tellers :

class CompareTellers implements Comparator{ // overide compare() method public int compare(Teller o1, Teller o2) { return o1.getEndBusyTime() - o2.getEndBusyTime(); } }

class ServiceArea { // Private data fields: // define one priority queue private PriorityQueue busyTellerQ;

// define two FIFO queues private Queue customerQ; private Queue freeTellerQ;

// define customer queue limit private int customerQLimit;

// Constructor public ServiceArea() { // add statements }

// Constructor public ServiceArea(int numTellers, int customerQlimit) { // add additional statements

// use ArrayDeque to construct FIFO queue objects

// construct PriorityQueue object // overide compare() in Comparator to compare Teller objects busyTellerQ= new PriorityQueue( numTellers, new CompareTellers());

// initialize customerQlimit

// Construct Teller objects and insert into FreeTellerQ // assign teller ID from 1, 2,..., numTellers }

// ------------------------------------------------- // freeTellerQ methods: remove, insert, empty, size // -------------------------------------------------

public Teller removeFreeTellerQ() { // remove and return a free teller // Add statetments return null; }

public void insertFreeTellerQ(Teller teller) { // insert a free teller // Add statetments }

public boolean emptyFreeTellerQ() { // is freeTellerQ empty? // Add statetments return false; }

public int numFreeTellers() { // get number of free tellers // Add statetments return 0; }

// ------------------------------------------------------- // busyTellerQ methods: remove, insert, empty, size, peek // -------------------------------------------------------

public Teller removeBusyTellerQ() { // remove and return a busy teller // Add statetments return null; }

public void insertBusyTellerQ(Teller teller) { // insert a busy teller // Add statetments }

public boolean emptyBusyTellerQ() { // is busyTellerQ empty? return busyTellerQ.isEmpty(); }

public int numBusyTellers() { // get number of busy tellers // Add statetments return 0; }

public Teller getFrontBusyTellerQ() { // get front of busy tellers // "retrieve" but not "remove" // Add statetments return null; }

// ------------------------------------------------------- // customerQ methods: remove, insert, empty, size // and check isCustomerQTooLong() // -------------------------------------------------------

public Customer removeCustomerQ() { // remove and return a customer // Add statetments return null; }

public void insertCustomerQ(Customer customer) { // insert a customer // Add statetments }

public boolean emptyCustomerQ() { // is customerQ empty? // Add statetments return false; }

public int numWaitingCustomers() { // get number of customers // Add statetments return 0; }

public boolean isCustomerQTooLong() { // is customerQ too long? // Add statetments return false; }

public void printStatistics() { System.out.println("\t# waiting customers : "+numWaitingCustomers()); System.out.println("\t# busy tellers : "+numBusyTellers()); System.out.println("\t# free tellers : "+numFreeTellers()); }

public static void main(String[] args) {

// quick check

// create a ServiceArea and 4 customers ServiceArea sc = new ServiceArea(4, 5); Customer c1 = new Customer(1,18,10); Customer c2 = new Customer(2,33,11); Customer c3 = new Customer(3,21,12); Customer c4 = new Customer(4,37,13);

// insert customers into customerQ sc.insertCustomerQ(c1); sc.insertCustomerQ(c2); sc.insertCustomerQ(c3); sc.insertCustomerQ(c4); System.out.println(""+sc.customerQ); System.out.println("==============================================="); System.out.println("Remove customer:"+sc.removeCustomerQ()); System.out.println("Remove customer:"+sc.removeCustomerQ()); System.out.println("Remove customer:"+sc.removeCustomerQ()); System.out.println("Remove customer:"+sc.removeCustomerQ()); System.out.println("===============================================");

// remove tellers from freeTellerQ System.out.println("freeTellerQ:"+sc.freeTellerQ); System.out.println("==============================================="); Teller p1=sc.removeFreeTellerQ(); Teller p2=sc.removeFreeTellerQ(); Teller p3=sc.removeFreeTellerQ(); Teller p4=sc.removeFreeTellerQ(); System.out.println("Remove free teller:"+p1); System.out.println("Remove free teller:"+p2); System.out.println("Remove free teller:"+p3); System.out.println("Remove free teller:"+p4); System.out.println("==============================================="); System.out.println("freeTellerQ:"+sc.freeTellerQ); System.out.println("busyTellerQ:"+sc.busyTellerQ); System.out.println("===============================================");

// insert customers to tellers p1.freeToBusy (c1, 13); p2.freeToBusy (c2, 13); p3.freeToBusy (c3, 13); p4.freeToBusy (c4, 13); System.out.println("Assign customers to free tellers");

// insert tellers to busyTellerQ System.out.println("==============================================="); System.out.println("Insert tellers to busyTellerQ"); sc.insertBusyTellerQ(p1); sc.insertBusyTellerQ(p2); sc.insertBusyTellerQ(p3); sc.insertBusyTellerQ(p4); System.out.println("busyTellerQ:"+sc.busyTellerQ); System.out.println("===============================================");

// remove tellers from busyTellerQ p1=sc.removeBusyTellerQ(); p2=sc.removeBusyTellerQ(); p3=sc.removeBusyTellerQ(); p4=sc.removeBusyTellerQ();

p1.busyToFree(); p2.busyToFree(); p3.busyToFree(); p4.busyToFree();

System.out.println("Remove busy teller:"+p1); System.out.println("Remove busy teller:"+p2); System.out.println("Remove busy teller:"+p3); System.out.println("Remove busy teller:"+p4);

}

};

///////////////////////////////////////////////////////////////////////////////////////////////

package PJ3;

class Teller {

// teller id and current customer which is served by this cashier private int tellerID; private Customer customer;

// start time and end time of current free/busy interval private int startFreeTime; private int endFreeTime; private int startBusyTime; private int endBusyTime;

// for keeping statistical data private int totalFreeTime; private int totalBusyTime; private int totalCustomers;

// Constructor Teller() { this(-1); }

// Constructor with teller id Teller(int tellerId) { // add statements }

//-------------------------------- // accessor methods //--------------------------------

int getTellerID () { return tellerID; }

Customer getCustomer() { return customer; }

int getEndBusyTime() { return endBusyTime; }

//-------------------------------- // mutator methods //--------------------------------

void setCustomer(Customer newCustomer) { customer = newCustomer; }

void setStartFreeTime (int time) { startFreeTime = time; }

void setStartBusyTime (int time) { startBusyTime = time; } void setEndFreeTime (int time) { endFreeTime = time; }

void setEndBusyTime (int time) { endBusyTime = time; } void updateTotalFreeTime() { //add statements }

void updateTotalBusyTime() { //add statements }

void updateTotalCustomers() { //add statements }

//-------------------------------- // Teller State Transition methods //--------------------------------

// From free interval to busy interval void freeToBusy (Customer newCustomer, int currentTime) { // goal : start serving newCustomer at currentTime // // steps : set endFreeTime, update TotalFreeTime // set startBusyTime, endBusyTime, customer // update totalCustomers

//add statements }

// Transition from busy interval to free interval Customer busyToFree () { // goal : end serving customer at endBusyTime // // steps : update TotalBusyTime, set startFreeTime // return customer

//add statements return null; }

//-------------------------------- // Print statistical data //-------------------------------- void printStatistics () { // print teller statistics, see project statement System.out.println("\t\tTeller ID : "+tellerID); System.out.println("\t\tTotal free time : "+totalFreeTime); System.out.println("\t\tTotal busy time : "+totalBusyTime); System.out.println("\t\tTotal # of customers : "+totalCustomers); if (totalCustomers > 0) System.out.format("\t\tAverage transaction time : %.2f%n ",(totalBusyTime*1.0)/totalCustomers); }

public String toString() { return "tellerID="+tellerID+ ":startFreeTime="+startFreeTime+":endFreeTime="+endFreeTime+ ":startBusyTime="+startBusyTime+":endBusyTime="+endBusyTime+ ":totalFreeTime="+totalFreeTime+":totalBusyTime="+totalBusyTime+ ":totalCustomer="+totalCustomers+">>customer:"+customer; }

public static void main(String[] args) { // quick check Customer mycustomer = new Customer(1,5,15); Teller myteller = new Teller(5); myteller.setStartFreeTime(0); System.out.println(myteller); myteller.freeToBusy(mycustomer, 20); System.out.println(" "+myteller); myteller.busyToFree(); System.out.println(" "+myteller); System.out.println(" "); myteller.printStatistics();

}

};

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

Multidimensional Array Data Management In Databases

Authors: Florin Rusu

1st Edition

1638281483, 978-1638281481

More Books

Students also viewed these Databases questions

Question

2. Identify the purpose of your speech

Answered: 1 week ago