-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulationApp.cpp
More file actions
110 lines (91 loc) · 3.34 KB
/
SimulationApp.cpp
File metadata and controls
110 lines (91 loc) · 3.34 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* SimulationApp.cpp
*
* Description: Handles simulation of events
* Last Modified: July 2017
* Author: Brendin Green
*/
#include <iostream>
#include "Queue.h"
#include "PriorityQueue.h"
#include "Event.h"
using namespace std;
#include <sstream>
#include <iomanip>
int sum = 0;
void processArrival(Event& arrivalEvent, PriorityQueue<Event>& eventPriorityQueue, Queue<Event>& bankLine, int & currentTime, bool & tellerAvailable){
cout << "Processing an arrival event at time:" << setw(5) << right << currentTime << endl;
eventPriorityQueue.dequeue();
int departureTime;
if (bankLine.isEmpty() && tellerAvailable){
departureTime = currentTime + arrivalEvent.getLength();
Event departureEvent = Event(departureTime);
eventPriorityQueue.enqueue(departureEvent);
tellerAvailable = false;
} else {
bankLine.enqueue(arrivalEvent);
}
}
void processDeparture(Event& departureEvent, PriorityQueue<Event>& eventPriorityQueue, Queue<Event>& bankLine, int & currentTime, bool & tellerAvailable){
cout << "Processing a departure event at time:" << setw(4) << right << currentTime << endl;
eventPriorityQueue.dequeue();
int departureTime;
if (!bankLine.isEmpty()){
try {
Event customer = bankLine.peek();
sum = sum + currentTime - customer.getTime();
bankLine.dequeue();
departureTime = currentTime + customer.getLength();
Event newDepartureEvent = Event(departureTime);
eventPriorityQueue.enqueue(newDepartureEvent);
} catch (EmptyDataCollectionException& exception){
cout << exception.what() << endl;
}
} else {
tellerAvailable = true;
}
}
int main(int argc, char* argv[]){
cout << "Simulation Begins" << endl;
int currentTime = 0;
Queue<Event> bankLine = Queue<Event>();
PriorityQueue<Event> eventPriorityQueue = PriorityQueue<Event>();
bool tellerAvailable = true;
double customerCount = 0.0;
string aLine = "";
int length = 0;
int time = 0;
Event streamEvent;
while(getline(cin >> ws, aLine)) { // while (there is data)
stringstream ss(aLine);
ss >> time >> length;
streamEvent = Event();
streamEvent.setLength(length);
streamEvent.setTime(time);
if (!eventPriorityQueue.enqueue(streamEvent)){
return 1;
}
}
customerCount = eventPriorityQueue.getElementCount();
// Event Loop
while (!eventPriorityQueue.isEmpty()) {
try {
Event newEvent = eventPriorityQueue.peek();
currentTime = newEvent.getTime();
if (newEvent.getType() == Event::ARRIVAL){
processArrival(newEvent, eventPriorityQueue, bankLine, currentTime, tellerAvailable);
} else {
processDeparture(newEvent, eventPriorityQueue, bankLine, currentTime, tellerAvailable);
}
} catch (EmptyDataCollectionException & exception){
cout << exception.what() << endl;
}
}
// Print statistics
cout << "Simulation Ends" << endl;
cout << endl;
cout << "Final Statistics: " << endl;
cout << " Total number of people processed: " << customerCount << endl;
cout << " Average amount of time spent waiting: " << (float)sum/(float)customerCount << endl;
return 0;
}