-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworld.cpp
More file actions
373 lines (316 loc) · 11.3 KB
/
world.cpp
File metadata and controls
373 lines (316 loc) · 11.3 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
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include "World.h"
#include "Airplane.h"
#include "Airport.h"
#include "Flights.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <math.h>
#include "Clock.h"
#include <iomanip>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
using namespace std;
Airplane::Airplane(string Aircraft, string file_name) {
ifstream file(file_name);
string name, IS, passengers, range, speed, height;
if (!file.is_open()) {
cout << "No open";
}
getline(file, name, '\n');
while (getline(file, name)) {
getline(file, name, ',');
if (Aircraft == name) {
model = name;
getline(file, IS, ',');
getline(file, passengers, ',');
max_seats = stoi(passengers);
getline(file, range, ',');
max_range = stoi(range);
getline(file, speed, ',');
max_speed = stoi(speed);
getline(file, height, '\n');
max_height = stoi(height);
}
}
}
void World::create_airplane(string file_name1, string file_name2) {
vector<Airport>::iterator it;
ifstream file(file_name1);
string id, Model, Seats, Location, compare, sub;
if (!file.is_open()) {
cout << "No open";
}
getline(file, Location, '\n');
while (getline(file, id, ','),
getline(file, Model, ','),
getline(file, Seats, ','),
getline(file, Location, '\n')) {
Airplane obj = Airplane(Model, file_name2);
obj.set_ID(id);
obj.set_avail_seats(std::stoi(Seats));
airplane.push_back(obj);
for (it = airport.begin(); it < airport.end(); it++) {
sub = Location.substr(0, Location.size() - 1);
compare = it->get_ICAO().substr(1, it->get_ICAO().size() - 2);
if (compare.compare(sub) == 0) {
it->add_plane(obj);
}
}
}
}
void World::create_airport(std::string file_name) {
ifstream file(file_name);
string id, alti, longi, lati, country, timezone, type, ICAO, IATA, city, source, name, DST, tz;
if (!file.is_open()) {
cout << "No open";
}
while (getline(file, id, ','),
getline(file, name, ','),
getline(file, city, ','),
getline(file, country, ','),
getline(file, IATA, ','),
getline(file, ICAO, ','),
getline(file, lati, ','),
getline(file, longi, ','),
getline(file, alti, ','),
getline(file, timezone, ','),
getline(file, DST, ','),
getline(file, tz, ','),
getline(file, type, ','),
getline(file, source, '\n')) {
Airport obj = Airport(stoi(id), stof(alti), stod(longi), stod(lati), country, timezone, type, ICAO, IATA, city, source, name);
airport.push_back(obj);
}
}
void World::create_passenger(std::string file_name) {
ifstream file(file_name);
string id, dest, location, reqtime;
double d;
if (!file.is_open()) {
cout << "No open";
}
getline(file, id, '\n');
while (getline(file, id, ','),
getline(file, dest, ','),
getline(file, reqtime, ','),
getline(file, location, '\n')) {
Passenger obj = Passenger(dest, location, reqtime, stoi(id));
passenger.push_back(obj);
try {
bool full_plane = true;
int i = 0, j = 0;
i = get_airport(location);
j = get_airport(dest);
d = calculate_dist(airport[i].get_latitude(), airport[j].get_latitude(), airport[i].get_longitude(), airport[j].get_longitude());
if (airport[i].get_departure_status() == false) {
full_plane = fligths[get_flight_place(airport[i].get_spec_flight(dest))].add_passenger(obj);
}
if (full_plane) {
if (airport[i].get_plane_status()) {
if (airport[i].get_arrival_status()) {
//BOOK PLANE
cout << "No plane to book" << endl;
} else {
fligths.push_back(airport[i].create_flight(airport[i].find_arrival(obj.get_req_date(), obj.get_req_time_h(), d), obj, airport[j], airport[i], d));
airport[i].add_departure(get_flight(stoi(id)));
airport[j].add_arrival(get_flight(stoi(id)));
airport[i].remove_plane(airport[i].find_arrival(obj.get_req_date(), obj.get_req_time_h(), d));
}
} else {
fligths.push_back(airport[i].create_flight(airport[i].best_plane(d), obj, airport[j], airport[i], d));
airport[i].add_departure(get_flight(stoi(id)));
airport[j].add_arrival(get_flight(stoi(id)));
airport[i].remove_plane(airport[i].best_plane(d));
}
}
} catch (const char* msg) {
cerr << msg << endl;
}
}
}
double World::calculate_dist(double lat1, double lat2, double long1, double long2) {
double r, la1, la2, lo1, lo2, d;
r = 6371;
la1 = lat1 * M_PI / 180;
la2 = lat2 * M_PI / 180;
lo1 = long1 * M_PI / 180;
lo2 = long2 * M_PI / 180;
d = 2 * r * sqrt((sin((la2 - la1) / 2) * sin((la2 - la1) / 2))+(cos(la1) * cos(la2)*(sin((lo2 - lo1) / 2) * sin((lo2 - lo1) / 2))));
return d;
}
Flight Airport::create_flight(Airplane plane, Passenger passenger, Airport destination, Airport departure, double dist) {
vector<Airplane>::iterator it;
Flight flight(plane, passenger, destination.get_name(), departure.get_name(), departure.get_ICAO(), destination.get_ICAO());
flight.set_id(passenger.get_id());
flight.set_time(passenger.get_req_date(), passenger.get_req_time_h(), passenger.get_req_time_m(), dist, plane.get_max_speed());
return flight;
}
Airplane Airport::find_plane(std::string plane_id) {
int i = 0;
for (vector<Airplane>::iterator it = airplane.begin(); it < airplane.end(); it++) {
if (it->get_id() == plane_id) {
break;
}
i++;
}
return airplane[i];
}
int World::get_airport(std::string ICAO) {
int i = 0;
bool exist = false;
for (vector<Airport>::iterator it = airport.begin(); it < airport.end(); it++) {
if (it->get_ICAO().substr(1, it->get_ICAO().size() - 2) == ICAO) {
exist = true;
break;
}
i++;
}
if (exist == false) {
cout << ICAO << " ";
throw "There is no airport with that name";
}
return i;
}
Flight World::get_flight(int id) {
int i = 0;
bool exist = false;
for (vector<Flight>::iterator it = fligths.begin(); it < fligths.end(); it++) {
if (it->get_id() == id) {
exist = true;
break;
}
i++;
}
if (exist == false) {
throw "There is no airport with that name";
}
return fligths[i];
}
int World::get_flight_place(int id) {
int i = 0;
bool exist = false;
for (vector<Flight>::iterator it = fligths.begin(); it < fligths.end(); it++) {
if (it->get_id() == id) {
exist = true;
break;
}
i++;
}
if (exist == false) {
throw "There is no airport with that name";
}
return i;
}
Airplane Airport::best_plane(double d) {
int range, i;
string plane_id;
for (vector<Airplane>::iterator it = airplane.begin(); it != airplane.end(); it++) {
if (d < it->get_range()) {
if (it == airplane.begin()) {
range = it->get_range();
i = it->get_max_seat();
plane_id = it->get_id();
} else if (range > it->get_range()) {
range = it->get_range();
plane_id = it->get_id();
}
}
}
if (plane_id == "") {
throw "Inget Plan Passar";
}
return find_plane(plane_id);
}
int Clock::geth() {
return h;
}
int Clock::getm() {
return m;
}
void Clock::set(int hh, int mm) {
h = hh;
m = mm;
}
void Clock::tick() {
m = (m + 1) % 60;
if (m == 0)
h = (h + 1) % 24;
}
bool Airport::add(Passenger p, string dest) {
bool add = false;
int i = 0;
for (vector<Flight>::iterator it = get_departures().begin(); it != get_departures().end(); it++) {
if (it->get_dest().size() != 4) {
throw "Fel dest";
}
string sub2 = it->get_dest().substr(1, it->get_dest().size() - 2);
cout << sub2;
if (dest == sub2) {
add = get_departures()[i].add_passenger(p);
break;
}
i++;
}
return add;
}
int Airport::get_spec_flight(string dest) {
int id;
for (vector<Flight>::iterator it = departures.begin(); it != departures.end(); it++) {
if (it->get_destination_ICAO().size() != 6) {
throw "Fel dest";
}
if (dest == it->get_destination_ICAO().substr(1, 4)) {
id = it->get_id();
break;
}
}
return id;
}
Airplane Airport::find_arrival(std::string date, int h, int range) {
int i = 0;
for (vector<Flight>::iterator it = arrival.begin(); it != arrival.end(); it++) {
if (h > it->get_arrival_time()) {
if (it->get_range() > range) {
break;
}
i++;
}
}
return arrival[i].get_airplane();
}
void Airport::remove_plane(Airplane plane) {
vector<Airplane>::iterator it;
int i = 0;
for (it = airplane.begin(); it < airplane.end(); it++) {
if (it->get_id() == plane.get_id()) {
airplane.erase(airplane.begin() + i);
}
i++;
}
}
void World::print_csv(string file_name, string source) {
ofstream file;
int id;
file.open(file_name);
file << "Source,Flight id,Departure location,Time of departure,Arrival location,Time of arrival,Passengers id ---->,\n";
for(int i = 0; i<fligths.size(); i++){
int j =0;
file <<source<<","<<fligths[i].get_id()<<","<<fligths[i].get_dep()<<","<<fligths[i].get_dep_time()<<","<<fligths[i].get_dest()<<","<<fligths[i].get_arr_time()<<",";
for(vector<Passenger>::iterator it = fligths[i].get_passenger().begin(); it != fligths[i].get_passenger().end(); it++){
id = fligths[i].get_passenger()[j].get_id();
j++;
file << id<<",";
}
file << "\n";
}
}