Train ticket booking project using Python

 


I am using Visual studio code editor for this project. Its really awesome for writing  huge programs as it has awesome extensions which makes your coding life ease. It has triggered love for many coders as it is one of the Microsoft Product.

Link for vs code: Visual studio code

Feel free to use other editors Like pycharm. But for my convince I am using VS Code.





The Project begins with importing some required modules by using "import" keyword. 
  • Datetime 
  • Random
Datetime Module: This module is used to fetch the current time and date as the name suggests. I have used "datetime.datetime.now()" for Bookticket function to do this task.

Random Module: This module is used to generate a random number. For more checkout the documentation  Random module

Strftime: It used to convert time into string.

For more details checkout the documentation: Strftime






To create a class use the keyword class:

A class is a Template in which all required functions are written to perform task . In this case is
def__init__()-- This is an  inbuilt function in python "def" represents "define"  and "__init__()" . Represents initialize the parameters (seats, fare). Here self refers to current instance of the class. This means that whatever the object we create that object gets passed as self i.e. (that object is used as self).







AvaiableSeats: This function is used to display total number of Available seats and fare for each seats. In the print function I have used f-strings. This is special type of programming feature offered by python. We can place variables in {} curly brackets.





Bookticket: In this method I have used "try" and "except" commands to handle the error.
this function takes input as number of passengers. Subsequently it asks to enter for number of passenger name to book their individual ticket. 

After this it will allot a random ticket number for each passenger between 2500 and 3500 with the use of "random.randint(2500,3500)" function in random module. 

It creates a separate text file of individual Passenger name and writes 
  • Name of Passenger
  • Time of booking ticket
  • Date of booking
  • Ticket Number 






Suppose for example Available seats are 6 and we enter 8
Then it books ticket for only 6 Passengers and for rest two it displays message:
Like Seats are unavailable.



 




CancleTicket: In this method I have used inbuilt  OS module called as operating system. This method asks to enter "Booked passenger name". It will create delete the respective booked passengers file by "os.remove()" function and automatically creates "Indian Railways.txt" as writes that "Passenger [name] has Cancelled ticket".




Suppose if we won't enter booked person name it  displays the message: 



And if we do not cancel the ticket then it prints the message as:


Code:
import datetime
import random
today=datetime.datetime.now()
date=today.strftime("%d %B %Y")
current_time=today.strftime("%I:%M%p")

class Train:
    def __init__(self,seats,fare):
        self.seats=seats
        self.fare=fare
    
    def AvailableSeats(self):
        print(f"The Available seats are {self.seats} and fare is {self.fare} for each Passenger ")
    
    def BookTicket(self):
            try:
                tcount=0
                member=int(input("Enter the number of Passengers "))
                self.prize=self.fare*member
                for member in range(member):
                    if self.seats>0:
                        self.seats=self.seats-1
                        member=input(f"Enter member no {member+1} ")
                        ticketno=random.randint(2500,3500)
                        tcount+=1
                        with open(f"{member}.txt","a") as f:
                            
                            f.write(f"\nPassenger: {member}\nTicket: {str(ticketno)}\nBooked date: {date}\nTime: {current_time}\n")
                    else:
                        print("Sorry Cant allot ticket for next passenger!. Kindly try for tatkal")
                
                print(f"{tcount} Tickets have been booked Sucessfully! on {date}.Check the files sent by Indian Railways\nThe total fare amount is Rs {self.prize}")
            except:
                print("Please enter the member number ")
                exit()

    def CancelTicket(self):
        import os
        try:
            traveller=input("Enter the Passenger name ")
            os.remove(f"{traveller}.txt")
            with open("Indian Railways.txt","a") as ct:
                ct.write(f'Passenger {traveller} has Cancelled Booked Seat on {date}\nTime: {current_time}\n')
            print(f"{traveller} your Booked seat has been cancelled sucessfully. Now fare is Rs {self.prize-90}")
        except:
            print(f"{traveller} has not booked a ticket.Make sure that you enter a Booked Passenger name ")

s=Train(6,90)
s.AvailableSeats()
s.BookTicket()
cancel=input("Do you want to cancel Ticket YES(y) NO(n) ")
if cancel=='y':
    s.CancelTicket()
else:
    print("************* Thanks for using Indian Railways Booking Platform *************")



 

Comments

Popular posts from this blog

Automate your file manager using Python