-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplete_Code.cpp
More file actions
1346 lines (1090 loc) · 35.7 KB
/
Copy pathComplete_Code.cpp
File metadata and controls
1346 lines (1090 loc) · 35.7 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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <list> //double linked list
#include <iostream>
#include <string> // for the atoi conversion
#include <sstream> // for the stringstream (used to convert into to string)
#include <iomanip> // for organizing output into columns
#include <vector>
char check_pass[100];
struct passport_and_corresponding_no
{
long int passport_storage;
int corres_no;
}p1[100];
int i=0;
using namespace std;
// forward declaration
class Person;
class Flight;
class Queue;
/* ----CLASSES---- */
class Menu //class for managing the app's menuo
{
public:
static void displayMenu(); // displays the menu
static void select(int selection); // receives user's input and performs the corresponding function
static void exit_prog(); // exits the app
};
struct Time // structure for managing the Arrival and Departing times
{
int min;
int hour;
};
class Flight // class for managing the availabe functions of a flight
{
private:
int flightNo, cost, seats, booked_seats;
string from, to, plane_type;
Time t_leave, t_arrive;
public:
/* --ADD/REMOVE FLIGHTS--*/
void addFlight(); //διαχειρίζεται την προσθήκη πτήσεων (επιλογή μενού Νο 1)
static void deleteFlight(int num); //διαχειρίζεται την αφαίρεση πτήσεων (επιλογή μενού Νο 2)
/* --DISPLAY FLIGHTS--*/
static void displayFlightInfo(int num); //προβάλλει τις πληροφορίες μιας πτήσης (επιλογή μενού Νο 3)
static void displaySchedule(); //προβάλλει τις πτήσεις που υπάρχουν καταχωρημένες στο σύστημα (επιλογή μενού Νο 4)
/* --BOOK SEATS--*/
static void resSeat(int num, int val); //αυξομειώνει τις κατοχυρωμένες θέσεις μιας πτήσεις
/* --FLIGHT CHECKERS--*/
static bool flightExists(int num); // check flight is booked
static bool checkForSeats(int num); // checks for already registerd flight in the system
/* --GETTERS--*/
int getFlightNo() { return this -> flightNo; }
Time getLeavingTime() { return this -> t_leave; }
Time getArrivingTime() { return this -> t_arrive; }
string getDeparture() { return this -> from; }
string getDestination() { return this -> to; }
};
class Person //class for managing the functions of a passenger
{
private:
int passportNo, tel;
list<int> flights;
string name, surname, nationallity, address;
string pass;
public:
/* --BOOK FLIGHT-- */
void book(); // book flight for each customer (Menu Option Νο 6)
void bookFromQueue(int num); // book flight for the oldest customer in
// (call ONLY when a reservation is cancelled)
void cancel(); //cancel a reservation (Menu Option Νο 7)
/* --SHOW PASSENGER'S INFO-- */
static bool displayPersonInfo(int passport);
/* --CHECHKER FOR UNIQUE PASSPORT-- */
static bool uniquePass(int passport);
/* --GETTERS-- */
int getPassport() { return this -> passportNo; }
string getName() { return this -> name; }
string getSurname() { return this -> surname; }
string getpassword(){return this->pass;}
void setpassword(string temp_pass);
}p[100];
void Person::setpassword(string temp_pass)
{
pass=temp_pass;
}
struct Node //structure for managing each flight's queue
{
Person person;
Node* next;
};
class Queue // class that stores passengers on each flight's queue
{
private:
int queueNo;
Node* rear;
Node* front;
public:
/* --CREATE NEW QUEUE-- */
Queue(int queueId); // queueID is the same as the flight number for each flight
/* --ADD/REMOVE ELEMENTS-- */
void enqueue(Person p); // add a new passenger
void dequeue(); // remove the passenger add last
/* --ΠΡΟΒΟΛΗ ΣΤΟΙΧΕΙΩΝ ΟΥΡΑΣ-- */
static void display(int num); //εμφανίζει τους πελάτες, που βρίσκονται κατοχυρωμένοι
//στην ουρά αναμονής της εκάστοτε πτήσης
/* --ΕΠΙΣΤΡΟΦΗ ΑΡΧΑΙΟΤΕΡΟΥ ΧΡΗΣΤΗ-- */
Person oldest(); //επιστρέφει το αντικέιμενο του αρχαιότερου πελάτη, μέσω της κλάσης Person
/* --CHECK FOR EMPTY QUEUE-- */
bool isEmpty(); // returns true if queue is empty
/* --GETTERS-- */
int getNo() { return this -> queueNo; }
Node* getFront() { return this -> front; }
Node* getRear() { return this -> rear; }
/* --SETTERS-- */
void setFront(Node* f) {
if (f == 0){
this -> front = NULL;
}else{
this -> front = f;
}
}
void setRear(Node* r) {
this -> rear = r;
}
};
/* ----FUNCTIONS-CHECKERS FOR THE INPUTS---- */
/* --CHECKERS FOR DIRECT FLIGHTS-- */
bool checkTime(string time) { // check the validity of the time (must be xx:xx, where x = Natural number)
// enter ONLY if string is NOT empty
if(!time.empty()){
for (int i=0; i < time.length(); i++) {
if (i==2){
if (time.at(i) != ':') {
return false;
}
}else if ( !(time.at(i) >= '0' && time.at(i) <= '9') ) {
return false;
}
}
return true;
}else{
return false;
}
}
bool checkNumber(string s){ // checks if the input string consists ONLY of numbers
// enter if string is NOT empty
if(!s.empty()){
for (int i = 0; i < s.length(); i++){
if ( ((s.at(i) >= 'a' && s.at(i) <= 'z') || (s.at(i) >= 'A' && s.at(i) <= 'Z') || (s.at(i) == ' ')) ){
return false;
}
}
// check if string is a postive number
if ( atoi(s.c_str()) > 0 ){
return true;
}else{
return false;
}
}else{
return false;
}
}
bool checkString(string s){ // checks if string consists only of letters
if(!s.empty()){
for (int i = 0; i < s.length(); i++){
if ( !((s.at(i) >= 'a' && s.at(i) <= 'z') || (s.at(i) >= 'A' && s.at(i) <= 'Z')) ){
return false;
}
}
return true;
}
else{ //return false
return false;
}
}
/* --CHECKER FOR NON-DIRECT FLIGHTS-- */
bool checkTime2(Time tLeaving, Time tArriving){ // checks the validity of arrival and departure time
if(tLeaving.hour > tArriving.hour){
return true;
}else if(tLeaving.hour == tArriving.hour){
if(tLeaving.min > tArriving.min){
return true;
}
}
return false;
}
/*
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IMPLEMENTATION
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
*/
/* ----GLOBAL VARS---- */
list<Flight> flist; // store the flights of the system
list<Person> plist; // store the passengers
list<Queue> qlist; // store the customers in the flights' waiting queues
/* ----FUNCTION OF CLASS Queue---- */
Queue::Queue(int queueId){
this -> queueNo = queueId;
this -> rear = NULL;
this -> front = NULL;
}
void Queue::enqueue(Person p){
Node* temp = new Node;
temp -> person = p;
temp -> next = NULL;
if(this -> front == NULL){
this -> front = temp;
}else{
this -> rear -> next = temp;
}
this -> rear = temp;
}
void Queue::dequeue(){
Node* temp = new Node;
temp = this -> front;
this -> front = this -> front -> next;
delete temp;
}
void Queue::display(int num){
for (std::list<Queue>::iterator i = qlist.begin(); i != qlist.end(); ++i) {
if(num == i -> queueNo){
Node* p = new Node;
p = i -> front;
if(i -> front == NULL){
cout<<"\nEmpty queue.\n";
}else{
cout << "\nPassengers waiting on queue:" << endl;
cout << left << setw(15) << "Passport" << left << setw(15) << "Name" << left << setw(15) << "Surame" << endl;
while(p!=NULL){
cout << left << setw(15) << p -> person.getPassport() << left << setw(15) << p -> person.getName() << left << setw(15) << p -> person.getSurname() << endl;
p = p -> next;
}
}
}
}
}
Person Queue::oldest(){
return this -> front -> person;
}
bool Queue::isEmpty(){
if(this -> front == NULL && this -> rear == NULL){
return true;
}
return false;
}
/* ----FUNCTIONS OF CLASS Menu---- */
void Menu::displayMenu(){
int selection; // user's menu choice
string temp; // temp to store user's input
do{
cout << "-----------------------------------------------";
cout << "\n\t AIRLINE RESERVATION SYSTEM \n\n";
cout << "Please pick an option from the menu below. \n";
cout << "1. Add new flights \n";
cout << "2. Delete flights \n";
cout << "3. Display flight schedule \n";
cout << "4. Display flight details \n";
cout << "5. Display Passenger Personal info \n";
cout << "6. Book reservation \n";
cout << "7. Cancel reservation \n";
cout << "8. Exit \n\n";
cout << "-----------------------------------------------" << endl;
cout << "Enter the number of a menu option: ";
cin >> temp;
// check validity of input
while (!checkNumber(temp)) {
cout << "Please enter a number!" << endl;
cin.clear();
cin.ignore(256,'\n');
cin >> temp;
}
cout << endl;
selection = atoi(temp.c_str());
select(selection);
}while(true);
}
void Menu::select(int selection){
Flight f; // FLight's object
Person p; // class Person's object
string temp; // temp to store input
switch(selection){
case 1:
f.addFlight();
break;
case 2:
if (!flist.empty()) {
cout << "Enter the Flight Number you want to delete: "; cin >> temp;
cout << endl;
while (!checkNumber(temp)){
cout << "Flight Number must be a number!" << endl;
cin.clear();
cin.ignore(256,'\n');
cin >> temp;
cout << endl;
}
Flight::deleteFlight( atoi(temp.c_str()) );
}else {
cout << "There are no flights to delete" << endl;
}
break;
case 3:
if (!flist.empty()) {
Flight::displaySchedule();
}else {
cout << "There are no scheduled flights!" << endl;
}
break;
case 4:
if (!flist.empty()) {
cout << "Please insert flight's number: ";
cin >> temp; cout << endl;
while (!checkNumber(temp)){
cout << "Flight number must be a number!" << endl;
cin.clear();
cin.ignore(256,'\n');
cin >> temp;
cout << endl;
}
Flight::displayFlightInfo( atoi(temp.c_str()) );
}else {
cout << "There are no scheduled flights!" << endl;
}
break;
case 5:
if (!plist.empty()){
cout << "Please insert passport number: ";
cin >> temp;
while (!Person::displayPersonInfo( atoi(temp.c_str()) )) {
cout << "Wrong passport number!" << endl;
cin.clear();
cin.ignore(256,'\n');
cin >> temp;
cout << endl;
}
}else{
cout << "There are no registered clients at the moment!" << endl;
}
break;
case 6:
p.book();
break;
case 7:
p.cancel();
break;
case 8:
Menu::exit_prog();
break;
default:
cout << "Invalid selection \n";
}
}
void Menu::exit_prog(){
cout << "Thank you for using our system! \n";
exit(1); // exit
}
/* ----FUNCTIONS OF CLASS Flight---- */
void Flight::addFlight(){
/* ----INITIALISE VARS----*/
bool flag = false; // used in loops
/* --Check for arrival, departure time validity (USED ONLY IN BOARDING & ARRIVING TIME)-- */
vector<string> fields; // stores, splitted, user input to be checked
string temp; // stores user's input temp
char* pch; // stores result of function strtok()
int hour; // stores hour
int min; // store minutes
cout << "Add new flights by giving the following attributes: \n";
// clean stream
cin.clear();
cin.ignore(256,'\n');
/* --FLIGHT NUBMER-- */
cout << "Flight Number: ";
// get user's input
cin>>temp;
do{
flag = true;
// check input
if (!checkNumber(temp)){
cout << "Please insert a valid Flight Number! " << endl;
flag = false;
cin>>temp;
}else if (Flight::flightExists( atoi(temp.c_str()) )) {
cout << "This Flight already exists!" << endl;
cout << "Please insert a valid Flight Number!" << endl;
flag = false;
cin>>temp;
}else {
flag = true;
this -> flightNo = atoi(temp.c_str());
}
}while(!flag);
/* --DEPARTURE-- */
cout << "Departure: ";
flag = false;
// check input
LOOP:do{
cin>>temp;
if ( (temp.length() <= 10) && (checkString(temp)) ){
this -> from = temp;
flag = true;
}else {
cout << "Please insert a valid Departure city! ";
goto LOOP;
}
}while(!flag);
/* --DESTINATION-- */
cout << "Destination: ";
flag = false;
// check input
LOOP2:do{
cin>>temp;
if ( (temp.length() <= 10) && (checkString(temp)) && (temp.compare(this -> from)) ){
this -> to = temp;
flag = true;
}else{
cout << "Please insert a valid Destination city! ";
goto LOOP2;
}
}while(!flag);
/* --DEPARTURE TIME-- */
cout << "Boarding time (e.g. 19:40): "; //ask from user for the boarding time
flag = false;
// check input
LOOP3:do{
cin>>temp;
if( temp.length() != 5 || !checkTime(temp) ){
cout << "Please insert a valid boarding time (e.g. 19:40)! ";
goto LOOP3;
}
char t_temp[temp.length()];
strcpy(t_temp, temp.c_str());
//split string
pch = strtok(t_temp, ":");
while(pch != NULL){
fields.push_back(pch);
pch = strtok(NULL, ":");
}
hour = atoi(fields[0].c_str());
min = atoi(fields[1].c_str());
// check time
if ((hour >=0 && hour<=23) && (min>=0 && min <=59)){
this -> t_leave.hour = hour;
this -> t_leave.min = min;
flag = true;
}else{
cout << "Please insert a valid boarding time (e.g. 19:40)! ";
fields.clear();
}
}while(!flag);
/* --ARRIVAL TIME-- */
cout << "Arriving time (e.g. 21:40): ";
flag = false;
fields.clear(); // clear fields (because it was used before, at "DEPARTURE TIME")
// check input
LOOP4:do{
cin>>temp;
if( temp.length() > 5 || !checkTime(temp) ){
cout << "Please insert a valid boarding time (e.g. 19:40)! ";
goto LOOP4;
}
char t_temp[temp.length()];
strcpy(t_temp, temp.c_str());
//split string
pch = strtok(t_temp, ":");
while(pch != NULL){
fields.push_back(pch);
pch = strtok(NULL, ":");
}
hour = atoi(fields[0].c_str());
min = atoi(fields[1].c_str());
// check validity of time
if ((hour >=0 && hour<=23) && (min>=0 && min <=59)){
this -> t_arrive.hour = hour;
this -> t_arrive.min = min;
flag = true;
}else{
cout << "Please insert a valid arriving time (e.g. 19:40)! ";
fields.clear();
}
}while(!flag);
/* --TICKET COST-- */
cout << "Ticket price: ";
LOOP5:do{
cin>>temp;
flag = true;
// check input
if (!checkNumber(temp)){
cout << "Please insert a valid ticket price!" << endl;
flag = false;
goto LOOP5;
}else{
flag = true;
this -> cost = atoi(temp.c_str());
}
}while(!flag);
/* --AIRCRAFT TYPE-- */
cout << "Aeroplane type: ";
cin>>this -> plane_type;
while(this -> plane_type.empty()){
cout << "Please insert a valid Aeroplane type!" << endl;
cin>>this -> plane_type;
}
/* --No OF SEATS-- */
cout << "Number of seats: ";
string temp1;
LOOP6:do{
cin>>temp1;
flag = true;
// check input
if (!checkNumber(temp1)){
cout << "Please insert a valid number of seats!" << endl;
flag = false;
goto LOOP6;
}else{
flag = true;
this -> seats = atoi(temp1.c_str());
}
}while(!flag);
/* --No of BOOKED SEATS-- */
cout << "Number of booked seats: ";
LOOP7:do{
cin>>temp;
flag = true;
// check input
if (!checkNumber(temp)){
cout << "Please insert a valid number of booked seats!" << endl;
flag = false;
goto LOOP7;
}else if ( atoi(temp.c_str()) > this -> seats ) {
cout << "Booked seats must be less than plane's seats!" << endl;
flag = false;
goto LOOP7;
}else {
flag = true;
this -> booked_seats = atoi(temp.c_str());
}
}while(!flag);
cout << endl;
flist.push_back(*this); // add object to the flist
Queue q(this -> flightNo); // create new queue for the newly added flight
qlist.push_back(q); // add object to the qlist
cout << "Flight No: "<< this -> flightNo << " was successfully added!" << endl;
}
void Flight::deleteFlight(int num){
for (std::list<Queue>::iterator i = qlist.begin(); i != qlist.end(); ++i){
if( num == i -> getNo() ){
// enter if waiting queue for the flight is NOT empty
if (!i -> isEmpty()) {
// delete object from flist
for (std::list<Flight>::iterator i2 = flist.begin(); i2 != flist.end(); ++i2){
if( num == (i2 -> flightNo) ){
i2 = flist.erase(i2);
i = qlist.erase(i);
cout << "Flight with number: " << num << " was successfully deleted" << endl;
return;
}
}
}else{
cout << "There are passengers in the queue of the flight with No: " << num << endl;
cout << "Remove ALL of them from the queue first!" << endl;
return;
}
}
}
cout << "This flight number doesn't exist!" << endl;
return;
}
void Flight::displayFlightInfo(int num){
string l_time, a_time; // departure and arrival time
stringstream convert;
stringstream convert2;
for (std::list<Flight>::iterator i = flist.begin(); i != flist.end(); ++i) {
if (num == i -> flightNo) {
convert.str(std::string()); // clear stringstream "convert"
convert2.str(std::string()); // clear stringstream "convert2"
convert << i -> t_leave.hour;
convert2 << i -> t_leave.min;
l_time = convert.str() + ":" + convert2.str();
convert.str(std::string()); // clear stringstream "convert"
convert2.str(std::string()); // clear stringstream "convert2"
convert << i -> t_arrive.hour;
convert2 << i -> t_arrive.min;
a_time = convert.str() + ":" + convert2.str();
cout << left << setw(10) << "FLIGHT" << left << setw(10) << "FROM" << left << setw(10) << "TO" << left << setw(10) << "LEAVE" << left << setw(10) << "ARRIVE" << left << setw(10) << "COST" << left << setw(10) << "TYPE" << left << setw(10) << "SEATS" << left << setw(10) << "BOOKED" << endl;
cout << left << setw(10) << i -> flightNo << left << setw(10) << i -> from << left << setw(10) << i -> to << left << setw(10) << l_time << left << setw(10) << a_time << left << setw(10) << i -> cost << left << setw(10) << i -> plane_type << left << setw(10) << i -> seats << left << setw(10) << i -> booked_seats << endl;
Queue::display(num);
return;
}
}
cout << "Invalid number of flight was given." << endl;
}
void Flight::displaySchedule(){
string l_time, a_time; // departure and arrivale time
stringstream convert;
stringstream convert2;
cout << "\n\t\t\t\t FLIGHT SCHEDULE" << endl << endl;
cout << left << setw(10) << "FLIGHT" << left << setw(10) << "FROM" << left << setw(10) << "TO" << left << setw(10) << "LEAVE" << left << setw(10) << "ARRIVE" << left << setw(10) << "COST" << left << setw(10) << "TYPE" << left << setw(10) << "SEATS" << left << setw(10) << "BOOKED" << endl;
for (std::list<Flight>::iterator i = flist.begin(); i != flist.end(); ++i) {
convert.str(std::string()); // clear stringstream "convert"
convert2.str(std::string()); // clear stringstream "convert2"
convert << i -> t_leave.hour;
convert2 << i -> t_leave.min;
l_time = convert.str() + ":" + convert2.str();
convert.str(std::string()); // clear stringstream "convert"
convert2.str(std::string()); // clear stringstream "convert2"
convert << i -> t_arrive.hour;
convert2 << i -> t_arrive.min;
a_time = convert.str() + ":" + convert2.str();
cout << left << setw(10) << i -> flightNo << left << setw(10) << i -> from << left << setw(10) << i -> to << left << setw(10) << l_time << left << setw(10) << a_time << left << setw(10) << i -> cost << left << setw(10) << i -> plane_type << left << setw(10) << i -> seats << left << setw(10) << i -> booked_seats << endl;
}
cout << endl;
}
void Flight::resSeat(int num, int val){
for (std::list<Flight>::iterator i = flist.begin(); i != flist.end(); ++i){
if( num == (i -> flightNo) ){
i -> booked_seats += val ;
break;
}
}
}
bool Flight::flightExists(int num){
for (std::list<Flight>::iterator i = flist.begin(); i != flist.end(); ++i){
if (num == i -> flightNo){
return true;
}
}
return false;
}
bool Flight::checkForSeats(int num){
for (std::list<Flight>::iterator i = flist.begin(); i != flist.end(); ++i){
if( num == (i -> flightNo) ){
if( i -> seats == i -> booked_seats )
return false;
else
return true;
}
}
}
/* ----FUNCTIONS OF CLASS Person---- */
void Person::book(){
/* ----INITIALISE VARS----*/
/* --FLIGHTS-- */
string temp; //temp to store user's input, to be checked
int num; // stores flight's number, after successful check
/* --VARS FOR NON DIRECT FLIGHTS-- */
int counter = 1; // stores the amount(>=2) of the non-direct flights
string choice; // stores user's choice for adding or not more flights to their reservation
Time tArriving;
Time tLeaving;
string temp1;
string Departure;
string Destination;
list<int> nums; // store flights' numbers
list<int>::iterator i1 = nums.begin(); //iterator for accessing List nums
/* --VAR FOR LOOPS-- */
bool flag = true;
// clean stream
cin.clear();
cin.ignore(256,'\n');
if (!flist.empty()) {
cout << "Insert y (yes) for a new client or n (no) for an existing client. ";
cin>>choice;
// enter if client is new
if (choice == "y" || choice == "Y") {
cout << "Please give us your personal info. " << endl;
/* --NAME-- */
cout << "Name: ";
flag = false;
// check input
LOOP8:do{
cin>>this -> name;
if ( (this -> name.length() <= 10) && (checkString(this -> name)) ){
flag = true;
}else {
cout << "Please insert a valid Name! ";
goto LOOP8;
}
}while(!flag);
/* --SURNAME-- */
cout << "Surname: ";
flag = false;
// check input
LOOP9:do{
cin>>this -> surname;
if ( (this -> surname.length() <= 10) && (checkString(this -> surname)) ){
flag = true;
}else {
cout << "Please insert a valid Surname! ";
goto LOOP9;
}
}while(!flag);
/* --PASPPORT No-- */
cout << "Passport number: ";
// check input
LOOP10:do{
cin>>temp;
flag = true;
if (!checkNumber(temp)){
cout << "Please insert a valid passport number" << endl;
flag = false;
goto LOOP10;
}else if (!Person::uniquePass( atoi(temp.c_str()) )) {
cout << "Please check the validity of your passport number" << endl;
flag = false;
goto LOOP10;
}else{
flag = true;
this -> passportNo = atoi(temp.c_str());
}
}while(!flag);
p1[i].passport_storage=passportNo;
p1[i].corres_no=i;
cout<<"Please set the password :\n";
cin>>temp1;
p[i].setpassword(temp1);
i++;
/* --NATIONALLITY-- */
cout << "Nationallity: ";
flag = false;
// check input
LOOP11:do{
cin>>this->nationallity;
if ( (this -> nationallity.length() <= 10) && (checkString(this -> nationallity)) ){
flag = true;
}
else
cout<<"Enter valid Nationality : ";
}while(!flag);
/* --ADDRESS-- */
cout << "Address: ";
cin>>this->address;
/* --TEL-- */
cout << "Telephone: ";
cin>>temp;
// check input
while (!checkNumber(temp))
{
cout << "Please insert a valid telephone number!" << endl;
cin>>temp;
}
this -> tel = atoi(temp.c_str());
}else { // existing customer
cout << "Please give us your passport No: ";
cin>>temp;
// check input
while(!checkNumber(temp)){
cout << "Please insert a valid passport number!" << endl;
flag = false;
cin>>temp;
}
// check if passport No is unique
if ( !(Person::uniquePass( atoi(temp.c_str()) ))) {
for (std::list<Person>::iterator i = plist.begin(); i != plist.end(); ++i){
if (atoi(temp.c_str()) == i -> passportNo) {
this -> name = i -> name;
this -> surname = i -> surname;
this -> passportNo = i -> passportNo;
this -> nationallity = i -> nationallity;
this -> address = i -> address;
this -> tel = i -> tel;
for (std::list<int>::iterator i2 = i->flights.begin(); i2 != i->flights.end(); ++i2) {
this -> flights.push_back(*i2);
}
// after copying the customers info,
// delete the old object pointing to them
i = plist.erase(i);
break;
}
}
}else {
cout << "Wrong passport number!" << endl;
return;
}
}
// display flights
Flight::displaySchedule();
/* --FLIGTH No-- */
cout << "\n\nEnter the number of the flight you 're intrested in: ";
cin>>temp;
flag = true;
// check input
while ( !checkNumber(temp) && !Flight::flightExists(atoi(temp.c_str())) ) {
cout << "Please insert a valid Flight No!" << endl;
cin>>temp;
}
num = atoi(temp.c_str());
/*
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
BOOK CUSTOMER'S CHOSEN FLIGHT(S)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
*/
//DIRECT FLIGHT
if(Flight::checkForSeats(num)){ // check for vacancy
this -> flights.push_back(num);
Flight::resSeat(num,1);
cout << "Your flight with No: " << num << " was successfully booked." << endl;
// NONO-DIRECT FLIGHT
}else{
choice = "y";