-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.cpp
More file actions
573 lines (490 loc) · 19.2 KB
/
manager.cpp
File metadata and controls
573 lines (490 loc) · 19.2 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
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#include "manager.h"
using namespace std;
/*********************************************************************
** Function: Default Constructor
** Description: Creates manager objects using the default constructor
** Parameters: None
** Pre-Conditions: No parameters passed
** Post-Conditions: Object is created
*********************************************************************/
Manager::Manager(){
this->num_airports = 0;
this->a_arr = nullptr;
}
/*********************************************************************
** Function: non-default constructor
** Description: Creates manager objects using the default constructor
** Parameters:None
** Pre-Conditions: num_airports is passed
** Post-Conditions: Object is created
*********************************************************************/
Manager::Manager(int new_num_airports) {
this->num_airports = new_num_airports;
}
/*********************************************************************
** Function: Destructor
** Description: Free any dynamic memory
** Parameters: None
** Pre-Conditions: Object exists
** Post-Conditions: Object is deleted
*********************************************************************/
Manager::~Manager() {
if (this->a_arr != nullptr){
delete [] a_arr;
a_arr = nullptr;
}
}
/*********************************************************************
** Function: set_num_airports
** Description: Set the number of airports to some int
** Parameters: constant integer
** Pre-Conditions: num_airports exists
** Post-Conditions: new_num_airports exists
*********************************************************************/
void Manager::set_num_airports(const int new_num_airports){
this->num_airports = new_num_airports;
}
/*********************************************************************
** Function: set_a_arr
** Description: Set a_arr to something
** Parameters: Array pointer of Airport class
** Pre-Conditions: a_arr exists
** Post-Conditions: new_a_arr exists
*********************************************************************/
void Manager::set_a_arr(Airport* new_a_arr){
this->a_arr = new_a_arr;
}
/* Accessors */
/*********************************************************************
** Function: get_num_airports
** Description: returns num_airports member variable from the Manager class
** Parameters: None
** Pre-Conditions:
** Post-Conditions:
*********************************************************************/
int Manager::get_num_airports() const{
return this->num_airports;
}
/*********************************************************************
** Function: get_a_arr
** Description: returns a_arr member array from the Manager class
** Parameters: None
** Pre-Conditions: a_arr exists
** Post-Conditions: None
*********************************************************************/
Airport* Manager::get_a_arr() const{
return this->a_arr;
}
/*********************************************************************
** Function: populate
** Description: takes info from the txt file "airport.txt" and populates class member variables of every class
** Parameters: ifstream variable reference
** Pre-Conditions: ifstream variable is created and opened a file
** Post-Conditions: None
*********************************************************************/
void Manager::populate(ifstream &inputFile) {
//Your code goes here:
inputFile >> this->num_airports;
this->a_arr = new Airport [this->num_airports];
for (int i = 0 ; i < this->num_airports ; ++i){
string new_name;
int new_num_flights;
int new_cap;
inputFile >> new_name >> new_num_flights >> new_cap;
this->a_arr[i].set_name(new_name);
this->a_arr[i].set_num_flights(new_num_flights);
this->a_arr[i].set_cap(new_cap);
this->a_arr[i].populate_airport(inputFile, new_num_flights);
}
return;
}
/*********************************************************************
** Function: init
** Description: Opens the airport.txt file. Error checks to make sure it exists. Calls populate function.
** After program finishes, close the file and call edit_file.
** Parameters: None
** Pre-Conditions: ifstream object is created
** Post-Conditions: File is closed
*********************************************************************/
int Manager::init(){
ifstream inputFile("airport.txt");
//Error opening file?:
//Your code goes here:
if (!inputFile.is_open()){
cout << "File cannot be opened" << endl;
return 1;
}
Manager::populate(inputFile);
inputFile.close();
edit_file();
return 1;
}
/*********************************************************************
** Function: edit_file
** Description: Wipe the airport.txt file, and enter new airport information into it
** Parameters: None
** Pre-Conditions: airport.txt is closed
** Post-Conditions: airport.txt is updated
*********************************************************************/
void Manager::edit_file(){
ofstream outputFile("airport.txt", ios::trunc);
outputFile << this->num_airports;
outputFile << endl;
for (int i = 0 ; i < this->num_airports ; ++i){
outputFile << this->a_arr[i].get_name() << " " << a_arr[i].get_num_flights() << " " << a_arr[i].get_cap() << endl;
a_arr[i].edit_file2(outputFile);
}
}
/*********************************************************************
** Function: print_menu
** Description: Display a GUI to the user
** Parameters: None
** Pre-Conditions: None
** Post-Conditions: None
*********************************************************************/
void Manager::print_menu(){
cout << endl;
cout << "1. View all Airports & Flights info" << endl;
cout << "2. Check flight info" << endl;
cout << "3. Add a new flight" << endl;
cout << "4. Cancel a flight" << endl;
cout << "5. Take off a flight" << endl;
cout << "6. Print airport stats" << endl;
cout << "7. Take off all flights" << endl;
cout << "8. Quit" << endl;
cout << "Your choice: ";
}
/*********************************************************************
** Function: get_menu_choice
** Description: Get the user's menu choice
** Parameters: None
** Pre-Conditions: None
** Post-Conditions: return user's choice integer
*********************************************************************/
int Manager::get_menu_choice() {
int choice = 0;
Manager::print_menu();
cin >> choice;
cout << endl;
//Don't forget error handling!!!
return choice;
}
/*********************************************************************
** Function: print_all
** Description: Print's all airpot information (all shallow class member variables)
** Parameters: None
** Pre-Conditions: Class member variables all exist
** Post-Conditions: None
*********************************************************************/
void Manager::print_all(){
//Your code goes here:
for (int i = 0 ; i < this->num_airports ; ++i){
cout << "**********************************" << endl;
cout << "******Airport Name: " << this->a_arr[i].get_name() << "******" << endl;
cout << "Capacity: " << this->a_arr[i].get_cap() << endl;
this->a_arr[i].print_airport();
}
return;
}
/*********************************************************************
** Function: check_flight_control
** Description: Ask the user to enter a flight number. If flight is not found print an error. If found, print the fflight's information
** Parameters: None
** Pre-Conditions: flight array and airport array is populated
** Post-Conditions: None
*********************************************************************/
void Manager::check_flight_control() {
string enterFlight;
bool foundInAnyAirport = false;
cout << "Enter the flight number: ";
cin >> enterFlight;
for (int i = 0; i < this->num_airports; ++i) {
// Check if the flight is found in the current airport
if (this->a_arr[i].find_flight(enterFlight)) {
foundInAnyAirport = true;
}
}
// If the flight is not found in any airport, print error
if (!foundInAnyAirport) {
cout << "Flight not found" << endl;
}
return;
}
/*********************************************************************
** Function: add_flight_control
** Description: Add a flight to the flight array by creating a new flight object and using setters to set Flight class member variables based on user input.
** If the airport the user wants to adda flight to is full, print an error.
** If it is not full, succesfully add a flight by using the AOO and incrementing the flight array by one, then setting the new flight at the last index.
** Parameters: Nnne
** Pre-Conditions: Flight object is created
** Post-Conditions: flight array is incremented
*********************************************************************/
void Manager::add_flight_control() {
Flight f1;
string user_loc;
string user_flight_num;
string user_dest;
int user_num_pilots;
string* user_pilots;
cout << "=== Choices ===" << endl;
for (int i = 0 ; i < this->num_airports ; ++i){
cout << "(" << a_arr[i].get_name() << ") - Airport " << a_arr[i].get_name() << endl;
}
cout << "Choose an airport: ";
cin >> user_loc;
f1.set_curr_loc(user_loc);
int airportIndex = -1;
for (int i = 0; i < this->num_airports; i++) {
if (user_loc == this->a_arr[i].get_name()) {
airportIndex = i;
break;
}
}
if (airportIndex == -1) {
cout << "Airport not found." << endl;
return;
}
if (this->a_arr[airportIndex].get_num_flights() == this->a_arr[airportIndex].get_cap()) {
cout << "Cannot add more flights to this airport" << endl;
return;
}
cout << "Enter flight number: ";
cin >> user_flight_num;
f1.set_flight_num(user_flight_num);
cout << "Enter destination: ";
cin >> user_dest;
f1.set_dest(user_dest);
cout << "Enter number of pilots: ";
cin >> user_num_pilots;
f1.set_num_pilots(user_num_pilots);
user_pilots = new string [user_num_pilots];
for (int i = 0; i < user_num_pilots; i++){
cout << "Name of Pilot #" << i +1 << ": ";
cin >> user_pilots[i];
f1.set_pilots(user_pilots);
}
for(int i = 0; i < this->num_airports; i++){
if(user_loc == this->a_arr[i].get_name()){
this->a_arr[i].add_a_flight(f1);
}
}
user_pilots = nullptr;
return;
}
/*********************************************************************
** Function: cancel_flight_control
** Description: Present airport options. Recieve user input and present all flights within the selected airport. Recieve user's selected flight.
** Error check both statements to check and make sure the airport and flight exist.
** return a copy of the selected flight, set all indexies back one in the array.
** Parameters: None
** Pre-Conditions: airport and flight array exist
** Post-Conditions: updated flight array
*********************************************************************/
void Manager::cancel_flight_control() {
string user_loc;
cout << "=== Choices ===" << endl;
for (int i = 0; i < this->num_airports; ++i) {
cout << "(" << a_arr[i].get_name() << ") - Airport " << this->a_arr[i].get_name() << endl;
}
cout << "Choose an airport: ";
cin >> user_loc;
int airportIndex = -1;
for (int i = 0; i < this->num_airports; i++) {
if (user_loc == this->a_arr[i].get_name()) {
airportIndex = i;
break;
}
}
if (airportIndex == -1) {
cout << "Airport not found." << endl;
return;
}
cout << "**********************************" << endl;
cout << "******Airport Name: " << this->a_arr[airportIndex].get_name() << "******" << endl;
cout << "Capacity: " << this->a_arr[airportIndex].get_cap() << endl;
this->a_arr[airportIndex].print_airport();
int flightIndex;
cout << "Choose a flight to cancel: ";
cin >> flightIndex;
Flight flightRemoved = this->a_arr[airportIndex].remove_a_flight(flightIndex - 1);
return;
}
/*********************************************************************
** Function: take_off_control
** Description: Simulates sending a flight to its destination.
** Ask the user to pick an airport, and flight.
** Once selected, remove that flight from the array, make a copy of it and return it to the Manager.
** Once returned, add tthe flight to its destination and finally, set the curr_loc as its dest.
** Parameters: None
** Pre-Conditions: flight exists
** Post-Conditions: updated arrays
*********************************************************************/
void Manager::take_off_control() {
//Your code goes here:
string user_loc;
cout << "=== Choices ===" << endl;
for (int i = 0; i < this->num_airports; ++i) {
cout << "(" << a_arr[i].get_name() << ") - Airport " << this->a_arr[i].get_name() << endl;
}
cout << "Choose an airport: ";
cin >> user_loc;
int airportIndex = -1;
for (int i = 0; i < this->num_airports; i++) {
if (user_loc == this->a_arr[i].get_name()) {
airportIndex = i;
break;
}
}
if (airportIndex == -1) {
cout << "Airport not found." << endl;
return;
}
cout << "**********************************" << endl;
cout << "******Airport Name: " << this->a_arr[airportIndex].get_name() << "******" << endl;
cout << "Capacity: " << this->a_arr[airportIndex].get_cap() << endl;
this->a_arr[airportIndex].print_airport();
int flightChoice;
cout << "Choose a flight to takeoff: ";
cin >> flightChoice;
if (flightChoice < 1 || flightChoice > this->a_arr[airportIndex].get_num_flights()) {
cout << "Invalid flight choice." << endl;
return;
}
Flight copyFlight = a_arr[airportIndex].remove_a_flight2(flightChoice - 1);
copyFlight.set_curr_loc(copyFlight.get_dest());
/* Checking to make sure the desitnation airport is not already at max capacity */
/* If it is, add the flight back to the original airport, and print an error */
for (int i = 0; i < this->num_airports; i++) {
if (copyFlight.get_dest() == this->a_arr[i].get_name() && this->a_arr[i].get_num_flights() >= this->a_arr[i].get_cap()) {
cout << "Destination airport has reached its maximum capacity of flights." << endl;
copyFlight.set_curr_loc(this->a_arr[airportIndex].get_name());
this->a_arr[airportIndex].add_a_flight2(copyFlight);
return;
}
}
if (copyFlight.get_dest() == "A"){
a_arr[0].add_a_flight2(copyFlight);
}
else if (copyFlight.get_dest() == "B"){
a_arr[1].add_a_flight2(copyFlight);
}
else if (copyFlight.get_dest() == "C"){
a_arr[3].add_a_flight2(copyFlight);
}
cout << "Flight has taken off and landed!" << endl;
return;
}
/*********************************************************************
** Function: take_off_all_control
** Description: Same as take_off_control but instead of asking the user for a specific flight, it sends all flights using a for loop.
** Parameters: None
** Pre-Conditions: Arrays exist
** Post-Conditions: Arrays are updated
*********************************************************************/
void Manager::take_off_all_control() {
for (int i = 0; i < this->num_airports; ++i) {
Flight copyFlight = a_arr[i].remove_all_flights(i);
copyFlight.set_curr_loc(copyFlight.get_dest());
for (int j = 0; j < this->num_airports; j++) {
if (i != j) {
validate_destination(j, copyFlight);
}
}
if (copyFlight.get_dest() == "A") {
a_arr[0].add_a_flight2(copyFlight);
} else if (copyFlight.get_dest() == "B") {
a_arr[1].add_a_flight2(copyFlight);
} else if (copyFlight.get_dest() == "C") {
a_arr[2].add_a_flight2(copyFlight);
} else if (copyFlight.get_dest() == "D"){
a_arr[3].add_a_flight2(copyFlight);
}
}
}
/*********************************************************************
** Function: validate_destination
** Description: Validate that the flight taking off is not being flown into an airport that is already at max capacity.
** Parameters: index of loop, reference of the copied flight object being taken off
** Pre-Conditions: copyFlight exists
** Post-Conditions: None
*********************************************************************/
void Manager::validate_destination(int idx, Flight ©Flight){
if (copyFlight.get_dest() == this->a_arr[idx].get_name() && this->a_arr[idx].get_num_flights() >= this->a_arr[idx].get_cap()) {
cout << "Destination airport has reached its maximum capacity of flights." << endl;
copyFlight.set_curr_loc(this->a_arr[idx].get_name());
this->a_arr[idx].add_a_flight2(copyFlight);
return;
}
}
/*********************************************************************
** Function: stats_control
** Description: Print out airport's, their capacity and how many flights have that airport as a destination
** Parameters: None
** Pre-Conditions: None
** Post-Conditions: None
*********************************************************************/
void Manager::stats_control() {
//Your code goes here:
int* foundDestinationArr = new int [this->num_airports];
for (int i = 0 ; i < this->num_airports ; ++i){
foundDestinationArr[i] = 0;
}
for (int i = 0 ; i < this->num_airports ; ++i){
this->a_arr[i].find_destinations(this->num_airports, foundDestinationArr);
}
for (int i = 0 ; i < this->num_airports ; ++i){
cout << "Airport " << this->a_arr[i].get_name() << endl;
cout << "Capacity: " << this->a_arr[i].get_cap() << endl;
cout << "Listed as destination: " << foundDestinationArr[i] << " flights" << endl;
}
delete [] foundDestinationArr;
foundDestinationArr = nullptr;
return;
}
/*********************************************************************
** Function: run
** Description: Start of the program. Branching statement to decide what function to call based on user's menu choice
** Parameters: get user's choice
** Pre-Conditions: None
** Post-Conditions: Program is exited
*********************************************************************/
void Manager::run() {
cout << endl;
cout << "Welcome to Flight Manager!!" << endl;
if (Manager::init() != 1)
return;
int choice = -1;
while (choice != 8)
{
choice = Manager::get_menu_choice();
//print all
if (choice == 1)
Manager::print_all();
//flight info
else if (choice == 2){
Manager::check_flight_control();
}
//add a new flight
else if (choice == 3) {
Manager::add_flight_control();
}
//cancel a flight
else if (choice == 4) {
Manager::cancel_flight_control();
}
//take off a flight
if (choice == 5){
Manager::take_off_all_control();
}
//airport stats
else if (choice == 6){
Manager::stats_control();
}
else if (choice == 7) {
Manager::take_off_all_control();
}
}
cout << "Bye!" << endl << endl;
edit_file();
return;
}