-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflight.cpp
More file actions
138 lines (121 loc) · 3.52 KB
/
flight.cpp
File metadata and controls
138 lines (121 loc) · 3.52 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
// Implementation for Flight class
#include "passenger.h"
#include "flight.h"
#include "seat.h"
using namespace std;
Flight::Flight(): flightID(), rows(0), seatsPR(0){}
Flight::Flight(const string &flightNo, int row, int seats)
: flightID(flightNo), rows(row), seatsPR(seats) {}
void Flight::populate_flight_from_file(const string &fileName)
{
ifstream file("flight_info.txt");
if (!file.is_open())
{
cerr << "Error opening file!" << endl;
return;
}
// Read flight number, rows, and seats per row
file >> flightID >> rows >> seatsPR;
createSeatMap(); // Initialize the seat map
string line;
getline(file, line); // Skip to the next line
while (getline(file, line))
{
istringstream iss(line);
char fntest[21], lntest[21], phtest[21];
string fName, lName, phone;
int row, passId;
char passSeat;
iss.get(fntest, 21, '\n');
iss.get(lntest, 21, '\n');
iss.get(phtest, 21, '\n');
fName = fntest;
lName = lntest;
phone = phtest;
if (!(iss >> row >> passSeat >> passId))
{
break;
}
Seat new_seat(row, passSeat);
Passenger new_passenger(fName, lName, phone, new_seat, passId);
passengers.push_back(new_passenger);
}
file.close();
}
void Flight::displayPassengers() const
{
for (const auto &passenger : passengers)
{
passenger.pass_display();
}
}
void Flight::addPassenger(const Passenger &passenger)
{
passengers.push_back(passenger);
}
void Flight::removePassenger(int id)
{
passengers.erase(
remove_if(passengers.begin(), passengers.end(),
[id](const Passenger &p)
{ return p.getPassId() == id; }),
passengers.end());
}
void Flight::saveDataToFile(const string &fileName)
{
ofstream file("flight_info.txt");
if (!file.is_open())
{
cerr << "Error opening file!" << endl;
return;
}
for (const auto &passenger : passengers)
{
Seat seat = passenger.getSeat();
file << passenger.getFName() << " "
<< passenger.getLName() << " "
<< passenger.getPhone() << " "
<< seat.get_rowNum() << seat.get_seatNum() << " "
<< passenger.getPassId() << endl;
}
file.close();
}
void Flight::createSeatMap()
{
for (int i = 1; i <= rows; i++)
{
vector<Seat> rowSeats;
for (char j = 'A'; j < 'A' + seatsPR; j++)
{
rowSeats.emplace_back(i, j);
}
seatMap[i] = rowSeats;
}
}
void Flight::displaySeatMap() const
{
cout << "\n Seat Map for Flight: " << flightID << "\n";
cout << " Aircraft Seat Map\n";
cout << " ";
for (int i = 0; i < seatsPR; i++){
cout<<char('A' + i)<< " ";
}
cout<< endl;
for (const auto &row : seatMap)
{
cout << " ";
for (int i = 0; i < seatsPR; i++){
cout<<"+---";
}
cout <<"+"<< endl << " |";
for (const auto &seat : row.second)
{
bool isOccupied = any_of(passengers.begin(), passengers.end(),
[&](const Passenger &p)
{ return p.getSeat().get_rowNum() == seat.get_rowNum() &&
p.getSeat().get_seatNum() == seat.get_seatNum(); });
cout << (isOccupied ? " X |" : " |");
}
cout << " Row " << row.first << endl;
}
}