-
Notifications
You must be signed in to change notification settings - Fork 1
/
FLIGHT.c
603 lines (603 loc) · 17.7 KB
/
FLIGHT.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
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//Program to implement flight information system
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int countuser=0;
int countflight=0;
int countplaces=0;
//creating a structure to store flight details
struct Flight
{
char flightID[10];
char source[20];
char arrivalTime[10];
char destination[20];
char departureTime[10];
float flightTime;
float fare;
}flights[100];
//creating a structure to store cities
struct city
{
char cities[20];
}city_list[100];
//creating a structure to store user details
struct userdetails
{
char username[100];
char email[100];
char password[100];
}user[50];
//creating a structure to store the details of cheapest flight
struct cheapestflight
{
float fare;
char source[20];
char stop[20];
char destination[20];
char flightID1[10];
char flightID2[10];
}cheapflight[10];
//Creating prototypes for function
void files();
void userdata();
void userlogin();
void createnewaccount();
void login();
void displayallflights();
void displaysource();
void displaysourcetodesination();
int choice();
//Main function
int main()
{
int flightn=20;
//fetching all files
files();
userdata();
//user login
userlogin();
//Asking user to select option
choice();
return 0;
}
int choice()
{
int choice;
do
{
printf("Select a option\n");
printf("1.To check all the flights available.\n");
printf("2.To check all the flights from one place.\n");
printf("3.To check all the flights from one place to another place.\n");
printf("4.Logout\n");
//To verify user is giving option in intergers
while((scanf("%d",&choice)!=1))
{
while(getchar()!='\n');
printf("Error in option ,Please type respective option.\n");
}
switch(choice)
{
case 1:displayallflights();
break;
case 2:displaysource();
break;
case 3:displaysourcetodesination();
break;
case 4:printf("Succefully logged out... :)\n");
return 0;
default:printf("Invalid choice\n");
break;
}
} while (choice!=4);
return 0;
}
void files()
{
FILE *fp,*fp2;
int i=0;
//opening FLIGHT DETAILS csv file
fp = fopen("FLIGHTDETAILS.csv", "r");
if (fp == NULL)
{
printf("Error opening file FLIGHT DETAILS");
return ;
}
//opening LOCATION csv file
fp2 = fopen("PLACES.csv", "r");
if (fp2 == NULL)
{
printf("Error opening file PLACES");
return ;
}
//reading flight details from csv file
char line1[50];
while (fgets(line1, sizeof(line1), fp))
{
if(sscanf(line1, "%[^,],%[^,],%[^,],%[^,],%[^,],%f,%f", flights[i].flightID, flights[i].source, flights[i].arrivalTime, flights[i].destination,flights[i].departureTime,&flights[i].flightTime,&flights[i].fare) == 7)
{
i++;
countflight++;
}
}
i=0;
char line2[50];
//reading city details from csv file
while (fscanf(fp2, "%s", city_list[i].cities) == 1)
{
i++;
countplaces++;
}
//closing files
fclose(fp);
fclose(fp2);
}
void userdata()
{
FILE *fp3;
fp3 = fopen("Userdetails.csv", "r");
if (fp3 == NULL)
{
printf("Error opening user files");
return ;
}
int i=0;
//reading user details
char line3[50];
while (fgets(line3, sizeof(line3), fp3)!=NULL)
{
if(sscanf(line3, "%[^,],%[^,],%[^,]", user[i].username,user[i].email,user[i].password)==3)
{
i++;
countuser++;
}
}
//closing file
fclose(fp3);
}
//function for user to create a new account or login
void userlogin()
{
char option;
for(int j=0;j<60;j++)
{
printf("-");
}
printf("\n");
//asking user whether the user hav account or not
printf("%40s","Welcome to Flight Finder\n");
printf("%5s","Do you have a travel account with us? [Y/N] ");
do
{
scanf(" %s",&option);
if (option == 'y' || option == 'Y')
{
//if yes asking user to login
login();
return;
}
else if(option == 'n'|| option == 'N')
{
//if no asking user to login
createnewaccount();
return;
}
else
{
//asking user to enter the option if chose any option rather than y or n
printf("\nERROR: Please type [y/Y] or [n/N]: ");
}
}while(1);
for(int j=0;j<60;j++)
{
printf("-");
}
printf("\n");
}
//function for user to create a new account
void createnewaccount()
{
if (countuser>50)
{
printf("Maximum number of users reached.\n");
return;
}
char username[100], email[100], password[100];
for(int j=0;j<60;j++)
{
printf("-");
}
printf("\n\n%10s","Lets create your new account :)\n");
do
{
name1:
//asking user to enter their new username
printf("Enter Your Username: ");
scanf(" %[^\n]", username);
getchar();
// Asking user to keep username more than 3 characters
if (strlen(username) < 3|| strchr(username,' ')!=NULL)
{
printf("Username must be at least 3 characters long and no spaces are allowed\n");
continue;
}
// Checking for duplicate usernames
for (int i = 0; i < countuser; i++)
{
if (strcmp(username, user[i].username) == 0)
{
printf("Username already exists. Please choose a different username.\n");
goto name1;
}
}
// If no issues found break out of the loop
break;
} while (1);
do
{
email1:
printf("Enter your Email Address: ");
scanf(" %49s", email);
getchar();
// email validation
if (strchr(email, '@') == NULL)
{
printf("Invalid email address. Please enter a valid email address.\n");
continue;
}
for (int i = 0; i < countuser; i++)
{
if (strcmp(email, user[i].email) == 0)
{
printf("Email already exists. Please choose a different email.\n");
goto email1;
}
}
break;
} while (1);
do
{
printf("Enter your password: ");
scanf(" %49[^\n]", password);
getchar();
// Basic password validation (you can add more checks)
if (strlen(password) < 8)
{
printf("Password must be at least 8 characters long.\n");
continue;
}
//password validation
break;
} while (1);
strcpy(user[countuser].username, username);
strcpy(user[countuser].email, email);
strcpy(user[countuser].password, password);
countuser++;
printf("\nAccount created successfully!\n");
FILE *fp;
fp = fopen("Userdetails.csv", "a");
if (fp == NULL)
{
printf("Error opening user files");
return ;
}
fprintf(fp,"%s,%s,%s\n", username, email, password);
for(int j=0;j<60;j++)
{
printf("-");
}
// Close the file
fclose(fp);
printf("\n%10s%s\n","Welcome!! ",username);
printf("%10s","How may i help you\n");
}
//function for user to login
void login()
{
if (countuser == 0)
{
printf("No user data available. Please create a new account.\n");
createnewaccount();
return;
}
char name[100], password[100], option;
int error = 0;
name2:
printf("Enter your username or email address:\n");
scanf("%s", name);
if (error == 2) {
printf("Invalid login\n Maximum attempts reached.\n");
do
{
printf("Do You want to create account? [Y/N] ");
scanf(" %s", &option);
if (option == 'y' || option == 'Y') {
createnewaccount();
return;
} else if (option == 'n' || option == 'N') {
printf("Sorry could not verify.\n");
exit(0);
} else
{
printf("\nERROR: Please type [y/Y] or [n/N]: \n");
}
} while (1);
}
for (int i = 0; i < countuser; i++)
{
if (strcmp(name, user[i].username) == 0 || strcmp(name, user[i].email) == 0)
{
// Reset error count for each user
error = 0;
while (fgetc(stdin) != '\n' && !feof(stdin));
password1:
printf("Enter your password:\n");
if(fgets(password,sizeof(password),stdin)==NULL)
{
printf("Error reading password\n");
exit(0);
}
if (strcasecmp(password, user[i].password) == 0)
{
printf("Welcome Back!! %s\n", user[i].username);
return;
}
else
{
printf("Wrong password!! Please try again\n");
error++;
if (error == 3)
{
printf("Invalid login\nMaximun number of attempts reached\n");
do
{
printf("Do You want to create account? [Y/N] ");
scanf(" %s", &option);
if (option == 'y' || option == 'Y')
{
createnewaccount();
return;
}
else if (option == 'n' || option == 'N')
{
printf("Sorry could not verify.\n");
exit(0);
}
else
{
printf("\nERROR: Please type [y/Y] or [n/N]: ");
}
} while (1);
}
else
{
goto password1;
}
}
}
}
printf("Please Enter valid username\n");
error++;
goto name2;
}
//function to display all flights
void displayallflights()
{
//Displaying all flights
printf("%-10s %-10s %-10s %-10s %-10s %-10s %-10s\n", "Flight ID", "Source", "Arrival", "Destination", "Departure", "Time", "Fare");
for(int i=0;i<75;i++)
{
printf("-");
}
printf("\n");
for (int i = 0; i < countflight; i++)
{
printf("%-10s %-10s %-10s %-10s %-10s %-10.2f %-10.2f\n",
flights[i].flightID, flights[i].source, flights[i].arrivalTime,
flights[i].destination, flights[i].departureTime, flights[i].flightTime, flights[i].fare);
}
for(int i=0;i<75;i++)
{
printf("-");
}
printf("\n");
}
//function to display a flight from user given source
void displaysource()
{
int option;
//Asking user to select a source
jump_here1:
printf("Select the source\n");
for(int i=0;i<countplaces;i++)
{
printf("%d.%s\n",i+1,city_list[i].cities);
}
//To verify user is giving option in intergers
while((scanf("%d",&option)!=1))
{
while(getchar()!='\n');
{
printf("Error in option ,Please type respective option.\n");
}
}
if(option>countplaces)
{
printf("Please Enter different source\n");
goto jump_here1;
}
printf("%s\n",city_list[option-1].cities);
//Displaying all flights from selected source
printf("%-10s %-10s %-10s %-10s %-10s %-10s %-10s\n", "Flight ID", "Source", "Arrival", "Destination", "Departure", "Time", "Fare");
for(int j=0;j<75;j++)
{
printf("-");
}
printf("\n");
//Display flights
for(int i=0;i<countflight;i++)
{
if(strcmp(city_list[option-1].cities,flights[i].source)==0)
{
printf("%-10s %-10s %-10s %-10s %-10s %-10.2f %-10.2f \n",
flights[i].flightID, flights[i].source, flights[i].arrivalTime,
flights[i].destination, flights[i].departureTime, flights[i].flightTime, flights[i].fare);
}
}
for(int j=0;j<75;j++)
{
printf("-");
}
printf("\n");
}
//function to display flights from user given source and destination
void displaysourcetodesination()
{
int option,option1,verify=0,cheap=0,temp=0;
float totaltime,totalfare;
//Asking user to select the source
jump_here2:
printf("Select the source\n");
for(int i=0;i<countplaces;i++)
{
printf("%d.%s\n",i+1,city_list[i].cities);
}
//To verify user is giving option in intergers
while((scanf("%d",&option)!=1)||option>countplaces)
{
while(getchar()!='\n');
{
printf("Error in option ,Please type respective option.\n");
}
}
printf("%s\n",city_list[option-1].cities);
//Asking user to select destination
printf("Select the destination\n");
for(int i=0;i<countplaces;i++)
{
if(strcmp(city_list[option-1].cities,city_list[i].cities)==0)
{
continue;
}
printf("%d.%s\n",i+1,city_list[i].cities);
}
//To verify user is giving option in intergers
while((scanf("%d",&option1)!=1)||option1>countflight)
{
while(getchar()!='\n');
{
printf("Error in option ,Please type respective option.\n");
}
}
printf("%s\n",city_list[option1-1].cities);
//Asking user to select different destination
if(city_list[option-1].cities==city_list[option1-1].cities||option1>=8||option>=8)
{
printf("Please Enter correct source and destination\n");
goto jump_here2;
}
//printing direct flights from source to destintation
printf("%40s\n","DIRECT FLIGHTS");
printf("%-10s %-10s %-10s %-10s %-10s %-10s %-10s\n", "Flight ID", "Source", "Arrival", "Destination", "Departure", "Time", "Fare");
for(int j=0;j<75;j++)
{
printf("-");
}
printf("\n");
//Display flights
for(int i=0;i<countflight;i++)
{
if(strcmp(city_list[option-1].cities,flights[i].source)==0 && strcmp(city_list[option1-1].cities,flights[i].destination)==0)
{
verify=1;
printf("%-10s %-10s %-10s %-10s %-10s %-10.2f %-10.2f \n",
flights[i].flightID, flights[i].source, flights[i].arrivalTime,
flights[i].destination, flights[i].departureTime, flights[i].flightTime, flights[i].fare);
}
}
//Displaying no direct files if not available
if(verify==0)
{
printf("\t\t\tNO DIRECT FLIGHTS ):\n");
}
for(int j=0;j<75;j++)
{
printf("-");
}
printf("\n\n");
verify=0;
//Displaying Indirect flights
printf("%40s\n","INDIRECT FLIGHTS");
printf("%-10s %-10s %-10s %-10s %-10s %-10s %-10s\n", "Flight ID", "Source", "Arrival", "Destination", "Departure", "Time", "Fare");
for(int j=0;j<75;j++)
{
printf("-");
}
printf("\n");
//Display flights
for(int i=0;i<countflight;i++)
for(int j=0;j<countflight;j++)
{
if(strcmp(city_list[option-1].cities,flights[i].source)==0 && strcmp(flights[j].source,flights[i].destination)==0 && strcmp(city_list[option1-1].cities,flights[j].destination)==0)
{
verify=1;
printf("%s","**FLIGHT 1**\n\n");
printf("%-10s %-10s %-10s %-10s %-10s %-10.2f %-10.2f \n",
flights[i].flightID, flights[i].source, flights[i].arrivalTime,
flights[i].destination, flights[i].departureTime, flights[i].flightTime, flights[i].fare);
printf("%s","\n**FLIGHT 2**\n\n");
printf("%-10s %-10s %-10s %-10s %-10s %-10.2f %-10.2f \n",
flights[j].flightID, flights[j].source, flights[j].arrivalTime,
flights[j].destination, flights[j].departureTime, flights[j].flightTime, flights[j].fare);
totalfare=flights[i].fare+flights[j].fare;
totaltime=flights[i].flightTime+flights[j].flightTime;
printf("\n");
printf("TOTAL TIME = %0.2f hours\n",totaltime);
printf("TOTAL FARE = Rs. %0.2f\n",totalfare);
//storing the details of indirect flights
cheapflight[cheap].fare=totalfare;
strcpy(cheapflight[cheap].source,flights[i].source);
strcpy(cheapflight[cheap].stop,flights[i].destination);
strcpy(cheapflight[cheap].destination,flights[j].destination);
strcpy(cheapflight[cheap].flightID1,flights[i].flightID);
strcpy(cheapflight[cheap].flightID2,flights[j].flightID);
cheap++;
for(int j=0;j<75;j++)
{
printf("-");
}
printf("\n");
}
}
//Displaying no Indirect files if not available
if(verify==0)
{
printf("\t\t\tNO INDIRECT FLIGHTS ):\n");
}
//if indirect flights are available displaying details of cheapest flight
else
{
for(int i=0;i<cheap-1;i++)
{
if(cheapflight[temp].fare>cheapflight[temp+1].fare)
{
temp=temp+1;
}
}
printf("\n%40s\n","CHEAPEST INDIRECT FLIGHT");
for(int j=0;j<75;j++)
{
printf("-");
}
printf("\n");
printf("\nFLIGHTID'S: %s, %s\n",cheapflight[temp].flightID1,cheapflight[temp].flightID2);
printf("\nCHECKPOINTS: %s->%4s->%4s\n",cheapflight[temp].source,cheapflight[temp].stop,cheapflight[temp].destination);
printf("\nFARE = Rs. %0.2f\n",cheapflight[temp].fare);
}
for(int j=0;j<75;j++)
{
printf("-");
}
printf("\n");
}