-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBankDES.cpp
More file actions
65 lines (58 loc) · 2.05 KB
/
Copy pathBankDES.cpp
File metadata and controls
65 lines (58 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// Created by Tristan Mayfield on 2019-03-31.
//
#include "BankDES.h"
// add a priority queue event
void BankDES::addEvent(EventStruct &event) {
eventQueue.push(event);
}
// add a bank queue customer (not really an event)
void BankDES::addBankQueueCustomer(EventStruct &event) {
bankQueue.push(event);
}
void BankDES::onArrival(EventStruct nextEvent) {
if (tellersAvailable) {
// enter departure event in eventQueue
nextEvent.eventTime = currentTime + nextEvent.duration;
// nextEvent.totalServiceTime = (nextEvent.eventTime - nextEvent.arrivalTime) / 60; // divide by 60 to get minutes
serviceTimes.push_back((nextEvent.eventTime - nextEvent.arrivalTime) / 60);
nextEvent.event = DEPARTURE;
addEvent(nextEvent);
tellersAvailable--;
} else {
// no tellers available, put customer in bank queue
addBankQueueCustomer(nextEvent);
}
}
void BankDES::onDeparture(EventStruct nextEvent) {
if (!bankQueue.empty()) {
EventStruct nextCustomer = bankQueue.front(); // getting the next customer from the bank queue
bankQueue.pop();
nextCustomer.eventTime = currentTime + nextEvent.duration;
// calculate total service time, including waits
serviceTimes.push_back((nextEvent.eventTime - nextEvent.arrivalTime) / 60);
nextCustomer.event = DEPARTURE;
addEvent(nextCustomer);
} else {
tellersAvailable++;
}
}
// run the simulation
void BankDES::runSim() {
while (!eventQueue.empty()) {
EventStruct nextEvent = eventQueue.top(); // get next event in priority queue
currentTime = nextEvent.eventTime; // updating the time
eventQueue.pop();
switch (nextEvent.event) {
case ARRIVAL:
onArrival(nextEvent);
break;
case DEPARTURE:
onDeparture(nextEvent);
break;
default:
cout << "ERROR: Should never get here! " << endl;
}
}
sort(serviceTimes.begin(), serviceTimes.end());
}