Designing Movie Ticket Booking System


Requirements:

Sequnece Diagram
cache
Classes and Responsibilities:

Code Implementation:
import java.util.*;

class Movie {
    private String title;
    private String genre;
    private int duration; // in minutes

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

    public String getTitle() {
        return title;
    }

    public String getGenre() {
        return genre;
    }

    public int getDuration() {
        return duration;
    }
}
class Seat {
    private int seatNumber;
    private String seatType; // e.g., normal, premium
    private double price;
    private boolean isBooked;

    public Seat(int seatNumber, String seatType, double price) {
        this.seatNumber = seatNumber;
        this.seatType = seatType;
        this.price = price;
        this.isBooked = false;
    }

    public int getSeatNumber() {
        return seatNumber;
    }

    public String getSeatType() {
        return seatType;
    }

    public double getPrice() {
        return price;
    }

    public boolean isBooked() {
        return isBooked;
    }

    public void bookSeat() {
        if (!isBooked) {
            isBooked = true;
        } else {
            throw new IllegalStateException("Seat already booked.");
        }
    }
}
class Show {
    private Movie movie;
    private String timing; // e.g., "6:00 PM"
    private List<Seat> seats;

    public Show(Movie movie, String timing, int totalSeats) {
        this.movie = movie;
        this.timing = timing;
        this.seats = new ArrayList<>();
        initializeSeats(totalSeats);
    }

    private void initializeSeats(int totalSeats) {
        for (int i = 1; i <= totalSeats; i++) {
            String seatType = i <= 10 ? "Premium" : "Normal";
            double price = i <= 10 ? 300.0 : 150.0;
            seats.add(new Seat(i, seatType, price));
        }
    }

    public Movie getMovie() {
        return movie;
    }

    public String getTiming() {
        return timing;
    }

    public List<Seat> getSeats() {
        return seats;
    }
}
class Theater {
    private String name;
    private List<Show> shows;

    public Theater(String name) {
        this.name = name;
        this.shows = new ArrayList<>();
    }

    public String getName() {
        return name;
    }

    public List<Show> getShows() {
        return shows;
    }

    public void addShow(Show show) {
        shows.add(show);
    }
}
class Booking {
    private Show show;
    private List<Seat> bookedSeats;
    private double totalAmount;

    public Booking(Show show, List<Integer> seatNumbers) {
        this.show = show;
        this.bookedSeats = new ArrayList<>();
        this.totalAmount = 0.0;

        for (int seatNumber : seatNumbers) {
            Seat seat = show.getSeats().get(seatNumber - 1);
            seat.bookSeat();
            bookedSeats.add(seat);
            totalAmount += seat.getPrice();
        }
    }

    public void printBookingDetails() {
        System.out.println("Movie: " + show.getMovie().getTitle());
        System.out.println("Timing: " + show.getTiming());
        System.out.println("Seats Booked: ");
        for (Seat seat : bookedSeats) {
            System.out.println(" - Seat " + seat.getSeatNumber() + " (" + seat.getSeatType() + ")");
        }
        System.out.println("Total Amount: $" + totalAmount);
    }
}
public class MovieTicketBookingSystem {
    public static void main(String[] args) {
        // Sample Data
        Movie movie1 = new Movie("Avengers: Endgame", "Action", 180);
        Movie movie2 = new Movie("Frozen 2", "Animation", 120);

        Theater theater = new Theater("Cineplex");

        Show show1 = new Show(movie1, "6:00 PM", 20);
        Show show2 = new Show(movie2, "8:00 PM", 20);

        theater.addShow(show1);
        theater.addShow(show2);

        // Booking Process
        System.out.println("Available shows at " + theater.getName() + ":");
        for (Show show : theater.getShows()) {
            System.out.println("- " + show.getMovie().getTitle() + " at " + show.getTiming());
        }

        System.out.println("\nBooking seats for 'Avengers: Endgame' at 6:00 PM.");
        Booking booking = new Booking(show1, Arrays.asList(1, 2, 11));
        booking.printBookingDetails();
    }
}

Sample Input:
Sample Output:
Movie: Avengers: Endgame
Timing: 6:00 PM
Seats Booked: 
 - Seat 1 (Premium)
 - Seat 2 (Premium)
 - Seat 11 (Normal)
Total Amount: $750.0
    

Explanation:

Scalability Considerations: