-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlightManager.cpp
More file actions
169 lines (144 loc) · 5.94 KB
/
Copy pathFlightManager.cpp
File metadata and controls
169 lines (144 loc) · 5.94 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
#include "FlightManager.h"
#include <fstream>
#include <sstream>
#include <vector>
#include <exception>
void FlightManager::addFlight(const Flight& flight) {
for (const auto& existingFlight : flights) {
if (existingFlight.getFlightId() == flight.getFlightId()) {
std::cerr << "Полет с ID " << flight.getFlightId() << " вече съществува.\n";
return;
}
}
flights.push_back(flight);
}
void FlightManager::addAircraft(const Aircraft& aircraft) {
for (const auto& existingAircraft : aircrafts) {
if (existingAircraft.getAircraftId() == aircraft.getAircraftId()) {
std::cerr << "Самолет с ID " << aircraft.getAircraftId() << " вече съществува.\n";
return;
}
}
aircrafts.push_back(aircraft);
}
std::vector<Flight> FlightManager::findFlightsByDestination(const std::string& destination) const {
std::vector<Flight> result;
for (const auto& flight : flights) {
if (flight.getDestination() == destination) {
result.push_back(flight);
}
}
return result;
}
const std::vector<Aircraft>& FlightManager::getAircrafts() const {
return aircrafts;
}
void FlightManager::displayAllFlights() const {
for (const auto& flight : flights) {
std::cout << flight << std::endl;
}
}
void FlightManager::displayAllAircrafts() const {
if (aircrafts.empty()) {
std::cout << "Няма добавени самолети\n";
} else {
for (const auto& aircraft : aircrafts) {
std::cout << aircraft << std::endl;
}
}
}
void FlightManager::saveToFile() const {
try {
if (aircrafts.empty() && flights.empty()) {
std::cerr << "Няма данни за записване." << std::endl;
return;
}
std::ofstream aircraftFile("aircrafts.txt", std::ios::out);
if (!aircraftFile) {
throw std::ios_base::failure("Неуспешно отваряне на файла aircrafts.txt");
}
std::ofstream flightFile("flights.txt", std::ios::out);
if (!flightFile) {
throw std::ios_base::failure("Неуспешно отваряне на файла flights.txt");
}
for (const auto& aircraft : aircrafts) {
aircraftFile << aircraft.getAircraftId() << " "
<< aircraft.getAircraftClass().getManufacturer() << " "
<< aircraft.getAircraftClass().getModel() << " "
<< aircraft.getAircraftClass().getSeats() << " "
<< aircraft.getAircraftClass().getMinRunwayLength() << " "
<< aircraft.getAircraftClass().getFuelConsumptionPerKMPerSeat() << " "
<< aircraft.getAircraftClass().getFuelTankCapacity() << " "
<< aircraft.getAircraftClass().getAverageSpeed() << " "
<< aircraft.getCrewCost() << "\n";
}
for (const auto& flight : flights) {
flightFile << flight.getFlightId() << " "
<< flight.getDeparture() << " "
<< flight.getDestination() << " "
<< flight.getDistance() << " "
<< flight.getAircraft().getAircraftId() << " "
<< flight.getPassengers() << "\n";
}
} catch (const std::exception& e) {
std::cerr << "Възникна грешка по време на запис на файлове: " << e.what() << std::endl;
}
}
void FlightManager::loadFromFile() {
aircrafts.clear();
flights.clear();
std::ifstream aircraftFile("aircrafts.txt");
std::ifstream flightFile("flights.txt");
if (aircraftFile.is_open()) {
std::string aircraftId, manufacturer, model;
int seats;
double minRunwayLength, fuelConsumption, fuelTankCapacity, averageSpeed, crewCost;
while (aircraftFile >> aircraftId >> manufacturer >> model >> seats >> minRunwayLength
>> fuelConsumption >> fuelTankCapacity >> averageSpeed >> crewCost) {
bool exists = false;
for (const auto& aircraft : aircrafts) {
if (aircraft.getAircraftId() == aircraftId) {
exists = true;
break;
}
}
if (!exists) {
AircraftClass aircraftClass(manufacturer, model, seats, minRunwayLength, fuelConsumption, fuelTankCapacity, averageSpeed);
Aircraft newAircraft(aircraftId, aircraftClass, crewCost);
aircrafts.push_back(newAircraft);
}
}
}
if (flightFile.is_open()) {
std::string line;
while (std::getline(flightFile, line)) {
if (line.empty()) continue;
std::istringstream iss(line);
std::string flightId, departure, destination, aircraftId;
int passengers;
double distance;
if (iss >> flightId >> departure >> destination >> distance >> aircraftId >> passengers) {
bool exists = false;
for (const auto& flight : flights) {
if (flight.getFlightId() == flightId) {
exists = true;
break;
}
}
if (!exists) {
for (const auto& aircraft : aircrafts) {
if (aircraft.getAircraftId() == aircraftId) {
Flight newFlight(flightId, departure, destination, distance, aircraft, passengers);
flights.push_back(newFlight);
break;
}
}
}
} else {
std::cerr << "Невалиден ред във файла: " << line << std::endl;
}
}
}
aircraftFile.close();
flightFile.close();
}