An online movie ticket system in Java is a project that allows us to view movies, we can also search for showtimes, and book tickets. This project helps beginners to learn core concepts like object-oriented programming, collections, and user-input handling as well.
In this article, we will build an online movie ticket booking system in Java. Before moving further, let's first understand the implementation and core features of the system.
Working of the Movie Ticket System
Here is how our movie ticket system is going to work.
- Movies: The system will store movies with details like name, genre, and duration.
- Theaters: It will manage theaters and their respective showtimes for each movie.
- Showtimes: A showtime will be associated with a specific movie and a time.
- Bookings: The user will be able to book tickets for available showtimes.
The System will allow users to:
- The user can browse through a list of movies.
- The user can search for showtimes by movie and theater.
- The user can book a ticket for a movie at a given showtime.
- The user can also view and manage the list of bookings.
Project Structure
The image below demonstrates the project structure:
We will organize the code into these parts which are listed below:
- Movie class: This class will define the movie entity including its name and duration.
- Theater class: This class tells about the Theater including its name, location and showtimes.
- Showtime class: This class defines the available showtimes for each movie at the Theaters.
- Booking class: This class handles the booking process for movie at specific showtimes.
- MovieTicketBookingSystem class: This is the main class that handles all the operations like adding movies to the Theaters, searching and booking tickets.
Let's now try to implement the code.
Java Online Movie Ticket System Project Implementation
1. Movie.java:
This class will store all the details like name, genre, and duration.
package com.movieticketbooking;
import java.util.Objects;
public class Movie implements Comparable<Movie> {
private String name;
private String genre;
private int duration; // Duration in minutes
public Movie(String name, String genre, int duration) {
this.name = name;
this.genre = genre;
this.duration = duration;
}
public String getName() {
return name;
}
public String getGenre() {
return genre;
}
public int getDuration() {
return duration;
}
@Override
public int compareTo(Movie o) {
return this.name.compareTo(o.name);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Movie movie = (Movie) o;
return name.equals(movie.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
@Override
public String toString() {
return "Movie{name='" + name + "', genre='" + genre + "', duration=" + duration + "}";
}
}
2. Theater.java:
This class represents a theater, including its name, location, and showtimes.
package com.movieticketbooking;
import java.util.HashSet;
import java.util.Set;
import java.util.Objects;
public class Theater {
private String name;
private String location;
private Set<Showtime> showtimes;
public Theater(String name, String location) {
this.name = name;
this.location = location;
this.showtimes = new HashSet<>();
}
public void addShowtime(Showtime showtime) {
this.showtimes.add(showtime);
}
public Set<Showtime> getShowtimes() {
return showtimes;
}
public String getName() {
return name;
}
public String getLocation() {
return location;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Theater theater = (Theater) o;
return name.equals(theater.name) && location.equals(theater.location);
}
@Override
public int hashCode() {
return Objects.hash(name, location);
}
}
3. Showtime.java:
This class defines the available showtimes for a movie at a specific theater.
package com.movieticketbooking;
import java.time.LocalDateTime;
import java.util.Objects;
public class Showtime {
private LocalDateTime time;
private Movie movie;
private boolean isAvailable;
public Showtime(LocalDateTime time, Movie movie) {
this.time = time;
this.movie = movie;
this.isAvailable = true; // Initially available
}
public LocalDateTime getTime() {
return time;
}
public Movie getMovie() {
return movie;
}
public boolean isAvailable() {
return isAvailable;
}
public void book() {
if (isAvailable) {
isAvailable = false;
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Showtime showtime = (Showtime) o;
return time.equals(showtime.time) && movie.equals(showtime.movie);
}
@Override
public int hashCode() {
return Objects.hash(time, movie);
}
}
4. Booking.java:
This class is used to manages the booking details for a movie and showtime.
package com.movieticketbooking;
public class Booking {
private Movie movie;
private Showtime showtime;
private String customerName;
public Booking(Movie movie, Showtime showtime, String customerName) {
this.movie = movie;
this.showtime = showtime;
this.customerName = customerName;
}
public Movie getMovie() {
return movie;
}
public Showtime getShowtime() {
return showtime;
}
public String getCustomerName() {
return customerName;
}
}
5. MovieTicketBookingSystem.java:
This the main class that manages movies, theaters, showtimes, and bookings.
package com.movieticketbooking;
import java.util.*;
import java.util.concurrent.*;
import java.time.LocalDateTime;
public class MovieTicketBookingSystem {
private List<Movie> movies = new CopyOnWriteArrayList<>();
private Set<Theater> theaters = new CopyOnWriteArraySet<>();
private Map<Theater, List<Showtime>> theaterShowtimes = new ConcurrentHashMap<>();
private List<Booking> bookings = new CopyOnWriteArrayList<>();
// Add a movie to the system
public void addMovie(Movie movie) {
movies.add(movie);
}
// Add a theater to the system
public void addTheater(Theater theater) {
theaters.add(theater);
theaterShowtimes.put(theater, new ArrayList<>());
}
// Add a showtime to a theater
public void addShowtimeToTheater(Theater theater, Showtime showtime) {
theaterShowtimes.get(theater).add(showtime);
}
// Book a ticket
public void bookTicket(Movie movie, Showtime showtime, String customerName) {
if (showtime.isAvailable()) {
showtime.book(); // Mark the showtime as unavailable
bookings.add(new Booking(movie, showtime, customerName));
System.out.println("Booking successful for " + customerName);
} else {
System.out.println("Sorry, this showtime is unavailable.");
}
}
// Search for movies by name
public List<Movie> searchMoviesByName(String name) {
List<Movie> result = new ArrayList<>();
for (Movie movie : movies) {
if (movie.getName().contains(name)) {
result.add(movie);
}
}
return result;
}
// Sort movies by name
public void sortMovies() {
Collections.sort(movies);
}
// Search for showtimes by movie and theater
public List<Showtime> searchShowtimes(Movie movie, Theater theater) {
List<Showtime> result = new ArrayList<>();
for (Showtime showtime : theaterShowtimes.get(theater)) {
if (showtime.getMovie().equals(movie)) {
result.add(showtime);
}
}
return result;
}
public static void main(String[] args) {
MovieTicketBookingSystem system = new MovieTicketBookingSystem();
// Create movies
Movie movie1 = new Movie("Inception", "Sci-Fi", 148);
Movie movie2 = new Movie("The Dark Knight", "Action", 152);
system.addMovie(movie1);
system.addMovie(movie2);
// Create theaters
Theater theater1 = new Theater("IMAX Theater", "City Center");
Theater theater2 = new Theater("Regal Cinemas", "Downtown");
system.addTheater(theater1);
system.addTheater(theater2);
// Create showtimes
Showtime showtime1 = new Showtime(LocalDateTime.of(2025, 6, 10, 19, 30), movie1);
Showtime showtime2 = new Showtime(LocalDateTime.of(2025, 6, 10, 21, 30), movie2);
system.addShowtimeToTheater(theater1, showtime1);
system.addShowtimeToTheater(theater2, showtime2);
// Booking a ticket
system.bookTicket(movie1, showtime1, "Geek");
// Searching and sorting movies
List<Movie> searchResults = system.searchMoviesByName("Inception");
System.out.println("Search Results: " + searchResults);
system.sortMovies();
System.out.println("Sorted Movies: " + system.movies);
}
}
Output:
How to Run the Project?
To run the project on IntelliJ IDEA we just need to follow these steps which are listed below:
- Open IntelliJ IDEA and create a new Java project.
- Start creating the classes and the structure must be look like this:
- Movie class
- Theater class
- Showtime class
- Booking class
- MovieTicketBookingSystem class.
- Save all the files.
- Run the MovieTicketBookingSystem class as the entry point for the application.
