-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAirPort.cpp
511 lines (455 loc) · 16.6 KB
/
AirPort.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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
#include <fstream>
using namespace std;
// Class: Date
class Date {
private:
int day, month, year, hour, minute;
public:
void setDay(int d) { day = d; }
int getDay() const { return day; }
void setMonth(int m) { month = m; }
int getMonth() const { return month; }
void setYear(int y) { year = y; }
int getYear() const { return year; }
void setHour(int h) { hour = h; }
int getHour() const { return hour; }
void setMinute(int m) { minute = m; }
int getMinute() const { return minute; }
friend ostream& operator<<(ostream& out, const Date& date) {
out << setw(2) << setfill('0') << date.day << "/"
<< setw(2) << setfill('0') << date.month << "/"
<< date.year << " "
<< setw(2) << setfill('0') << date.hour << ":"
<< setw(2) << setfill('0') << date.minute;
return out;
}
friend istream& operator>>(istream& in, Date& date) {
cout << "Enter date (DD MM YYYY HH MM): ";
in >> date.day >> date.month >> date.year >> date.hour >> date.minute;
return in;
}
bool operator<(const Date& other) const {
if (year != other.year) return year < other.year;
if (month != other.month) return month < other.month;
if (day != other.day) return day < other.day;
if (hour != other.hour) return hour < other.hour;
return minute < other.minute;
}
};
// Class: Food
class Food {
private:
string type; // Economic or Business
string meal;
public:
void setType(const string& t) { type = t; }
string getType() const { return type; }
void setMeal(const string& m) { meal = m; }
string getMeal() const { return meal; }
friend ostream& operator<<(ostream& out, const Food& food) {
out << "Type: " << food.type << ", Meal: " << food.meal;
return out;
}
friend istream& operator>>(istream& in, Food& food) {
cout << "Enter food type (Economic/Business): ";
in >> food.type;
cout << "Enter meal: ";
in >> ws;
getline(in, food.meal);
return in;
}
};
// Class: Airport
class Airport {
protected:
vector<string> domesticFlights;
vector<string> foreignFlights;
public:
virtual void addFlight(const string& flight) = 0;
virtual void removeFlight(const string& flight) = 0;
virtual void displayFlights() const = 0;
virtual ~Airport() = default;
};
// Class: DomesticFlights
class DomesticFlights : public Airport {
private:
struct Flight {
string originCity;
string destinationCity;
Date flightDate;
string planeName;
bool isCancelled;
};
vector<Flight> flights;
public:
void addFlight(const string& flight) override {
cout << "This method is not directly used in this class. Use the extended version.\n";
}
void addFlight(const string& origin, const string& destination, const Date& date, const string& plane, bool cancelled) {
if (flights.size() >= 10) {
cout << "Cannot add more than 10 flights." << endl;
return;
}
flights.push_back({origin, destination, date, plane, cancelled});
cout << "Flight added successfully." << endl;
}
void removeFlight(const string& flight) override {
cout << "This method is not directly used in this class. Use the extended version.\n";
}
void removeFlight(const string& origin, const string& destination) {
auto it = find_if(flights.begin(), flights.end(), [&](const Flight& f) {
return f.originCity == origin && f.destinationCity == destination;
});
if (it != flights.end()) {
flights.erase(it);
cout << "Flight removed successfully." << endl;
} else {
cout << "Flight not found." << endl;
}
}
void displayFlights() const override {
vector<Flight> sortedFlights = flights;
sort(sortedFlights.begin(), sortedFlights.end(), [](const Flight& a, const Flight& b) {
return a.flightDate < b.flightDate;
});
ofstream outFile("DomesticFlights.txt");
if (outFile.is_open()) {
for (const auto& f : sortedFlights) {
outFile << "Origin: " << f.originCity << " | Destination: " << f.destinationCity
<< " | Date: " << f.flightDate << " | Plane: " << f.planeName
<< " | Cancelled: " << (f.isCancelled ? "Yes" : "No") << endl;
cout << "Origin: " << f.originCity << " | Destination: " << f.destinationCity
<< " | Date: " << f.flightDate << " | Plane: " << f.planeName
<< " | Cancelled: " << (f.isCancelled ? "Yes" : "No") << endl;
}
outFile.close();
cout << "Flight details saved to DomesticFlights.txt." << endl;
} else {
cout << "Error opening file to save flights." << endl;
}
}
};
// Class: ForeignFlights
class ForeignFlights : public Airport {
private:
struct Flight {
string originCity;
string destinationCity;
Date flightDate;
string planeName;
bool isCancelled;
};
vector<Flight> flights;
public:
void addFlight(const string& flight) override {
cout << "This method is not directly used in this class. Use the extended version.\n";
}
void addFlight(const string& origin, const string& destination, const Date& date, const string& plane, bool cancelled) {
if (flights.size() >= 10) {
cout << "Cannot add more than 10 flights." << endl;
return;
}
flights.push_back({origin, destination, date, plane, cancelled});
cout << "Flight added successfully." << endl;
}
void removeFlight(const string& flight) override {
cout << "This method is not directly used in this class. Use the extended version.\n";
}
void removeFlight(const string& origin, const string& destination) {
auto it = find_if(flights.begin(), flights.end(), [&](const Flight& f) {
return f.originCity == origin && f.destinationCity == destination;
});
if (it != flights.end()) {
flights.erase(it);
cout << "Flight removed successfully." << endl;
} else {
cout << "Flight not found." << endl;
}
}
void displayFlights() const override {
vector<Flight> sortedFlights = flights;
sort(sortedFlights.begin(), sortedFlights.end(), [](const Flight& a, const Flight& b) {
return a.flightDate < b.flightDate;
});
ofstream outFile("ForeignFlights.txt");
if (outFile.is_open()) {
for (const auto& f : sortedFlights) {
outFile << "Origin: " << f.originCity << " | Destination: " << f.destinationCity
<< " | Date: " << f.flightDate << " | Plane: " << f.planeName
<< " | Cancelled: " << (f.isCancelled ? "Yes" : "No") << endl;
cout << "Origin: " << f.originCity << " | Destination: " << f.destinationCity
<< " | Date: " << f.flightDate << " | Plane: " << f.planeName
<< " | Cancelled: " << (f.isCancelled ? "Yes" : "No") << endl;
}
outFile.close();
cout << "Flight details saved to ForeignFlights.txt." << endl;
} else {
cout << "Error opening file to save flights." << endl;
}
}
};
// Class: Tax
class Tax {
private:
const double percentage;
public:
Tax(double p) : percentage(p) {}
double calculateTax(double price) const {
return price + (price * (percentage / 100));
}
};
// Class: Customer
class Customer {
private:
string name;
double balance;
vector<string> purchasedFlights;
public:
void setName(const string& n) { name = n; }
string getName() const { return name; }
void setBalance(double b) { balance = b; }
double getBalance() const { return balance; }
void addPurchasedFlight(const string& flight) {
purchasedFlights.push_back(flight);
cout << "Flight purchased: " << flight << endl;
ofstream outFile("PurchasedFlights.txt", ios::app);
if (outFile.is_open()) {
outFile << flight << endl;
outFile.close();
} else {
cout << "Error saving purchased flight to file." << endl;
}
}
void refundTicket(const string& flight, double penalty) {
auto it = find(purchasedFlights.begin(), purchasedFlights.end(), flight);
if (it != purchasedFlights.end()) {
purchasedFlights.erase(it);
balance -= penalty;
cout << "Flight refunded: " << flight << " | Penalty: " << penalty << endl;
} else {
cout << "Ticket not found." << endl;
}
}
void displayPurchasedFlights() const {
cout << "Purchased Flights:" << endl;
for (const auto& flight : purchasedFlights) {
cout << "- " << flight << endl;
}
}
void increaseBalance(double amount) {
balance += amount;
cout << "Balance increased by " << amount << ". New balance: " << balance << endl;
}
};
// Class: Admin
class Admin {
private:
string adminName;
string adminPassword;
public:
void setAdminName(const string& name) { adminName = name; }
string getAdminName() const { return adminName; }
void setAdminPassword(const string& password) { adminPassword = password; }
string getAdminPassword() const { return adminPassword; }
bool authenticate(const string& name, const string& password) const {
return (name == adminName && password == adminPassword);
}
};
// Admin Panel
void adminPanel(Admin& admin, DomesticFlights& domestic, ForeignFlights& foreign) {
string name, password;
cout << "Enter Admin Name: ";
cin >> name;
cout << "Enter Admin Password: ";
cin >> password;
if (admin.authenticate(name, password)) {
int choice;
do {
cout << "\n--- Admin Panel ---\n";
cout << "1. Add Domestic Flight\n";
cout << "2. Add Foreign Flight\n";
cout << "3. Remove Domestic Flight\n";
cout << "4. Remove Foreign Flight\n";
cout << "5. Display Domestic Flights\n";
cout << "6. Display Foreign Flights\n";
cout << "7. Logout\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
string origin, destination, plane;
Date date;
bool cancelled;
cout << "Enter origin city: ";
cin >> origin;
cout << "Enter destination city: ";
cin >> destination;
cout << "Enter flight date: \n";
cin >> date;
cout << "Enter plane name: ";
cin >> plane;
cout << "Is flight cancelled (1 for Yes, 0 for No): ";
cin >> cancelled;
domestic.addFlight(origin, destination, date, plane, cancelled);
break;
}
case 2: {
string origin, destination, plane;
Date date;
bool cancelled;
cout << "Enter origin city: ";
cin >> origin;
cout << "Enter destination city: ";
cin >> destination;
cout << "Enter flight date: \n";
cin >> date;
cout << "Enter plane name: ";
cin >> plane;
cout << "Is flight cancelled (1 for Yes, 0 for No): ";
cin >> cancelled;
foreign.addFlight(origin, destination, date, plane, cancelled);
break;
}
case 3: {
string origin, destination;
cout << "Enter origin city: ";
cin >> origin;
cout << "Enter destination city: ";
cin >> destination;
domestic.removeFlight(origin, destination);
break;
}
case 4: {
string origin, destination;
cout << "Enter origin city: ";
cin >> origin;
cout << "Enter destination city: ";
cin >> destination;
foreign.removeFlight(origin, destination);
break;
}
case 5:
domestic.displayFlights();
break;
case 6:
foreign.displayFlights();
break;
case 7:
cout << "Logging out...\n";
break;
default:
cout << "Invalid choice. Try again.\n";
}
} while (choice != 7);
} else {
cout << "Authentication failed.\n";
}
}
// Customer Panel
void customerPanel(Customer& customer, DomesticFlights& domestic, ForeignFlights& foreign, Tax& tax) {
int choice;
do {
cout << "\n--- Customer Panel ---\n";
cout << "1. View Domestic Flights\n";
cout << "2. View Foreign Flights\n";
cout << "3. Purchase Flight\n";
cout << "4. Refund Flight\n";
cout << "5. View Purchased Flights\n";
cout << "6. Increase Balance\n";
cout << "7. Logout\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
domestic.displayFlights();
break;
case 2:
foreign.displayFlights();
break;
case 3: {
string origin, destination;
double price;
cout << "Enter origin city: ";
cin >> origin;
cout << "Enter destination city: ";
cin >> destination;
cout << "Enter ticket price: ";
cin >> price;
double totalPrice = tax.calculateTax(price);
if (customer.getBalance() >= totalPrice) {
customer.setBalance(customer.getBalance() - totalPrice);
customer.addPurchasedFlight(origin + "-" + destination);
cout << "Ticket purchased successfully. Total cost: " << totalPrice << endl;
} else {
cout << "Insufficient balance." << endl;
}
break;
}
case 4: {
string flight;
double penalty;
cout << "Enter flight to refund: ";
cin.ignore();
getline(cin, flight);
cout << "Enter refund penalty: ";
cin >> penalty;
customer.refundTicket(flight, penalty);
break;
}
case 5:
customer.displayPurchasedFlights();
break;
case 6: {
double amount;
cout << "Enter amount to increase balance: ";
cin >> amount;
customer.increaseBalance(amount);
break;
}
case 7:
cout << "Logging out...\n";
break;
default:
cout << "Invalid choice. Try again.\n";
}
} while (choice != 7);
}
int main() {
DomesticFlights domestic;
ForeignFlights foreign;
Customer customer;
Admin admin;
Tax tax(10.0); // 10% tax
string adminName = "admin";
string adminPassword = "1234";
admin.setAdminName(adminName);
admin.setAdminPassword(adminPassword);
int mainChoice;
do {
cout << "\n--- Main Menu ---\n";
cout << "1. Admin Panel\n";
cout << "2. Customer Panel\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> mainChoice;
switch (mainChoice) {
case 1:
adminPanel(admin, domestic, foreign);
break;
case 2:
customerPanel(customer, domestic, foreign, tax);
break;
case 3:
cout << "Exiting the system. Goodbye!\n";
break;
default:
cout << "Invalid choice. Try again.\n";
}
} while (mainChoice != 3);
return 0;
}