-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.cpp
543 lines (432 loc) · 12.9 KB
/
Controller.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/****************************************************************
FILE: Controller.cpp
AUTHOR: Tommy Deng
s199762338
PURPOSE: CPP file for the controller (airline) object
(Controller.h). Controls/handles the interaction
of most objects and other program functionalities.
****************************************************************/
#include "Controller.h"
#include <algorithm>
// used with sorting algo., compares values and returns if the first one is lesser than the second
bool cmd(Passenger p1, Passenger p2);
Controller::Controller()
{
numPlanes = 3;
run = true;
// initialize planes
for (int i = 0; i < numPlanes; i++)
{
plane->push_back(new Plane(i, "CITY, COUNTRY"));
}
// initialize message
message = Message();
// note: these appear to do the same thing
//cout << (*plane)[0]->getDestination() << endl;
//cout << (*(*plane)[0]).getDestination() << endl;
}
Controller::~Controller()
{
}
bool Controller::getRun()
{
return run;
}
void Controller::setRun(bool run)
{
this->run = run;
}
void Controller::retrieveData()
{
file.readPassengers(plane);
file.readFlightSchedule(plane);
file.readDestination(plane);
}
void Controller::display()
{
cout << message.menu();
string input;
cin >> input;
cout << endl;
OptionTree(input);
}
void Controller::OptionTree(string input)
{
// clear screen
system("cls");
if (input.size() == 1)
{
switch (input[0])
{
case '1':
bookReservation();
break;
case '2':
cancelReservation();
break;
case '3':
displayPassengerInfo();
break;
case '4':
displaySchedule();
break;
case '5':
displayDestinations();
break;
case '6':
displayReservations();
break;
case '7':
searchPassengerInfo();
break;
case '8':
cancelEntireFlight();
break;
case '9':
exitProgram();
break;
default:
cout << message.invalid() << endl;
break;
}
}
else
{
cout << message.invalid() << endl;
}
system("pause");
system("cls");
}
void Controller::bookReservation()
{
// display reservation availability when booking
displayReservations();
// get flight id input and checks validity
int flightId = input.flightId(numPlanes);
// get seat id input and checks validity
int seatId = input.seatId(plane, flightId);
if ((*plane)[flightId]->getSeat(seatId).getReserved())
{
cout << "The seat is unavailable. Consider cancelling it first." << endl;
}
else
{
string firstName, lastName;
string streetNum, streetName, streetType;
string phone;
string classType;
string from, toCity, toCountry;
string drink, meal;
cout << "First and last name: ";
cin >> firstName >> lastName;
cout << "Address (format: ### Address St.): ";
cin >> streetNum >> streetName >> streetType;
cout << "Phone number: ";
cin >> phone;
// get pass type input and checks validity
cout << "Boarding pass type, E or B (economy or business): ";
classType = input.boardingPass();
from = "Ottawa"; // determined by airline location
cout << endl;
cout << "The destination for this flight is " << (*plane)[flightId]->getDestinationCity()
<< ", " << (*plane)[flightId]->getDestinationCountry() << endl;
cout << "See the flight schedule and destination board for more detail" << endl;
toCity = (*plane)[flightId]->getDestinationCity();
toCountry = (*plane)[flightId]->getDestinationCountry();
if (classType == "B")
{
drink = input.drink();
meal = input.meal();
}
else
{
drink = "_";
meal = "_";
}
// combining strings
stringstream ssStreet;
ssStreet << streetNum << " " << streetName << " " << streetType;
// assign information to objects
input.setReservation(plane, flightId, seatId, firstName, lastName, ssStreet.str(), phone, classType, from, toCity, toCountry, drink, meal);
// write to file as entries are made (appends entry to end)
file.writePassengers(plane, flightId, seatId, true);
// actively updates the file
// it's not really necessary to do this (doesn't impact anything and file already updates as program is closing)
file.updateFile(plane, numPlanes);
}
cout << endl;
}
void Controller::cancelReservation()
{
// display reservation availability when canceling
displayReservations();
cout << "Cancel a reservation." << endl;
int flightId = input.flightId(numPlanes);
int seatId = input.seatId(plane, flightId);
cout << endl;
if ((*(*plane)[flightId]).getSeat(seatId).getReserved() == true)
{
cout << "Cancelling " << (*plane)[flightId]->getSeat(seatId).getPassenger().getFirstName() << "'s flight" << endl;
(*plane)[flightId]->getSeat(seatId).setPassenger("_____", "_____", "__________", "__________");
(*plane)[flightId]->getSeat(seatId).setReserved(false);
cout << "Seat " << seatId << " of plane " << flightId << " is now available" << endl;
// actively updates the file
// it's not really necessary to do this (doesn't impact anything and file already updates as program is closing)
file.updateFile(plane, numPlanes);
}
else
{
cout << "The seat was already not reserved" << endl;
}
cout << endl;
}
void Controller::displayPassengerInfo()
{
// copy reserved passengers to passenger vector
vector<Passenger> pCopy;
initPassengerAll(pCopy, plane);
cout << "Manifest of all passengers of all flights:" << endl << endl;
cout << "Flight and seat number order" << endl;
outputManifest(pCopy);
cout << endl << endl;
// sort algorithm from library
sort(pCopy.begin(), pCopy.end(), cmd);
cout << "Alphabetical order (Lastname, Firstname)" << endl;
outputManifest(pCopy);
cout << endl << endl;
cout << "Enter a flight (0 - " << numPlanes - 1 << ") to display the manifest for a particular flight." << endl;
int flightId = input.flightId(numPlanes);
// clear vector copy and replace with a single flight
pCopy.clear();
initPassengerOne(pCopy, plane, flightId);
cout << "Manifest of passengers of flight " << flightId << ":" << endl << endl;
cout << "Seat number order" << endl;
outputManifest(pCopy);
cout << endl << endl;
// sort algorithm from library
sort(pCopy.begin(), pCopy.end(), cmd);
cout << "Alphabetical order (Lastname, Firstname)" << endl;
outputManifest(pCopy);
cout << endl;
}
void Controller::displaySchedule()
{
cout << "Plane\tDepart Time\tArrive Time\tDuration" << endl;
for (int i = 0; i < numPlanes; i++)
{
cout << i << "\t" << (*plane)[i]->getTimeDepart() << "\t\t" << (*plane)[i]->getTimeArrive();
cout << "\t\t" << (*plane)[i]->getDuration() << endl;
}
cout << endl;
}
void Controller::displayDestinations()
{
cout << "Plane\tDestinations" << endl;
for (int i = 0; i < numPlanes; i++)
{
cout << i << "\t" << (*plane)[i]->getDestinationCity() << ", " << (*plane)[i]->getDestinationCountry() << endl;
}
cout << endl;
}
void Controller::displayReservations()
{
cout << "Plane\tSeat\tReserved\tName" << endl;
for (int i = 0; i < numPlanes; i++)
{
for (int j = 0; j < 10; j++)
{
int numTabs = 0;
cout << i << "\t" << j << "\t";
if ((*plane)[i]->getSeat(j).getReserved() == true)
cout << "Yes";
else if ((*plane)[i]->getSeat(j).getReserved() == false)
cout << "No";
cout << "\t\t" << (*plane)[i]->getSeat(j).getPassenger().getFirstName();
cout << " " << (*plane)[i]->getSeat(j).getPassenger().getLastName();
cout << endl;
}
}
cout << endl;
}
void Controller::searchPassengerInfo()
{
vector<Passenger> pCopy;
// copy reserved passengers to passenger vector
initPassengerAll(pCopy, plane);
// sort algorithm from library
// not needed, used linear search (opposed to binary)
//sort(pCopy.begin(), pCopy.end(), cmd);
cout << "Search for a passenger." << endl;
string firstName, lastName;
cout << "Enter the first and last name: ";
cin >> firstName >> lastName;
cout << endl;
stringstream name;
name << firstName << " " << lastName;
// copy returned search vector which contains search result
vector<int> pos = linearSearch(pCopy, name.str());
if (pos.size() != 0)
{
cout << "Number of search result(s): " << pos.size() << endl << endl;
// output all search result(s)
for (unsigned int i = 0; i < pos.size(); i++)
{
// alternatively output the search input
cout << "Name: " << pCopy[pos[i]].getFirstName() << " " << pCopy[pos[i]].getLastName() << endl;
cout << "Address: " << pCopy[pos[i]].getAddress() << endl;
cout << "Phone number: " << pCopy[pos[i]].getPhoneNumber() << endl;
cout << "Flight ID: " << pCopy[pos[i]].getFlightId() << endl;
cout << "Seat ID: " << pCopy[pos[i]].getSeatId() << endl;
// boarding pass information
if (pCopy[pos[i]].getEconomy().getHasEcon())
{
cout << "Pass Type: " << pCopy[pos[i]].getEconomy().getClassName() << endl;
cout << "From: " << pCopy[pos[i]].getEconomy().getFrom() << endl;
cout << "To: " << pCopy[pos[i]].getEconomy().getToCity() << ", " << pCopy[pos[i]].getEconomy().getToCountry() << endl;
}
else if (pCopy[pos[i]].getBusiness().getHasBus())
{
cout << "Pass type: " << pCopy[pos[i]].getBusiness().getClassName() << endl;
cout << "From: " << pCopy[pos[i]].getBusiness().getFrom() << endl;
cout << "To: " << pCopy[pos[i]].getBusiness().getToCity() << ", " << pCopy[pos[i]].getBusiness().getToCountry() << endl;
cout << "Preferred drink: " << pCopy[pos[i]].getBusiness().getDrink() << endl;
cout << "Preferred meal: " << pCopy[pos[i]].getBusiness().getMeal() << endl;
}
cout << endl;
}
}
else
{
cout << "Passenger " << name.str() << " was not found." << endl;
}
cout << endl;
}
void Controller::cancelEntireFlight()
{
cout << "Are you sure you want to cancel an entire flight? ('Y' or 'N')" << endl;
string in;
cin >> in;
while (in != "Y" && in != "N")
{
cout << "Invalid input. Enter \"Y\" for yes, or \"N\" for no." << endl;
cin >> in;
}
if (in == "Y")
{
int flightId = input.flightId(numPlanes);
cout << endl;
cout << "Contact information:" << endl;
// output flight data
// copy all passengers of that flight
vector<Passenger> pCopy;
initPassengerOne(pCopy, plane, flightId);
outputManifest(pCopy);
for (int i = 0; i < (*plane)[flightId]->getNumOfSeats(); i++)
{
if ((*(*plane)[flightId]).getSeat(i).getReserved() == true)
{
(*plane)[flightId]->getSeat(i).setPassenger("_____", "_____", "__________", "__________");
(*plane)[flightId]->getSeat(i).setReserved(false);
}
}
cout << endl << "Flight " << flightId << " was cancelled" << endl;
}
else
{
cout << "No flight was cancelled." << endl;
}
}
void Controller::exitProgram()
{
setRun(false);
cout << message.exitProgram() << endl;
file.updateFile(plane, numPlanes);
exit(0);
}
void Controller::outputManifest(vector<Passenger> pCopy)
{
cout << "Name\t\t\tAddress\t\t\tPhone number\t\tPlane\tSeat" << endl;
for (unsigned int i = 0; i < pCopy.size(); i++)
{
int numTabs = 0;
// for formatting purposes; so that the vertical columns line up
int nameSize, addressSize, phoneSize;
// output name
cout << pCopy[i].getLastName() << ", " << pCopy[i].getFirstName();
// output address
nameSize = pCopy[i].getFirstName().size() + pCopy[i].getLastName().size() + 2; // +2 for added space and comma
cout << message.tabs(spacing(nameSize)) << pCopy[i].getAddress();
// output phone number
addressSize = pCopy[i].getAddress().size();
cout << message.tabs(spacing(addressSize)) << pCopy[i].getPhoneNumber();
// output flight and seating numbers
phoneSize = pCopy[i].getPhoneNumber().size();
cout << message.tabs(spacing(phoneSize)) << pCopy[i].getFlightId() << "\t" << pCopy[i].getSeatId();
cout << endl;
}
}
void Controller::initPassengerAll(vector<Passenger>& pCopy, const vector<Plane*> *plane)
{
for (int i = 0; i < numPlanes; i++)
{
for (int j = 0; j < 10; j++)
{
if ((*plane)[i]->getSeat(j).getReserved())
{
pCopy.push_back((*plane)[i]->getSeat(j).getPassenger());
}
}
}
}
void Controller::initPassengerOne(vector<Passenger>& pCopy, const vector<Plane*>* plane, int flightId)
{
for (int i = 0; i < 10; i++)
{
if ((*plane)[flightId]->getSeat(i).getReserved())
{
pCopy.push_back((*plane)[flightId]->getSeat(i).getPassenger());
}
}
}
int Controller::spacing(int size)
{
int numTabs = 0;
if (size < 4)
numTabs = 3;
else if (size < 8)
numTabs = 3;
else if (size < 12)
numTabs = 2;
else if (size < 16)
numTabs = 2;
else
numTabs = 1;
return numTabs;
}
vector<int> Controller::linearSearch(vector<Passenger> pCopy, string search)
{
// array of full names, the first and last name combined
string *fullName = new string[pCopy.size()];
for (unsigned int i = 0; i < pCopy.size(); i++)
{
stringstream name;
name << pCopy[i].getFirstName() << " " << pCopy[i].getLastName();
fullName[i] = name.str();
}
// linear search algorithm (handles people with identical names)
vector<int> pos;
int i = 0;
while (i < pCopy.size())
{
if (fullName[i] == search)
pos.push_back(i);
i++;
}
return pos;
}
bool cmd(Passenger p1, Passenger p2)
{
if (p1.getLastName() != p2.getLastName())
return p1.getLastName() < p2.getLastName();
return p1.getFirstName() < p2.getFirstName();
}