-
Notifications
You must be signed in to change notification settings - Fork 0
/
expedia_flights_api.h
362 lines (268 loc) · 8.6 KB
/
expedia_flights_api.h
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
/*
* expedia_flights_api.h
*
* Created on: Sep 18, 2020
* Author: moustafa
*/
#ifndef EXPEDIA_FLIGHTS_API_H_
#define EXPEDIA_FLIGHTS_API_H_
using namespace std;
class AirCanadaCustomerInfo {
};
class AirCanadaFlight {
public:
double price;
string date_time_from;
string date_time_to;
};
class AirCanadaOnlineAPI {
public:
static vector<AirCanadaFlight> GetFlights(string from, string from_date, string to, string to_date, int adults, int childern) {
vector<AirCanadaFlight> flights;
flights.push_back( { 200, "25-01-2022", "10-02-2022" });
flights.push_back( { 250, "29-01-2022", "10-02-2022" });
return flights;
}
static bool ReserveFlight(const AirCanadaFlight& flight, const AirCanadaCustomerInfo &info) {
return true;
}
};
class TurkishFlight {
public:
double cost;
string datetime_from;
string datetime_to;
};
class TurkishCustomerInfo {
};
class TurkishAirlinesOnlineAPI {
public:
void SetFromToInfo(string datetime_from, string from, string datetime_to, string to) {
}
void SetPassengersInfo(int infants, int childern, int adults) {
}
vector<TurkishFlight> GetAvailableFlights() const {
vector<TurkishFlight> flights;
flights.push_back( { 300, "10-01-2022", "10-02-2022" });
flights.push_back( { 320, "12-01-2022", "10-02-2022" });
return flights;
}
static bool ReserveFlight(const TurkishCustomerInfo &info, const TurkishFlight& flight) {
return false;
}
};
class Flight {
private:
string airline_name;
double total_cost = 0;
string date_time_from;
string date_time_to;
public:
// printing
string ToString() const {
ostringstream oss;
oss << "Airline: " << airline_name << " Cost: " << total_cost << " Departure Date " << date_time_from << " Arrival Date " << date_time_to;
return oss.str();
}
// setters and getters
const string& GetDateTimeFrom() const {
return date_time_from;
}
void SetDateTimeFrom(const string& date_time_from) {
this->date_time_from = date_time_from;
}
const string& GetDateTimeTo() const {
return date_time_to;
}
void SetDateTimeTo(const string& date_time_to) {
this->date_time_to = date_time_to;
}
double GetTotalCost() const {
return total_cost;
}
void SetTotalCost(double total_cost) {
this->total_cost = total_cost;
}
const string& GetAirlineName() const {
return airline_name;
}
void SetAirLineName(const string& airline_name) {
this->airline_name = airline_name;
}
};
class FlightRequest {
private:
string datetime_from;
string from;
string datetime_to;
string to;
int infants;
int childern;
int adults;
public:
// some setters and getters
string GetDatePartFrom() const {
return datetime_from;
}
string GetDatePartTo() const {
return datetime_to;
}
int GetAdults() const {
return adults;
}
void SetAdults(int adults) {
this->adults = adults;
}
int GetChildren() const {
return childern;
}
void SetChildren(int childern) {
this->childern = childern;
}
const string& GetDatetimeFrom() const {
return datetime_from;
}
void SetDatetimeFrom(const string& datetime_from) {
this->datetime_from = datetime_from;
}
const string& GetDatetimeTo() const {
return datetime_to;
}
void SetDatetimeTo(const string& datetime_to) {
this->datetime_to = datetime_to;
}
const string& GetFrom() const {
return from;
}
void SetFrom(const string& from) {
this->from = from;
}
int GetInfants() const {
return infants;
}
void SetInfants(int infants) {
this->infants = infants;
}
const string& GetTo() const {
return to;
}
void SetTo(const string& to) {
this->to = to;
}
};
class FlightReservation : public Reservation{
private:
FlightRequest request;
Flight flight;
public:
// default constructor with initializer list
FlightReservation(const FlightRequest& request, const Flight& flight) : request(request), flight(flight) {}
virtual FlightReservation* Clone() const override {
return new FlightReservation(*this);
}
virtual double TotalCost() const override {
return flight.GetTotalCost();
}
// printing
virtual string ToString() const override {
ostringstream oss;
oss << "Airline reservation with reservation: " << flight.GetAirlineName() << ": From " << request.GetFrom() << " on " << request.GetDatetimeFrom() << "\n";
oss << "\tTo " << request.GetTo() << " on " << request.GetDatetimeTo() << "\n";
oss << "\t" << "Adults: " << request.GetAdults() << " children: " << request.GetChildren() << " infants: " << request.GetInfants() << "\n";
oss << "\tTotal Cost:" << TotalCost() << "\n";
return oss.str();
}
// setters and getters
const FlightRequest& GetRequest() const {
return request;
}
const Flight& GetFlight() const {
return flight;
}
};
class IFlightsManager {
protected:
FlightRequest request;
public:
virtual void SetFlightRequest(const FlightRequest& request) {
this->request = request;
}
virtual vector<Flight> SearchFlights() const = 0;
virtual bool ReserveFlight(const FlightReservation& reservation) const = 0;
virtual string GetName() const = 0;
virtual ~IFlightsManager() {}
};
class AirCanadaFlightsManager : public IFlightsManager {
public:
virtual string GetName() const override {
return "AirCanada AirLines";
}
virtual vector<Flight> SearchFlights() const override {
vector<AirCanadaFlight> flights_aircanada = AirCanadaOnlineAPI::GetFlights(request.GetFrom(), request.GetDatePartFrom(), request.GetTo(), request.GetDatePartTo(), request.GetAdults(), request.GetChildren());
vector<Flight> flights;
// convert
// convert
for (auto & flight_aircanada : flights_aircanada) {
Flight flight;
flight.SetAirLineName(GetName());
flight.SetDateTimeFrom(flight_aircanada.date_time_from);
flight.SetDateTimeTo(flight_aircanada.date_time_to);
flight.SetTotalCost(flight_aircanada.price);
flights.push_back(flight);
}
return flights;
}
virtual bool ReserveFlight(const FlightReservation& reservation) const override {
// Just dummy. We should map from reservation to the agency api
return AirCanadaOnlineAPI::ReserveFlight(AirCanadaFlight(), AirCanadaCustomerInfo());
}
};
class TurkishFlighsManager: public IFlightsManager {
private:
public:
virtual string GetName() const override {
return "Turksih Airlines";
}
virtual vector<Flight> SearchFlights() const override {
TurkishAirlinesOnlineAPI api;
api.SetFromToInfo(request.GetDatetimeFrom(), request.GetFrom(), request.GetDatetimeTo(), request.GetTo());
api.SetPassengersInfo(request.GetInfants(), request.GetChildren(), request.GetAdults());
vector<TurkishFlight> flights_turkey = api.GetAvailableFlights();
vector<Flight> flights;
// convert
for (auto & flight_turkey : flights_turkey) {
Flight flight;
flight.SetAirLineName("Turksih Airlines");
flight.SetDateTimeFrom(flight_turkey.datetime_from);
flight.SetDateTimeTo(flight_turkey.datetime_to);
flight.SetTotalCost(flight_turkey.cost);
flights.push_back(flight);
}
return flights;
}
virtual bool ReserveFlight(const FlightReservation &reservation) const {
// Just dummy. We should map from reservation to the agency api
return TurkishAirlinesOnlineAPI::ReserveFlight(TurkishCustomerInfo(), TurkishFlight());
}
};
/*
* This class reduce the dependency of clients on specific managers. This is the only place that is coupled with the different types
*/
class FlightsFactory {
// We can design this factory in more efficient way: e.g. using map + singleton
public:
static vector<IFlightsManager*> GetManagers() {
vector<IFlightsManager*> flights_managers;
flights_managers.push_back(new TurkishFlighsManager());
flights_managers.push_back(new AirCanadaFlightsManager());
return flights_managers;
}
static IFlightsManager* GetManager(string name) {
for (IFlightsManager* mgr : FlightsFactory::GetManagers()) {
if (mgr->GetName() == name)
return mgr;
}
return nullptr;
}
};
#endif /* EXPEDIA_FLIGHTS_API_H_ */