-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflights.cpp
More file actions
190 lines (158 loc) · 6.13 KB
/
flights.cpp
File metadata and controls
190 lines (158 loc) · 6.13 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
#include "flights.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;
Flight::Flight() : numOfRowsM(0), seatsPerRowM(0) {
}
Flight::~Flight() {
}
void Flight::readDataFromFile(const std::string &textFile) {
std::ifstream file(textFile);
if (!file.is_open()) {
std::cerr << "Error opening file: " << textFile << std::endl;
return;
}
std::string line;
bool flightInfoRead = false;
while (getline(file, line)) {
std::istringstream iss(line);
if (!flightInfoRead) {
std::string flightNum;
int numOfRows, seatsPerRow;
std::getline(iss, flightNum, '\t');
iss >> numOfRows >> seatsPerRow;
flightInfoRead = true;
flightNumM = flightNum;
numOfRowsM = numOfRows;
seatsPerRowM = seatsPerRow;
seatMap.resize(numOfRowsM, std::vector<Passenger*>(seatsPerRowM, nullptr));
} else {
std::string firstName, lastName, phone, seatInfo, id;
std::getline(iss, firstName, '\t');
std::getline(iss, lastName, '\t');
std::getline(iss, phone, '\t');
std::getline(iss, seatInfo, '\t');
std::getline(iss, id, '\t');
int row = std::stoi(seatInfo.substr(0, seatInfo.size() - 1));
char col = seatInfo[seatInfo.size() - 1];
addPassenger(firstName, lastName, phone, id, row, col);
}
}
file.close();
}
#include <iomanip>
void Flight::displaySeatMap() {
int maxRowDigits = (numOfRowsM >= 10) ? 2 : 1;
cout << "Aircraft Seat Map" << endl;
cout << string(maxRowDigits + 1, ' ');
for (char c = 'A'; c < 'A' + seatsPerRowM; ++c) {
cout << " " << c << " ";
}
cout << endl;
cout << string(maxRowDigits, ' ') << " +";
for (int i = 0; i < seatsPerRowM; ++i) {
cout << "---+";
}
cout << endl;
for (int i = 0; i < numOfRowsM; ++i) {
cout << left << setw(maxRowDigits + 1) << (i + 1) << "|";
for (int j = 0; j < seatsPerRowM; ++j) {
if (seatMap[i][j] != nullptr) {
cout << " X |";
} else {
cout << " |";
}
}
cout << endl;
cout << string(maxRowDigits, ' ') << " +";
for (int k = 0; k < seatsPerRowM; ++k) {
cout << "---+";
}
cout << endl;
}
}
void Flight::displayPassengerInformation() {
std::cout << std::left;
std::cout << std::setw(16) << "First name"
<< std::setw(16) << "Last name"
<< std::setw(16) << "Phone"
<< std::setw(5) << "Row"
<< std::setw(5) << "Seat"
<< std::setw(6) << "ID" << std::endl;
std::cout << "---------------------------------------------------------------" << std::endl;
for (const Passenger& passenger : passengerListM) {
std::cout << std::setw(16) << passenger.getFirstName()
<< std::setw(16) << passenger.getLastName()
<< std::setw(16) << passenger.getPhoneNum()
<< std::setw(5) << passenger.getRow()
<< std::setw(5) << static_cast<char>('A' + passenger.getCol() - 1)
<< std::setw(6) << passenger.getId() << std::endl;
}
}
void Flight::addPassenger(const string& fName, const string& lName, const string& phone, const string& id, int row, char seat) {
int col = seat - 'A' + 1;
if (row < 0 || row > numOfRowsM || col < 0 || col > seatsPerRowM) {
cout << "Invalid seat position." << endl;
return;
}
if (seatMap[row-1][col-1] != nullptr) {
cout << "Seat " << seat << " in row " << row << " is already occupied." << endl;
return;
}
Passenger newPassenger(fName, lName, phone, id, row, col);
passengerListM.push_back(newPassenger);
seatMap[row-1][col-1] = &passengerListM.back();
}
void Flight::removePassenger(const std::string& id) {
for (int i = 0; i < passengerListM.size(); ++i ) {
cout << "Checking ID: " << passengerListM[i].getId() << "- " << id << endl;
if (passengerListM[i].getId() == id) {
int row = passengerListM[i].getRow() - 1;
int col = passengerListM[i].getCol() - 1;
if (row >= 0 && row < numOfRowsM && col >= 0 && col < seatsPerRowM) {
seatMap[row][col] = nullptr;
}
passengerListM.erase(passengerListM.begin() + i);
cout << "Passenger with ID " << id << " removed successfully." << endl;
return;
}
}
cout << "Passenger with ID " << id << " not found." << endl;
}
void Flight::saveDataToFile(const string &textFile) {
char option;
cout << "Do you want to save the data in the " << textFile << "? Please answer <Y or N> ";
cin >> option;
if (option == 'Y') {
ofstream file(textFile);
if (!file.is_open()) {
cerr << "Error opening file: " << textFile << endl;
return;
}
file << getFlightNum() << "\t" << getNumOfRows() << "\t" << getSeatsPerRow() << endl;
for (const Passenger& currentPassenger : passengerListM) {
file << currentPassenger.getFirstName() << "\t"
<< currentPassenger.getLastName() << "\t"
<< currentPassenger.getPhoneNum() << "\t"
<< currentPassenger.getRow() << static_cast<char>('A' + currentPassenger.getCol() - 1) << "\t"
<< currentPassenger.getId() << endl;
}
file.close();
cout << "All the data in the passenger list was saved into the " << textFile << endl;
} else if (option == 'N') {
cout << "Data was NOT saved into " << textFile << endl;
} else {
cout << "Invalid Option" << endl;
}
}
string Flight::getFlightNum() const {
return flightNumM;
}
int Flight::getNumOfRows() const {
return numOfRowsM;
}
int Flight::getSeatsPerRow() const {
return seatsPerRowM;
}