Python ticket booking program with JSON data

Python ticket booking program with JSON data:

In this post, we will learn how to create a ticket booking system in Python. This program will allow the user to select a movie, screen for that movie, timing and the total number of tickets to book.

We will use an object-oriented approach to solve this program. We will create a class to handle everything.

For data, we will use json files. This makes the program easy to configure. You need to edit these files to add/remove/edit data without changing the source file.

Also, if you work on a live application, or if you store the data in database, JSON is the way to communicate. So, you can modify this program to connect to an external server.

JSON files:

Following are the JSON files we will use in this program:

movies.json:

This file is to hold all the movies. The key is the id of each movie and value is the movie name:

{
  "0": "Movie 1",
  "1": "Movie 2",
  "2": "Movie 3"
}

movie_screens.json:

This is the JSON file to hold the mapping of each movie id with the screens for that movie.

{
  "0": [
    "Screen 1",
    "Screen 2"
  ],
  "1": [
    "Screen 2",
    "Screen 3"
  ],
  "2": [
    "Screen 3",
    "Screen 1"
  ]
}

The key is the movie id and the value is an array holding the screens. For example, movie with id 0 is showing at Screen 1 and Screen 2.

timing_screen_1.json:

This file holds the timing of all movies showing in Screen 1:

{
  "0": [
    "10:00 - 12:30",
    "16:00 - 18:30"
  ],
  "2": [
    "13:00 - 15:30"
  ]
}

The key is the movie id and value is an array holding the times in 24 hour format.

Similar to this file, we have two more JSON files for Screen 2 and Screen 3:

timing_screen_2.json:

Timing for all movies showing in Screen 2:

{
  "0": [
    "10:10 - 12:40",
    "16:10 - 18:40"
  ],
  "1": [
    "13:10 - 15:40"
  ]
}

timing_screen_3.json:

Timing for all movies showing in Screen 3:

{
  "1": [
    "10:20 - 12:50",
    "16:20 - 18:50"
  ],
  "2": [
    "13:20 - 15:50"
  ]
}

Python program:

Create all these JSON files in the root folder with the python .py file. Below is the code for the program:

import json


class MovieTicketBooking:
    movie_file = open('movies.json')
    movie_screens_file = open('movie_screens.json')
    screen_1_timing_file = open('timing_screen_1.json')
    screen_2_timing_file = open('timing_screen_2.json')
    screen_3_timing_file = open('timing_screen_3.json')

    available_movies = json.load(movie_file)
    movie_screens = json.load(movie_screens_file)
    screen_1_timing = json.load(screen_1_timing_file)
    screen_2_timing = json.load(screen_2_timing_file)
    screen_3_timing = json.load(screen_3_timing_file)

    selected_movie = ''
    selected_screen = ''
    selected_time = ''
    total_tickets = ''

    def select_movie(self):
        print()
        print('**** Available Movies: ****')
        for k in self.available_movies:
            print(f'{k} => {self.available_movies[k]}')

        movie_id = input('Enter the movie id: ')

        if movie_id in self.available_movies:
            print(f'You have selected: {self.available_movies[movie_id]}')
            self.selected_movie = self.available_movies[movie_id]
            self.select_screen(movie_id)
        else:
            print('**** Please enter a valid id ****')
            self.select_movie()

    def select_screen(self, movie_id):
        print()
        if movie_id in self.movie_screens:
            print('**** Available screens: ****')
            for i in range(len(self.movie_screens[movie_id])):
                print(f'{i} => {self.movie_screens[movie_id][i]}')

            screen_id = int(input('Enter the screen id: '))

            if screen_id < 0 or screen_id >= len(self.movie_screens[movie_id]):
                print('Please enter a valid id !')
                self.select_screen(movie_id)
            else:
                print(f'You have selected: {self.movie_screens[movie_id][screen_id]}')
                self.selected_screen = self.movie_screens[movie_id][screen_id]
                self.select_timing(self.selected_screen, movie_id)
        else:
            print('Data Error !!')

    def select_timing(self, screen, movie_id):
        print()
        if screen == 'Screen 1':
            screen_timing = self.screen_1_timing
        elif screen == 'Screen 2':
            screen_timing = self.screen_2_timing
        else:
            screen_timing = self.screen_3_timing

        timings = screen_timing[movie_id]

        print("**** Available timings: ****")
        for i in range(len(timings)):
            print(f'{i} => {timings[i]}')

        time_index = int(input('Please select a time: '))

        if time_index < 0 or time_index >= len(timings):
            print('**** Please enter a valid time ****')
            self.select_timing(screen, movie_id)
        else:
            self.selected_time = timings[time_index]
            self.total_tickets = input('Enter total number of tickets: ')
            print('Booking completed !!')
            self.print_booked_data()

    def print_booked_data(self):
        print()
        print(f'Movie: {self.selected_movie}')
        print(f'Screen: {self.selected_screen}')
        print(f'Time: {self.selected_time}')
        print(f'Total tickets: {self.total_tickets}')

    def start_booking(self):
        print('Welcome !!')
        self.select_movie()

    movie_file.close()
    movie_screens_file.close()
    screen_1_timing_file.close()
    screen_2_timing_file.close()
    screen_3_timing_file.close()


if __name__ == '__main__':
    m = MovieTicketBooking()
    m.start_booking()

Here,

  • MovieTicketBooking class is used to handle all the bookings. It reads the data from the JSON files and reads all user input values.
    • Inside this class, we are using the json module to open the JSON files and reading the contents.
  • start_booking method is the entry point. This method shows one welcome message and calls select_movie method.
  • select_movie method is used to do the movie selection. It asks the user to select a movie, and stores that value in a variable in the class object.
  • select_screen method is used to do the screen selection. This method is called from the select_movie method with the movie id.
  • select_timing method is used to do the time selection. This is called from select_screen. It takes the screen name and movie id as its parameters. The selected time is stored in a variable in the object. This method also reads the total number of tickets.
  • print_booked_data method is used to print the booked information. It prints the movie name, screen name, time, and total tickets that the user has selected.

Sample output:

If you run this program, it will print output as like below:

Welcome !!

**** Available Movies: ****
0 => Movie 1
1 => Movie 2
2 => Movie 3
Enter the movie id: 2
You have selected: Movie 3

**** Available screens: ****
0 => Screen 3
1 => Screen 1
Enter the screen id: 1
You have selected: Screen 1

**** Available timings: ****
0 => 13:00 - 15:30
Please select a time: 0
Enter total number of tickets: 4
Booking completed !!

Movie: Movie 3
Screen: Screen 1
Time: 13:00 - 15:30
Total tickets: 4

Python ticket booking example program

You might also like: