Skip to main content

Design a Movie Ticket Booking system

Problem Statement

Design a movie ticket booking system that allows users to view movies, select shows, book seats, and cancel tickets. The design should ensure that seats cannot be double booked and should properly update seat availability when a booking is cancelled.


Functional Requirements

  • The design should support multiple movies
  • Each movie can have multiple shows
  • Users should be able to view available seats for a show
  • A user should be able to book one or more seats
  • A booked seat cannot be booked again
  • The design should support ticket cancellation
  • Cancelled seats should become available again
  • The design should generate a booking object after successful payment

Objects Required

  • Movie
  • Show
  • Seat
  • Booking
  • User
  • BookingStatus
  • MovieBookingSystem

BookingStatus Enum


public enum BookingStatus {
    BOOKED,
    CANCELLED
}

The BookingStatus enum represents the current state of a booking. A booking can either remain active or be cancelled later by the user.


Movie Class

The Movie class stores basic movie related information.


public class Movie {

    private String movieId;
    private String title;
    private int duration;

    public Movie(String movieId, String title, int duration) {
        this.movieId = movieId;
        this.title = title;
        this.duration = duration;
    }

    public String getTitle() {
        return title;
    }
}

The constructor initializes the movie with its id, title, and duration.

The getTitle() method is used whenever other classes need movie information without directly accessing internal fields.


Seat Class

The Seat class represents a single seat inside a theatre.


public class Seat {

    private int seatNumber;
    private boolean isBooked;

    public Seat(int seatNumber) {
        this.seatNumber = seatNumber;
        this.isBooked = false;
    }

    public boolean isAvailable() {
        return !isBooked;
    }

    public void bookSeat() {
        isBooked = true;
    }

    public void cancelSeat() {
        isBooked = false;
    }

    public int getSeatNumber() {
        return seatNumber;
    }
}

The constructor creates a seat and marks it as available initially.

The isAvailable() method checks whether the seat is free for booking.

The bookSeat() method marks the seat as occupied once a booking is successful.

The cancelSeat() method makes the seat available again after cancellation.


Show Class

The Show class represents a particular movie screening.


import java.util.*;

public class Show {

    private String showId;
    private Movie movie;
    private List<Seat> seats;

    public Show(String showId, Movie movie, List<Seat> seats) {
        this.showId = showId;
        this.movie = movie;
        this.seats = seats;
    }

    public List<Seat> getAvailableSeats() {

        List<Seat> availableSeats = new ArrayList<>();

        for (Seat seat : seats) {
            if (seat.isAvailable()) {
                availableSeats.add(seat);
            }
        }

        return availableSeats;
    }

    public boolean bookSeats(List<Seat> selectedSeats) {

        for (Seat seat : selectedSeats) {
            if (!seat.isAvailable()) {
                return false;
            }
        }

        for (Seat seat : selectedSeats) {
            seat.bookSeat();
        }

        return true;
    }

    public void cancelSeats(List<Seat> selectedSeats) {

        for (Seat seat : selectedSeats) {
            seat.cancelSeat();
        }
    }
}

The constructor initializes the show with a movie and a list of seats.

The getAvailableSeats() method filters all free seats and returns them to the user interface.

The bookSeats() method first checks whether all selected seats are available. This validation is important because even one already booked seat should fail the booking operation.

Only after successful validation are all seats marked as booked.

The cancelSeats() method loops through the selected seats and releases them back into the system.


User Class


public class User {

    private String userId;
    private String name;

    public User(String userId, String name) {
        this.userId = userId;
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

The User class stores user related information required during booking.


Booking Class

The Booking class stores details about a completed booking.


import java.util.*;

public class Booking {

    private String bookingId;
    private User user;
    private Show show;
    private List<Seat> bookedSeats;
    private BookingStatus status;

    public Booking(String bookingId,
                   User user,
                   Show show,
                   List<Seat> bookedSeats) {

        this.bookingId = bookingId;
        this.user = user;
        this.show = show;
        this.bookedSeats = bookedSeats;
        this.status = BookingStatus.BOOKED;
    }

    public void cancelBooking() {

        show.cancelSeats(bookedSeats);
        status = BookingStatus.CANCELLED;
    }
}

The constructor creates a booking object and stores all booking related details.

The booking status is initially marked as BOOKED.

The cancelBooking() method releases all booked seats through the show object and updates the booking status to CANCELLED.


MovieBookingSystem Class

The MovieBookingSystem class coordinates the overall booking flow.


import java.util.*;

public class MovieBookingSystem {

    private List<Show> shows;

    public MovieBookingSystem(List<Show> shows) {
        this.shows = shows;
    }

    public Booking createBooking(User user,
                                 Show show,
                                 List<Seat> seats) {

        boolean booked = show.bookSeats(seats);

        if (!booked) {
            System.out.println("Seats not available");
            return null;
        }

        return new Booking(
                UUID.randomUUID().toString(),
                user,
                show,
                seats
        );
    }
}

The constructor initializes the system with available movie shows.

The createBooking() method first asks the show object to reserve seats.

If any selected seat is already booked, the booking fails immediately.

If all seats are available, a booking object is created with a unique booking id.


Main Class

The Main class is used to simulate the booking flow.


import java.util.*;

public class Main {

    public static void main(String[] args) {

        Movie movie =
                new Movie("M1", "Inception", 180);

        List<Seat> seats = new ArrayList<>();

        for (int i = 1; i <= 10; i++) {
            seats.add(new Seat(i));
        }

        Show show =
                new Show("S1", movie, seats);

        User user =
                new User("U1", "Prasanna");

        MovieBookingSystem system =
                new MovieBookingSystem(
                        Arrays.asList(show)
                );

        List<Seat> selectedSeats =
                Arrays.asList(seats.get(0), seats.get(1));

        Booking booking =
                system.createBooking(
                        user,
                        show,
                        selectedSeats
                );

        if (booking != null) {
            System.out.println("Booking Successful");
        }

        booking.cancelBooking();

        System.out.println("Booking Cancelled");
    }
}

The main() method starts by creating a movie and a list of seats.

A show object is then created for the movie.

The booking system receives seat booking requests from the user and creates a booking if seats are available.

Finally, the booking is cancelled to demonstrate how seats become available again.


Class Diagram

MoviemovieId : Stringtitle : Stringduration : intgetTitle() : StringSeatseatNumber : intisBooked : booleanisAvailable() : booleanbookSeat() : voidcancelSeat() : voidgetSeatNumber() : intShowshowId : Stringmovie : Movieseats : List<Seat>getAvailableSeats() : List<Seat>bookSeats(selectedSeats : List<Seat>) : booleancancelSeats(selectedSeats : List<Seat>) : voidUseruserId : Stringname : StringgetName() : StringBookingStatusBOOKEDCANCELLEDBookingbookingId : Stringuser : Usershow : ShowbookedSeats : List<Seat>status : BookingStatuscancelBooking() : voidMovieBookingSystemshows : List<Show> show : Show,createBooking(user : User,seats : List<Seat>) : BookingMainmain(args : String[]) : void

Also See

Comments

Popular posts from this blog

Designing a Parking Lot - Low Level Design

Problem Statement Design a parking lot that can handle vehicles entering and leaving while managing parking across multiple floors. Each vehicle should be assigned a suitable parking spot based on its type, and the spot should be freed once the vehicle exits. The design should also support generating a ticket at entry and optionally calculating the parking fee based on the duration of stay. Asked In Companies Amazon Google Microsoft Uber Walmart Flipkart Meta PayPal Oracle Salesforce Adobe Apple Intuit LinkedIn Atlassian Functional Requirements The design should support multiple vehicle types such as bikes, cars, and trucks A vehicle must be assigned a parking spot compatible with its type A parking spot cannot be assigned to more than one vehicle at a time The parking lot should support multiple levels (floors) The design should search and allocate an availa...

Most Frequently Asked Low Level Design(LLD) Interview Questions

Below are the curated list of most commonly asked Low Level Design (LLD) interview problems. Each problem includes a short description and a link to the complete solution with code and class diagrams. Design Parking Lot System The system should handle parking for different vehicle types such as bikes, cars, and trucks. It should manage slot allocation, availability tracking, and entry/exit flow. The design also ensures efficient usage of parking space under varying load conditions. View Solution Design Elevator / Lift System The system should support multiple elevators operating across floors with request handling logic. It focuses on scheduling algorithms to minimize wait time and optimize movement. It also manages direction control and concurrent floor requests. View Solution Design Movie Ticket Booking System The system should allow users to browse movies, select shows, and book seats. It handles seat ...

Software Design Patterns for LLD Interviews: A Complete Guide

Software Design Patterns for LLD Interviews: A Complete Guide In Software Development Engineer (SDE) interviews—especially for mid-level and senior roles—low-level design (LLD) rounds assess your ability to write clean, reusable, maintainable, and extensible code. The foundation of resolving these architectural challenges lies in the standard Gang of Four (GoF) Design Patterns. Rather than memorizing theoretical definitions, interviewers expect you to apply these patterns to real-world scenarios, identifying the trade-offs of each. Below is a comprehensive guide to the 12 most frequently asked design patterns in LLD interviews, categorized by their classification (Creational, Structural, and Behavioral). Each pattern contains a concrete, real-world Java implementation and a detailed breakdown of design decisions. Creational Design Patterns Creational design patterns deal with object creation mechanisms. They abstract the instantiation process, making a system independent of how...