-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
323 lines (293 loc) · 9.26 KB
/
Copy pathmain.cpp
File metadata and controls
323 lines (293 loc) · 9.26 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
#include <iostream>
#include <iomanip> //for set()
#include <string>
using namespace std;
const int NUM = 10;
const float FLIGHT = 1600.00;
const float F_TIMES[5][2] = {{7.00, 9.30}, {9.00, 11.30},
{11.00, 13.30}, {13.00, 15.30},
{15.00, 17.30}};
struct travelBooking {
string travellerName;
string travelClass;
float departureTime;
float arrivalTime;
string seatNo;
};
bool validateTimeOption(int opt){
if(opt == 1 || opt == 2 || opt == 3 || opt == 4 || opt == 5)
return true;
else
return false;
}
bool validateSeat(travelBooking t[], string sNo, int timeChoice){
bool notBooked = true;
float dTime;
dTime = F_TIMES[timeChoice - 1][0];
int i = 0;
// check if it is already booked
while(i < NUM){
if(t[i].seatNo == sNo && t[i].departureTime == dTime){
notBooked = false;
break;
}
i++;
}
if(notBooked == false)
return false;
else
return true;
}
void timeMenu(int & timeChoice){
cout << "The available travel times for "
<< "flights are:" << endl;
cout << setw(16) << " Depart"
<< setw(7) << "Arrive" << endl;
for(int i = 0; i < 5; i++)
cout << i + 1 << setw(3) << "." << setw(12)
<< F_TIMES[i][0] << F_TIMES[i][1] << endl;
cout << "Choose the time by entering the "
<< "option number from the displayed list:" << endl;
do {
cin >> timeChoice;
if(!validateTimeOption(timeChoice))
cout << "Incorrect option! Please choose from 1-5.";
}while(!validateTimeOption(timeChoice));
}
//called the very first time, for any of the listed times
void displaySeats1(){
int seatFlag = 0;
int divValue;
char row = 'A';
int col = 1;
divValue = 3;
cout.setf(ios::left);
// display the seats
cout << "First Class(";
cout << FLIGHT + FLIGHT * 0.20;
cout << ")" << endl;
cout << "|";
for(int i = 1; i <= 5; i++){
if(i == 25){
cout << "Economy class(";
cout << FLIGHT;
cout << ")" << endl;
cout << "|";
}
// display seats
cout << row << col++;
// arranging the seats in each row
if(i % divValue == 0){
if(seatFlag == 0){
cout << "|";
cout << setw(4) << "---- ";
seatFlag = 1;
}
else
{
cout << "|";
cout << endl;
row = row + 1;
seatFlag = 0;
col =1;
}
}
cout << "|";
}
}
// called from second time onwards, for any particular time
void displaySeats2(travelBooking t[], int timeChoice){
int seatFlag = 0;
// int number;
bool notBooked;
int divValue;
char row = 'A';
int col = 1;
divValue = 3;
// float dTime;
string sNo;
cout.setf(ios::left);
// displaying the seats
cout << "First Class(";
cout << FLIGHT + FLIGHT + 0.20;
cout << ")" << endl;
cout << "|";
for(int i = 1; i <= 50; i++){
//cout << "Current seat no: << sNo << endl;
sNo = ""; // very important
notBooked = true;
if(i == 20){
cout << "Economy class(";
cout << FLIGHT;
cout << ")" << endl;
cout << "|";
}
sNo += row;
sNo += to_string(col);
// check if it is already booked
notBooked = validateSeat(t, sNo, timeChoice);
// display seats
if(notBooked == false){
cout << "**";
col++;
}
else
cout << row << col++;
// arranging the seats for each row
if(i % divValue == 0){
if(seatFlag == 0){
cout << "|";
cout << setw(4) << "---- ";
seatFlag = 1;
}
else{
cout << "|";
cout << endl;
row = row + 1;
seatFlag = 0;
col = 1;
}
}
cout << "|";
}
}
float calcTicketPrice(travelBooking t){
float price = 0.0;
if((t.seatNo.substr(0, 1)) > "D") // economy class
price = FLIGHT;
else
price = FLIGHT + FLIGHT * 0.20;
return price;
}
void displayTicket(travelBooking t){
cout << setw(25) << "*************************" << endl;
if((t.seatNo.substr(0, 1)) > "D")
t.travelClass = "Economy class";
else
t.travelClass = "First class";
cout << "\nTravel ticket for " << "FLIGHT" << endl;
cout << setw(25) << "*************************" << endl;
cout << setw(15) << "Name" << setw(3) << ":"
<< setw(20) << t.travellerName;
cout << setw(20) << "Travel Ticket class"
<< setw(3) << ":" << t.travelClass << endl;
cout << setw(38) << ":" << setw(20) << "Seat No"
<< setw(3) << ":" << t.seatNo << endl;
cout << setw(15) << "Departure" << setw(3) << ":"
<< "Johannesburg";
cout << setw(8) << "" << setw(20) << "Departure Time"
<< setw(3) << ":" << t.departureTime << endl;
cout << setw(15) << "Destination" << setw(3) << ":"
<< "Cape Town";
cout << setw(11) << "" << setw(20) << "Arrival Time"
<< setw(3) << ":" << t.arrivalTime << endl;
// display amount
cout << "*************************" << endl;
cout << "Amount:R" << calcTicketPrice(t);
cout << " Thank you for booking with COS1511."
<< "Your travel agent for queries is "
<< "Annie Mathew. " << endl;
cout << "*************************" << endl;
}
int main()
{
travelBooking traveller[NUM]; // array of structs
string fullName;
string seatNo;
int depTimeChoice;
char answer;
int timeCount1, timeCount2, timeCount3, timeCount4, timeCount5, bCount;
timeCount1 = timeCount2 = timeCount3 = timeCount4 = timeCount5 = 0;
int i = 0;
do{
cout << "Welcome to COS1511 Flight Booking system"
<< endl;
cout << "\nEnter full name: " << endl;
getline(cin, fullName, '\n');
cout << endl;
cout.setf(ios::left);
cout.precision(2);
cout.setf(ios::fixed);
// call function to display time chart
timeMenu(depTimeChoice);
cout << "\nThe available seats for "
<< F_TIMES[depTimeChoice - 1][0]
<< " are as follows:" << endl;
switch(depTimeChoice){
case 1:
bCount = timeCount1;
break;
case 2:
bCount = timeCount2;
break;
case 3:
bCount = timeCount3;
break;
case 4:
bCount = timeCount4;
break;
case 5:
bCount = timeCount5;
break;
}
if(bCount == 0){
displaySeats1(); // first time seat dispaly
cout << "\nPlease key in a seat number "
<< "to choose a seat(e.g:A2)" << endl;
}
else{
// seat display after bookings are made
displaySeats2(traveller, depTimeChoice);
cout << "\n\nSeats that are already taken "
<< "are indicated with an asterisk " << endl;
cout << "Please key in a seat number to "
<< "choose a seat(e.g:A2)" << endl;
}
// validating the chosen number
do{
cin >> seatNo;
if((!validateSeat(traveller, seatNo, depTimeChoice)))
cout << "Sorry, the seat is taken. "
<< "Please choose a seat that is available "
<< endl;
}while(!validateSeat(traveller, seatNo, depTimeChoice));
// assign the booking details to the array of structs
traveller[i].travellerName = fullName;
if((seatNo.substr(0, 1)) > "D")
traveller[i].travelClass = "Economy class";
else
traveller[i].travelClass = "First class";
traveller[i].departureTime = F_TIMES[depTimeChoice - 1][0];
traveller[i].arrivalTime =F_TIMES[depTimeChoice - 1][1];
traveller[i].seatNo = seatNo;
// display ticket
displayTicket(traveller[i]);
i++;
cout << "Do you want to make another booking(Y/N)?" << endl;
cin >> answer;
cin.get();
// based on time choice increment the counts
if(depTimeChoice == 1)
timeCount1++;
else if(depTimeChoice == 2)
timeCount2++;
else if(depTimeChoice == 3)
timeCount3++;
else if(depTimeChoice == 4)
timeCount4++;
else if(depTimeChoice == 5)
timeCount5++;
}while(toupper(answer) == 'Y');
cout << endl;
cout << "Number of bookings made for " << F_TIMES[0][0]
<< "a.m" << ":" << timeCount1 << endl;
cout << "Number of bookings made for " << F_TIMES[1][0]
<< "a.m" << ":" << timeCount2 << endl;
cout << "Number of bookings made for " << F_TIMES[2][0]
<< "a.m" << ":" << timeCount3 << endl;
cout << "Number of bookings made for " << F_TIMES[3][0]
<< "a.m" << ":" << timeCount4 << endl;
cout << "Number of bookings made for " << F_TIMES[4][0]
<< "a.m" << ":" << timeCount5 << endl;
return 0;
}