07queue - Part03 - ATM Machine

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

1

PART 3
ATM Machine Simulation
Queue Simulation : waiting line at the ATM machine

• With equal probability, a customer spends (serviceTime):


 one minute, Generate
 two minutes , or serviceTime (1-3)
 three minutes using random
at the ATM machine. function

• Assumptions: During any minute: Generate


 no customer arrive (50% chance) (val: 0-4) #arrival (0-9)
 one customer arrives (40% chance), or (val: 5-8) using
 two customers arrive (10% chance). (val:9) random
function
Queue Simulation : waiting line at the ATM machine
3

• At the end of an hour, display the following summary


statistics:
 the number of customers served, i.e., the number who accessed
the ATM machine,
 the average time a that customer waits in line before being
served, and
 the number of customers that remain in the waiting line at the
end of the simulation.
The algorithm :
For each minute from 0 through 59 {
Determine the number of new customers arriving: 0, 1, or 2;
For each new customer
Place the new customer in the queue;
If there are customers waiting and the ATM is available {
Remove a customer from the queue;
Increment the number of customers served ;
Add to the total waiting time the waiting time of the
current customer;
Update the time the ATM is next available;
}
}
Print the summary statistics;
public void simulate() {
for (int time = 0; time < 60; time++) { // for each minute
numArrivals = getArrivals(); // how many customers arrive?
for (int i = 1; i <= numArrivals; i++) // place each arrival into the queue
queue.enqueue(new Customer(time));
if (!queue.isEmpty() && ATMisAvailable <= time) {
customer = queue.dequeue(); // remove the next customer from the line
// Determine the next time that the ATM is available:
// current time service time
ATMisAvailable = time + customer.getServiceTime();
// how long did this customer wait?
int timeCustomerWaited = time + customer.getArrivalTime();
totalWaitingTime = timeCustomerWaited; // add customer's wait to total wait
numCustomersServed++;
}
}
displayStatistics();
}

Reference: Bravaco & Simonson. 2009. Java Programming: From The Ground Up: Chapter 16
5
public class Customer {
private int id;
private int arrivalTime; // 0..60, minute of customer arrival
private int serviceTime; // 1, 2, or 3 minutes
public Customer() { // default constructor
arrivalTime = 0;
serviceTime = 0;
}
public Customer(int arrTime) { // one argument constructor
arrivalTime = arrTime;
Random rand = new Random();
serviceTime = rand.nextInt(3) + 1; // 1, 2, or 3 minutes
}
:
:
}

6
• Output Running the application three times produced the
7
following output:
Number of customers served 30
Average wait is about 5 minutes
Customers left in queue: 16

Number of customers served 29


Average wait is about 8 minutes
Customers left in queue: 13

Number of customers served 32


Average wait is about 6 minutes
Customers left in queue: 6
[0]: Arrive : 1 Queue: [#1 ]
[1]: Arrive : 0 Queue: []
[2]: Arrive : 0 Queue: []
[3]: Arrive : 0 Queue: []
[4]: Arrive : 0 Queue: []
[5]: Arrive : 1 Queue: [#2 ]
[6]: Arrive : 0 Queue: []
[7]: Arrive : 0 Queue: [] with some modification we may
[8]: Arrive : 1 Queue: [#3 ]
[9]: Arrive : 0 Queue: []
display a more verbose output
[10]: Arrive : 2 Queue: [#4 #5 ]
[11]: Arrive : 1 Queue: [#4 #5 #6 ]
[12]: Arrive : 0 Queue: [#5 #6 ]
[13]: Arrive : 1 Queue: [#5 #6 #7 ]
[14]: Arrive : 1 Queue: [#6 #7 #8 ]
[15]: Arrive : 1 Queue: [#6 #7 #8 #9 ]
[16]: Arrive : 2 Queue: [#7 #8 #9 #10 #11 ]
[17]: Arrive : 0 Queue: [#7 #8 #9 #10 #11 ]
[18]: Arrive : 2 Queue: [#7 #8 #9 #10 #11 #12 #13 ]
:
[57]: Arrive : 1 Queue: [#27 #28 #29 #30 #31 #32 #33 ]
[58]: Arrive : 0 Queue: [#28 #29 #30 #31 #32 #33 ]
[59]: Arrive : 0 Queue: [#28 #29 #30 #31 #32 #33 ]
Number of customers served 28
Average wait is about 3 minutes
Customers left in queue: 5
8
9
Queue Applications

Some applications. Simulations of the real world.


• iTunes playlist. • Guitar string.
• Data buffers (iPod, TiVo). • Traffic analysis.
• Asynchronous data transfer (file IO, • Waiting line at the ATM machine
pipes, sockets). • Determining number of cashiers to
• Dispensing requests on a shared have at a supermarket.
resource (printer, processor).
10
“ It's hard enough to find an error in your code when
you're looking for it; it's even harder when you've
assumed your code is error-free. ”

- Steve McConnel

11

You might also like