Bus1
Bus1
PROJECT REPORT
Language – C++
Topic – BUS TICKET RESERVATION
Introduction
This project for reservation of bus ticket for various buses. Each bus has
50 seats. The project is inspired by travel booking sites such as
Makemytrip & RedBus. In this project, we have use the following
algorithms -
a) Searching
b) Deletion
lOMoARcPSD|55521570
c) File handling
handling
FILES
DATA FILES
∙ Name,
lOMoARcPSD|55521570
∙ Phone no.,
PROGRAM FILE
CODE
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<fstream.h>
#include<dos.h>
/
***********************
CLASS FOR BUS
***********************
/ class bus
{ int id;
char from[50]; char
dest[50]; char
timeA[10],timeD[10]; int
maxseats; int booked; int
fare; int ticbook; int gst;
lOMoARcPSD|55521570
gst=0;
}
void input(); void
show(); void
display(); void
selectedbus();
int getid()
{
return id;
}
void ticketcount(int n)
{
ticbook=n;
}
void book(int n)
{
booked=booked+n; }
char* getfrom()
lOMoARcPSD|55521570
{
return from;
}
char* getdest()
{
return dest;
}
char* gettimeA()
{
return timeA;
10
}
char* gettimeD()
{
return timeD;
}
int getbooked()
{
return booked;
}
int getfare()
{
return fare;
}
int getgst()
{
return gst;
}
lOMoARcPSD|55521570
};
/**************************
CLASS FOR TICKET
**************************/ class ticket
{ char name[25];
char phone[15];
bus b;
public:
// FUNC. TO GENERATE TICKET
void generate(char cname[],bus tb,char cphone[])
{
11
strcpy(name,cname);
strcpy(phone,cphone);
b=tb;
}
// FUNC. TO DISPLAY TICKET
void display()
{
clrscr(); cout<<"\
n
***************************************************
* **********\n"; cout<<"\t\t\
t BUS TRAVEL AGENCY\n"; cout<<"
***************************************************
* **********\n"; gotoxy(8,6);
cout<<"CUSTOMER: "<<name ;
gotoxy(47,14);
lOMoARcPSD|55521570
cout<<"Contact No.:"<<phone;
b.show();
}
char* getname()
{ return name;
}
char* getphone()
{ return phone;
}
};
12
void bus::display()
{
cout<<"\t"<<id<<"\t"<<from<<"\t"<<dest<<"\t"<<timeD<<"\
t"< <timeA<<"\t"<<maxseats<<"\t"<<booked<<"\t"<<fare<<"\
n"; }
// FUNC. TO SHOW DATA void
bus::show()
13
{
gotoxy(57,6);
cout<<"BUS ID: "<<id;
gotoxy(8,7);
cout<<"--------------------------------------------------------------";
gotoxy(8,8); cout<<"Journey: "<<from<<" - "<<dest;
gotoxy(8,10);
cout<<"Departure: "<<timeD;
gotoxy(47,10);
cout<<"Arrival: "<<timeA;
gotoxy(8,12); cout<<"Fare :
"<<fare; gotoxy(47,12);
cout<<"GST: "<<gst<<"%";
gotoxy(8,14);
cout<<"Tickets Booked: "<<ticbook;
gotoxy(47,18);
cout<<"TOTAL AMOUNT:
lOMoARcPSD|55521570
"<<((2*gst*fare*ticbook)/100)+
(fare*ticbook); gotoxy(46,17);
cout<<"--------------------";
gotoxy(45,18);
cout<<"|";
gotoxy(66,18);
cout<<"|";
gotoxy(46,19);
cout<<"--------------------";
gotoxy(8,20);
14
cout<<"--------------------------------------------------------------";
gotoxy(22,21); cout<<" THANK YOU, HAVE A HAPPY
JOURNEY "; gotoxy(8,22);
cout<<"********************************************
* *****************";
}
// FUNC. TO SHOW DATA OF THE SELECTED
BUS void bus::selectedbus()
{
cout<<"BUS ID.-"<<id<<endl
<<"From- "<<from<<" To- "<<dest<<endl
<<"Departure- "<<timeD<<"\tArrival-"<<timeA<<endl
<<"Fare:"<<fare<<endl
<<"GST:"<<gst<<"%"<<endl;
}
lOMoARcPSD|55521570
/**********************************
FUNC. FOR ADMIN
**********************************
/ void admin() { bus b; ticket t; int ch;
fstream f1,f2,f3,f4; do
{ clrscr();
cout<<"\n\t\t\t***********************\n";
15
cout<<"\t\t\t\tWELCOME"; cout<<"\n\t\t\
t***********************\n"; cout<<"\t\t\t 1.Add a new
bus"<<endl
<<"\t\t\t 2.Show selected bus"<<endl
<<"\t\t\t 3.Display all bus"<<endl
<<"\t\t\t 4.Delete a bus"<<endl
<<"\t\t\t 5.Modify a bus"<<endl
<<"\t\t\t 6.Exit"<<endl
<<"\t\t\t\n Enter your choice:";
cin>>ch;
switch(ch)
{
case 1 :
//ADD NEW BUS
f1.open("tourism.
dat",ios::app|
ios::binary);
b.input();
f1.write((char*)&b,sizeof(b));
lOMoARcPSD|55521570
}
}
if(ck==0)
cout<<"Bus not found"<<endl;
}
f3.close(); break;
lOMoARcPSD|55521570
case 3 :
//DISPLAY ALL BUSES
f1.open("tourism.dat",ios::in|ios::binary);
if(f1.fail())
cout<<"Can't open file"<<endl;
else
{ clrscr(); cout<<"\
n
***************************************************
* **********\n";
cout<<"\t\t\t BUS TRAVEL AGENCY\n\n";
17
cout<<"\tBUS
ID.\tFROM\tDEST\tDEP\tARRV\tSEATS\tBOOKED\tFARE\
n"; cout<<"
***************************************************
* **********\n"; while(f1.read((char*)&b,sizeof(b)))
b.display();
}
f1.close();
cout<<"\nPress a key to continue";
getch();
clrscr(); break;
case 4 :
//DELETE A BUS ck=0;
cout<<"Enter the bus ID:";
cin>>bid;
lOMoARcPSD|55521570
f1.open("tourism.dat",ios::in|ios::binary);
f2.open("temp.dat",ios::out|ios::binary);
f3.open("ticket.dat",ios::in|ios::binary);
f4.open("temp2.dat",ios::out|ios::binary);
if(f1.fail())
cout<<"Can't open file"<<endl;
else
{
while(f1.read((char*)&b,sizeof(b)))
{
if(b.getid()!=bid)
18
{
f2.write((char*)&b,sizeof(b));
}
else
{
b.selectedbus();
ck=1;
}
}
if(ck==0)
cout<<"Bus not found"<<endl; else
cout<<"Bus deleted"<<endl; }
f1.close(); f2.close();
remove("tourism.dat");
rename("temp.dat","tourism.dat") ;
lOMoARcPSD|55521570
while(f1.read((char*)&t,sizeof(t)))
if(b.getid()!=bid)
{
f2.write((char*)&b,sizeof(b)); }
f3.close();
f4.close();
remove("ticket.dat");
rename("temp2.dat","ticket.dat");
cout<<"\nPress a key to continue";
getch();
19
clrscr();
break; case 5 :
//MODIFY A BUS cout<<"Enter bus
id to be updated:";
cin>>bid;
f1.open("tourism.dat",ios::binary|ios::in|
ios::out); if(f1.fail())
cout<<"Can't open file"<<endl;
else
{
while(f1.read((char*)&b,sizeof(b)))
{
if(b.getid()==bid)
{
f1.seekp(f1.tellg()-sizeof(b));
b.input();
lOMoARcPSD|55521570
f1.write((char*)&b,sizeof(b));
}
else
{ b.selectedbus();
ck=1;
}
}
if(ck==0) cout<<"\nBus
not found"; else
cout<<"\nBus updated\n";
20
}
f1.close();
break;
}
}while(ch!=6);
}
/****************************
FUNC. FOR CUSTOMER
****************************/
void customer()
{ int ch;
fstream f1,f2;
bus b; ticket
t;
do
lOMoARcPSD|55521570
{ clrscr()
;
cout<<"\n\t\t\t***********************\n"; cout<<"\t\t\
t\tWELCOME";
cout<<"\n\t\t\t***********************\n";
cout<<"\t\t\t 1.Book a ticket"<<endl <<"\t\t\t
2.Show ticket"<<endl
<<"\t\t\t 3.Delete ticket"<<endl
<<"\t\t\t 4.Exit"<<endl
<<"\t\t\t\n Enter your
choice:"; cin>>ch; switch(ch)
21
{
case 1 :
//BOOK A TICKET char
destination[50],cname[25],cphone[15]; int
ck=0,bid,n; f2.open("tourism.dat",ios::in|
ios::binary);
if(f2.fail())
cout<<"Can't open file"<<endl;
else
{ clrscr();
cout<<"\t\t\t BUS TRAVEL AGENCY";
cout<<"\n
***************************************************
* **********\n";
cout<<"\tBUS
lOMoARcPSD|55521570
ID.\tFROM\tDEST\tDEP\tARRV\tSEATS\tBOOKED\tFARE\
n"; cout<<"
***************************************************
* **********\n"; while(f2.read((char*)&b,sizeof(b)))
b.display();
}
f2.close(); getch();
cout<<"\n\nEnter BUS ID:";
cin>>bid; f1.open("tourism.dat",ios::in|ios::out|
ios::binary); if(f1.fail())
cout<<"Can't open file!"<<endl;
22
else
{
while(f1.read((char*)&b,sizeof(b))) {
if(b.getid()==bid)
{
b.selectedbus();
ck=1;
if(b.getbooked()>=50)
{
cout<<"\nALL TICKETS ARE
BOOKED"; }
else
{ cout<<"\nEnter the customer name:";
gets(cname);
cout<<"\nEnter the no. of tickets:";
cin>>n;
lOMoARcPSD|55521570
b.ticketcount(n);
cout<<"\nEnter the phone no.:";
gets(cphone); b.book(n);
t.generate(cname,b,cphone);
}
f2.open("ticket.dat",ios::app|ios::binary);
f2.write((char*)&t,sizeof(t));
f2.close(); f1.seekp(f1.tellg()-
sizeof(b),ios::beg);
f1.write((char*)&b,sizeof(b));
cout<<"\nTicket booked"<<endl;
23
do
{
clrscr();
//MENU FOR METHOD OF SEARCHING cout<<"\n\
t\t\t1.Search by name"
<<"\n\t\t\t2.Search by phone no"
<<"\n\t\t\t3.Exit"
<<"\nEnter choice:";
cin>>ch1;
switch(ch1)
{
case 1 :
//SEARCH BY NAME
cout<<"Enter name:";
gets(cname);
f2.open("ticket.dat",ios::in|ios::binary);
if(f2.fail())
cout<<"Can't open file!"<<endl;
else
{
while(f2.read((char*)&t,sizeof(t)))
if(strcmp(t.getname(),cname)==0)
lOMoARcPSD|55521570
{
cout<<"\nBooked Ticket\n\n";
t.display();
ck=1;
}
else if(ck==0)
{
25
cout<<"\nBooked Ticket\n\n";
t.display();
}
else if(ck==0)
{
cout<<"No record of ticket
found"; }
}
26
}
}while(ch1!=3); //END OF SEARCH MENU break;
case 3 : //DELETE
TICKET int ch2; do
{ clrscr();
cout<<"\t\t\t 1.Delete by name"
<<"\n\t\t\t 2.Delete by phone no."
<<"\n\t\t\t 3.Exit"
<<"\nEnter choice:";
cin>>ch2;
switch(ch2)
{
case 1 :
lOMoARcPSD|55521570
//DELETE BY NAME
ck=0;
cout<<"Enter name:";
gets(cname);
f1.open("ticket.dat",ios::in|ios::binary);
f2.open("temp.dat",ios::out|ios::binary);
if(f1.fail())
cout<<"Can't open file"<<endl;
27
else
{
while(f1.read((char*)&t,sizeof(t)))
if(strcmp(t.getname(),cname)!
=0)
{ f2.write((char*)&t,sizeof(t));
}
else
{ clrscr()
;
t.display();
ck=1;
}
}
if(ck==0) cout<<"Ticket not
found"<<endl; else cout<<"\
nTicket deleted"<<endl;
lOMoARcPSD|55521570
f1.close(); f2.close();
remove("ticket.dat");
rename("temp.dat","ticket.dat");
getch();
clrscr();
break; case 2 :
//DELETE BY PHONE NO.
28
ck=0;
cout<<"Enter Phone no.:";
gets(cphone);
f1.open("ticket.dat",ios::in|ios::binary);
f2.open("temp.dat",ios::out|ios::binary);
if(f1.fail())
cout<<"Can't open file"<<endl;
else
{
while(f1.read((char*)&t,sizeof(t)))
if(strcmp(t.getphone(),cphone)!
=0)
{ f2.write((char*)&t,sizeof(t)) ;
}
else
{ clrscr()
;
t.display();
ck=1;
}
lOMoARcPSD|55521570
}
if(ck==0)
cout<<"Ticket not found"<<endl;
else cout<<"\nTicket
deleted"<<endl; f1.close();
f2.close();
remove("ticket.dat");
rename("temp.dat","ticket.dat");
29
getch();
clrscr();
break;
}
}while(ch!=3); //END OF TICKET DELETION
MENU }
}while(ch!=4); //END OF ADMIN MENU
}
/*******************************
MAIN FUCTION
31
*******************************/
void main()
{ clrscr(); cout<<"\nPress any key to
continue...."; getch(); clrscr();
int ch;
//LOADING SCREEN
for(int s=0;s<100;s++)
lOMoARcPSD|55521570
{ clrscr();
gotoxy(35,12); cout<<"LOADING
"<<s<<"%"; delay(30);
}
do
{ clrscr();
cout<<"\n
***************************************************
* **********\n";
cout<<"\t\t\t B";
delay(100);
cout<<"U";
delay(100);
cout<<"S";
delay(100); cout<<"
T"; delay(100);
32
cout<<"R";
delay(100);
cout<<"A";
delay(100);
cout<<"V";
delay(100);
cout<<"E";
delay(100);
cout<<"L";
delay(100);
cout<<" A";
lOMoARcPSD|55521570
delay(100);
cout<<"G";
delay(100);
cout<<"E";
delay(100);
cout<<"N";
delay(100);
cout<<"C";
delay(100);
cout<<"Y";
delay(100);
cout<<"\n
***************************************************
* **********\n";
delay(1000); //FIRST
MENU cout<<"\t\t\t
1.Admin\n"
<<"\t\t\t 2.Customer\n"
33
<<"\t\t\t 3.Exit\n"
<<"\t\t\t\n Enter your choice:";
cin>>ch;
switch(ch)
{
case 1 :
//PASSWORD PROGRAM TO ACCESS ADMIN FUNC.
char pwd[20]; char
pswd[]="212173314";
lOMoARcPSD|55521570
cout<<"Enter password:";
for(int i=0;i<9;i++)
{ pwd[i]=getch();
cout<<"*";
}
pwd[i]='\0';
if(strcmp(pwd,pswd)==0)
{
//ACCESSING ADMIN FUNC.
admin();
}
else
cout<<"\nInvalid password!!!";
getch();
break;
case 2 :
//ACCESSING CUSTOMER FUNC.
customer(); break;
34
case 3 :
//EXIT MESSAGE
clrscr(); gotoxy(30,12);
cout<<"THANK YOU FOR VISITING";
delay(300);
clrscr();
delay(300);
gotoxy(30,12);
lOMoARcPSD|55521570
cout<<"THANK YOU
FOR VISITING";
delay(300);
clrscr(); gotoxy(30,12);
cout<<"THANK YOU FOR VISITING";
delay(300);
clrscr(); delay(300); gotoxy(30,12);
cout<<"THANK YOU FOR VISITING";
delay(300);
clrscr(); gotoxy(30,12);
cout<<"THANK YOU FOR VISITING";
delay(300);
clrscr(); delay(300); gotoxy(30,12);
cout<<"THANK YOU FOR VISITING";
35
36
OUTPUT 37
lOMoARcPSD|55521570
38
1.Admin
lOMoARcPSD|55521570
39
40
41
v) Modify a bus
lOMoARcPSD|55521570
42
43
i) Book ticket
lOMoARcPSD|55521570
44
a) Search by name
a) Delete by name
3) EXIT
//END OF PROGRAM
lOMoARcPSD|55521570
Bibliography
∙ www.google.com
∙ www.cbseportal.com
lOMoARcPSD|55521570
48