-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecieve.cpp
More file actions
305 lines (241 loc) · 6.79 KB
/
Recieve.cpp
File metadata and controls
305 lines (241 loc) · 6.79 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
#include "Recieve.h"
#include "Point.h"
using namespace std;
// constructor
Recieve::Recieve() {}
// constructor
Recieve::~Recieve() {}
// constructor
Recieve::Recieve(TaxiCenter *taxiCenter) : taxiCenter(taxiCenter) {}
// print Invalid
void Recieve::printInvalid() {
cout << "-1" << endl;
}
// recieve Matrix
Matrix2D *Recieve::recieveMatrix() {
int sizeGridX, sizeGridY, numbersOfObstacles;
string sGrid, sObstacle, sPoint;
//GETTING SIZE OF GRID.
getline(cin, sGrid);
boost::trim(sGrid);
vector<string> parts = split(sGrid, ' ');
if (parts.size() != 2) {
return NULL;
}
if (!isInt(parts[0]) && !isInt(parts[1])) {
return NULL;
}
sizeGridX = stoi(parts[0]);
sizeGridY = stoi(parts[1]);
// checking range of grid.
if (!isInRangeOfGrid(sizeGridX) || !isInRangeOfGrid(sizeGridY)) {
return NULL;
}
// getting obstacles.
getline(cin, sObstacle);
boost::trim(sObstacle);
if (!isInt(sObstacle)) {
return NULL;
}
numbersOfObstacles = stoi(sObstacle);
//check if negative
if (numbersOfObstacles < 0) {
return NULL;
}
matrix2D = new Matrix2D(sizeGridX, sizeGridY);
//getting location of obstacles.
for (int j = 0; j < numbersOfObstacles; ++j) {
int x, y;
sPoint.clear();
parts.clear();
getline(cin, sPoint);
boost::trim(sPoint);
parts = split(sPoint, ',');
if (parts.size() != 2) {
delete(matrix2D);
return NULL;
}
if (!isInt(parts[0]) && !isInt(parts[1])) {
delete(matrix2D);
return NULL;
}
x = stoi(parts[0]);
y = stoi(parts[1]);
Point obstacle = Point(x, y);
Node* node = matrix2D->getNodeInMatrix((Node*) &obstacle);
if (node == NULL) {
delete(matrix2D);
return NULL;
}
node->setObstacle(true);
}
return matrix2D;
}
// recieve Trip
Trip *Recieve::recieveTrip() {
int ride_id, x_start, y_start, x_end, y_end,
num_passengers, startTime;
double tariff;
string s;
getline(cin, s);
boost::trim(s);
vector<string> parts = split(s, ',');
if (parts.size() != 8) {
return NULL;
}
if (!isInt(parts[0]) && !isInt(parts[1]) && !isInt(parts[2]) && !isInt(parts[3]) &&
!isInt(parts[4]) && !isInt(parts[5]) && !isDouble(parts[6]) && !isInt(parts[7])) {
return NULL;
}
ride_id = stoi(parts[0]);
x_start = stoi(parts[1]);
y_start = stoi(parts[2]);
x_end = stoi(parts[3]);
y_end = stoi(parts[4]);
num_passengers = stoi(parts[5]);
tariff = stod(parts[6]);
startTime = stoi(parts[7]);
//check if negative
if (ride_id < 0 || num_passengers < 0 || startTime <= 0 || tariff < 0) {
return NULL;
}
Point pStart(x_start, y_start);
Point pEnd(x_end, y_end);
Node *start = matrix2D->getNodeInMatrix(&pStart);
Node *end = matrix2D->getNodeInMatrix(&pEnd);
if (start == NULL || end == NULL || *start == end) {
return NULL;
}
//check if the TRIP id is already exist.
Trip* trip = taxiCenter->getTripByid(ride_id);
if (trip != NULL) {
return NULL;
}
return new Trip(start, end, ride_id, num_passengers,
tariff, startTime);
}
//recieve Taxi
Taxi *Recieve::recieveTaxi() {
int taxi_id, taxi_type;
char manufacturerer, colorOfTaxi;
string s;
getline(cin, s);
boost::trim(s);
vector<string> parts = split(s, ',');
if (parts.size() != 4) {
return NULL;
}
if (parts[2].size() != 1 || parts[3].size() != 1) {
return NULL;
}
if (!isInt(parts[0]) && !isInt(parts[1]) &&
!isManufacturer(*parts[2].c_str()) && !isColor(*parts[3].c_str())) {
return NULL;
}
taxi_id = stoi(parts[0]);
taxi_type = stoi(parts[1]);
manufacturerer = *parts[2].c_str();
colorOfTaxi = *parts[3].c_str();
//check if negative
if (taxi_id < 0 || (taxi_type != 1 && taxi_type != 2)) {
return NULL;
}
//check if the taxi id is already exist.
Taxi* taxi = taxiCenter->getTaxiById(taxi_id);
if (taxi != NULL) {
return NULL;
}
// checking the type of the taxi and adding it to the
// taxi center.
switch (taxi_type) {
case 1: {
taxi = new TaxiStandard(taxi_id, colorOfTaxi, manufacturerer);
break;
}
case 2: {
taxi = new TaxiLuxury(taxi_id, colorOfTaxi, manufacturerer);
break;
}
default: {
return NULL;
}
}
////dont forget to add to taxi center from the server: taxiCenter->addTaxi(taxi);
return taxi;
}
// is manufacturer.
bool Recieve::isManufacturer(char c) {
return c == 'H' || c == 'S' || c == 'T' || c == 'F';
}
//is color
bool Recieve::isColor(char c) {
return c == 'R' || c == 'B' || c == 'G' || c == 'P' || c == 'W';
}
//is marital status
bool Recieve::isMaritalStatus(char c) {
return c == 'S' || c == 'M' || c == 'W' || c == 'D';
}
// is in range of grid
bool Recieve::isInRangeOfGrid(int num) {
return num > 0;
}
//from stackoverflow. is int
bool Recieve::isInt(const string& s) {
string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
//from stackoverflow. is double
bool Recieve::isDouble(const string& s) {
istringstream iss(s);
double d;
char c;
return iss >> d && !(iss >> c);
}
//from stackoverflow. split
template<typename Out>
void Recieve::split(const std::string &s, char delim, Out result) {
stringstream ss;
ss.str(s);
string item;
while (getline(ss, item, delim)) {
*(result++) = item;
}
}
//from stackoverflow. split
vector<string> Recieve::split(const std::string &s, char delim) {
vector<string> elems;
split(s, delim, back_inserter(elems));
return elems;
}
// receive driver
Driver *Recieve::recieveDriver() {
int driver_id, age, xp, vehicle_id;
char status;
// getting details.
string s;
getline(cin, s);
boost::trim(s);
vector<string> parts = split(s, ',');
if (parts.size() != 5) {
return NULL;
}
if (parts[2].size() != 1) {
return NULL;
}
if (!isInt(parts[0]) && !isInt(parts[1]) && !isMaritalStatus(*parts[2].c_str())
&& !isInt(parts[3]) && !isInt(parts[4])) {
return NULL;
}
driver_id = stoi(parts[0]);
age = stoi(parts[1]);
status = *parts[2].c_str();
xp = stoi(parts[3]);
vehicle_id = stoi(parts[4]);
//check if negative
if (driver_id < 0 || age < 0 || xp < 0 || vehicle_id < 0) {
return NULL;
}
return new Driver(driver_id, age, status, xp,
vehicle_id);
}