You are to write a program name bank.java that simulates a bank. The program wil
ID: 3758588 • Letter: Y
Question
You are to write a program name bank.java that simulates a bank. The program will prompt the user asking if they want to run the program again. If yes, then start the program over. If no, then terminate the program.
The execution phase run for 2 minutes during which time customers will arrive randomly between 2 - 6 seconds and be placed into a queue. Each customer will have a property relating to the amount of time he/she wants to spend with a teller, which is to be randomly generated to be between 2 and 5 seconds.
There would be a maximum of 5 tellers to attend to the customers. When you start the simulation, each teller is occupied.You will need to generate a random time for each of the first 5 customers occupying the tellers at the begining of the 2 minutes simulation.
As they finish attending a customer (based upon the amount of time associated with each customer), that teller becomes available for the next customer in the queue. As a customer is removed from the queue and sent to an "available" teller, then their availability is set to "False". Customers are allocated to any one of the 5 tellers that becomes available, and so on... until the time of 2 minutes for the simulation is finished.
If after 2 minutes, there are still customers in the queue, we would discard them, but still count them in the total count of customers that visited the bank. Also add into the total count of customer the first five customers that the tellers started out with as well as to the individual teller's total.
Finally display on the screen (at the end of each execution):
The total amount of customers that visited the bank for that 2 minutes.
The total amount of customers that each teller helped.
The total amount of time that each teller was occupied.
The total amount of customers that did not get to see a teller.
Explanation / Answer
Code:
import java.io.*;
import java.util.*;
import java.util.Queue;
import java.util.LinkedList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Banksim
{
public static void main(String[] args)
{
Scanner sn = new Scanner(System.in);
while(true)
{
// REad user choice
System.out.println("Would you like to run the bank simulation? Enter "yes" or "no": ");
String choice = sn.nextLine();
choice.toLowerCase();
if (choice.equals("yes") || choice.equals("y"))
{
// Method to run the code continuously to simulate bank
Runnable bankRunnable = new Runnable()
{
// Declare the required varaibles
Random bankrand = new Random();
Queue<Integer> bankqueue = new LinkedList<Integer>();
boolean bankteller1,bankteller2,bankteller3,bankteller4,bankteller5 = false;
boolean[] banktellersArray = {bankteller1, bankteller2, bankteller3, bankteller4, bankteller5};
int[] banktellersArray2 = {(bankrand.nextInt(5 - 2) + 2),(bankrand.nextInt(5 - 2) + 2),(bankrand.nextInt(5 - 2) + 2),(bankrand.nextInt(5 - 2) + 2),(bankrand.nextInt(5 - 2) + 2)};
int bi = 0;
int ba1, bb1, bc1, bd1, be1;
int custServed1, custServed2, custServed3, custServed4, custServed5 = 0;
int occuTime1, occuTime2, occuTime3, occuTime4, occuTime5 = 0;
int bv, bw, bx, by, bz = 1;
// Method to run the code:
public void run()
{
// Code to check time is reached to two minutes
if(bi == 120)
{
//checks if 120 seconds (2 minutes) are up then prints output
System.out.println("The amount of customers that visited the bank was: " + (custServed1 + custServed2+ custServed3
+ custServed4 + custServed5 + 5 + bankqueue.size()) + " customers.");
System.out.println("The amount of customers that each teller helped was: ");
//numerous outpues as requested by assignment
System.out.println("Teller 1: " + (custServed1 + 1));
System.out.println("Teller 2: " + (custServed2 + 1));
System.out.println("Teller 3: " + (custServed3 + 1));
System.out.println("Teller 4: " + (custServed4 + 1));
System.out.println("Teller 5: " + (custServed5 + 1));
System.out.println("For ba1 total of " + (custServed1 + custServed2 + custServed3
+ custServed4 + custServed5 + 5) + " customers.");
System.out.println("The amount of time the tellers were occupied was: " + (occuTime1 + occuTime2 + occuTime3
+ occuTime4 + occuTime5) + " seconds.");
System.out.println("The amount of customers that didn't get to see ba1 teller was : " + bankqueue.size() + " customers");
System.out.println("Ending simulation...");
System.exit(0);
}
// Time is below two minutes then add customers randomly between 2-6 seconds into queue
else
{
if(bankrand.nextInt(4) < 6)
{
int btime = (bankrand.nextInt(5 - 2) + 3);
bankqueue.add(btime);
}
// Code to check each teller done service to customer
while(bv == 1)
{
ba1 = bi;
bv = 0;
}
// Code to checks the passed time greater then or equal to random processing number for customer
if ((bi - ba1) >= banktellersArray2[0])
{
banktellersArray[0] = true;
//Code to set the teller to new customer in queue and increments customersServed or occupiedtime
if (banktellersArray[0] == true && bankqueue.isEmpty() == false)
{
occuTime1 += banktellersArray2[0];
banktellersArray2[0] = bankqueue.poll();
bv = 1;
custServed1++;
}
}
// Code to check each teller done service to customer
while(bw == 1)
{
bb1 = bi;
bw = 0;
}
// Code to checks the passed time greater then or equal to random processing number for customer
if ((bi - bb1) >= banktellersArray2[1])
{
banktellersArray[1] = true;
if (banktellersArray[1] == true && bankqueue.isEmpty() == false)
{
occuTime2 += banktellersArray2[1];
banktellersArray2[1] = bankqueue.poll();
bw = 1;
custServed2++;
}
}
// Code to checks the passed time greater then or equal to random processing number for customer
while(bx == 1)
{
bc1 = bi;
bx = 0;
}
// Code to checks the passed time greater then or equal to random processing number for customer
if ((bi - bc1) >= banktellersArray2[2])
{
banktellersArray[2] = true;
if (banktellersArray[2] == true && bankqueue.isEmpty() == false)
{
occuTime3 += banktellersArray2[2];
banktellersArray2[2] = bankqueue.poll();
bx = 1;
custServed3++;
}
}
// Code to checks the passed time greater then or equal to random processing number for customer
while(by == 1)
{
bd1 = bi;
by = 0;
}
// Code to checks the passed time greater then or equal to random processing number for customer
if ((bi - bd1) >= banktellersArray2[3])
{
banktellersArray[3] = true;
if (banktellersArray[3] == true && bankqueue.isEmpty() == false)
{
occuTime4 += banktellersArray2[3];
banktellersArray2[3] = bankqueue.poll();
by = 1;
custServed4++;
}
}
// Code to checks the passed time greater then or equal to random processing number for customer
while(bz == 1)
{
be1 = bi;
bz = 0;
}
// Code to checks the passed time greater then or equal to random processing number for customer
if ((bi - be1) >= banktellersArray2[4])
{
banktellersArray[4] = true;
if (banktellersArray[4] == true && bankqueue.isEmpty() == false)
{
occuTime5 += banktellersArray2[4];
banktellersArray2[4] = bankqueue.poll();
bz = 1;
custServed5++;
}
}
bi++;
if(bankqueue.size() == 1)
{
System.out.println("There is " + bankqueue.size() + " person currently in the queue."); //each second, prints how many people are in the bankqueue
}
else
{
System.out.println("There are " + bankqueue.size() + " people currently in the queue.");
}
System.out.println("There are " + (120 - bi) + " seconds left in the simulation."); //also prints how many seconds left in simulation
System.out.println("--------");
}
}
};
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(bankRunnable, 0, 1, TimeUnit.SECONDS);
}
// Code to validate invalid input
else if (choice.equals("no") || choice.equals("n"))
{
System.out.println("Ending program...");
System.exit(0);
}
else
{
System.out.println("Incorrect input. Enter "yes" or "no": "); //check that input was correct
}
}
}
}
Result:
D:Program FilesJavajdk1.8.0_40in>java Banksim
Would you like to run the bank simulation? Enter "yes" or "no":
y
There is 1 person currently in the queue.
There are 119 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 118 seconds left in the simulation.
--------
There are 3 people currently in the queue.
There are 117 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 116 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 115 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 114 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 113 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 112 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 111 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 110 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 109 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 108 seconds left in the simulation.
--------
There are 3 people currently in the queue.
There are 107 seconds left in the simulation.
--------
There are 95 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 94 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 93 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 92 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 91 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 90 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 89 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 88 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 87 seconds left in the simulation.
--------
There are 3 people currently in the queue.
There are 86 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 85 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 84 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 83 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 82 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 81 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 80 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 79 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 78 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 77 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 76 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 75 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 74 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 73 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 72 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 71 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 70 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 69 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 68 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 67 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 66 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 65 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 64 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 63 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 62 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 61 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 60 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 59 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 58 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 57 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 56 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 55 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 54 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 53 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 52 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 51 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 50 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 49 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 48 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 47 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 46 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 45 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 44 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 43 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 42 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 41 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 40 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 39 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 38 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 37 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 36 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 35 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 34 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 33 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 32 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 31 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 30 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 29 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 28 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 27 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 26 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 25 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 24 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 23 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 22 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 21 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 20 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 19 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 18 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 17 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 16 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 15 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 14 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 13 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 12 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 11 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 10 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 9 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 8 seconds left in the simulation.
--------
There are 2 people currently in the queue.
There are 7 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 6 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 5 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 4 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 3 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 2 seconds left in the simulation.
--------
There are 0 people currently in the queue.
There are 1 seconds left in the simulation.
--------
There is 1 person currently in the queue.
There are 0 seconds left in the simulation.
--------
The amount of customers that visited the bank was: 125 customers.
The amount of customers that each teller helped was:
Teller 1: 25
Teller 2: 25
Teller 3: 24
Teller 4: 26
Teller 5: 24
For ba1 total of 124 customers.
The amount of time the tellers were occupied was: 466 seconds.
The amount of customers that didn't get to see ba1 teller was : 1 customers
Ending simulation...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.