-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.cpp
126 lines (113 loc) · 5.21 KB
/
Database.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
//
// Created by Jonathan Zhu on 2023-06-06.
//
#include "Database.h"
#include "FlightDB.h"
Database::Database(){
in.open(FILE_LOCATION);
jsonReader.parse(in, db);
}
void Database::load(FlightDB* inMemoryDB){
//Load airport variables values from .json file
for (const string to : db["Airports"].getMemberNames()){
Json::Value& airportJSON = db["Airports"][to];
string name = airportJSON["Name"].asString();
inMemoryDB->airports[to] = Airport{to, name};
Json::Value& distancesJSON = airportJSON["Distances"];
for (const string from : distancesJSON.getMemberNames()){
inMemoryDB->airports[to].addTimeToAirport(from, distancesJSON[from].asInt());
}
}
//Load flight variables values from .json file
for (const string id : db["Flights"].getMemberNames()){
Json::Value& flightJSON = db["Flights"][id];
string from = flightJSON["From"].asString();
string to = flightJSON["To"].asString();
string attendentID = flightJSON["Attendent ID"].asString();
string time = flightJSON["Departure"].asString();
int y = stoi(time.substr(0, 4));
int m = stoi(time.substr(5, 7));
int d = stoi(time.substr(8, 10));
int h = stoi(time.substr(11, 13));
int min = stoi(time.substr(14, 16));
inMemoryDB->flights[id] = Flight{from, to, id, attendentID, y, m, d, h, min, inMemoryDB->airports[to].getTimesToAirport()[from]};
for (int i = 0; i < flightJSON["Passengers"].size(); i++){
inMemoryDB->flights[id].getPassengers()[i] = &inMemoryDB->passengers[flightJSON["Passengers"][i].asString()];
}
for (int i = 0; i < flightJSON["Seats Taken"].size(); i++){
inMemoryDB->flights[id].getSeatTaken()[i] = flightJSON["Seats Taken"][i].asBool();
}
}
//Load passenger variables values from .json file
for (const string id : db["Passengers"].getMemberNames()){
Json::Value& passengerJSON = db["Passengers"][id];
string fn = passengerJSON["First Name"].asString();
string ln = passengerJSON["Last Name"].asString();
string address = passengerJSON["Address"].asString();
string phone = passengerJSON["Phone Number"].asString();
string pw = passengerJSON["Password"].asString();
inMemoryDB->passengers[id] = Passenger{fn, ln, address, phone, id, pw};
}
//Load attendent variables values from .json file
for (const string id : db["Attendents"].getMemberNames()){
Json::Value& passengerJSON = db["Attendents"][id];
string fn = passengerJSON["First Name"].asString();
string ln = passengerJSON["Last Name"].asString();
string address = passengerJSON["Address"].asString();
string phone = passengerJSON["Phone Number"].asString();
string pw = passengerJSON["Password"].asString();
inMemoryDB->attendents[id] = Attendent{fn, ln, address, phone, id, pw};
}
}
void Database::save(FlightDB* inMemoryDB){
Json::Value ndb;
//Save airport variables values from .json file
for (auto[code, airport] : inMemoryDB->airports){
Json::Value& airportJSON = ndb["Airports"][code];
airportJSON["Name"] = airport.getName();
Json::Value& distancessJSON = airportJSON["Distances"];
for (auto[from, time] : airport.getTimesToAirport()){
distancessJSON[from] = time;
}
}
//Save passenger variables values from .json file
for (auto[id, passenger] : inMemoryDB->passengers){
Json::Value& passengerJSON = ndb["Passengers"][id];
passengerJSON["First Name"] = passenger.getFirstName();
passengerJSON["Last Name"] = passenger.getLastName();
passengerJSON["Address"] = passenger.getAddress();
passengerJSON["Phone Number"] = passenger.getPhoneNumber();
passengerJSON["Password"] = passenger.getPassword();
}
//Save attendent variables values from .json file
for (auto[id, attendent] : inMemoryDB->attendents){
Json::Value& passengerJSON = ndb["Attendents"][id];
passengerJSON["First Name"] = attendent.getFirstName();
passengerJSON["Last Name"] = attendent.getLastName();
passengerJSON["Address"] = attendent.getAddress();
passengerJSON["Phone Number"] = attendent.getPhoneNumber();
passengerJSON["Password"] = attendent.getPassword();
}
//Save flight variables values from .json file
for (auto[id, flight] : inMemoryDB->flights){
Json::Value& flightJSON = ndb["Flights"][flight.getFlightID()];
flightJSON["From"] = flight.getFrom();
flightJSON["To"] = flight.getTo();
flightJSON["Attendent ID"] = flight.getAttedentID();
flightJSON["Departure"] = flight.getDepartureTime();
flightJSON["Passengers"] = Json::arrayValue;
for (auto passenger : flight.getPassengers()){
cout << passenger->getPassengerID() << endl;
flightJSON["Passengers"].append(passenger->getPassengerID());
}
flightJSON["Seats Taken"] = Json::arrayValue;
for (bool b : flight.getSeatTaken()){
flightJSON["Seats Taken"].append(b);
}
}
db = ndb;
cout << db << endl;
out.open(FILE_LOCATION);
out << db;
out.close();
}