-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassenger.cpp
More file actions
419 lines (340 loc) · 12.1 KB
/
passenger.cpp
File metadata and controls
419 lines (340 loc) · 12.1 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "passenger.hpp"
// Global variables definitions
Plane plane[max_planes];
int planeCount = 0;
Passenger* head = nullptr;
// convert column letter to index
int columnToIndex(string col) {
if (col == "A") return 0;
if (col == "B") return 1;
if (col == "C") return 2;
if (col == "D") return 3;
if (col == "E") return 4;
if (col == "F") return 5;
return -1;
}
// convert index back to column letter
string indexToColumn(int index) {
string cols[] = {"A", "B", "C", "D", "E", "F"};
if (index >= 0 && index < 6) return cols[index];
return "?";
}
// for input validation
int validateInput(int min, int max) {
int choice;
while (true) {
cin >> choice;
// Check if input is a valid integer
if (cin.fail()) {
cin.clear(); // Clear error flag
cin.ignore(1000, '\n'); // Discard invalid input
cout << " > Invalid input! \n Please enter number (" << min << "-" << max << "): ";
}
// Check if input is within range
else if (choice < min || choice > max) {
cout << " > Out of range! \n Please enter a number between " << min << " and " << max << ": ";
}
else {
return choice;
}
}
}
// Memory usage analyzer
void spaceTrack(string str) {
long long memoryUsed = 0;
string name = "";
// 1. Calculate based on type
if (str == "array") {
memoryUsed = sizeof(plane); // Fixed size (Actual bytes)
}
else if (str == "list") {
// Count nodes for list
Passenger* current = head;
while (current != nullptr) {
memoryUsed += sizeof(Passenger); // Size of node (Actual bytes)
current = current->next;
}
}
// 2. Output One Line
// Format: [Type] ~ Total Memory: 12345 bytes (12.5 KB)
cout << left << setw(20) << " ~ Total Memory: " << memoryUsed << " bytes (" << fixed << setprecision(2) << (memoryUsed / 1024.0) << " KB)" << endl;
}
int selectAvailablePlane(){
cout << "\n==========================================" << endl;
cout << "------------ AVAILABLE FLIGHT ------------" << endl;
cout << "==========================================" << endl;
int total = max_cols * max_rows;
int no_ = 1;
int planeChose;
int rowChose;
string columnChose_A;
string name;
int id;
for (int i=0; i < planeCount; i++){
if(plane[i].occupiedSeats < total){
cout << "\t " << no_ << ". Plane "<< (i+1) << " " << endl;
no_ ++;
}
}
cout << "==========================================" << endl;
bool validPlane = false;
while (!validPlane) {
cout << "Please enter the plane number (1-" << planeCount << ") or 0 to go back: Plane ";
planeChose = validateInput(0, planeCount);
if (planeChose == 0) {
return -1;
}
int planeIndex = planeChose - 1;
if (plane[planeIndex].occupiedSeats < total) {
validPlane = true;
return planeChose;
} else {
cout << "Plane " << planeChose << " is fully occupied!\n";
cout << "Please choose another plane.\n";
}
}
return -1;
}
int getUniquePassengerID(){
bool uniqueID = false;
while (!uniqueID){
cout << "==========================================" << endl;
cout << "Please enter your IDs or (0 to cancel): ";
int id = validateInput(0, 999999);
if (id == 0){
return -1;
}
Passenger* current = head;
bool idExists = false;
while (current != nullptr){
if (current->id == id){
idExists = true;
break;
}
current = current->next;
}
if (idExists){
cout << "ID " << id << " already exists..." << endl;
}else{
uniqueID = true;
return id;
}
}
return -1;
}
string getPassengerName(){
cout << "Please enter your name: ";
cin.ignore();
string name;
getline(cin,name);
return name;
}
bool selectSeat(int planeChose, int& rowChose, string& columnChose_A){
bool validSeat = false;
int planeIndex = planeChose - 1;
while (!validSeat){
displaySeatMap(planeChose);
cout << "\n==========================================" << endl;
cout << "Please enter the row number (1-30) or 0 to cancel: ";
rowChose = validateInput(0, 30);
if (rowChose == 0){
return false;
}
cout << "Please enter the column letter (A-F): ";
cin >> columnChose_A;
if (columnChose_A.length() == 1) {
columnChose_A[0] = toupper(columnChose_A[0]);
}
int columnChose_I = columnToIndex(columnChose_A);
if (columnChose_I == -1){
cout << "Invalid column...";
continue;
}
int rowChose_I = rowChose - 1;
if (plane[planeIndex].seat[rowChose_I][columnChose_I]){
cout << "Seat " << rowChose << columnChose_A << " is occupied!\n";
}else{
validSeat = true;
return true;
}
}
return false;
}
string determineFlightClass(int rowChose){
if (rowChose >= 1 && rowChose <= 3) {
return "First Class";
} else if (rowChose >= 4 && rowChose <= 10) {
return "Business Class";
} else {
return "Economy Class";
}
}
void addedSuccessfull(int id, string name, int planeChose, int rowChose, string columnChose_A, string flightClass){
cout << "\n==========================================" << endl;
cout << " SEAT RESERVED SUCCESSFULLY!" << endl;
cout << "==========================================" << endl;
cout << "Passenger ID: " << id << endl;
cout << "Name: " << name << endl;
cout << "Plane: " << planeChose << endl;
cout << "Seat: " << rowChose << columnChose_A << endl;
cout << "Class: " << flightClass << endl;
cout << "==========================================" << endl;
cout << "\n[Done]\n\nReturning to Main Menu...\n";
}
// create empty plane
void initializeSeatMap() {
planeCount = 0;
head = nullptr;
// Initialize first plane
for (int i = 0; i < max_rows; i++) {
for (int j = 0; j < max_cols; j++) {
plane[0].seat[i][j] = false;
plane[0].info[i][j].id = 0;
plane[0].info[i][j].name = "";
plane[0].info[i][j].seatRow = 0;
plane[0].info[i][j].seatColumn = "";
plane[0].info[i][j].flightClass = "";
plane[0].info[i][j].planeNo = 0;
plane[0].info[i][j].next = nullptr;
}
}
plane[0].occupiedSeats = 0;
planeCount = 1;
}
// for add Passenger to both structure
void addPassenger(Plane &plane, int r, int c, Passenger p) {
// 1. Array Update
plane.info[r][c] = p;
plane.seat[r][c] = true;
plane.occupiedSeats++;
// 2. Linked List Update
Passenger* newNode = new Passenger;
*newNode = p; // Copy the data
newNode->next = head;
head = newNode;
}
void loadData(string filename) {
ifstream file(filename);
if (!file.is_open()) {
cout << "Error: Could not open file " << filename << endl;
return;
}
cout << "\nLoading data..." << endl;
string line;
int loadedCount = 0;
getline(file, line); // ship hearder
while (getline(file, line)) {
stringstream ss(line);
string idStr, name, seatRowStr, seatColumn, flightClass;
// 1. Parse CSV
getline(ss, idStr, ',');
getline(ss, name, ',');
getline(ss, seatRowStr, ',');
getline(ss, seatColumn, ',');
getline(ss, flightClass, ',');
int id = stoi(idStr);
int seatRow = stoi(seatRowStr);
int rowIndex = seatRow - 1;
int colIndex = columnToIndex(seatColumn);
// passenger info
Passenger psgInfo;
psgInfo.id = id;
psgInfo.name = name;
psgInfo.seatRow = seatRow;
psgInfo.seatColumn = seatColumn;
psgInfo.flightClass = flightClass;
psgInfo.next = nullptr;
bool seated = false;
// 2. Check existing planes for this specific seat
for (int k = 0; k < planeCount; k++) {
if (!plane[k].seat[rowIndex][colIndex]) { // Check if the seat is empty or not
// if yes then
psgInfo.planeNo = k + 1; // plane number
addPassenger(plane[k], rowIndex, colIndex, psgInfo);
loadedCount++;
seated = true;
break;
}
}
// 3. If seat was taken in existing planes, create a NEW plane
if (!seated) {
int currentPlaneNo = planeCount;
// Initialize the new plane (Clear memory to be safe)
for (int r = 0; r < max_rows; r++) {
for (int c = 0; c < max_cols; c++) {
plane[currentPlaneNo].seat[r][c] = false;
plane[currentPlaneNo].info[r][c].id = 0;
plane[currentPlaneNo].info[r][c].name = "";
plane[currentPlaneNo].info[r][c].next = nullptr;
}
}
plane[currentPlaneNo].occupiedSeats = 0;
planeCount++; // update have one more plane
psgInfo.planeNo = currentPlaneNo + 1; // update passenger plane no
addPassenger(plane[currentPlaneNo], rowIndex, colIndex, psgInfo);
loadedCount++;
}
}
file.close();
cout << "\nDone! Loaded " << loadedCount << " passengers into " << planeCount << " planes." << endl;
}
// for visual
void displaySeatMap(int planeNo) {
// Validate plane number
if (planeNo < 1 || planeNo > planeCount) {
cout << "\n > [Error] Invalid plane number! \n > Valid range: 1-" << planeCount << endl;
return;
}
int planeIndex = planeNo - 1; // Convert to array index
cout << "\n============ PLANE " << planeNo << " SEAT MAP ============\n";
cout << " A B C \t D E F\n";
for (int row = 0; row < max_rows; row++) {
cout << setw(2) << (row + 1) << " ";
for (int col = 0; col < 6; col++) {
if (col == 3) {
cout << "\t";
}
if (plane[planeIndex].seat[row][col]) cout << "[ R ] ";
else cout << "[ ] ";
}
cout << endl;
}
cout << "\nOccupied seats: " << plane[planeIndex].occupiedSeats
<< " / " << (max_rows * max_cols) << endl;
cout << "[ R ] = Reserved [ ] = Empty\n";
}
// display loaded data summary
void displaySummary() {
if (planeCount == 0) {
cout << "\nNo planes available.\n";
return;
}
int totalSeatsPerPlane = max_rows * max_cols;
int fullyOccupiedPlanes = 0;
int availablePlanes = 0;
int totalPassengers = 0;
int availablePlaneNumbers[max_planes]; // Store plane numbers with available seats
int availableIndex = 0;
// Count total passengers from linked list
Passenger* current = head;
while (current != nullptr) {
totalPassengers++;
current = current->next;
}
// Calculate statistics
for (int i = 0; i < planeCount; i++) {
if (plane[i].occupiedSeats >= totalSeatsPerPlane) {
fullyOccupiedPlanes++;
} else {
availablePlanes++;
availablePlaneNumbers[availableIndex] = i + 1; // Store plane number (1-based)
availableIndex++;
}
}
// Display summary
cout << ".............................................." << endl;
cout << left << setw(30) << "Total Passengers Loaded:" << totalPassengers << endl;
cout << left << setw(30) << "Total Planes Created:" << planeCount << endl;
cout << left << setw(30) << "Fully Occupied Planes:" << fullyOccupiedPlanes << endl;
cout << left << setw(30) << "Available Planes:" << availablePlanes << endl;
}