-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
1811 lines (1509 loc) · 58 KB
/
test.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
//
// Created by bzahov on 24.11.19 г..
//
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <list>
#include <exception>
#include <utility>
#include <vector>
#include <regex>
#include <functional>
#include <bits/stdc++.h>
using namespace std;
// ----Const strings
static const char *const MENU_EXIT_OPTIONS = "0.For exit(0 or 'q' or 'Q')";
static const char *const MENU_EXITING = "Exiting menu";
static const char *const MENU_EXITING_WITHOUT_SAVING = "Exiting menu without saving...";
static const char *const MENU_CHOSEN = "Chosen > ";
static const char *const MENU_WRONG_OPTION_ERROR = "Please select right option";
static const char *const ADMIN_MENU_NEW_POINT = "1.Create new Point object/s";
static const char *const ADMIN_MENU_NEW_ROUTE = "2.Create new Route object/s";
static const char *const ADMIN_MENU_NEW_CAR = "3.Create new Car object/s";
static const char *const ADMIN_MENU_NEW_TAXI = "4.Create new Taxi object/s";
static const char *const ADMIN_MENU_NEW_TAXI_NO_ROUTES = "4.Create new Taxi object/s without routes";
static const char *const ADMIN_MENU_CALCULATE_ROUTE_TOTAL_DISTANCE = "5.Calculate total distance for all Routes";
static const char *const ADMIN_MENU_CALCULATE_TAXI_FUEL_USAGE = "6.Calculate fuel per km for all Taxi routes";
static const char *const CAR_MENU_ADD_REG_NUMBER = "1. Add car's Registration Number";
static const char *const CAR_MENU_ADD_MODEL = "2. Add car's model";
static const char *const CAR_MENU_NUM_SEATS = "3. Add number of Seats";
static const char *const CAR_MENU_BRAND = "4. Add Car's brand";
static const char *const CAR_MENU_YEARS_OF_USE = "5. Add car's years of use";
static const char *const CAR_MENU_FUEL_PER_KM = "6. Add car's fuel per km usage";
static const char *const CAR_MENU_SAVE_CAR = "7. Save current car specifications";
static const char *const ROUTE_MENU_ADD_NAME = "1. Add Route's Name";
static const char *const ROUTE_MENU_ADD_DISTANCE = "2. Add Route's distance";
static const char *const ROUTE_MENU_NUM_DAILY_REPEAT = "3. Add number of daily repeats";
static const char *const ROUTE_MENU_ADD_POINT = "4. Add to the Route new point";
static const char *const ROUTE_MENU_CALC_TOTAL_DISTANCE = "5. Calculate Route's total distance";
//static const char *const ROUTE_MENU_FUEL_PER_KM = "6. Add Route's fuel per km usage";
static const char *const ROUTE_MENU_SAVE = "6. Save current Route specifications";
static const char *const POINT_MENU_ADD = "1. Add Point attributes";
static const char *const POINT_MENU_SAVE = "2. Save current Point specifications";
static const char *const TAXI_MENU_ADD_DRIVER_NAME = "1. Add Taxi's Driver Name";
static const char *const TAXI_MENU_ADD_ROUTE = "2. Add Route's information";
static const char *const TAXI_MENU_ADD_CAR = "3. Add Car's information";
static const char *const TAXI_MENU_ADD_NONE = "4. NONE Add to Car new Route";
static const char *const TAXI_MENU_CALC_TOTAL_DISTANCE = "5. Calculate Route's total distance";
//static const char *const TAXI_MENU_FUEL_PER_KM = "6. Add Route's fuel per km usage";
static const char *const TAXI_MENU_SAVE = "6. Save current Taxi's specifications";
//static const char *const TAXI_MENU_ADD_DRIVER_NAME = "1. Add Taxi's Driver Name";
static const char *const FILE_PATH_ROOT = "/home/bzahov/Documents/gitRepos/TU-University-Tasks/Programming Languages(PE) C++/Tasks/TaxiRouteSystem(CourseWork)/car_database.txt";
static const char *const FILE_PATH_CAR = "car_database.txt";
static const char *const FILE_PATH_POINT = "point_database.txt";
static const char *const FILE_PATH_ROUTE = "route_database.txt";
static const char *const FILE_PATH_TAXI = "taxi_database.txt";
static const char *const FILE_PATH_DEBUG = "debug_database.txt";
static const char *const REGEX_DESERIALIZATION_OF_DATA = "([,|>](\\w+)[:])";
static const char *const REGEX_NAME = "([[A-Z a-z])+";
static const char *const REGEX_CAR_NUMBER = "^([A-Z]{2}[0-9]{4}[A-Z]{2})";
static const char *const REGEX_INFO = "([A-Z a-z0-9])+";
static regex regexName(REGEX_NAME);
static regex regexInfo(REGEX_INFO);
static regex regexRegNumb(REGEX_CAR_NUMBER);
static const char *const SERIALIZATION_STR_DEFAULT = "";
static const string PREFIX_POINT = ">PointName";
static const string PREFIX_CAR = ">CarName";
static const string PREFIX_ROUTE = ">RouteName";
static const string PREFIX_TAXI = ">TaxiName";
static const bool DEBUG = false;
static const char *const DELIMITER_START_LINE = ">";
// ---Class declarations--------------
class Serializable;
class InvalidInputException;
class Point;
class Car;
class Taxi;
class Route;
class InvalidInputException : public std::exception {
public:
string generalInfo = "InvalidInputException in: ";
string info;
explicit InvalidInputException(string info) : info(std::move(info)) {}
string getMessage() const {
string result = generalInfo + info;
return result;
}
const char *what() const noexcept override {
return getMessage().c_str();
}
};
//--------Declaration of Methods---------
template<class T>
static bool getAndIterateEachLineOfFileWithCallbackSaveIntoVector(const string &fileName, vector<T> &dataVector,
void (*callback)(const string &, vector<T> &));
template<class T>
static void extractDataFromLineIntoGenericsList(const string &lineOfData, list<T> &list);
template<class T>
static void extractDataFromLineIntoGenericsObj(const string &lineOfData, T &dataObject);
template<class T>
static bool getAndPutFileContentIntoGenericsVector(const string &fileName, vector<T> &vector);
template<class T>
static void extractDataFromLineIntoGenericsVector(const string &lineOfData, vector<T> &vectorWithData);
static void extractDataFromLineIntoPointVector(const string &lineOfData, vector<Point> &pointsVector);
static void extractDataFromLineIntoCarVector(const string &lineOfData, vector<Car> &carsVector);
template<class T>
static void listToString(list<T> list, string &result);
template<class T>
static void stringToList(const string &stringWithData, list<T> &);
static string insertDelimiterAtBeginnig(string &b, const string &delimiter);
static void removeWhiteSpacesOfString(string &stringWithData);
static void deserializeStrData(string &stringWithData);
static void adminMenu() noexcept(false);
static bool pointsMenu(bool singlePick = false);
static bool carsMenu(/*vector<Car> &resultVector,*/ bool singlePick = false);
static bool routesMenu(/*vector<Route> &resultVector,*/ bool singlePick = false);
static bool taxiMenu();
static void loadDataVectorsFromFiles();
static vector<string> split(const string &str, const string &delimiter);
//---------------------
void testTaxiFunctionality();
template<class T>
static bool isItemExistInVector(basic_string<char> identificationName, const vector<T> &dataVector);
static vector<Car> carsVector;
static vector<Route> routesVector;
static vector<Taxi> taxiVector;
static vector<Point> pointVector;
//********
class Serializable {
public:
virtual void saveToFile(string fileName) = 0;
virtual bool isObjectInFileDatabase(string filename) = 0;
virtual string getIdentification() const = 0;
virtual string toString() const = 0;
virtual void print() = 0;
virtual string getFilePath() const = 0;
};
//-------------
static bool isValidCoords(double a, double b);
class Point : Serializable {
private:
string name;
double x{};
double y{};
public:
Point() {
setXY(0, 0);
name = SERIALIZATION_STR_DEFAULT;
}
Point(const string &newName, double xx, double yy) {
setXY(xx, yy);
setName(name);
}
Point(double x, double y) : x(x), y(y) {}
virtual ~Point() {
setXY(0, 0);
}
double getX() const {
return x;
}
void setX(double newX) noexcept(false) {
if (newX >= 0) {
Point::x = newX;
} else throw InvalidInputException("Point -> setX");
}
double getY() const {
return y;
}
void setY(double y) {
Point::y = y;
}
const string &getName() const;
bool isValid() const;
void setName(const string &newName) noexcept(false);
void setXY(const double a, const double b) {
if (isValidCoords(a, b)) {
x = a;
y = b;
} else {
throw InvalidInputException("Invalid Coords");
}
};
void print() override;
string toString() const override;
friend ostream &operator<<(ostream &os, const Point &point) {
os << point.toString();
return os;
}
friend istream &operator>>(istream &is, Point &point) {
is >> point.name;
is >> point.x;
is >> point.y;
return is;
}
public:
void saveToFile(string fileName) override;
bool isObjectInFileDatabase(string filename) override;
bool isEmpty();
string getIdentification() const override;
string getFilePath() const override {
return FILE_PATH_POINT;
}
};
string Point::getIdentification() const {
return getName();
}
const string &Point::getName() const {
return name;
}
void Point::setName(const string &newName) noexcept(false) {
if (regex_match(newName, regexName)) {
Point::name = newName;
} else { throw InvalidInputException("Point: Invalid Name: " + newName); }
}
bool Point::isValid() const {
return getX() > 0 && getY() > 0;
}
void Point::print() {
cout << toString();
}
string Point::toString() const {
return PREFIX_POINT + ":" + getName() + ",X: " + to_string(getX()) + ",Y: " + to_string(getY());
}
void Point::saveToFile(string fileName) {
if (!isObjectInFileDatabase(fileName)) {
cout << "saving Point: " << Point::toString() << " to file: " << fileName << endl;;
std::ofstream ofs(fileName, fstream::in | fstream::out | fstream::app);
ofs << this->toString() << endl;
ofs.close();
} else {
cout << "Item Point with Identification Name: " << getIdentification() << " - already in the file" << endl;
}
}
bool Point::isObjectInFileDatabase(string filename) {
// TODO: make one more check inside file database
return isItemExistInVector(getIdentification(), pointVector);
}
bool Point::isEmpty() {
return name.empty() && x == 0 && y == 0;
}
bool isValidCoords(double a, double b) {
return a >= 0 && b >= 0;
}
// ---------
class Car : Serializable {
protected:
string registrationNumber;
string brand;
string model;
int numSeats;
double fuelPerKm;
int yearsOfUse;
public:
Car() {
brand = SERIALIZATION_STR_DEFAULT;
model = SERIALIZATION_STR_DEFAULT;
yearsOfUse = 0;
numSeats = 0;
fuelPerKm = 0;
registrationNumber = SERIALIZATION_STR_DEFAULT;
}
Car(const string &brand, const string &model, int yearsOfUse, int numSeats, double fuelPerKm,
const string ®istrationNumber) noexcept(false) {
setBrand(brand);
setModel(model);
setYearsOfUse(yearsOfUse);
setNumSeats(numSeats);
setFuelPerKm(fuelPerKm);
setRegistrationNumber(registrationNumber);
}
const string &getRegistrationNumber() const;
bool setRegistrationNumber(const string ®Number) noexcept(false);
const string &getBrand() const {
return brand;
}
// TODO: make input check
void setBrand(const string &newBrand) noexcept(false) {
if (regex_match(newBrand, regexInfo)) {
Car::brand = newBrand;
} else {
throw InvalidInputException("Invalid Brand: " + newBrand);
}
}
const string &getModel() const {
return model;
}
void setModel(const string &newModel) {
if (regex_match(newModel, regexInfo)) {
this->model = newModel;
} else { throw InvalidInputException("Car: Invalid Model: " + newModel); }
}
int getYearsOfUse() const {
return yearsOfUse;
}
void setYearsOfUse(int newYearsOfUse) noexcept(false) {
if (newYearsOfUse >= 0) {
Car::yearsOfUse = newYearsOfUse;
} else { throw InvalidInputException("Invalid newYearsOfUse"); }
}
int getNumSeats() const {
return numSeats;
}
void setNumSeats(int newNumSeats) noexcept(false) {
if (newNumSeats >= 0) {
Car::numSeats = newNumSeats;
} else { throw InvalidInputException("Invalid newNumSeats"); }
}
double getFuelPerKm() const {
return fuelPerKm;
}
void setFuelPerKm(double newFuelPerKm) noexcept(false) {
if (newFuelPerKm >= 0.1) {
Car::fuelPerKm = newFuelPerKm;
} else { throw InvalidInputException("Invalid newFuelPerKm"); }
}
void print()
override;
string toString() const override;
void saveToFile(string fileName) override;
bool isObjectInFileDatabase(string filename) override;
// static std::vector<Car> readAllFromFile(const string& str);
friend ostream &operator<<(ostream &os, const Car &car) {
os << car.toString() << endl;
return os;
}
friend istream &operator>>(istream &is, Car &car) {
//cout<< CAR_MENU_ADD_REG_NUMBER;
is >> car.registrationNumber;
//cout << CAR_MENU_BRAND;
is >> car.brand;
//cout << CAR_MENU_ADD_MODEL;
is >> car.model;
//cout << CAR_MENU_NUM_SEATS;
is >> car.numSeats;
is >> car.fuelPerKm;
//cout << CAR_MENU_YEARS_OF_USE;
is >> car.yearsOfUse;
return is;
}
string getIdentification() const
override;
bool isCarDataValid() const;
string getFilePath() const override {
return FILE_PATH_CAR;
}
};
string Car::getIdentification() const {
return registrationNumber;
}
string Car::toString() const {
string result =
PREFIX_CAR + ": " + getRegistrationNumber() +
",brand: " + getBrand() +
",model: " + getModel() +
",seats: " + to_string(getNumSeats()) +
",fuelPerKm: " + to_string(getFuelPerKm()) +
",yearsOfUse:" + to_string(getYearsOfUse());
return result;
}
void Car::print() {
cout << "Car info:\n" << Car::toString() << endl;
}
const string &Car::getRegistrationNumber() const {
return registrationNumber;
}
bool Car::setRegistrationNumber(const string ®Num) noexcept(false) {
if (regex_match(regNum, regexRegNumb)) {
Car::registrationNumber = regNum;
return true;
} else throw InvalidInputException("Wrong registration Number");
return false;
}
void Car::saveToFile(const string fileName) {
if (!isObjectInFileDatabase(fileName)) {
cout << "saving car: " << Car::toString() << " to file: " << fileName << endl;;
std::ofstream ofs(fileName, fstream::in | fstream::out | fstream::app);
ofs << this->toString() << endl;
ofs.close();
} else {
cout << "Item Car with Identification Name: " << getIdentification() << " - already in the file" << endl;
}
}
bool Car::isObjectInFileDatabase(string filename) {
// TODO: make one more check inside file database
return isItemExistInVector(getIdentification(), carsVector);
}
//std::vector<Car> Car::readAllFromFile(const string& str) {
// std::vector<Car> result;
// /*char *line= "";
// size_t len = 0;
// ssize_t nread;
// cout << "\n-----g3g---\n";
///*
//
// stream = fopen(FILE_PATH_CAR, "r");
// if (stream == NULL) {
// perror("fopen");
// throw (InvalidInputException("wrong file path"));
// }
//
//
// while ((nread = getline(&line, &len, stream)) != -1) {
// printf("Retrieved line of length %zu:\n", nread);
// cout << line;
// //fwrite(line, nread, 1, stdout);
// }
//
// free(line);
// fclose(stream);
////-----
// std::ifstream ifs(FILE_PATH_CAR,fstream::in);
// cout<< ifs.get();
// ifs.getline(line,256);
// cout << line;
// ifs.close();*/
//
// std::ifstream infile(FILE_PATH_CAR, fstream::in);
// int a, b;
// while (infile >> a >> b) {
// // process pair (a,b)
// }
// return result;
//}
bool Car::isCarDataValid() const {
try {
Car(brand, model, yearsOfUse, numSeats, fuelPerKm, registrationNumber);
} catch (InvalidInputException &e) {
cerr << "Car: " << e.getMessage() << endl;
return false;
}
return true;
}
//------------
class Route : Serializable {
private:
string name;
double distance;
int dailyRepeat;
list<Point> routePointsList;
public:
Route() {
name = "";
distance = 0;
dailyRepeat = 0;
}
Route(const string &name, double distance, int dailyRepeat) {
setName(name);
setDistance(distance);
setDailyRepeat(dailyRepeat);
}
const list<Point> &getRoutePointsList() const;
void setRoutePointsList(const list<Point> &newRoutePointsList);
const string &getName() const {
return name;
}
void setName(const string &newName) noexcept(false) {
if (regex_match(newName, regexName)) {
Route::name = newName;
} else throw InvalidInputException("Route: Wrong Name");
}
double getDistance() const {
return distance;
}
void setDistance(double dist) { // RENAMED param
if (dist > 0) {
Route::distance = dist;
} else throw InvalidInputException("Route: Distance is too short");
}
int getDailyRepeat() const {
return dailyRepeat;
}
void setDailyRepeat(int newDailyRepeat) {
if (newDailyRepeat > 0) {
Route::dailyRepeat = newDailyRepeat;
} else throw InvalidInputException("Route: newDailyRepeat is invalid");
}
void addNewRoutePoint(const Point &p);
string toString() const override;
void print() override;
void saveToFile(string fileName) override;
bool isObjectInFileDatabase(string filename) override;
friend ostream &operator<<(ostream &os, const Route &route) {
os << route.toString();
return os;
}
friend istream &operator>>(istream &is, Route &route) {
is >> route.name;
is >> route.distance;
is >> route.dailyRepeat;
// add inner points data
do {
Point point;
is >> point;
// cerr << endl << point.toString() << endl;
if (point.isEmpty()) {
break;
}
route.addNewRoutePoint(point);
} while (true);
return is;
}
double calcRouteTotalDistance() const {
return dailyRepeat * distance;
}
string getIdentification() const override;
bool isValid() const;
string getFilePath() const override {
return FILE_PATH_ROUTE;
}
};
string Route::getIdentification() const {
return name;
}
void Route::addNewRoutePoint(const Point &p) noexcept(false) {
if (p.isValid()) {
routePointsList.push_back(p);
} else
throw InvalidInputException(
"Route -> Point " + p.getName() + " with coords(x,y): " + to_string(p.getX()) + "," +
to_string(p.getY()) + " is invalid ");
}
string Route::toString() const {
string pointsStr;
listToString(routePointsList, pointsStr);
return PREFIX_ROUTE + ":" + name + ",distance: " + to_string(distance) + ",dailyRepeat: " +
to_string(dailyRepeat) + "" + pointsStr;
}
void Route::print() {
cout << Route::toString();
}
void Route::saveToFile(string fileName) {
// check does nested objects are in the database, if not, they will be added
for (auto point : getRoutePointsList()) {
point.saveToFile(FILE_PATH_POINT);
}
if (!isObjectInFileDatabase(fileName)) {
cout << "saving Route: " << Route::toString() << " to file: " << fileName << endl;
std::ofstream ofs(fileName, fstream::in | fstream::out | fstream::app);
ofs << this->toString() << endl;
ofs.close();
} else {
cout << "Item Route with Identification Name: " << getIdentification() << " - already in the file" << endl;
}
}
bool Route::isObjectInFileDatabase(string filename) {
// TODO: make one more check inside file database
return isItemExistInVector(getIdentification(), routesVector);
}
const list<Point> &Route::getRoutePointsList() const {
return routePointsList;
}
void Route::setRoutePointsList(const list<Point> &newRoutePointsList) {
routePointsList = newRoutePointsList;
}
bool Route::isValid() const {
try {
Route route(name, distance, dailyRepeat);
route.setRoutePointsList(this->getRoutePointsList());
} catch (InvalidInputException &e) {
cerr << e.getMessage() << endl;
return false;
}
return true;
}
// -----------
class Taxi : public Car {
protected:
string driverName;
Route currentRoute;
public:
Taxi() : Car() {}
Taxi(const string &newDriverName, const string &brand, const string &model, int yearsOfUse,
int numSeats, double fuelPerKm, const string &driverName, const string ®Num)
: Car(brand, model, yearsOfUse, numSeats, fuelPerKm, regNum) {
setDriverName(newDriverName);
}
Taxi(const string &newDriverName, const Car &car) : Car(car.getBrand(), car.getModel(), car.getYearsOfUse(),
car.getNumSeats(), car.getFuelPerKm(),
car.getRegistrationNumber()) {
setDriverName(newDriverName);
}
Taxi(const string &newDriverName, const Car &car, const Route &route)
: Car(car.getBrand(), car.getModel(), car.getYearsOfUse(), car.getNumSeats(), car.getFuelPerKm(),
car.getRegistrationNumber()) {
setDriverName(newDriverName);
setCurrentRoute(route);
}
Route getCurrentRoute() const {
return currentRoute;
}
void setCurrentRoute(const Route &newRoute) noexcept(false) {
if (newRoute.isValid()) {
Taxi::currentRoute = newRoute;
} else throw InvalidInputException("Taxi: setCurrentRoute -> route is invalid\"");
}
const string &getDriverName() const {
return driverName;
}
void setDriverName(const string &newDriverName) noexcept(false) {
if (regex_match(newDriverName, regexName)) {
Taxi::driverName = newDriverName;
} else throw InvalidInputException("Taxi: newDriverName -> is invalid\"");
}
void print() override;
double calculatePerDayFuelUse() const;
string toString() const override;
void saveToFile(string fileName) override;
bool isObjectInFileDatabase(string filename) override;
void addNewRoutePoint(const Point &p);
void addNewCarInfo(const Car &car);
friend ostream &operator<<(ostream &os, const Taxi &taxi);
friend istream &operator>>(istream &is, Taxi &taxi);
string getIdentification() const override;
// Car getCurrentCarInfo();
void setCurrentCarInfo(const Car &car);
bool isTaxiDataValid() const;
Car getCurrentCarInfo() const;
string getFilePath() const override {
return FILE_PATH_TAXI;
}
};
bool Taxi::isTaxiDataValid() const {
try {
bool isValid = regex_match(driverName, regexName) && getCurrentCarInfo().isCarDataValid() &&
getCurrentRoute().isValid();
if (!isValid) {
cerr << "Taxi: is not valid" << endl;
}
return isValid;
// Car c(brand, model, yearsOfUse, numSeats, fuelPerKm, registrationNumber);
// Route();
} catch (InvalidInputException &e) {
cerr << "Taxi: " << e.getMessage() << endl;
return false;
}
return true;
}
string Taxi::getIdentification() const {
return driverName;
}
ostream &operator<<(ostream &os, const Taxi &taxi) {
os << taxi.toString() << endl;
return os;
}
istream &operator>>(istream &is, Taxi &taxi) {
// (Car) taxi;
is >> taxi.driverName;
Car car;
Route route;
// cout << TAXI_MENU_ADD_DRIVER_NAME;
is >> car;
is >> route;
taxi.addNewCarInfo(car);
taxi.setCurrentRoute(route);
return is;
}
void Taxi::print() {
cout << "Taxi info: \n" + this->toString() << endl;
}
double Taxi::calculatePerDayFuelUse() const {
//извежда информация колко гориво да се зареди за извършване нa дневната обиколка
// (repeats * distance) * fuelPerKM
double result = this->getCurrentRoute().calcRouteTotalDistance() * fuelPerKm;
cout << endl << "CalculatePerDayFuelUse: " << result << " liters of fuel for Taxi with: daily Repeats: "
<< getCurrentRoute().getDailyRepeat() << " distance: " << getCurrentRoute().getDistance() << " fuelPerKm: "
<< fuelPerKm << endl;
return result;
}
string Taxi::toString() const {
return PREFIX_TAXI + ":" + driverName + Car::toString().append(currentRoute.toString());
}
void Taxi::saveToFile(string fileName) {
// check does nested objects are in the database, if not, they will be added
getCurrentRoute().saveToFile(FILE_PATH_ROUTE);
getCurrentCarInfo().saveToFile(FILE_PATH_CAR);
if (!isObjectInFileDatabase(fileName)) {
cout << "saving taxi: " << Taxi::toString() << " to file: " << fileName << endl;
std::ofstream ofs(fileName, fstream::in | fstream::out | fstream::app);
ofs << Taxi::toString() << endl;
ofs.close();
} else {
cout << "Item Taxi with Identification Name: " << getIdentification() << " - already in the file" << endl;
}
}
bool Taxi::isObjectInFileDatabase(string filename) {
// TODO: make one more check inside file database
return isItemExistInVector(getIdentification(), taxiVector);
}
void Taxi::addNewRoutePoint(const Point &p) {
if (p.isValid()) {
this->getCurrentRoute().addNewRoutePoint(p);
} else cerr << "Taxi addNewRoutePoint -> Point is invalid" << endl;
}
void Taxi::addNewCarInfo(const Car &car) {
this->setModel(car.getModel());
this->setBrand(car.getBrand());
this->setRegistrationNumber(car.getRegistrationNumber());
this->setNumSeats(car.getNumSeats());
this->setFuelPerKm(car.getFuelPerKm());
this->setYearsOfUse(car.getYearsOfUse());
}
Car Taxi::getCurrentCarInfo() const {
return Car(brand, model, yearsOfUse, numSeats, fuelPerKm,
registrationNumber);
}
void Taxi::setCurrentCarInfo(const Car &car) {
setBrand(car.getBrand());
setModel(car.getModel());
setYearsOfUse(car.getYearsOfUse());
setNumSeats(car.getNumSeats());
setFuelPerKm(car.getFuelPerKm());
setRegistrationNumber(car.getRegistrationNumber());
}
// --------Implementation of static methods--------------------
void getCarFromMenu(Car &newCar);
void getRouteFromMenu(Route &newRoute);
//void getTaxiFromMenu(Taxi &newTaxi);
void getPointFromMenu(Point &point);
static void adminMenu() noexcept(false) {
bool isLooping = true;
cout << endl << "------------Main Menu-------------------------------------------" << endl
<< "Hello to taxi route system" << endl
<< "Hello taxi manager ";
do {
cout << endl << "----------------------------------------------------------------" << endl
<< "Please choose from the following options:" << endl
<< ADMIN_MENU_NEW_POINT << endl
<< ADMIN_MENU_NEW_ROUTE << endl
<< ADMIN_MENU_NEW_CAR << endl
<< ADMIN_MENU_NEW_TAXI << endl
// << ADMIN_MENU_NEW_TAXI_NO_ROUTES << endl
<< ADMIN_MENU_CALCULATE_ROUTE_TOTAL_DISTANCE << endl
<< ADMIN_MENU_CALCULATE_TAXI_FUEL_USAGE << endl
<< MENU_EXIT_OPTIONS << endl
<< endl;
cin.clear();
cout.clear();
char task = 0;
cin >> task;
switch (task) {
case '1' :
cout << MENU_CHOSEN << ADMIN_MENU_NEW_POINT << endl << endl;
isLooping = pointsMenu();
break;
case '2' :
cout << MENU_CHOSEN << ADMIN_MENU_NEW_ROUTE << endl << endl;
isLooping = routesMenu();
break;
case '3' :
cout << MENU_CHOSEN << ADMIN_MENU_NEW_CAR << endl << endl;
isLooping = carsMenu();
break;
case '4' :
cout << MENU_CHOSEN << ADMIN_MENU_NEW_TAXI << endl << endl;
isLooping = taxiMenu();
break;
case '5' :
cout << MENU_CHOSEN << ADMIN_MENU_CALCULATE_ROUTE_TOTAL_DISTANCE << endl << endl;
for (const Route &r : routesVector) {
cout << "For Route: " << r.toString() << endl <<
"Total distance is: " << r.calcRouteTotalDistance() << endl << endl;
}
break;
case '6' :
cout << MENU_CHOSEN << ADMIN_MENU_CALCULATE_TAXI_FUEL_USAGE << endl << endl;
for (const Taxi &taxi : taxiVector) {
cout << "For Taxi " << taxi.toString() << endl <<
"Total distance for current Route is: " << taxi.getCurrentRoute().calcRouteTotalDistance()
<< endl <<
"And Fuel Usage Per km is: ";
taxi.calculatePerDayFuelUse();
cout << endl;
}
break;
case '7' :
cout << MENU_CHOSEN << endl;
break;
case 'q' :
case 'Q' :
case '0' :
cout << MENU_EXITING << endl;
isLooping = false;
default:
cout << MENU_WRONG_OPTION_ERROR << endl << endl;
break;
}
} while (isLooping);