0% found this document useful (0 votes)
27 views

Train Ticket Reservation Programl

This document contains code to create and populate tables in a MySQL database for a train reservation system. It defines tables for train schedules, fares, and availability from Chennai. Functions are defined to display schedules, calculate fares based on concessions, and generate tickets. The code prompts the user for travel details and calls the functions to book and display a one-way ticket, offering to also book a return.

Uploaded by

Puviyarasi M P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Train Ticket Reservation Programl

This document contains code to create and populate tables in a MySQL database for a train reservation system. It defines tables for train schedules, fares, and availability from Chennai. Functions are defined to display schedules, calculate fares based on concessions, and generate tickets. The code prompts the user for travel details and calls the functions to book and display a one-way ticket, offering to also book a return.

Uploaded by

Puviyarasi M P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

import mysql.

connector as puvi
mcon=puvi.connect(host='localhost',user='root',passwd='Research@#16',database='puvi')
cur=mcon.cursor()

# To Create days table


cur.execute('create table DAYS_AVAILABLE(D_Code VARCHAR(6) Primary key,Days varchar(30))')
i1='insert into DAYS_AVAILABLE(D_Code,Days) values ("{}","{}")'
dt=[['SS01','Sunday'],['WS02','Friday,Saturday,Sunday'],['TS03','Tuesday,Thursday'],['WDS04','Monday,Wednesday,Friday'],['AS05','All days']]
for i in dt:
cur.execute(i1.format(i[0],i[1]))
mcon.commit()
#To create fare table
cur.execute('create table TRAIN_FARE(Price_code int(3) Primary key,SC int,SlC int,3AC int,2AC int,1AC int)')
i2='insert into TRAIN_FARE (Price_code,SC,SlC,3AC,2AC,1AC) values({},{},{},{},{},{})'
pl=['Price_code','SC','SlC','3AC','2AC','1AC']
ft=[[101,45,55,70,80,85],[102,40,50,65,75,80],[103,50,60,75,85,90]]
for i in ft:
cur.execute(i2.format(i[0],i[1],i[2],i[3],i[4],i[5]))
mcon.commit()
#To create train table from Chennai
cur.execute('''create table TRAIN_FROM_CHENNAI(Train_No int Primary key,Train_Name varchar(50),
Source_station varchar(10) default 'Chennai',Final_station varchar(40),D_code varchar(40)
,City_code varchar(10),Price_code int,Boarding_time char(8))''')
dfc= [[120011,'Brindhavan Express','Bangalore','WDS04','CHBAN',101,'21:30:00'],[120012,'Kovai Express','Coimbatore','WS02','CHCOI',103,'06:30:00'],
[120013,'Rockfort Express','Trichy','WDS04','CHTRI',103,'09:15:00'],[120014,'Pandian Express','Madurai','AS05','CHMAD',101,'08:50:00'],
[120015,'Tamilnadu Express','New Delhi','TS03','CHND',102,'12:35:00'],[120016,'Ganga Kauvery Express','Varanasi','SS01','CHVAR',102,'19:40:00'],
[120017,'Coromandel Express','Howra','WS02','CHHOW',102,'15:20:00'],[120018,'Mumbai Express','Mumbai','WDS04','CHMUM',101,'10:30:00'],
[120019,'Charminar Express','Hyderabad','TS04','CHHYD',103,'05:10:00'],[120020,'Navjeevan Express','Ahmedabad','SS01','CHAMD',102,'17:45:00']]
i3="insert into TRAIN_FROM_CHENNAI (Train_No,Train_Name,Final_station,D_Code,City_code,Price_code,Boarding_time) values({},'{}','{}','{}','{}',{},'{}')"
for i in dfc:
cur.execute(i3.format(i[0],i[1],i[2],i[3],i[4],i[5],i[6]))
mcon.commit()
#To create train table to Chennai
cur.execute('''create table TRAIN_TO_CHENNAI(Train_No int,Train_Name varchar(50) Primary key,
Source_station varchar(20),Final_station varchar(10) default 'Chennai',D_code varchar(40)
,City_code varchar(10),Price_code int,Boarding_time char(8))''')
i4="insert into TRAIN_TO_CHENNAI (Train_No,Train_Name,Source_station,D_Code,City_code,Price_code,Boarding_time) values({},'{}','{}','{}','{}',{},'{}')"
dtc=[[220011,'Brindhavan Express','Bangalore','WS02','CHBAN',101,'10:20:00'],[220012,'Kovai Express','Coimbatore','WS02','CHCOI',103,'17:30:00'],
[220013,'Rockfort Express','Trichy','WDS04','CHTRI',103,'12:15:00'],[220014,'Pandian Express','Madurai','WDS04','CHMAD',101,'22:50:00'],
[220015,'Tamilnadu Express','New Delhi','WS02','CHND',102,'05:35:00'],[220016,'Ganga Kauvery Express','Varanasi','TS03','CHVAR',102,'08:40:00'],
[220017,'Coromandel Express','Howra','SS01','CHHOW',102,'23:20:00'],[220018,'Mumbai Express','Mumbai','WS02','CHMUM',101,'09:30:00'],
[220019,'Charminar Express','Hyderabad','SS01','CHHYD',103,'13:10:00'],[220020,'Navjeevan Express','Ahmedabad','SS01','CHAMD',102,'07:45:00']]
for i in dtc:
cur.execute(i4.format(i[0],i[1],i[2],i[3],i[4],i[5],i[6]))
mcon.commit()
#stoppings
c='Chennai'
si={'CHBAN':{c:[0,400],'Ambur':[200,200],'Bangarapet':[300,100],'Bangalore':[400,0]},'CHCOI':{c:[0,500],'Salem':[350,150],'Erode':[400,100],'Coimbatore':[500,0]},
'CHTRI':{c:[0,350],'Ariyalur':[300,50],'Trichy':[350,0]},'CHMAD':{c:[0,500],'Dindigul':[400,100],'Kodaikanal':[450,50],'Madurai':[500,0]},
'CHND':{c:[0,2200],'Bhopal':[1500,700],'Agra':[2000,200],'New Delhi':[2200,0]},'CHVAR':{c:[0,2150],'Nagpur':[1100,1050],'Allahabad':[2000,150],'Varanasi':[2150,0]},
'CHHOW':{c:[0,1650],'Vizag':[800,850],'Bhubaneshwar':[1200,450],'Howrah':[1650,0]},'CHMUM':{c:[0,1300],'Cuddapah':[250,1050],'Pune':[1100,200],'Mumbai':[1300,0]},
'CHHYD':{c:[0,800],'Nellore':[200,600],'Vijayawada':[450,350],'Warangal':[650,150],'Hyderabad':[800,0]},
'CHAMD':{c:[0,1900],'Wardha':[900,1000],'Surat':[1650,250],'Ahmedabad':[1900,0]}}

ct={'AM90':['Army',90,2],'DA25':['Differently abled',25,2],'CP40':['Cancer patient',40,2],'SP60':['Sports person',60,2],'AW75':['Award winner',75,2],'P100':['Staff Pass',100,3]}

def gap():
print('''
''')
def g():
print()
def fetch():
f=cur.fetchall()
for d in f:
print(d)
def l():
for j in range(95):
print('_',end='_')
print()
def s():
for i in range(45):
print(' ',end=' ')
def error():
s()
for i in range(13):
print('-',end='')
g()
s()
print('| ! ERROR ! |')
s()
for i in range(13):
print('-',end='')
def ticket():
print('THE TRAIN TICKET FOR ONE WAY TO "',stpng,'" WILL BE GENERATED SOON.')
l()
gap()
for i in range(45):
print(' ',end=' ')
print('TRAIN TICKET')
gap()
g()
print('Train no. : ',f[0],' '*20,'Train name : ',f[1],' '*(45-len(f[1])),'Source station : ',f[2],' '*(40-len(f[2])),'Final station : ',f[3])
gap()
print('Destination : ',stpng,' '*(45-len(stpng)),'Class : ',pclass,' '*(20-len(pclass)),'Date : ',date,' '*30,'Time : ',f[4])
gap()
print('Name:',pname,' '*(40-len(pname)),'Age : ',pAge,' '*(30-len(pAge)),'Gender : ',pgender,' '*30,'Concession : ',con)
gap()
print('Total Fare : Rs.',fprice,'/- only')
gap()
l()
def date1():
g()
print('The day(s) and time your train is available are as follows in the format (DAY(S) , HH:MM:SS) : ')
g()
cur.execute(sdd.format(fs))
fetch()

def dtpr():
g()
chk2=True
while chk2==True:

g()
day=input('Enter the day of your travel(the first letter must be uppercase and remaining must be lowercase) : ')
for i in kt:
if fs in i:
for j in dt:
if i[3] in j:
x=j[1]
avail=x.split(',')
if day not in avail:
error()
print('Error!!! Enter a date on which the train is available!!!')
chk2=True
else:
chk2=False

for i in range(0,40):
print('-',end='-')
print('WELCOME TO INDIAN RAILWAYS!!!',end='-')
for i in range(0,40):
print('-',end='-')
gap()
for i in range(0,18):
print(' ',end=' ')
print(' WE ASSURE YOU A SAFE AND HAPPY JOURNEY :)')
gap()

print('The details of the trains available from Chennai are:')


l()
print('|',' Train_No ','|',' Train_Name ',' |','Source_station',' |',' Final_station ',
' |',' D_Code ','|',' City_code ',' |','PriceCode','|',' Boarding_time ','|',)
l()
for i in dfc:
print('| ',i[0],' | ',i[1],' '*(22-len(i[1])),'| ',c,' | ',i[2],' '*(15-len(i[2])),'| ',i[3],' '*(6-len(i[3])),'| ',i[4],' '*(11-len(i[4])),'| ',i[5],
' | ',i[6],' | ',)
l()
gap()
print('''The abbreviations of the classes available:
SC- Second Class,
SlC- Sleeper class,
3AC- Three tier(A/C)
2AC- Two tier(A/C)
1AC - First class(A/C)''')
g()
print('The price details of different classes are:')
l()
print('| ','Price_code ','| ','SecondClass','|SleeperClass','| ','3AC ','| ','2AC ','| ','1AC ','|')
l()
for i in ft:
print('|',' '*(7-3),i[0],' '*(7-2),'|',' ',i[1],' ','|',' ',i[2],' ','|',' ',i[3],' ','|',' ',i[4],' ','|',' ',i[5],' ','|')
l()
gap()
print('CONCESSIONS')
g()
l()
print('|','CONCESSION CODE','| ','CONCESSION',' |','DISCOUNT(%) ','|')
l()
for i in ct:
print('|',' '*5,i,' '*4,'| ',ct[i][0],' '*(18-len(ct[i][0])),'|',' '*5,ct[i][1],' '*(6-ct[i][2]),'|')
l()

chk=True
while chk==True:
fs=input('Enter the city code of the train you want to travel : ')
for i in dfc:
if fs in i:
K=False
break
if K!=False:
error()
print('ERROR!!! ENTER A VALID CITY CODE!!!')
chk=True
else:
chk=False
g()
print('THE STOPPINGS AT WHICH THE TRAIN STOPS ARE : ',end=' ')
g
()
sl1=len(si[fs])
for i in si[fs]:
if i==c:
continue
else:
print(i,end=',')
sdd='select days,boarding_time from days_available d,Train_FROM_CHENNAI fc where d.d_code=fc.d_code and city_code="{}"'
date1()
cfrm=input('If you agree with the above days and timing enter "CONTINUE" : ')
if cfrm=='CONTINUE'or cfrm=='continue':
stpng=input('Enter the name of the station you want to reach: ')
pname=input('Enter passenger name : ')
pAge=input('Enter passenger age in number : ')
pgender=input('Enter passenger gender : ')
pclass=input('Enter the desired class for travelling as per the valid abbreviation: ')
g()
chk1=int(input('Is this passenger eligible for concession? [Enter 0 for "NO" and 1 for "YES"] '))
if chk1==1:
g()
conc=input('Enter the concession code the passenger can avail : ')
conn=ct[conc][1]
con=ct[conc][0]
else:
con='NONE'
conn=0
ptfc='select price_code from Train_from_chennai where city_code="{}"'
kt=dfc
g()
date=input('Enter the date you want to travel (in the format "DD-MM-YYYY") : ')
dtpr()
cur.execute(ptfc.format(fs))
pcode=cur.fetchone()
ind=pl.index(pclass)
for i in ft :
if pcode[0] in i:
j=i
break
else:
continue
pr1=j[ind]
pr2=(si[fs][stpng][0]/50)*int(pr1)
disc=(pr2*conn)/100
fprice=pr2-disc
sst=c
tdfc='select train_no,train_name,source_station,final_station,boarding_time from Train_from_chennai where city_code="{}"'
cur.execute(tdfc.format(fs))
f=cur.fetchone()
print(f)
ticket()
gap()

cfrm1=int(input('ENTER(1 if "YES" and 0 if "NO") WHETHER YOU ALSO WANT TO BOOK A RETURN TICKET : '))
if cfrm1==1:
print('ENTER THE FOLLOWING DETAILS FOR BOOKING A RETURN TICKET: ')
sdd='select days,boarding_time from days_available d,Train_TO_CHENNAI fc where d.d_code=fc.d_code and city_code="{}"'
date1()
kt=dtc
pclass=input('Enter the desired class for travelling as per the valid abbreviation: ')
ptfc='select price_code from Train_to_chennai where city_code="{}"'
date=input('Enter the date you want to travel (in the format "DD-MM-YYYY") : ')
dtpr()
cur.execute(ptfc.format(fs))
pcode=cur.fetchone()
ind=pl.index(pclass)
for i in ft :
if pcode[0] in i:
j=i
break
else:
continue
pr1=j[ind]
pr2=(si[fs][stpng][0]/50)*int(pr1)
disc=(pr2*conn)/100
fprice=pr2-disc
sst=stpng
cur.execute('select train_no,train_name,source_station,final_station,boarding_time from Train_to_chennai where city_code="{}"'.format(fs))
f=cur.fetchone()
ticket()

You might also like