-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkyTrack.cpp
More file actions
292 lines (268 loc) · 9.37 KB
/
SkyTrack.cpp
File metadata and controls
292 lines (268 loc) · 9.37 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
#include <iostream>
#include <stdlib.h>
#include <string.h>
#define FILENAME "flights.dat"
using namespace std;
struct Flight {
char flightNumber[10];
char departureCity[30];
char destinationCity[30];
char departureDateTime[20];
char arrivalDateTime[20];
int firstClassAvailable;
int firstClassSold;
int coachAvailable;
int coachSold;
} ;
void createFlightRecord();
void deleteFlightRecord(const char *flightNumber);
void updateFlightRecord(const char *flightNumber);
void makeReservation(const char *flightNumber, const char *seatClass, int seats);
void cancelReservation(const char *flightNumber, const char *seatClass, int seats);
void displayFlights();
int main() {
int choice;
char flightNumber[10], seatClass[10];
int seats;
do {
cout<<"\n--- Airline Reservation System ---"<<endl;
cout<<"1. Create Flight Record"<<endl;
cout<<"2. Delete Flight Record"<<endl;
cout<<"3. Update Flight Record"<<endl;
cout<<"4. Make Reservation"<<endl;
cout<<"5. Cancel Reservation"<<endl;
cout<<"6. Display Flights"<<endl;
cout<<"0. Exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch (choice) {
case 1:
createFlightRecord();
break;
case 2:
cout<<"Enter Flight Number to Delete: ";
cin>>flightNumber;
deleteFlightRecord(flightNumber);
break;
case 3:
cout<<"Enter Flight Number to Update: ";
cin>>flightNumber;
updateFlightRecord(flightNumber);
break;
case 4:
cout<<"Enter Flight Number: ";
cin>>flightNumber;
cout<<"Enter Seat Class (First/Coach): ";
cin>>seatClass;
cout<<"Enter Number of Seats: ";
cin>>seats;
makeReservation(flightNumber, seatClass, seats);
break;
case 5:
cout<<"Enter Flight Number: ";
cin>>flightNumber;
cout<<"Enter Seat Class (First/Coach): ";
cin>>seatClass;
cout<<"Enter Number of Seats: ";
cin>>seats;
cancelReservation(flightNumber, seatClass, seats);
break;
case 6:
displayFlights();
break;
case 0:
cout<<"Exiting...\n";
break;
default:
cout<<"Invalid choice! Please try again.\n";
}
} while (choice != 0);
return 0;
}
void createFlightRecord() {
FILE *file = fopen(FILENAME, "ab");
if (!file) {
perror("Error opening file");
return;
}
struct Flight flight;
cout<<"Enter Flight Number: ";
cin>>flight.flightNumber;
cout<<"Enter Departure City: ";
cin>>flight.departureCity;
cout<<"Enter Destination City: ";
cin>>flight.destinationCity;
cout<<"Enter Departure Date and Time: ";
cin>>flight.departureDateTime;
cout<<"Enter Arrival Date and Time: ";
cin>>flight.arrivalDateTime;
cout<<"Enter First Class Seats Available: ";
cin>>flight.firstClassAvailable;
flight.firstClassSold = 0;
cout<<"Enter Coach Seats Available: ";
cin>>flight.coachAvailable;
flight.coachSold = 0;
fwrite(&flight, sizeof(Flight), 1, file);
fclose(file);
cout<<"Flight record created successfully."<<endl;
}
void deleteFlightRecord(const char *flightNumber) {
FILE *file = fopen(FILENAME, "rb");
FILE *temp = fopen("temp.dat", "wb");
if (!file || !temp) {
perror("Error opening file");
return;
}
Flight flight;
int found = 0;
while (fread(&flight, sizeof(Flight), 1, file)) {
if (strcmp(flight.flightNumber, flightNumber) != 0) {
fwrite(&flight, sizeof(Flight), 1, temp);
} else {
found = 1;
}
}
fclose(file);
fclose(temp);
remove(FILENAME);
rename("temp.dat", FILENAME);
if (found)
cout<<"Flight record deleted successfully."<<endl;
else
cout<<"Flight record not found."<<endl;
}
void updateFlightRecord(const char *flightNumber) {
FILE *file = fopen(FILENAME, "rb+");
if (!file) {
perror("Error opening file");
return;
}
Flight flight;
int found = 0;
while (fread(&flight, sizeof(Flight), 1, file)) {
if (strcmp(flight.flightNumber, flightNumber) == 0) {
found = 1;
cout<<"Enter New Departure City: ";
cin>> flight.departureCity;
cout<<"Enter New Destination City: ";
cin>> flight.destinationCity;
cout<<"Enter New Departure Date and Time: ";
cin>> flight.departureDateTime;
cout<<"Enter New Arrival Date and Time: ";
cin>> flight.arrivalDateTime;
cout<<"Enter New First Class Seats Available: ";
cin>>flight.firstClassAvailable;
cout<<"Enter New Coach Seats Available: ";
cin>>flight.coachAvailable;
fseek(file, -sizeof(Flight), SEEK_CUR);
fwrite(&flight, sizeof(Flight), 1, file);
break;
}
}
fclose(file);
if (found)
cout<<"Flight record updated successfully."<<endl;
else
cout<<"Flight record not found."<<endl;
}
void makeReservation(const char *flightNumber, const char *seatClass, int seats) {
FILE *file = fopen(FILENAME, "rb+");
if (!file) {
perror("Error opening file");
return;
}
Flight flight;
int found = 0;
while (fread(&flight, sizeof(Flight), 1, file)) {
if (strcmp(flight.flightNumber, flightNumber) == 0) {
found = 1;
if (strcmp(seatClass, "First") == 0) {
if (seats <= flight.firstClassAvailable) {
flight.firstClassAvailable -= seats;
flight.firstClassSold += seats;
fseek(file, -sizeof(Flight), SEEK_CUR);
fwrite(&flight, sizeof(Flight), 1, file);
cout<<"Reservation successful."<<endl;
} else {
cout<<"Not enough First Class seats available."<<endl;
}
} else if (strcmp(seatClass, "Coach") == 0) {
if (seats <= flight.coachAvailable) {
flight.coachAvailable -= seats;
flight.coachSold += seats;
fseek(file, -sizeof(Flight), SEEK_CUR);
fwrite(&flight, sizeof(Flight), 1, file);
cout<<"Reservation successful."<<endl;
} else {
cout<<"Not enough Coach seats available."<<endl;
}
} else {
cout<<"Invalid seat class."<<endl;
}
break;
}
}
fclose(file);
if (!found)
cout<<"Flight record not found."<<endl;
}
void cancelReservation(const char *flightNumber, const char *seatClass, int seats) {
FILE *file = fopen(FILENAME, "rb+");
if (!file) {
perror("Error opening file");
return;
}
Flight flight;
int found = 0;
while (fread(&flight, sizeof(Flight), 1, file)) {
if (strcmp(flight.flightNumber, flightNumber) == 0) {
found = 1;
if (strcmp(seatClass, "First") == 0) {
if (seats <= flight.firstClassSold && flight.firstClassSold > 0) {
flight.firstClassAvailable += seats;
flight.firstClassSold -= seats;
fseek(file, -sizeof(Flight), SEEK_CUR);
fwrite(&flight, sizeof(Flight), 1, file);
cout<<"Cancellation successful."<<endl;
} else {
cout<<"Not enough First Class reservations to cancel."<<endl;
}
} else if (strcmp(seatClass, "Coach") == 0) {
if (seats <= flight.coachSold) {
flight.coachAvailable += seats;
flight.coachSold -= seats;
fseek(file, -sizeof(Flight), SEEK_CUR);
fwrite(&flight, sizeof(Flight), 1, file);
cout<<"Cancellation successful."<<endl;
} else {
cout<<"Not enough Coach reservations to cancel."<<endl;
}
} else {
cout<<"Invalid seat class."<<endl;
}
break;
}
}
fclose(file);
if (!found)
cout<<"Flight record not found."<<endl;
}
void displayFlights() {
FILE *file = fopen(FILENAME, "rb");
if (!file) {
perror("Error opening file");
return;
}
Flight flight;
cout<<"\n--- Flight Records ---"<<endl;
while (fread(&flight, sizeof(Flight), 1, file)) {
cout<<"\nFlight Number:"<<flight.flightNumber<<endl;
cout<<"Departure City: "<<flight.departureCity<<endl;
cout<<"Destination City: "<<flight.destinationCity<<endl;
cout<<"Departure Date and Time: "<<flight.departureDateTime<<endl;
cout<<"Arrival Date and Time: "<<flight.arrivalDateTime<<endl;
cout<<"First Class Seats Available: "<<flight.firstClassAvailable<<"Sold: "<<flight.firstClassSold<<endl;
cout<<"Coach Seats Available: "<<flight.coachAvailable<< "Sold:"<<flight.coachSold<<endl;
}
fclose(file);
}