Page

Bus Ticket Booking System In C/C++

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<dos.h>
#include<string.h>
void seat_av();
void graph_init();
void add_new_she();
void main_menu();
void ticket_book();
void user_bus_list();
void admin_menu();
void user_menu();
void del_record();
void add_bus();
void list_of_bus();
void update_records();
void rev_list();
void search();
void admin_login();
void user_login();
typedef struct
{
char bus_name[40];
char org[30];
char dest[30];
char a_time[10];
char d_time[10];
int bus_no;
int seat;
int fare;
} bus;
typedef struct
{
char bus_name[30];
char p_name[30];
char j_date[15];
int seat_no;
int bus_no;
int age;
}book;
typedef struct
{
int bus_no;
char dt[15];
int t_seat;
}seat;
FILE *fb,*fr,*ft,*fs;
char another;
void main()
{
int i;
clrscr();
textcolor(4);
gotoxy(25,3);
cprintf("sourcecodernp.blogspot.com");
gotoxy(35,5);
cprintf("Project On");
gotoxy(27,7);
cprintf("Bus Ticket Booking System");
gotoxy(40,9);

cprintf("Please Wait File is Loding................");
textcolor(12);
gotoxy(10,24);
for(i=0;i<65;i++)
{
delay(100);
cprintf("%c",219);
}
main_menu();
getch();
}
void main_menu()
{
int ch;
clrscr();
graph_init();
setbkcolor(8);
rectangle(140,100,500,300);
setcolor(4);
gotoxy(35,6);
printf("Main Menu");
gotoxy(30,8);
printf("Adminstrator Mode [1]");
gotoxy(30,10);
printf("User Mode         [2]");
gotoxy(30,12);
printf("Exit              [3]");
gotoxy(30,14);
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
admin_login();
break;
case 2:
user_login();
break;
case 3:
exit(1);
break;
default:
main_menu();
break;
}
}
void admin_menu()
{
int ch;
clrscr();
graph_init();
setbkcolor(8);
rectangle(40,70,600,400);
setcolor(BLUE);
setfillstyle(SOLID_FILL,RED);
bar(40,70,600,120);
settextstyle(1,0,2);
outtextxy(170,85,"Welcome to Administrator Menu");
gotoxy(13,12);
printf("Add New Bus         [1]");
gotoxy(45,12);
printf("Update Records      [2]");
gotoxy(13,14);
printf("Delete Record       [3]");
gotoxy(45,14);
printf("List of Records     [4]");
gotoxy(13,16);
printf("Reservation List    [5]");
gotoxy(45,16);
printf("Add New Schedual    [6]");
gotoxy(13,18);
printf("Back Previous Menu  [7]");
gotoxy(13,20);
printf("Enter Your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
add_bus();
break;
case 2:
update_records();
break;
case 3:
del_record();
break;
case 4:
list_of_bus();
break;
case 5:
rev_list();
break;
case 6:
add_new_she();
case 7:
main_menu();
default:
printf("Wrong Input");
}
}
void add_bus()
{
bus b;
clrscr();
graph_init();
another='Y';
setbkcolor(8);
fb=fopen("bus.dat","ab");
while(another=='Y'||another=='y')
{
fflush(stdin);
printf("\nEnter bus No:");
scanf("%d",&b.bus_no);
fflush(stdin);
printf("\nEnter Bus Name:");
gets(b.bus_name);
fflush(stdin);
printf("\nEnter origin:");
gets(b.org);
fflush(stdin);
printf("\nEnter distination:");
gets(b.dest);
fflush(stdin);
printf("\nEnter Departure Time:");
gets(b.d_time);
fflush(stdin);
printf("\nEnter Arrival Time:");
gets(b.a_time);
fflush(stdin);
printf("\nEnter Total Seat : ");
scanf("%d",&b.seat);
fflush(stdin);
printf("\nEnter Fare : ");
scanf("%d",&b.fare);
fwrite(&b,sizeof(b),1,fb);
printf("\nRecord is added");
printf("\nAdd another record(Y/N):");
fflush(stdin);
another=getche();
}
fclose(fb);
admin_menu();
closegraph();
restorecrtmode();
}
void list_of_bus()
{
int i=1,p=3;
bus b;
clrscr();
graph_init();
setbkcolor(8);
gotoxy(1,1);
printf("Bus No");
gotoxy(10,1);
printf("Bus Name");
gotoxy(25,1);
printf("Origin");
gotoxy(35,1);
printf("Destination");
gotoxy(50,1);
printf("Departure Time");
gotoxy(67,1);
printf("Arrival Time");
fb=fopen("bus.dat","rb");
if(fb==NULL)
{
printf("\n\nError-File Cannot Open");
}
while(fread(&b,sizeof(b),1,fb)==1)
{
gotoxy(1,p);
printf("%d",b.bus_no);
gotoxy(10,p);
printf("%s",b.bus_name);
gotoxy(25,p);
printf("%s",b.org);
gotoxy(35,p);
printf("%s",b.dest);
gotoxy(50,p);
printf("%s",b.d_time);
gotoxy(67,p);
printf("%s",b.a_time);
if(i%10==0)
{
printf("\nPress any key to continue............!");
fflush(stdin);
getch();
clrscr();
p=3;
}
i++;
p+=2;
}
printf("\n\nPress any key to back to Menu.........!");
getch();
admin_menu();
}
void update_records()
{
long int resize;
int b_no;
bus b;
clrscr();
graph_init();
resize=sizeof(b);
another='Y';
fb=fopen("bus.dat","rb+");
while(another=='Y' || another=='y')
{
printf("Enter Bus No :");
scanf("%d",&b_no);
rewind(fb);
while(fread(&b,resize,1,fb)==1)
{
if(b.bus_no==b_no)
{
fflush(stdin);
printf("Enter Bus name:");
gets(b.bus_name);
fflush(stdin);
printf("\nEnter origin:");
gets(b.org);
fflush(stdin);
printf("\nEnter distination:");
gets(b.dest);
fflush(stdin);
printf("\nEnter total seat:");
scanf("%d",&b.seat);
fflush(stdin);
printf("\nEnter Fare:");
scanf("%d",&b.fare);
fseek(fb,-resize,SEEK_CUR);
fwrite(&b,sizeof(b),1,fb);
}
}
printf("\n\nData is Updated");
printf("\n\nModify another Data(Y/N);");
fflush(stdin);
another=getche();
}
fclose(fb);
getch();
admin_menu();
closegraph();
restorecrtmode();
}
void search()
{
int b_no;
bus b;
clrscr();
graph_init();
fb=fopen("bus.dat","rb");
another='Y';
printf("\nEnter bus no:");
scanf("%d",&b_no);
while(fread(&b,sizeof(b),1,fb)==1)
{
if(b.bus_no==b_no)
{
printf("\n\nBus is found");
printf("\n\n\tBus No         :  %d",b.bus_no);
printf("\n\n\tBus Name       :  %s",b.bus_name);
printf("\n\n\tDeparture      :  %s",b.org);
printf("\n\n\tDestination    :  %s",b.dest);
printf("\n\n\tDeparture Time :  %s",b.d_time);
printf("\n\n\tArrival Time   :  %s",b.a_time);
printf("\n\n\tSeat           :  %d",b.seat);
printf("\n\n\tFare           :  %d",b.fare);
printf("\n\nAnother search(Y/N) :");
fflush(stdin);
another=getchar();
if(another=='y'||another=='Y')
search();
else
{
fclose(fb);
user_menu();
}
}
}
printf("\n\nBus is Not Fount");
printf("\n\nDo you want to Search another Bus(Y/N) :");
fflush(stdin);
another=getchar();
if(another=='Y'||another=='y')
search();
else
{
fclose(fb);
user_menu();
}
closegraph();
restorecrtmode();
}
void del_record()
{
int c=1,z;
bus b;
int bno;
clrscr();
graph_init();
another='Y';
while(another=='Y' || another=='y')
{
printf("\nEnter Bus No for Deletion :");
scanf("%d",&bno);
ft=fopen("temp.dat","ab");
fb=fopen("bus.dat","rb");
while(fread(&b,sizeof(b),1,fb)==1)
{
if(b.bus_no==bno)
{
c++;
}
else
{
fwrite(&b,sizeof(b),1,ft);
}
}
fclose(fb);
fclose(ft);
remove("bus.dat");
rename("temp.dat","bus.dat");
fb=fopen("bus.dat","rb+");
setcolor(WHITE);
outtextxy(40,370,"THE RECORD IS DELETING");
rectangle(40,380,580,405);
for(z=40;z<=580;z++)
{
setfillstyle(SOLID_FILL,BLACK);
floodfill(0,0,BLACK);
rectangle(40,380,z,405);
delay(10);
setfillstyle(SOLID_FILL,BLUE);
floodfill(42,392,WHITE);
}
clrscr();
cleardevice();
printf("\n\nDelete another Records(Y/N) : ");
fflush(stdin);
another=getche();
}
fclose(fb);
getch();
admin_menu();
closegraph();
restorecrtmode();
}

void user_menu()
{
int ch;
clrscr();
graph_init();
setbkcolor(8);
rectangle(40,70,600,400);
setcolor(BLUE);
setfillstyle(SOLID_FILL,RED);
bar(40,70,600,120);
settextstyle(1,0,2);
outtextxy(170,85,"Welcome to User Menu");
gotoxy(13,12);
printf("Search Bus         [1]");
gotoxy(45,12);
printf("Booking Ticket     [2]");
gotoxy(13,14);
printf("Show List of Bus   [3]");
gotoxy(45,14);
printf("Seat Availability  [4]");
gotoxy(13,16);
printf("Back Previous Menu [5]");
gotoxy(13,18);
printf("Enter your choice : ");
scanf("%d",&ch);
while(1)
{
switch(ch)
{
case 1:
search();
break;
case 2:
ticket_book();
break;
case 3:
user_bus_list();
break;
case 4:
seat_av();
break;
case 5:
main_menu();
break;
default:
user_menu();
break;
}
}
}
void ticket_book()
{
int resize;
book b1;
seat s;
clrscr();
graph_init();
another='Y';
resize=sizeof(s);
rewind(fr);
fs=fopen("seat.dat","rb+");
fr=fopen("booking.dat","wb");
fseek(fr,0,SEEK_END);
while(another=='Y'|| another=='y')
{
fflush(stdin);
printf("\nEnter Bus No:");
scanf("%d",&b1.bus_no);
fflush(stdin);
printf("\nBus Name:");
gets(b1.bus_name);
fflush(stdin);
printf("\nEnter Passenger Name:");
gets(b1.p_name);
fflush(stdin);
printf("\nEnter age:");
scanf("%d",&b1.age);
fflush(stdin);
printf("\nEnter Journey Date(D/M/Y) :");
gets(b1.j_date);
fflush(stdin);
printf("\nEnter seat No:");
scanf("%d",&b1.seat_no);
fseek(fs,0,SEEK_SET);
while(fread(&s,sizeof(s),1,fs)==1)
{
if((s.bus_no==b1.bus_no) && (strcmp(s.dt,b1.j_date)==0))
{
s.t_seat=s.t_seat-1;
fseek(fs,-resize,SEEK_CUR);
fwrite(&s,sizeof(s),1,fs);
}
fclose(fs);
}
fwrite(&b1,sizeof(b1),1,fr);
printf("\nBook Another ticket(Y/N) : ");
fflush(stdin);
another=getche();
}
fclose(fr);
user_menu();
}
void rev_list()
{
book b2;
int i=1,p=3;
clrscr();
graph_init();
gotoxy(1,1);
printf("Bus No");
gotoxy(13,1);
printf("Bus Name");
gotoxy(27,1);
printf("Passenger Name");
gotoxy(50,1);
printf("Age");
gotoxy(57,1);
printf("Seat No");
gotoxy(68,1);
printf("Journey Date");
fr=fopen("booking.dat","rb");
while(fread(&b2,sizeof(b2),1,fr)==1)
{
gotoxy(1,p);
printf("%d",b2.bus_no);
gotoxy(13,p);
printf("%s",b2.bus_name);
gotoxy(27,p);
printf("%s",b2.p_name);
gotoxy(50,p);
printf("%d",b2.age);
gotoxy(57,p);
printf("%d",b2.seat_no);
gotoxy(68,p);
printf("%s",b2.j_date);
if(i%10==0)
{
printf("\nPress any key to continue............!");
fflush(stdin);
getch();
clrscr();
p=3;
}
i++;
p+=2;
}
printf("\n\nPress any key to back to Menu.........!");
getch();
admin_menu();
}
void admin_login()
{
int i,x=38,j;
char pwd[20];
char *pass="chandan";
clrscr();
graph_init();
setfillstyle(SOLID_FILL,4);
bar(0,0,640,480);
setfillstyle(SOLID_FILL,BLACK);
bar(10,10,625,430);
line (130,180,500,180);
rectangle ( 130,150,500,300);
setfillstyle ( SOLID_FILL, 7 );
bar (130,150,500,180);
setfillstyle ( SOLID_FILL, 8 );
bar (130,180,500,300 );
setcolor(4);
outtextxy(140,165, " P A S S W O R D   V E R I F I C A T I O N  " );
setfillstyle ( SOLID_FILL, 0 );
bar(290,222,450,245);
outtextxy ( 160, 229, "ENTER PASSWORD " );
for(i=0;i<20;i++)
{
pwd[i]=getch();
if(pwd[i]=='\r')
{
pwd[i]='\0';
break;
}
else
{
gotoxy(x,15);
printf("*");
x++;
}
}
if(strcmp(pass,pwd)==0)
{
clrscr();
cleardevice();
setbkcolor(12);
setcolor(BLUE);
settextstyle(1,0,4);
outtextxy(100,100,"Pls Wait File is in Process........");
gotoxy(15,14);
for(j=0;j<50;j++)
{
delay(200);
printf("%c",219);
}
admin_menu();
}
else
{
clrscr();
cleardevice();
setcolor(2);
settextstyle(1,0,4);
setbkcolor(12);
outtextxy(145,190,"Invalid Password");
sound(700);
delay(1000);
nosound();
admin_login();
}
closegraph();
restorecrtmode();
}
void graph_init()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"..\\bgi");
}
void user_login()
{
int x=37,i,j;
char user[20];
char *username="chandan";
char pwd[20];
char *pass="123456";
graph_init();
setfillstyle(SOLID_FILL,4);
bar(0,0,640,480);
setfillstyle(SOLID_FILL,BLACK);
bar(10,10,625,430);
line(130,180,500,180);
rectangle(130,150,500,300);
setfillstyle(SOLID_FILL, 7 );
bar(130,150,500,180);
setfillstyle ( SOLID_FILL, 8 );
bar(130,180,500,300);
setcolor(4);
outtextxy(140,165," P A S S W O R D   V E R I F I C A T I O N  " );
setfillstyle ( SOLID_FILL, 0 );
bar(280, 200, 450, 230 );
bar(280, 247, 450, 277 );
outtextxy(135,210,"Enter User Name :" );
outtextxy(135,258,"Enter Password  :");
gotoxy(37,14);
fflush(stdin);
gets(user);
for(i=0;i<20;i++)
{
pwd[i]=getch();
if(pwd[i]=='\r')
{
pwd[i]='\0';
break;
}
else
{
gotoxy(x,17);
printf("*");
x++;
}
}
if((strcmp(username,user)==0) && (strcmp(pass,pwd)==0))
{
clrscr();
cleardevice();
setbkcolor(12);
setcolor(BLUE);
settextstyle(1,0,4);
outtextxy(100,100,"Pls Wait File is in Process........");
gotoxy(14,14);
for(j=0;j<50;j++)
{
delay(200);
printf("%c",219);
}
user_menu();
}
else
{
clrscr();
cleardevice();
setcolor(14);
settextstyle(1,0,3);
setbkcolor(12);
outtextxy(145,190,"Invalid User or Password");
sound(700);
delay(1000);
nosound();
user_login();
}
closegraph();
restorecrtmode();
}
void user_bus_list()
{
int i=1;
bus b;
clrscr();
graph_init();
setbkcolor(12);
fb=fopen("bus.dat","rb");
if(fb==NULL)
{
printf("\n\nError-File Cannot Open");
}
while(fread(&b,sizeof(b),1,fb)==1)
{
setcolor(BLUE);
settextstyle(4,0,3);
outtextxy(200,10,"All Bus Information");
printf("\n\n\n\n\t************************************************************");
printf("\n\n\t\t\tBus No         :   %d",b.bus_no);
printf("\n\n\t\t\tBus Name       :   %s",b.bus_name);
printf("\n\n\t\t\tOrigin         :   %s",b.org);
printf("\n\n\t\t\tDestination    :   %s",b.dest);
printf("\n\n\t\t\tDeparture Time :   %s",b.d_time);
printf("\n\n\t\t\tArrival Time   :   %s",b.a_time);
printf("\n\n\t\t\tTotal Seat     :   %d",b.seat);
printf("\n\n\t\t\tTotal Fare     :   %d",b.fare);
if(i%1==0)
{
printf("\n\n\t\tPress any key to continue............!");
fflush(stdin);
getch();
clrscr();
}
i++;
}
printf("\n\nPress any key to back to Menu.........!");
getch();
user_menu();
closegraph();
restorecrtmode();
}
void add_new_she()
{
seat s;
clrscr();
graph_init();
cleardevice();
another='y';
fs=fopen("seat.dat","wb");
while(another=='y'||another=='Y')
{
printf("Enter Bus No  :");
scanf("%d",&s.bus_no);
fflush(stdin);
printf("Date of Departure  :");
gets(s.dt);
fflush(stdin);
printf("Enter Seat :");
scanf("%d",&s.t_seat);
fwrite(&s,sizeof(s),1,fs);
printf("Record is added");
printf("\nAdd another record:");
fflush(stdin);
another=getche();
}
fclose(fs);
getch();
admin_menu();
}
void seat_av()
{
int b_no;
char dat[15];
seat s;
clrscr();
graph_init();
cleardevice();
another='y';
fs=fopen("seat.dat","rb");
printf("\nEnter Bus No :");
scanf("%d",&b_no);
fflush(stdin);
printf("Enter Date   :");
gets(dat);
while(another=='y'||another=='Y')
{
while(fread(&s,sizeof(s),1,fs)==1)
{
if((s.bus_no==b_no) && (strcmp(s.dt,dat)==0))
{
printf("\n\nBus No :%d",s.bus_no);
printf("\n\nDate   :%s",s.dt);
printf("\n\nAvailable Seat :%d",s.t_seat);
printf("\n\nSearch Another Record:");
fflush(stdin);
another=getchar();
if(another=='y'||another=='Y')
seat_av();
else
{
fclose(fs);
user_menu();
}
}
}
printf("\n\nBus is Not Fount");
printf("\n\nDo you want to Search another Bus(Y/N) :");
fflush(stdin);
another=getchar();
if(another=='Y'||another=='y')
seat_av();
else
{
fclose(fs);
user_menu();
}
closegraph();
restorecrtmode();
}
}


No comments:

Post a Comment