-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogic.cpp
1102 lines (998 loc) · 27.2 KB
/
Logic.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 "includes.h"
#include "Date.h"
#include "Place.h"
#include "Trip.h"
#include "Vehicle.h"
#include "Logic.h"
#include <algorithm>
using namespace std;
/*
* @brief default logic constructor
*/
Logic::Logic() :
bst_vehicles(VehicleWrapper()) {
}
/*
* @brief logic constructor
* @param dir directory of where the files for loading will be. Directory must be in same directory as program executable!
*/
Logic::Logic(string dir) :
bst_vehicles(VehicleWrapper()) {
if (dir != "")
dir += "\\";
cfg_dir = dir;
cfg_file_regusers = cfg_dir + CFG_FILE_REGUSERS;
cfg_file_delregusers = cfg_dir + CFG_FILE_DEL_REGUSERS;
cfg_file_destinations = cfg_dir + CFG_FILE_DESTINATIONS;
cfg_file_deldestinations = cfg_dir + CFG_FILE_DEL_DESTINATIONS;
cfg_file_curtrips = cfg_dir + CFG_FILE_CURTRIPS;
cfg_file_deltrips = cfg_dir + CFG_FILE_DELTRIPS;
cfg_file_buddies = cfg_dir + CFG_FILE_BUDDIES;
login = false;
}
/**
* @brief Returns the current date and time
* @return Date with the current date and time
*/
Date Logic::get_curDate() const {
return Date::curDate();
}
/**
* @brief Returns the vector of registered users
* @return registered users vector
*/
vector<RegPerson*>& Logic::getRegUsers() {
return regUsers;
}
/**
* @brief Returns the vector of current trips
* @return current trips vector
*/
vector<Trip*>& Logic::getCurTrips() {
return cur_trips;
}
/**
* @brief Returns the vector of old trips
* @return old trips vector
*/
vector<Trip*>& Logic::getDelTrips() {
return del_trips;
}
/**
* @brief Returns the vector of destinations
* @return destinations vector
*/
vector<Place*>& Logic::getDestinations() {
return destinations;
}
/**
* @brief Returns the vector of deleted destinations
* @return deleted destinations vector
*/
vector<Place*>& Logic::getDelDestinations() {
return del_destinations;
}
/**
* @brief Returns the BST of vehicles
* @return vehicle BST
*/
BST<VehicleWrapper>& Logic::getBST() {
return bst_vehicles;
}
/**
* @brief Returns the Hash Table of inactive users
* @return inactive users hash
*/
RegHashTable& Logic::getInactiveUsers(){
return hash_reg;
}
/**
* @brief Sets current trips vector
* @param Reference to vector of Trip *
*/
void Logic::setCurTrips(vector<Trip*>& curTrips) {
cur_trips = curTrips;
}
/**
* @brief Sets deleted trips vector
* @param Reference to vector of Trip *
*/
void Logic::setDelTrips(vector<Trip*>& delTrips) {
del_trips = delTrips;
}
/**
* @brief Sets login boolean
* @param b true or false
*/
void Logic::setLogin(bool b) {
login = b;
}
/**
* @brief Sets destinations vector
* @param Reference to vector of Place *
*/
void Logic::setDestinations(vector<Place*>& destinations) {
this->destinations = destinations;
}
/**
* @brief Sets deleted destinations vector
* @param Reference to vector of Place *
*/
void Logic::setDelDestinations(vector<Place*>& del) {
del_destinations = del;
}
/**
* @brief Sets RegUsers vector
* @param Reference to vector of RegPerson *
*/
void Logic::setRegUsers(vector<RegPerson*>& regUsers) {
this->regUsers = regUsers;
}
/**
* Receives user and password combination and validates them, updates last login attribute and removes user from inactive users
* if he was previously inactive for over 30 days
* @brief Receives user and password combination and validates them
* @param usr username to validate
* @param passw password to validates
* @return returns true if it found the user/password combination in registered users, false otherwise
*/
bool Logic::userLogin(string usr, string passw) {
for (int i = 0; i < regUsers.size(); i++) {
if (regUsers[i]->getUsern() == usr
&& regUsers[i]->getPassw() == passw) {
curr_user = regUsers[i];
if(Date::numDays(regUsers[i]->getLastLogin(),get_curDate())>30){
RegHashTable::iterator it = hash_reg.find(regUsers[i]);
if (it != hash_reg.end()) {
hash_reg.erase(regUsers[i]);
}
}
regUsers[i]->setlastLogin(get_curDate());
UnregPerson* curr_unreg = NULL;
cout << "Logged in with User: " << regUsers[i]->getUsern() << endl;
setLogin(true);
save_data();
return true;
}
}
cout << "User not found " << endl;
return false;
}
/**
* @brief Deletes destination in index of destinations vector and puts it in deleted destinations
* @param index index of destination to delete
*/
void Logic::deleteDestinations(int index) {
if (index < 0 || index >= destinations.size()) {
cout << "Invalid index chosen" << endl;
return;
}
vector<Place *>::iterator it = destinations.begin();
while (it != destinations.end() && index != 0) {
it++;
index--;
}
del_destinations.push_back(destinations[index]);
this->destinations.erase(it);
}
/**
* @brief Deletes trip in index of current trips vector and puts it in deleted trips
* @param index index of trip to delete
*/
void Logic::deleteTrips(int index) {
if (index < 0 || index >= cur_trips.size()) {
cout << "Invalid index chosen" << endl;
return;
}
vector<Trip *>::iterator it = cur_trips.begin();
while (it != cur_trips.end() && index != 0) {
it++;
index--;
}
del_trips.push_back(cur_trips[index]);
this->cur_trips.erase(it);
}
/**
* Sorts vector of trips by date
* @brief sort trips by date
* @return vector of trips
*/
vector<Trip *> Logic::tripSortByDate(vector<Trip *> v) {
sort(v.begin(), v.end(), Trip::compareTrips);
return v;
}
/**
* Sorts vector of trips by driver name
* @brief sort trips by driver name
* @return vector of trips
*/
vector<Trip *> Logic::tripSortByDriverName(vector<Trip *> v) {
sort(v.begin(), v.end(), Trip::compareTripsDriverName);
return v;
}
/**
* @brief checks is username exists
* @param n username to check
* @return true if username n exists, false otherwise
*/
bool Logic::usernameExists(string n) {
for (unsigned int i = 0; i < regUsers.size(); i++) {
if (regUsers[i]->getUsern() == n)
return true;
}
return false;
}
/**
* Finds all the trips with vacancies that have a possible itenerary starting from place src and going through or ending in place dest,
* also only returns trips where youre not a traveller or a driver
* @param src source destination
* @param dest target destination
* @param p Needed to check if already a traveller or driver in this trip
* @brief finds trips with vacancies by giving start location and end location
* @return vector of trips, empty if no trips found
*/
vector<Trip *> Logic::findTrips(string src, string dest, Person* p) {
vector<Trip *> temp = vector<Trip *>();
for (int i = 0; i < cur_trips.size(); i++) {
if (cur_trips[i]->hasDestination(src, dest)) {
if (!cur_trips[i]->isFull(src, dest))
if (!cur_trips[i]->isTraveller(p))
if (cur_trips[i]->getDriver() != p->getUsern())
temp.push_back(cur_trips[i]);
}
}
return temp;
}
/**
* Finds all the trips with vacancies
* returns trips where youre not already a traveller or a driver
* @param p Needed to check if already a traveller or driver in this trip
* @brief finds trips with vacancies
* @return vector of trips, empty if no trips found
*/
vector<Trip *> Logic::findVacantTrips(Person* p) {
vector<Trip *> temp = vector<Trip *>();
for (unsigned int i = 0; i < cur_trips.size(); i++) {
if (!cur_trips[i]->isFull(cur_trips[i]->getRoute()[0].first->getName(),
cur_trips[i]->getRoute()[cur_trips[i]->getRoute().size() - 1].first->getName()))
if (!cur_trips[i]->isTraveller(p))
if (cur_trips[i]->getDriver() != p->getUsern())
temp.push_back(cur_trips[i]);
}
return temp;
}
/**
* Finds all the trips scheduled for user P
* @brief find scheduled trips
* @return vector of trips, empty if no trips found
*/
vector<Trip *> Logic::findFutureTrips(Person * p) {
vector<Trip *> temp = vector<Trip *>();
for (int i = 0; i < cur_trips.size(); i++) {
if (cur_trips[i]->isTraveller(p)) {
temp.push_back(cur_trips[i]);
}
if (cur_trips[i]->getDriver() == ((RegPerson*) p)->getUsern()) {
temp.push_back(cur_trips[i]);
}
}
return temp;
}
/**
* Finds RegPerson pointer of user with username
* @brief find a RegPerson by username
* @return pointer to the RegPerson,NULL if not found
*/
RegPerson * Logic::findRegPerson(string username) {
for (int i = 0; i < regUsers.size(); i++) {
if (regUsers[i]->getUsern() == username)
return regUsers[i];
}
return NULL;
}
/**
* Finds RegPerson pointer of user with username
* @brief find a RegPerson by username
* @return vector with RegPerson or empty vector if not found
*/
vector<RegPerson *> Logic::findRegPersonVec(string username) {
vector<RegPerson *> temp = vector<RegPerson *>();
for (int i = 0; i < regUsers.size(); i++) {
if (regUsers[i]->getUsern() == username) {
temp.push_back(regUsers[i]);
return temp;
}
}
return temp;
}
/**
* Finds Place* of name destname
* @brief find a RegPerson by username
* @param destname The place name
* @param f optional argument, if "loading", gets destinations from deleted destinations
* @return pointer to the found place or NULL if not found
*/
Place * Logic::findDest(string destname, string f) {
for (int i = 0; i < destinations.size(); i++) {
if (destinations[i]->getName() == destname)
return destinations[i];
}
if (f == "loading")
for (int i = 0; i < del_destinations.size(); i++) {
if (del_destinations[i]->getName() == destname)
return del_destinations[i];
}
return NULL;
}
/**
* @brief check if license plate already exists
* @param n_lp license plate to check
* @return true if unique license plate, false otherwise
*/
bool Logic::isUniqueLicensePlate(string n_lp) {
if (n_lp == "-1")
return false;
for (int i = 0; i < regUsers.size(); i++) {
vector<Vehicle *> usr_v = regUsers[i]->getVehicles();
for (int j = 0; j < usr_v.size(); j++) {
if (usr_v.at(j)->getLicensePlate() == n_lp) {
return false;
}
}
}
return true;
}
/**
* @brief Lists all the regPerson objects in the hash_reg HashTable
*
* Used in the RegHashTable.
*/
void Logic::printHash(){
tr1::unordered_set<RegPerson*,hstr,eqstr>::iterator it = hash_reg.begin();
while (it!=hash_reg.end()) {
((RegPerson*)*it)->printPersonAdmin();
it++;
}
}
/**
* Loads RegPerson data members from file and creates RegPerson * objects
* to push_back into the applications vector of registered users
* Will add inactive users to hashtable (users who havent logged in for over 30 days)
* Will add vehicles to the BST
* @brief loads RegPerson objects from file
* @return 0 on success, -1 otherwise
*/
int Logic::load_regUsers() {
ifstream fin;
stringstream ss;
// -------------------
// RegUsers file
// -------------------
fin.open(cfg_file_regusers.c_str());
if (fin.fail()) {
cout << "Opening file failed " << cfg_file_regusers.c_str() << endl;
return -1;
} else {
string line;
string key = "";
string value = "";
string name = "";
unsigned long phone = -1;
string username = "";
string password = "";
string address = "";
string lastLogin = "";
vector<Vehicle *> userVehicle = vector<Vehicle *>();
//vehicle stuff
string owner = "";
string type = "";
string brand = "";
string model = "";
unsigned short int year = -1;
string license_plate = "";
unsigned long seats = -1;
while (!fin.eof()) {
getline(fin, line);
if (line == "[RegUser]") {
name = "";
phone = -1;
lastLogin = "";
userVehicle.clear();
username = "";
password = "";
address = "";
} else if (line == "[/RegUser]") {
try {
Date last_login(lastLogin);
} catch (Date::InvalidDate &d) {
cout << "Invalid login Date in file" << endl;
throw CorruptedRegUser();
}
Date login_date(lastLogin);
if (name == "" || phone == -1 || username == ""
|| password == "")
throw CorruptedRegUser();
RegPerson *user = new RegPerson(name, address, phone, username,
password, login_date);
int nrDays = Date::numDays(login_date, get_curDate());
if ( nrDays > 30) {
hash_reg.insert(user);
}
for (int i = 0; i < userVehicle.size(); i++) {
user->getVehicles().push_back(userVehicle[i]);
}
regUsers.push_back(user);
regUsers[regUsers.size() - 1]->printPerson();
} else if (line == "[Vehicle]") {
owner = "";
type = "";
brand = "";
model = "";
year = -1;
license_plate = "";
unsigned long seats = -1;
} else if (line == "[/Vehicle]") {
if (owner == "" || type == "" || brand == "" || model == ""
|| year == -1 || license_plate == "" || seats == -1)
throw CorruptedRegUser();
Vehicle *v = new Vehicle(owner, type, brand, model, year,
license_plate, seats);
bst_vehicles.insert(v);
userVehicle.push_back(v);
}
else {
ss.str("");
ss.str(line + "\n");
getline(ss, key, '=');
getline(ss, value);
if (key == "name")
name = value;
else if (key == "phone")
phone = stol(value);
else if (key == "username")
username = value;
else if (key == "password")
password = value;
else if (key == "address")
address = value;
else if (key == "owner")
owner = value;
else if (key == "brand")
brand = value;
else if (key == "model")
model = value;
else if (key == "year")
year = stol(value);
else if (key == "plate")
license_plate = value;
else if (key == "seats")
seats = stol(value);
else if (key == "type")
type = value;
else if (key == "lastLogin")
lastLogin = value;
}
}
}
fin.close();
return 0;
}
/**
* Loads place data members from file and creates Place * objects
* to push_back into the applications vector of destinations
* @brief loads Place objects from file
* @return 0 on success, -1 otherwise
*/
int Logic::load_destinations() {
ifstream fin;
stringstream ss;
// -------------------
// Destinations file
// -------------------
fin.open(cfg_file_destinations.c_str());
if (fin.fail()) {
cout << "Opening file failed " << cfg_file_destinations.c_str() << endl;
return -1;
} else {
string line;
string key = "";
string value = "";
string name = "";
int x = -1;
int y = -1;
while (!fin.eof()) {
getline(fin, line);
if (line == "[Place]") {
name = "";
x = -1;
y = -1;
} else if (line == "[/Place]") {
if (name == "" || x == -1 || y == -1)
throw CorruptedDestination();
pair<int, int> coord(x, y);
Place *pl = new Place(name, coord);
destinations.push_back(pl);
cout << destinations[destinations.size() - 1]->toString()
<< endl;
} else {
ss.str("");
ss.str(line + "\n");
getline(ss, key, '=');
getline(ss, value);
if (key == "name")
name = value;
else if (key == "x")
x = stol(value);
else if (key == "y")
y = stol(value);
}
}
}
fin.close();
return 0;
}
/**
* Loads place data members from file and creates Place * objects
* to push_back into the applications vector of deleted destinations
* These deleted destinations are useful to mantain integrity of trip history
* for each user in case a Place entry is deleted by the application admnistration
* @brief loads "deleted" Place objects from file
* @return 0 on success, -1 otherwise
*/
int Logic::load_del_destinations() {
ifstream fin;
stringstream ss;
// -------------------
// Destinations file
// -------------------
fin.open(cfg_file_deldestinations.c_str());
if (fin.fail()) {
cout << "Opening file failed " << cfg_file_deldestinations.c_str()
<< endl;
return -1;
} else {
string line;
string key = "";
string value = "";
string name = "";
int x = -1;
int y = -1;
while (!fin.eof()) {
getline(fin, line);
if (line == "[DelPlace]") {
name = "";
x = -1;
y = -1;
} else if (line == "[/DelPlace]") {
if (name == "" || x == -1 || y == -1)
throw CorruptedDelDestination();
pair<int, int> coord(x, y);
Place *pl = new Place(name, coord);
del_destinations.push_back(pl);
cout
<< del_destinations[del_destinations.size() - 1]->toString()
<< endl;
} else {
ss.str("");
ss.str(line + "\n");
getline(ss, key, '=');
getline(ss, value);
if (key == "name")
name = value;
else if (key == "x")
x = stol(value);
else if (key == "y")
y = stol(value);
}
}
}
fin.close();
return 0;
}
/**
* Loads trip data members from file and creates Trip * objects
* to push_back into the applications vector of trips, if the trip's
* start date is lower than the current date, it will be added into the
* deleted trips vector instead and push_backed into the vector of tripHistory
* of the participants of said trip
* @brief loads Trip objects from file
* @return 0 on success, -1 otherwise
*/
int Logic::load_trips() {
ifstream fin;
stringstream ss;
// -------------------
// Trips file
// -------------------
fin.open(cfg_file_curtrips.c_str());
if (fin.fail()) {
cout << "Opening file failed " << cfg_file_curtrips.c_str() << endl;
return -1;
} else {
string line;
string key = "";
string value = "";
string vehicleowner = "";
string plate = "";
unsigned long seats = -1;
string place = "";
bool smoking = false;
string beginDate = "";
string endDate = "";
vector<pair<Place *, int> > route = vector<pair<Place *, int> >();
Person * driverP;
//Traveller stuff
string tuser = "";
string tplace = "";
vector<Place *> tplaces = vector<Place*>();
vector<pair<Person *, vector<Place *> > > travellers = vector<
pair<Person *, vector<Place *> > >();
while (!fin.eof()) {
getline(fin, line);
if (line == "[Trip]") {
plate = "";
vehicleowner = "";
seats = -1;
smoking = false;
place = "";
beginDate = "";
endDate = "";
route.clear();
travellers.clear();
} else if (line == "[/Trip]") {
if (vehicleowner == "" || plate == "" || seats == -1
|| route.size() == 0)
throw CorruptedTrip();
try {
Date start_date(beginDate);
} catch (Date::InvalidDate &d) {
cout << "Invalid start Date in file" << endl;
throw CorruptedTrip();
}
try {
Date end_date(endDate);
} catch (Date::InvalidDate &d) {
cout << "Invalid finish in file" << endl;
throw CorruptedTrip();
}
//make dates after confirming they're valid
Date start_date(beginDate);
Date end_date(endDate);
Trip *trip = new Trip(vehicleowner, plate, seats, smoking,
start_date, end_date);
trip->addRoute(route);
if (driverP != NULL) {
driverP->addTripHistory(trip);
}
for (int i = 0; i < travellers.size(); i++) {
trip->addTraveller(travellers[i].first,
travellers[i].second);
travellers[i].first->addTripHistory(trip);
}
if (trip->getStart() < get_curDate()) {
del_trips.push_back(trip);
cout << "OLD_TRIP:" << trip->toString() << endl;
} else {
cur_trips.push_back(trip);
cout << "CUR_TRIP:" << trip->toString() << endl;
}
} else if (line == "[Traveller]") {
tuser = "";
tplace = "";
tplaces.clear();
} else if (line == "[/Traveller]") {
if (tuser == "" || tplace == "")
throw CorruptedTrip();
Person * passenger;
if (tuser == "unregistered") {
passenger = new UnregPerson();
} else {
passenger = findRegPerson(tuser);
if (passenger == NULL) {
cout << "Invalid traveler: " << tuser << endl;
throw CorruptedTrip();
}
}
pair<Person *, vector<Place *> > traveler(passenger, tplaces);
travellers.push_back(traveler);
}
else {
ss.str("");
ss.str(line + "\n");
getline(ss, key, '=');
getline(ss, value);
if (key == "vehicleOwner") {
vehicleowner = value;
driverP = findRegPerson(value);
} else if (key == "plate")
plate = value;
else if (key == "seats")
seats = stol(value);
else if (key == "place") {
place = value;
Place * p = findDest(value, "loading");
if (p == NULL) {
cout << "Invalid place in trips file " << endl;
throw CorruptedTrip();
}
pair<Place *, int> r(p, 0);
route.push_back(r);
} else if (key == "tplace") {
tplace = value;
Place * p = findDest(value, "loading");
if (p == NULL) {
cout << "Invalid place in trips file " << endl;
throw CorruptedTrip();
}
tplaces.push_back(p);
} else if (key == "smoking") {
smoking = stol(value);
}
else if (key == "begindate")
beginDate = value;
else if (key == "finishdate")
endDate = value;
else if (key == "traveller")
tuser = value;
}
}
}
fin.close();
return 0;
}
/**
* Loads all the program information from the files (their names are defined in the top of Logic.h as constants).\n
* This includes the registeredUsers, destinations(the ones that exist and a record of deleted ones) and trips \n\n
*
* @brief Loads all the Program information from files
* @return 0 upon success, non-zero otherwise
*/
int Logic::load_data() {
try {
load_regUsers();
} catch (CorruptedRegUser& e) {
return -1;
}
try {
load_destinations();
} catch (CorruptedDestination& e) {
return -1;
}
try {
load_del_destinations();
} catch (CorruptedDelDestination& e) {
return -1;
}
try {
load_trips();
} catch (CorruptedTrip& e) {
return -1;
}
return 0;
}
////////////////--fix comments from here
/**
* Saves RegPerson data members to file
* @brief saves RegPerson objects to file
* @return 0 on success, -1 otherwise
*/
int Logic::save_regUsers() {
ofstream fout;
// -------------------
// RegUsers file
// -------------------
fout.open(cfg_file_regusers.c_str());
if (fout.fail()) {
cout << "Opening file failed " << cfg_file_regusers.c_str() << endl;
throw CorruptedRegUser();
}
else {
for (int i = 0; i < regUsers.size(); i++) {
fout << "[RegUser]\n";
fout << "name=" << regUsers[i]->getName() << endl;
fout << "address=" << regUsers[i]->getAddress() << endl;
fout << "phone=" << regUsers[i]->getTelNr() << endl;
fout << "username=" << regUsers[i]->getUsern() << endl;
fout << "password=" << regUsers[i]->getPassw() << endl;
fout << "lastLogin=" << regUsers[i]->getLastLogin() << endl;
if (regUsers[i]->getHasVehicle()) {
for (int j = 0; j < regUsers[i]->getVehicles().size(); j++) {
fout << "[Vehicle]\n";
fout << "owner="
<< regUsers[i]->getVehicles().at(j)->getOwner()
<< endl;
fout << "type="
<< regUsers[i]->getVehicles().at(j)->getType()
<< endl;
fout << "brand="
<< regUsers[i]->getVehicles().at(j)->getBrand()
<< endl;
fout << "model="
<< regUsers[i]->getVehicles().at(j)->getModel()
<< endl;
fout << "year="
<< regUsers[i]->getVehicles().at(j)->getYear()
<< endl;
fout << "plate="
<< regUsers[i]->getVehicles().at(j)->getLicensePlate()
<< endl;
fout << "seats="
<< regUsers[i]->getVehicles().at(j)->getCarSeats()
<< endl;
fout << "[/Vehicle]\n";
}
}
fout << "[/RegUser]\n";
}
}
fout.close();
return 0;
}
/**
* Saves buddies of registeredUsers data to file
* @brief saves buddies to file
* @return 0 on success, -1 otherwise
*/
int Logic::save_buddies() {
ofstream fout;
// -------------------
// Buddies file
// -------------------
fout.open(cfg_file_buddies.c_str());
if (fout.fail()) {
cout << "Opening file failed " << cfg_file_buddies.c_str() << endl;
throw CorruptedBuddies();
} else {
for (int i = 0; i < regUsers.size(); i++) {
if(regUsers[i]->getBuddies().size()>0)
{
fout << "[Buddies]\n";
fout << "name=" << regUsers[i]->getUsern() << endl;
for(int j = 0; j < regUsers[i]->getBuddies().size();j++)
{
fout << "friend=" << regUsers[i]->getBuddies()[j]->getUsern() << endl;
}
fout << "[/Buddies]\n";
}
}
}
fout.close();
return 0;
}
/**
* Saves place data members to file
* @brief saves Place objects to file
* @return 0 on success, -1 otherwise
*/
int Logic::save_destinations() {
ofstream fout;
// -------------------
// Destinations file
// -------------------
fout.open(cfg_file_destinations.c_str());
if (fout.fail()) {
cout << "Opening file failed " << cfg_file_destinations.c_str() << endl;
throw CorruptedDestination();
} else {
for (int i = 0; i < destinations.size(); i++) {
fout << "[Place]\n";
fout << "name=" << destinations[i]->getName() << endl;
fout << "x=" << destinations[i]->getCoords().first << endl;
fout << "y=" << destinations[i]->getCoords().second << endl;
fout << "[/Place]\n";
}
}
fout.close();
return 0;
}
/**
* Saves place data members to file
* These deleted destinations are useful to mantain integrity of trip history
* for each user in case a Place entry is deleted by the application admnistration
* @brief saves "deleted" Place objects from file
* @return 0 on success, -1 otherwise
*/
int Logic::save_del_destinations() {
ofstream fout;
// -------------------
// Destinations file
// -------------------
fout.open(cfg_file_deldestinations.c_str());
if (fout.fail()) {
cout << "Opening file failed " << cfg_file_deldestinations.c_str()
<< endl;
throw CorruptedDelDestination();
} else {
for (int i = 0; i < del_destinations.size(); i++) {
fout << "[DelPlace]\n";
fout << "name=" << del_destinations[i]->getName() << endl;
fout << "x=" << del_destinations[i]->getCoords().first << endl;
fout << "y=" << del_destinations[i]->getCoords().second << endl;
fout << "[/DelPlace]\n";
}
}