Low-Level Design (LLD) for a Movie Ticket Booking System in Java
The Movie Ticket Booking System is an application where users can search for movies, view available showtimes, and book or cancel tickets. The system also provides functionalities for managing movie details, theatre information, and show schedules.
In this LLD, we will address the following core functionalities:
Entities Representation: How to represent entities like movies, theatres, show timings, and their relationships.
Booking and Cancellation: How to handle booking, canceling, and checking seat availability.
User Interaction: How users interact with the system to search for movies, select timings, and book tickets.
Design Patterns Used: We will implement patterns like Factory Pattern, Observer Pattern, Strategy Pattern, and Singleton Pattern to structure the system.
Entities and Relationships
Key Entities:
Movie: Represents a movie with properties like title, genre, director, and language.
Theatre: Represents a movie theatre with a list of available movies and showtimes.
Show: Represents a specific show of a movie at a theatre with attributes like showtime, available seats, and ticket price.
Ticket: Represents a booking ticket for a specific show, including the seat number and booking details.
User: Represents a user who can book, cancel tickets, and search for movies and showtimes.
Classes and Data Structures
1. Movie Class
The Movie class represents a movie in the system.
class Movie {
private String title;
private String genre;
private String director;
private String language;
public Movie(String title, String genre, String director, String language) {
this.title = title;
this.genre = genre;
this.director = director;
this.language = language;
}
public String getTitle() {
return title;
}
public String getGenre() {
return genre;
}
public String getDirector() {
return director;
}
public String getLanguage() {
return language;
}
}
2. Theatre Class
The Theatre class represents a theatre, with a list of available movies and their corresponding showtimes.
import java.util.List;
class Theatre {
private String name;
private List<Show> shows;
public Theatre(String name, List<Show> shows) {
this.name = name;
this.shows = shows;
}
public String getName() {
return name;
}
public List<Show> getShows() {
return shows;
}
}
3. Show Class
The Show class represents a movie show in a particular theatre at a specified time, with seat availability and ticket prices.
import java.util.List;
class Show {
private Movie movie;
private String showtime;
private int totalSeats;
private int availableSeats;
private double ticketPrice;
private List<Ticket> tickets;
public Show(Movie movie, String showtime, int totalSeats, double ticketPrice) {
this.movie = movie;
this.showtime = showtime;
this.totalSeats = totalSeats;
this.availableSeats = totalSeats;
this.ticketPrice = ticketPrice;
}
public boolean isAvailable(int numberOfSeats) {
return availableSeats >= numberOfSeats;
}
public void bookSeats(int numberOfSeats) {
availableSeats -= numberOfSeats;
}
public void cancelSeats(int numberOfSeats) {
availableSeats += numberOfSeats;
}
public String getShowtime() {
return showtime;
}
public Movie getMovie() {
return movie;
}
public double getTicketPrice() {
return ticketPrice;
}
}
4. Ticket Class
The Ticket class represents a movie ticket with details like the show, seat number, and price.
class Ticket {
private Show show;
private int seatNumber;
private double price;
public Ticket(Show show, int seatNumber, double price) {
this.show = show;
this.seatNumber = seatNumber;
this.price = price;
}
public Show getShow() {
return show;
}
public int getSeatNumber() {
return seatNumber;
}
public double getPrice() {
return price;
}
}
5. User Class
The User class represents a user who can interact with the system.
import java.util.List;
class User {
private String name;
private String userId;
private List<Ticket> tickets;
public User(String name, String userId) {
this.name = name;
this.userId = userId;
}
public void bookTicket(Show show, int numberOfSeats) {
if (show.isAvailable(numberOfSeats)) {
show.bookSeats(numberOfSeats);
Ticket ticket = new Ticket(show, numberOfSeats, show.getTicketPrice());
tickets.add(ticket);
System.out.println("Ticket booked for " + numberOfSeats + " seats.");
} else {
System.out.println("Not enough available seats.");
}
}
public void cancelTicket(Ticket ticket) {
ticket.getShow().cancelSeats(1);
tickets.remove(ticket);
System.out.println("Ticket cancelled.");
}
}
6. BookingManager Class (Singleton Pattern)
The BookingManager class is responsible for handling the core logic of booking tickets and managing showtimes. It ensures only one instance of the manager exists.
import java.util.List;
class BookingManager {
private static BookingManager instance;
private List<Theatre> theatres;
private BookingManager() {
}
public static BookingManager getInstance() {
if (instance == null) {
instance = new BookingManager();
}
return instance;
}
public List<Theatre> getTheatres() {
return theatres;
}
public void addTheatre(Theatre theatre) {
theatres.add(theatre);
}
}
7. SearchStrategy Interface (Strategy Pattern)
The SearchStrategy interface allows for different strategies to search for movies based on criteria like movie name, genre, or director.
import java.util.List;
class SearchByTitleStrategy implements SearchStrategy {
@Override
public List<Show> search(List<Theatre> theatres, String criteria) {
List<Show> result = new ArrayList<>();
for (Theatre theatre : theatres) {
for (Show show : theatre.getShows()) {
if (show.getMovie().getTitle().equalsIgnoreCase(criteria)) {
result.add(show);
}
}
}
return result;
}
}
9. Notification System (Observer Pattern)
The NotificationSystem is responsible for sending notifications when a booking or cancellation is completed.
interface Observer {
void update(String message);
}
class UserObserver implements Observer {
private User user;
public UserObserver(User user) {
this.user = user;
}
@Override
public void update(String message) {
System.out.println("Notification to " + user.getName() + ": " + message);
}
}
class NotificationSystem {
private List<Observer> observers = new ArrayList<>();
public void addObserver(Observer observer) {
observers.add(observer);
}
public void notifyObservers(String message) {
for (Observer observer : observers) {
observer.update(message);
}
}
}
User Interface (UI)
The User Interface (UI) allows users to search for movies, select shows, and book or cancel tickets. The UI could be a command-line or web-based interface that interacts with the backend system. Here’s a simple way the user can interact with the system:
public class MovieTicketBookingSystem {
public static void main(String[] args) {
// Initialize the system
BookingManager bookingManager = BookingManager.getInstance();
// Create movies
Movie movie1 = new Movie("Inception", "Sci-Fi", "Christopher Nolan", "English");
Movie movie2 = new Movie("Titanic", "Romance", "James Cameron", "English");
// Create shows
Show show1 = new Show(movie1, "2025-02-21 19:00", 100, 12.99);
Show show2 = new Show(movie2, "2025-02-21 20:00", 80, 10.99);
// Create theatres
Theatre theatre1 = new Theatre("Cinema1", List.of(show1, show2));
// Add theatres to booking system
bookingManager.addTheatre(theatre1);
// Create user
User user = new User("John Doe", "U001");
// User books a ticket
user.bookTicket(show1, 2);
// Search for movies
SearchStrategy searchStrategy = new SearchByTitleStrategy();
List<Show> foundShows = searchStrategy.search(bookingManager.getTheatres(), "Inception");
// Display search results
for (Show show : foundShows) {
System.out.println("Movie: " + show.getMovie().getTitle() + ", Showtime: " + show.getShowtime());
}
// User cancels a ticket
Ticket ticket = user.getTickets().get(0);
user.cancelTicket(ticket);
}
}
Design Patterns Used
Factory Pattern: We could implement a factory pattern to create instances of Movie, Show, and Ticket. This allows flexibility in object creation, especially when adding new types of shows or tickets.
Observer Pattern: The NotificationSystem uses the Observer Pattern to notify users when their tickets are booked or canceled.
Strategy Pattern: The SearchStrategy interface allows us to implement different search strategies for movies based on criteria like title, genre, or director.
Singleton Pattern: The BookingManager class uses the Singleton Pattern to ensure that there is only one instance of the manager in the system.
Conclusion
The Movie Ticket Booking System design is modular and scalable. It uses key design patterns like Factory, Observer, Strategy, and Singleton to structure the system efficiently. The system allows users to search for movies, book tickets, and manage showtimes while keeping track of available seats and sending notifications about booking or cancellation. The separation of concerns in the design ensures easy extensibility and maintenance.