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.
Comments
Post a Comment