-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimulationApp.cpp
186 lines (146 loc) · 4.84 KB
/
SimulationApp.cpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* SimulationApp.cpp
*
* Class Description: Simulates a line up with one teller.
* Calculates the average wait time for each customer.
* Also outputs the number of customers and the time of each
* customer's arrival/departure in order.
*
* Last modified on: June 30, 2017
* Author: Jacky Tse, Vicky Lau
*/
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include "Customer.h"
#include "Queue.h"
#include "PriorityQueue.h"
using namespace std;
int main(void){
// Initializing variables
string aLine;
Customer newEvent;
Queue<Customer> *bankLine = new Queue<Customer>();
PriorityQueue<Customer> *eventPriorityQueue = new PriorityQueue<Customer>();
unsigned int time = 0;
unsigned int length = 0;
unsigned int departureTime = 0;
unsigned int currentTime = 0;
unsigned int numOfCustomers = 0;
float totalWaitTime = 0.0;
float averageWaitTime = 0.0;
bool tellerAvailable = true;
cout << "Simulation Begins" << endl;
/******************************************************
try{
cout << "Peeking before enqueuing:" << endl;
newEvent = eventPriorityQueue->peek();
((cout << "Now, let's have a look at the peeked Customer:")<< newEvent )<< endl;
}
catch(EmptyDataCollectionException&anException){
cout << "peeking() unsuccessful because " << anException.what() << endl;
// Place recovery code here!
}
******************************************************/
// Create and add arrival events to event queue
while(getline(cin >> ws, aLine)){ // while (there is data)
stringstream ss(aLine);
ss >> time >> length;
// Arrival Customer created
newEvent = Customer("A", time, length);
if (!eventPriorityQueue->enqueue(newEvent)){
return(1);
}
numOfCustomers++;
}
/**************************************************
try{
cout << "Peeking before Processing events:" << endl;
newEvent = eventPriorityQueue->peek();
((cout << "Now, let's have a look at the peeked Customer:")<< newEvent )<< endl;
}
catch(EmptyDataCollectionException&anException){
cout << "peeking() unsuccessful because " << anException.what() << endl;
// Place recovery code here!
}
**************************************************/
// Event loop
while(!eventPriorityQueue->isEmpty()){
try{
newEvent = eventPriorityQueue->peek();
}
catch(EmptyDataCollectionException&anException){
cout << "peeking() unsuccessful because " << anException.what() << endl;
}
// Calculates current time
currentTime = newEvent.getTime();
// Process an Arrival Event
if(newEvent.getType() == "A"){
cout << "Processing an arrival event at time: " << setw(2) << right
<< currentTime << endl;
if(!eventPriorityQueue->dequeue()){
return(1);
}
// Customer dooesn't line up and goes directly to teller
if(bankLine->isEmpty() && tellerAvailable){
departureTime = currentTime + newEvent.getLength();
// Departing Customer created since at the teller
newEvent = Customer("D", departureTime, 0);
// Adds a Departure Event to the Priority Queue
if(!eventPriorityQueue->enqueue(newEvent)){
return(1);
}
tellerAvailable = false;
}
// Customer lines up
else{
if(!bankLine->enqueue(newEvent)){
return(1);
}
}
}
// Process a Departure Event
else{
cout << "Processing a departure event at time: " << setw(2) << right
<< currentTime << endl;
// Dequeuing a departure event since customer is leaving
if(!eventPriorityQueue->dequeue()){
return(1);
}
// Enqueues the leaving customer into a delete queue
// since deleting here causes a segmentation fault
if(!bankLine->isEmpty()){
// Customer at front of line begins transaction
try{
newEvent = bankLine->peek();
}
catch(EmptyDataCollectionException&anException){
cout << "peeking() unsuccessful because " << anException.what() << endl;
}
totalWaitTime += (currentTime - newEvent.getTime());
if(!bankLine->dequeue()){
return(1);
}
departureTime = currentTime + newEvent.getLength();
// Next customer's departing event
newEvent = Customer("D", departureTime, 0);
if(!eventPriorityQueue->enqueue(newEvent)){
return(1);
}
}
else{
tellerAvailable = true;
}
}
}
cout << "Simulation Ends" << endl << endl;
averageWaitTime = totalWaitTime / numOfCustomers;
cout << "Final Statistics:" << endl;
cout << " Total number of people processed: " << numOfCustomers << endl;
cout << " Average amount of time spent waiting: "
<< averageWaitTime << endl;
delete bankLine;
delete eventPriorityQueue;
return(0);
}