-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSerializer.cpp
103 lines (87 loc) · 3.19 KB
/
Serializer.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
//
// Benjy Berkowicz - 336182589
// Advanced Programming 2016-2017 Bar Ilan
//
#include "Serializer.h"
#include "StandardTaxi.h"
#include "LuxuryTaxi.h"
using namespace std;
std::string Serializer::serializeDriver(Driver * d) {
ostringstream buildString;
buildString << d->getID() << ';' << d->getAge() << ';' << (int)d->getMarital()
<< ';' << d->getYears() << ';' << d->getID();
return buildString.str();
}
Driver *Serializer::deserializeDriver(std::string input) {
istringstream parser(input);
int id, age, years, carID, status;
char dummy;
parser >> id >> dummy >> age >> dummy >> status >> dummy >> years >> dummy >> carID;
Driver* d = new Driver(id, age, (MaritalStatus)status, years, carID);
return d;
}
std::string Serializer::serializeTaxi(Taxi * t) {
ostringstream buildString;
string location = serializePoint(*t->getLocation());
buildString << t->getPrice() << ';' << t->getID() << ';'
<< (int)t->getManufacturer() << ';' << (int)t->getColor() << ';' << location;
return buildString.str();
}
Taxi *Serializer::deserializeTaxi(std::string input) {
int type, id, manu, color;
char dummy;
Taxi * returnObj;
Point * location = new Point();
istringstream parser(input);
parser >> type >> dummy >> id >> dummy >> manu >> dummy >> color >> dummy >> location->x >>
dummy >> location->y;
if (type == 1) {
returnObj = new StandardTaxi(id, (CarMaker)manu, (Color)color);
} else {
returnObj = new LuxuryTaxi(id, (CarMaker)manu, (Color)color);
}
returnObj->setLocation(location);
return returnObj;
}
std::string Serializer::serializeTrip(TripInfo * t) {
ostringstream buildString;
Point next;
string startP, endP;
startP = serializePoint(t->getStartPoint());
endP = serializePoint(t->getEndPoint());
buildString << t->getRideID() << ';' << startP << ';'
<< endP << ';' << t->getNumPassengers()<< ';'
<< t->getTarrif() << ';' << t->getStartTime()
<< ';' << t->getRoute()->size();
// We now add the list of points:
int size = t->getRoute()->size();
for (int i = 0; i < size; i++) {
next = t->getRoute()->at(i);
buildString << ';' << serializePoint(next);
}
buildString << ';';
return buildString.str();
}
TripInfo *Serializer::deserializeTrip(std::string input) {
int id, numPass, tarr, numELM, startTime;
Point start, end;
char dummy;
TripInfo * returnObj;
istringstream parser(input);
parser >> id >> dummy >> start.x >> dummy >> start.y >> dummy >> end.x >> dummy >> end.y >>
dummy >> numPass >> dummy >> tarr >> dummy >> startTime >> dummy >> numELM >> dummy;
vector<Point> * route = new vector<Point>();
for (int i = 0; i < numELM; i++) {
Point newPoint;
parser >> newPoint.x >> dummy >> newPoint.y >> dummy;
route->emplace_back(newPoint);
}
returnObj = new TripInfo(id, start, end, numPass, tarr, startTime);
returnObj->assignRoute(route);
return returnObj;
}
std::string Serializer::serializePoint(Point p) {
ostringstream build;
build << p.x << ';' << p.y;
return build.str();
}