-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathMain.c
420 lines (403 loc) · 11.2 KB
/
Main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <time.h>
typedef struct BinarySearchTree BST;
// if bst is empty then we have to handle the error
struct BinarySearchTree
{
int PassnNo; // busNo0SeatNo.
char name[10];
struct BinarySearchTree *left;
struct BinarySearchTree *right;
};
BST *root = NULL;
int cost(BST *r); // calculates costs
void status(); // shows bus and seats status
void busLists(); // shows buslist and do booking seat and return customer ID
void DisplaySeat(int bus[33]); // Display the seats of buses
void cancel(int x); //cancel the booking
BST *reservationInfo(BST *, int, int *); // Display Reservation Info
BST *insert(BST **r, int custID); // inserting a node
int busSeat[32][9] = {0};
void redColor() /// Print the message in redcolor
{
printf("\033[1;31m");
}
void resetColor() /// reset the old color of console
{
printf("\033[0m");
}
BST *reservationInfo(BST *r, int s, int *custIDmatched) // find function
{
if (r == NULL)
return NULL;
BST *presentnode = r;
while (presentnode)
{
// --------------------
if (presentnode->PassnNo == s)
{
*custIDmatched = 1;
redColor();
printf("\n-----------------------------------------------------------------");
printf("\n|| NAME: %10s ||", (presentnode->name));
printf("\n|| CUSTOMER ID: %d ||", presentnode->PassnNo);
printf("\n|| BUS NUMBER: %d ||", (presentnode->PassnNo) / 1000);
printf("\n|| SEAT NUMBER: %d ||", (presentnode->PassnNo) % 100);
printf("\n|| TICKET COST: Rs.%d ||", cost(presentnode));
printf("\n-----------------------------------------------------------------");
resetColor();
getch();
return r;
}
else if (presentnode->PassnNo > s)
presentnode = presentnode->left;
else
presentnode = presentnode->right;
}
return NULL;
}
BST *insert(BST **r, int custId)
{
if (*r == NULL)
{
*r = (BST *)malloc(sizeof(BST));
(*r)->PassnNo = custId;
if (*r == NULL)
{
printf("No memory…");
return NULL;
}
else
{
(*r)->left = (*r)->right = NULL;
printf("\n ENTER THE PERSON NAME: ");
scanf("%s", &((*r)->name));
}
}
else
{
if ((*r)->PassnNo > custId)
{
(*r)->left = insert(&((*r)->left), custId);
}
else if ((*r)->PassnNo < custId)
{
(*r)->right = insert(&((*r)->right), custId);
}
}
return *r;
}
void DisplaySeat(int bus[33])
{
for (int i = 1; i <= 32; i++)
{
redColor();
if (i < 10 && i > 0)
{
printf("0%d .", i);
}
else
{
printf("%d .", i);
}
resetColor();
{
if (bus[i] == 0)
printf("EMPTY ");
else
printf("BOOKED"); // reserv
}
printf(" ");
if (i % 4 == 0)
printf("\n");
}
}
void login()
{
char userName[20] = "user";
char passWord[10] = "team18";
char matchPass[10];
char matchUser[10];
int value;
redColor();
printf("\n\n=========================================================================================\n");
printf("\n\t\t\tWELCOME TO ONLINE BUS RESERVATION");
printf("\n\n=========================================================================================\n\n");
resetColor();
login:
{
printf("\n\nUserName: ");
gets(matchUser);
printf("\nPassWord: ");
gets(matchPass);
}
value = strcmp(passWord, matchPass); /// string compare is function defined in headerfile i.e string.h
if (value != 0)
{
redColor();
printf("\nINVALID DETAILS TRY AGAIN...\n");
resetColor();
goto login;
}
else
{
printf("\nLOGED IN SUCCESFULLY...\n");
}
}
int cost(BST *r)
{
int cost, buscost;
buscost = (r->PassnNo) / 1000;
switch (buscost % 3)
{
case 1:
return 70;
break;
case 2:
return 55;
break;
case 0:
return 40;
break;
default:
return 0;
break;
}
}
void status()
{
int busNum;
busLists();
busInput:
printf("\n\nENTER YOUR BUS NUMBER : ");
scanf("%d", &busNum);
if (busNum <= 0 || busNum >= 10)
{
redColor();
printf("\n PLEASE ENTER CORRECT BUS NUMBER !!\n");
resetColor();
goto busInput;
}
printf("\n");
DisplaySeat(busSeat[busNum]);
getch();
}
void busLists()
{
redColor();
printf("-----------------------------------------------------------------------------------------");
printf("\nBus.No\tName\t\t\tDestinations \t\tCharges \t\tTime\n");
printf("-----------------------------------------------------------------------------------------");
resetColor();
printf("\n1\tGangaTravels \tDharan to Kavre \tRs.70 \t\t07:00 AM");
printf("\n2\tPhaphara Travels \tKavre To Dharan \tRs.55 \t\t01:30 PM");
printf("\n3\tShiv Ganga Travels \tAllahabad To Gorakhpur\tRs.40 \t\t03:50 PM");
printf("\n4\tSuper Deluxe \tPokhara To Benigha \tRs.70 \t\t01:00 AM");
printf("\n5\tSai Baba Travels \tMaitidevi To Janakpur \tRs.55 \t\t12:05 AM");
printf("\n6\tShine On Travels \tMadhubani to Patna \tRs.40 \t\t09:30 AM");
printf("\n7\tMayur Travels \tPatna To Gaya \tRs.70 \t\t11:00 PM");
printf("\n8\tRajjo Travels \tBegusarai To Patna \tRs.55 \t\t08:15 AM");
printf("\n9\tShree Travels \tGaya To Chhapra \tRs.40 \t\t04:00 PM");
printf("\n");
printf("\n PRESS 'ENTER' KEY TO CONTINUE ");
getch();
}
void cancel(int randomNum)
{
int reservationNo;
int seatNumber;
int choice;
char c;
int seatCancel;
aa:
{
printf("\nENTER YOUR RESERVATION NUMBER : ");
scanf("%d", &reservationNo);
if (reservationNo == randomNum)
{
printf("\nRESERVATION NUMBER IS IT CORRECT ? %d \nENTER (Y/N) : ", reservationNo);
scanf("%s", &c);
if (c == 'y' || c == 'Y')
{
printf("\n\n============================================\n\n");
printf(" ENTER THE BUS NUMBER: ");
scanf("%d", &choice);
printf("\n HOW MANY SEATS DO WANT TO CANCEL : ");
scanf("%d", &seatCancel);
for (int i = 0; i < seatCancel; i++)
{
printf(" \nENTER THE SEAT NUMBER: ");
scanf("%d", &seatNumber);
busSeat[choice][seatNumber] = 0;
}
printf("\n\nYOUR RESERVATION HAS BEEN CANCEL !!\n\n");
printf("\n PRESS 'ENTER' KEY TO CONTINUE \n");
getch();
DisplaySeat(busSeat[choice]);
}
else if (c == 'n' || c == 'N')
{
printf("\nYOUR RESERVATION CANCELATION HAS BEEN DENIED\n");
}
}
else
{
printf("\nNOT FOUND!! ENTER THE CORRECT RESERVATION NUMBER\n");
goto aa;
}
}
}
int main()
{
srand(time(0));
int randomNum = rand();
int num, i, custID, reservationNo;
BST *root1;
login();
main:
{
do
{
printf("\n\n====================================================================\n\n");
printf("\t\t\t\033[1;31mBUS RESERVATION\033[0m\t\t");
printf("\n\n=====================================================================\n");
printf("\n====================");
redColor();
printf(" MAIN MENU ");
resetColor();
printf("=====================\n\n");
printf(" \033[1;31m[1]\033[0m VIEW BUS LIST \n\n");
printf(" \033[1;31m[2]\033[0m BOOK TICKETS\n\n");
printf(" \033[1;31m[3]\033[0m CANCEL BOOKING\n\n");
printf(" \033[1;31m[4]\033[0m BUSES SEATS INFO\n\n");
printf(" \033[1;31m[5]\033[0m RESERVATION INFO\n\n");
printf(" \033[1;31m[6]\033[0m EXIT\n");
printf("\n=====================================================\n");
printf("\n ENTER YOUR CHOICE: ");
scanf("%d", &num);
switch (num)
{
case 1:
busLists(); // for list of bus
break;
case 2:
busLists(); // for booking the tickets
int CustId, choice, seats;
busChoice:
printf("\n\nCHOOSE YOUR BUS : ");
scanf("%d", &choice);
if (choice <= 0 || choice > 9)
{
redColor();
printf("\nENTER VALID BUS NUMBER !! \n");
resetColor();
getch();
goto busChoice;
}
printf("\n");
DisplaySeat(busSeat[choice]);
busSeatChoice:
printf("\n\nNO. OF SEATS YOU NEED TO BOOK : ");
scanf("%d", &seats);
if (seats <= 0)
{
redColor();
printf("\nENTER VALID SEAT NUMBER!!\n");
resetColor();
goto busSeatChoice;
}
else if (seats > 32)
{
redColor();
printf("\nENTER VALID SEAT NUMBER WE HAVE ONLY 32 SEATS IN A BUS !!\n");
resetColor();
goto busSeatChoice;
}
int seatNumber;
for (int i = 1; i <= seats; i++)
{
printf("\n\n==================================================================================\n\n");
seat:
printf(" ENTER THE SEAT NUMBER: ");
scanf("%d", &seatNumber);
if (seatNumber <= 0)
{
redColor();
printf("\n ENTER VALID SEAT NUMBER!!\n\n");
resetColor();
goto seat;
}
else if (seatNumber > 32)
{
redColor();
printf("\n ENTER VALID SEAT NUMBER WE HAVE ONLY 32 SEATS IN A BUS !!\n\n");
resetColor();
goto seat;
}
CustId = choice * 1000 + seatNumber; // CustumerId
busSeat[choice][seatNumber] = 1;
root = insert(&root, CustId);
redColor();
printf("\n YOUR CUSTOMER ID IS : %d", CustId);
resetColor();
printf("\n\n==================================================================================\n\n");
}
printf("\nYOUR RESERVATION NUMBER IS : ");
redColor();
printf("%d\n", randomNum);
printf("\nPLEASE NOTE DOWN YOUR RESERVATION NUMBER FOR CANCEL BOOKING TICKETS!!\n");
resetColor();
printf("PRESS 'ENTER' KEY TO CONTINUE ");
getch();
break;
case 3:
cancel(randomNum);
break;
case 4:
status(randomNum);
break;
case 5:
takingReservationNo:
printf("\n ENTER YOUR RESERVATION NUMBER :");
scanf("%d", &reservationNo);
if (randomNum == reservationNo)
{
cust:
printf("\n ENTER YOUR CUSTOMER ID :");
scanf("%d", &custID);
int custIDmatched = 0;
root1 = reservationInfo(root, custID, &custIDmatched);
if (custIDmatched == 0)
{
redColor();
printf("\n ENTER CORRECT CUSTOMER ID!!\n");
resetColor();
goto cust;
}
}
else
{
redColor();
printf("\n INVALID RESERVATION NUMBER PLEASE ENTER CORRECT RESERVATION NUMBER !!\n");
resetColor();
goto takingReservationNo;
}
break;
default:
redColor();
printf("\n\n INVALID INPUT CHOOSE CORRECT OPTION\n");
resetColor();
break;
}
} while (num != 6);
printf("\n\n=====================================================================\n\n");
printf("THANK YOU FOR USING THIS BUS RESERVATION SYSTEM");
printf("\n\nPRESS ANY KEY TO EXIT THE END PROGRAM !! \n");
printf("\n\n");
getch();
return 0;
}
}