-
Notifications
You must be signed in to change notification settings - Fork 0
/
agency.cpp
1792 lines (1500 loc) · 39.2 KB
/
agency.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
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 "agency.h"
#include "customUtilities.h"
#include <iomanip>
#include <map>
#include <set>
using namespace std;
Agency::Agency()
{
name = "Undefined Name";
url = "404";
fileNameClients = "";
fileNamePacks = "";
address = Address();
vat = 0;
packList = {};
clientList = {};
}
#pragma region GETTERS
string Agency::getName() const {
return this->name;
}
unsigned Agency::getVAT() const {
return this->vat;
}
Address Agency::getAddress() const {
return this->address;
}
string Agency::getURL() const {
return this->url;
}
vector<Client> Agency::getClientList() const {
return this->clientList;
}
size_t Agency::getClientListSize() const
{
return this->clientList.size();
}
vector<TravelPack> Agency::getPackList() const {
return this->packList;
}
size_t Agency::getPackListSize() const
{
return this->packList.size();
}
TravelPack Agency::getPackWithId(const int id) const {
for (size_t i = 0; i < this->packList.size(); i++) {
if (abs(this->packList.at(i).getId()) == id)
return this->packList.at(i);
}
return {};
}
bool Agency::getAvailabilityOfPack(const int id) const
{
int auxId = abs(id);
for (size_t i = 0; i < this->packList.size(); i++)
{
if (abs(this->packList.at(i).getId()) == auxId)
return this->packList.at(i).isAvailable();
}
return false;
}
string Agency::getFileNameClients() const {
return this->fileNameClients;
}
string Agency::getFileNamePacks() const {
return this->fileNamePacks;
}
#pragma endregion
#pragma region SETTERS
bool Agency::setName(std::string new_name) {
if (new_name.empty()) return false;
this->name = new_name;
return true;
}
bool Agency::setVATnumber(unsigned new_VAT) {
this->vat = new_VAT;
return true;
}
bool Agency::setAddress(Address new_address) {
this->address = new_address;
return true;
}
bool Agency::setURL(std::string new_url) {
if (new_url.size() == 0) return false;
this->url = new_url;
return true;
}
bool Agency::setClients(std::vector<Client> & new_clients)
{
this->clientList = new_clients;
return true;
}
bool Agency::setPackets(std::vector<TravelPack> & new_packList) {
this->packList = new_packList;
return true;
}
#pragma endregion
// READ METHODS
bool Agency::loadData(const std::string & agencyFileName, std::string & error)
{
ifstream file;
unsigned lineTracker = 1;
file.open(agencyFileName);
if (!file) // if file does not exist
{
error = "ERROR: Agency file \"" + agencyFileName + "\" does not exist";
file.close();
return false;
}
if (cu::isFileEmpty(file))
{
error = "ERROR: Agency file is empty";
file.close();
return false;
}
if (!this->readAgencyFromFile(file, lineTracker, error))
{
error = "ERROR: Wrong input at line " + to_string(lineTracker) + " of file \"" + agencyFileName + "\"\n" + error;
file.close();
return false;
}
file.close();
file.open(this->fileNamePacks);
if (!file) // if file does not exist
{
error = "ERROR: Packs file \"" + this->fileNamePacks + "\" does not exist";
file.close();
return false;
}
lineTracker = 1;
if (!this->readAllPacksFromFile(file, lineTracker, error))
{
file.close();
return false;
}
file.close();
file.open(this->fileNameClients);
if (!file) // if file does not exist
{
error = "ERROR: Clients file \"" + this->fileNameClients + "\" does not exist";
file.close();
return false; // Should never execute
}
lineTracker = 1;
if (!this->readAllClientsFromFile(file, lineTracker, error))
{
file.close();
return false;
}
file.close();
return true;
}
bool Agency::readAgencyFromFile(ifstream & file, unsigned & lineTracker, std::string & error) {
Address address;
getline(file, this->name);
if (!cu::checkStream(file, error)) return false;
lineTracker++;
file >> this->vat;
if (!cu::checkStream(file, error)) return false;
file.ignore(1000, '\n');
lineTracker++;
getline(file, this->url);
if (!cu::checkStream(file, error)) return false;
lineTracker++;
if (!this->address.readFromFile(file, lineTracker, error)) return false;
getline(file, this->fileNameClients);
if (!cu::checkStream(file, error)) return false;
lineTracker++;
getline(file, this->fileNamePacks);
// The next line would make it mandatory to have a newLine at the end of the file
// if (!cu::checkStream(file, error)) return false;
lineTracker++;
return true;
}
bool Agency::readNewClientUserInput() {
unsigned u;
string str;
Client newClient;
Address auxAddress;
bool fail;
do {
if (!cu::readStr(str, "Name")) return false;
} while (!newClient.setName(str));
do {
if (!cu::readUnsigned(u, "VAT")) return false;
fail = this->isVatUsed(u);
if (fail)
{
cout << "VAT is already registered to another Client" << endl;
continue;
}
fail = fail || !newClient.setVAT(u);
if (fail)
cout << "VAT must be a positive integer" << endl;
} while (fail);
do {
if (!cu::readUnsigned(u, "Household")) return false;
fail = !newClient.setHousehold(u);
if (fail)
cout << "Household must be an integer larger than 1" << endl;
} while (fail);
do
{
cout << "Address:" << endl;
if (!auxAddress.readUserInput())
return false;
} while (!newClient.setAddress(auxAddress));
this->clientList.push_back(newClient);
return true;
}
bool Agency::readAllClientsFromFile(std::ifstream & file, unsigned & lineTracker, std::string & error)
{
if (cu::isFileEmpty(file))
{
//cout << "WARNING: Clients file \"" << this->fileNameClients << "\" is empty" << endl;
//cout << "Assuming there are no clients" << endl;
this->packList = {};
return true;
}
while (true)
{
Client aux;
if (!this->readClientFromFile(file, aux, lineTracker, error))
{
error = "ERROR: Wrong input at line " + to_string(lineTracker) + " of file \"" +
this->fileNameClients + "\"\n" + error;
return false;
}
this->clientList.push_back(aux);
if (file.eof())
break;
if (file.peek() != ':')
break;
file.ignore(1000, '\n');
lineTracker++;
}
return true;
}
bool Agency::readNewPackUserInput()
{
string s;
int n;
vector<string> destinations = {};
Date date;
TravelPack pack;
bool fail;
if (!cu::readStr(s, "Main destination"))
return false;
destinations.push_back(s);
char c = 'o';
if (!cu::readConfirmation(c, "Insert secondary destinations"))
return false;
if (c == 'y' || c == 'Y')
{
cout << "Write \"stop\" to finish inputting secondary destinations" << endl;
do
{
if (!cu::readStr(s, "Destinations"))
return false;
destinations.push_back(s);
} while (s != "stop");
destinations.pop_back(); // Removes the extra "stop"
}
if (!pack.setDestinations(destinations))
return false; // Extra safety, should never return false
do
{
cout << "Departure date:" << endl;
if (!date.readUserInput())
return false;
} while (!pack.setDeparture(date));
do
{
cout << "Return date:" << endl;
if (!date.readUserInput())
return false;
fail = !pack.setReturn(date);
if (fail)
cout << "Return Date must be before departure Date" << endl;
} while (fail);
do
{
if (!cu::readInt(n, "Price"))
return false;
fail = !pack.setPrice(n);
if (fail)
cout << "Price must be a postive integer" << endl;
} while (fail);
pack.setCurrentBookings(0);
do
{
if (!cu::readInt(n, "Maximum bookings"))
return false;
fail = !pack.setMaxBookings(n);
if (fail)
cout << "Maximum bookings must be an integer larger than 1" << endl;
} while (fail);
pack.setId(++this->maxPackId);
this->packList.push_back(pack);
return true;
}
bool Agency::readAllPacksFromFile(std::ifstream & file, unsigned & lineTracker, std::string & error)
{
if (cu::isFileEmpty(file))
{
// cout << "WARNING: Packs file \"" << this->fileNamePacks << "\" is empty" << endl;
// cout << "Assuming there are no travel packs" << endl;
this->packList = {};
return true;
}
int n;
file >> n;
if (file.eof() || file.fail()) return false;
n = abs(n);
this->maxPackId = n;
lineTracker++;
unsigned packStart;
while (true)
{
TravelPack aux;
packStart = lineTracker;
if (!this->readPackFromFile(file, aux, lineTracker, error))
{
error = "ERROR: Wrong input at line " + to_string(lineTracker) + " of file \"" + this->fileNamePacks + "\"\n" + error;
return false;
}
// if a pack has an Id larger than the given maxPackId
if (abs(aux.getId()) > n)
{
error = "ERROR: Wrong input at line " + to_string(packStart) + " of file \"" +
this->fileNamePacks + "\"\nPack ID is larger than the Maximum Pack ID given in line 1";
return false;
}
this->packList.push_back(aux);
if (file.eof())
break;
if (file.peek() != ':')
break;
file.ignore(1000, '\n');
lineTracker++;
}
return true;
}
// CONSOLE OUTPUT METHODS
bool Agency::printClientByVAT(unsigned vat) const
{
cout << endl;
if (this->clientList.empty()) {
cout << "No registered clients" << endl;
return false;
}
for (size_t i = 0; i < this->clientList.size(); i++) {
if (this->clientList.at(i).getVAT() == vat) {
cout << this->clientList.at(i) << endl;
return true;
}
}
cout << "Client with VAT \"" << vat << "\" not found" << endl;
return false;
}
void Agency::printClients() const
{
cout << endl;
if (this->clientList.empty())
{
cout << "No registered clients" << endl;
return;
}
cout << this->clientList.front() << endl;
for (size_t i = 1; i < this->clientList.size(); i++)
{
cout << CLIENT_OUTPUT_SEPARATOR << endl;
cout << this->clientList.at(i) << endl;
}
}
void Agency::printClientList() const
{
for (size_t i = 0; i < this->clientList.size(); i++)
{
cout << i << " .";
this->clientList.at(i).printSummary();
cout << endl;
}
}
void Agency::printPacksByClient(const int vat) const
{
cout << endl;
if (this->clientList.empty()) {
cout << "No registered clients" << endl;
return;
}
if (this->packList.empty()) {
cout << "No registered packs" << endl;
return;
}
set<int> printedPacks = {};
int count = 0;
// Look for the client
for (size_t i = 0; i < this->clientList.size(); i++)
{
if (this->clientList.at(i).getVAT() == vat)
{
if (this->clientList.at(i).getTravelPacksListSize() == 0)
{
cout << "No packs registered to this Client" << endl;
return;
}
// Loop through all of its TravelPacks
for (size_t j = 0; j < this->clientList.at(i).getTravelPacksListSize(); j++)
{
// Inserts into set only if it doesn't already exist
printedPacks.insert(this->clientList.at(i).getTravelPackAt(j));
}
}
}
for (auto x : printedPacks)
{
// Print pack with this ID
if (count == 0)
{
this->printPackById(x);
}
else
{
cout << PACK_OUTPUT_SEPARATOR << endl;
this->printPackById(x);
}
}
if (count == 0)
cout << "Client with VAT \"" << vat << "\" not found" << endl;
return;
}
bool Agency::printPackById(const int id) const
{
cout << endl;
if (this->packList.empty())
{
cout << "No registered Packs";
return false;
}
int aux = abs(id);
for (size_t i = 0; i < this->packList.size(); i++)
{
if (abs(this->packList.at(i).getId()) == aux)
{
cout << this->packList.at(i) << endl;
return true;
}
}
cout << "Pack with id \"" << id << "\" not found" << endl;
return false;
}
void Agency::printPacksSoldToEveryone() const
{
set<int> packSet;
for (size_t i = 0; i < this->packList.size(); i++)
{
packSet.insert(abs(this->packList.at(i).getId()));
}
for (size_t i = 0; i < this->clientList.size(); i++)
{
if (packSet.empty())
break;
// Weird for loop beause we need to iterate and remove elements from the set at the same time
// Weird for loop beause we need to iterate and remove elements from the set at the same time
for (auto it = packSet.begin(); it != packSet.end(); )
{
if (!this->clientList.at(i).hasBought(*it))
{
it = packSet.erase(it);
// Returns the iterator to the next element (past the removed one)
// This is the way for c++11, in other standards it will break
}
else
++it; // If not erased, we need to move the iterator to the next element
}
}
if (packSet.empty())
{
cout << "There are no packs that have been sold to everyone" << endl;
return;
}
auto it = packSet.begin();
// First element (valid because we know it's not empty)
this->printPackById(*it);
++it;
// Iterating from the second element (if they exist)
for (; it != packSet.end(); ++it)
{
cout << PACK_OUTPUT_SEPARATOR << endl;
this->printPackById(*it);
}
}
void Agency::printPacksSold() const
{
set <int> packSet;
for (size_t i = 0; i < this->clientList.size(); i++)
{
for (size_t j = 0; j < this->clientList.at(i).getTravelPacksListSize(); j++)
{
packSet.insert(this->clientList.at(i).getTravelPackAt(j));
}
}
if (packSet.empty())
{
cout << "No packs have been sold to anyone" << endl;
}
auto it = packSet.begin();
// First element (valid because we know it's not empty)
this->printPackById(*it);
++it;
// Iterating from the second element (if they exist)
for (; it != packSet.end(); ++it)
{
cout << PACK_OUTPUT_SEPARATOR << endl;
this->printPackById(*it);
}
}
void Agency::printPacks(const bool onlyAvailable) const
{
cout << endl;
if (this->packList.size() == 0)
{
cout << "No registered packs" << endl;
return;
}
int count = 0;
for (size_t i = 0; i < this->packList.size(); i++)
{
// if onlyAvaible is activated, will only print if its available, if not, prints anyway
if (!onlyAvailable || this->packList.at(i).isAvailable())
{
if (count == 0)
{
cout << this->packList.at(i) << endl;
count++;
}
else
{
cout << PACK_OUTPUT_SEPARATOR << endl;
cout << this->packList.at(i) << endl;
}
}
}
if (count == 0)
// In this case, if there are no packs, it will enter the condition at the start
cout << "No available packs found" << endl;
}
void Agency::printPacksByDestination(const string & s, const bool onlyAvailable) const
{
string sl = s;
cu::strTrim(sl);
cu::strLower(sl);
cout << endl;
if (this->packList.size() == 0)
{
cout << "No registered packs" << endl;
return;
}
int count = 0;
for (size_t i = 0; i < this->packList.size(); i++)
{
if (this->packList.at(i).containsDestination(sl))
{
// if onlyAvaible is activated, will only print if its available, if not, prints anyway
if (!onlyAvailable || this->packList.at(i).isAvailable())
{
if (count == 0)
{
cout << this->packList.at(i) << endl;
count++;
}
else
{
cout << PACK_OUTPUT_SEPARATOR << endl;
cout << this->packList.at(i) << endl;
}
}
}
}
if (count == 0)
{
if (onlyAvailable)
cout << "No available packs matching the location \"" << s << "\"" << endl;
else
cout << "No packs matching the location \"" << s << "\"" << endl;
}
}
void Agency::printPacksByDate(const Date & start, const Date & end, const bool onlyAvailable) const
{
cout << endl;
if (this->packList.size() == 0)
{
cout << "No registered packs" << endl;
return;
}
int count = 0;
for (size_t i = 0; i < this->packList.size(); i++)
{
// if onlyAvaible is activated, will only check if its available, if not, checks anyway
if (!onlyAvailable || this->packList.at(i).isAvailable())
{
if (start <= this->packList.at(i).getDeparture()
&& end >= this->packList.at(i).getReturn())
{
if (count == 0)
{
cout << this->packList.at(i) << endl;
count++;
}
else
{
cout << PACK_OUTPUT_SEPARATOR << endl;
cout << this->packList.at(i) << endl;
}
}
}
}
if (count == 0)
{
if (onlyAvailable)
cout << "No available packs during the interval " << start << " to " << end << endl;
else
cout << "No packs during the interval " << start << " to " << end << endl;
}
}
void Agency::printPacksByDestinationAndDate(const string& s, const Date& start, const Date& end, const bool onlyAvailable) const
{
string sl = s;
cu::strTrim(sl);
cu::strLower(sl);
cout << endl;
if (this->packList.size() == 0)
{
cout << "No registered packs" << endl;
return;
}
int count = 0;
for (size_t i = 0; i < this->packList.size(); i++)
{
// If onlyAvailable is activated, will only print if its available, if not, prints anyway
// Also immediatly checks if the it's in the given Dates range
if ((!onlyAvailable || this->packList.at(i).isAvailable())
&& (start <= this->packList.at(i).getDeparture()
&& end >= this->packList.at(i).getReturn())
&& this->packList.at(i).containsDestination(sl))
{
if (count == 0)
{
cout << this->packList.at(i) << endl;
count++;
}
else
{
cout << PACK_OUTPUT_SEPARATOR << endl;
cout << this->packList.at(i) << endl;
}
}
} // End outer for loop
if (count == 0)
{
if (onlyAvailable)
cout << "No available packs matching the location \"" << s << "\"" << endl;
else
cout << "No packs matching the location \"" << s << "\"" << endl;
}
}
void Agency::printStatistics() const {
int soldPacks = 0;
int totalSold = 0;
for (size_t i = 0; i < this->clientList.size(); i++) {
totalSold += this->clientList.at(i).getTotalSpent();
soldPacks += this->clientList.at(i).getTravelPacksListSize();
}
cout << "Number of packs sold: " << soldPacks << endl;
cout << "Total value of packs sold: " << totalSold << endl;
}
void Agency::printMostVisitedDestinations(const int n) const {
int auxInt = 1;
multimap<int, string> reversedPackMap;
this->generatePopularDestinations(reversedPackMap);
for (auto iter = reversedPackMap.rbegin(); iter != reversedPackMap.rend(); ++iter) {
if (iter->first == 0) {
if (auxInt == 1)
cout << "No places visited by any client..." << endl;
break;
}
cout << '\t' << auxInt << ". " << iter->second;
cout << " (Total Visits: " << iter->first << ")\n\n";
auxInt++;
if (n + 1 == auxInt) break;
}
}
void Agency::printClientRecommendations(const int n) const {
int auxInt;
bool foundRecommendation = true;
bool auxBool = false;
string recommendation;
multimap<int, string> reversedPackMap;
this->generatePopularDestinations(reversedPackMap);
if (n <= 0) {
cout << "No possible recommendations" << endl; return;
}
for (size_t i = 0; i < this->clientList.size(); i++) {
auxInt = 1;
for (auto iter = reversedPackMap.rbegin(); iter != reversedPackMap.rend(); ++iter) {
recommendation = iter->second;
cu::strLower(recommendation);
foundRecommendation = true;
auxBool = false;
// checks if the recommendation is possible
for (size_t w = 0; w < this->packList.size(); w++) {
if (this->packList.at(w).containsDestination(recommendation) && this->packList.at(w).isAvailable()) {
auxBool = true;
break;
}
}
if (!auxBool) continue;
for (size_t j = 0; j < this->clientList.at(i).getTravelPacksListSize(); j++)
{
// checks if the client already went to the recommendation
if (getPackWithId(this->clientList.at(i).getTravelPackAt(j)).containsDestination(recommendation)) {
foundRecommendation = false;
break;
}
}
if (foundRecommendation) {
// searches for the available pack with the recommendation and prints it
for (size_t k = 0; k < this->packList.size(); k++) {
if (this->packList.at(k).containsDestination(recommendation) && this->packList.at(k).isAvailable()) {
cout << this->clientList.at(i).getName() << " recommended pack:" << endl;
cout << this->packList.at(k) << endl << endl;
break;
}
}
}
if (foundRecommendation) break;
auxInt++;
if (n + 1 == auxInt) break;
}
// in case no available recommendation is possible
if (!foundRecommendation || !auxBool) {
cout << this->clientList.at(i).getName() << ':' << endl;
cout << "Recommendation for this client not available" << endl << endl;;
}
foundRecommendation = true;
}
}
// FILE OUTPUT METHODS
void Agency::saveData() const
{
printAllClientsToFile();
printAllPacksToFile();
return;
}
// OTHER PUBLIC METHODS
void Agency::generatePopularDestinations(std::multimap<int, string> & reversedPackMap) const
{
map<string, int> packMap;
reversedPackMap.clear();
// create a map of a place and the number of times it was visited by all clients
for (size_t i = 0; i < this->clientList.size(); i++) {
for (size_t j = 0; j < this->clientList.at(i).getTravelPacksList().size(); j++) {
for (size_t k = 0; k < this->getPackWithId(this->clientList.at(i).getTravelPacksList().at(j)).getDestinationsSize(); k++) {
if (packMap.find(this->getPackWithId(this->clientList.at(i).getTravelPacksList().at(j)).getDestinationAt(k)) == packMap.end())
packMap[this->getPackWithId(this->clientList.at(i).getTravelPacksList().at(j)).getDestinationAt(k)] = 1;
else
packMap[this->getPackWithId(this->clientList.at(i).getTravelPacksList().at(j)).getDestinationAt(k)] += 1;
}
}
}
// adds in packMap the places with 0 visitors
for (size_t a = 0; a < this->packList.size(); a++) {
for (size_t b = 0; b < this->packList.at(a).getDestinationsSize(); b++) {
if (packMap.find(this->packList.at(a).getDestinationAt(b)) == packMap.end())
packMap[this->packList.at(a).getDestinationAt(b)] = 0;
}
}
// change the key and value of packMap in order to order by the number of visits
for (auto& iter : packMap)
reversedPackMap.insert(pair <int, string>(iter.second, iter.first));
}
bool Agency::purchasePack(const int vat, const int id, const int tickets)
{
Client* client = this->getClientPointer(vat);
if (client == nullptr)
{
cout << "Client with VAT \"" << vat << "\" not found" << endl;
}
TravelPack* pack = this->getPackPointer(id);
if (pack == nullptr) {
cout << "Pack with ID \"" << id << "\" not found" << endl;
return false;
}
if (!pack->isAvailable()) {
cout << "Pack with ID \"" << id << "\" is unavailable" << endl;
return false;
}
int availableTickets = pack->getMaxBookings() - pack->getCurrentBookings();
if (tickets <= 0)
{
cout << "Can't purchase less than one ticket" << endl;
return false;
}
if (tickets > availableTickets)
{
cout << "Can't overbook a Pack" << endl;
return false;
}
// After this point, all inputs were valid
// Update pack info
pack->setCurrentBookings(pack->getCurrentBookings() + tickets);
if (pack->getCurrentBookings() >= pack->getMaxBookings())
pack->makeUnavailable();
// Update client info
client->addPack(id, pack->getPrice(), tickets);
cout << "Purchase successful" << endl;
return true;
}