Movie Booking
import java.util.*; class Movie { private String id; private String title; private String genre; private int duration; // Duration in minutes public Movie(String id, String title, String genre, int duration) { this.id = id; this.title = title; this.genre = genre; this.duration = duration; } // Getters and other methods } class Theater { private String id; private String name; private List<Screen> screens; public Theater(String id, String name) { this.id = id; this.name = name; this.screens = new ArrayList<>(); } public void addScreen(Screen screen) { screens.add(screen); } // Getters and other methods } class Screen { private String id; private int capacity; private List<Seat> seats; private List<Show> shows; public Screen(String id, int capacity) { this.id = id; this.capacity = capacity; this.seats = new ArrayList<>(); this.shows = new ArrayList<>(); createSeats(capacity); } private void createSeats(int capacity) { for (int i = 1; i <= capacity; i++) { seats.add(new Seat(i)); } } public void addShow(Show show) { shows.add(show); } // Getters and other methods } class Show { private String id; private Movie movie; private Date startTime; private Screen screen; private Map<Integer, Boolean> seatAvailability; public Show(String id, Movie movie, Date startTime, Screen screen) { this.id = id; this.movie = movie; this.startTime = startTime; this.screen = screen; this.seatAvailability = new HashMap<>(); for (Seat seat : screen.getSeats()) { seatAvailability.put(seat.getSeatNumber(), true); } } public boolean isSeatAvailable(int seatNumber) { return seatAvailability.get(seatNumber); } public void bookSeat(int seatNumber) { if (isSeatAvailable(seatNumber)) { seatAvailability.put(seatNumber, false); } } // Don't implement is cancellation is not needed public void releaseSeat(int seatNumber) { if (!isSeatAvailable(seatNumber)) { seatAvailability.put(seatNumber, true); } } // Getters and other methods } class Seat { private int seatNumber; public Seat(int seatNumber) { this.seatNumber = seatNumber; } public int getSeatNumber() { return seatNumber; } } class User { private String id; private String name; public User(String id, String name) { this.id = id; this.name = name; } public Booking bookShow(Show show, List<Integer> seatNumbers) { List<Seat> bookedSeats = new ArrayList<>(); for (int seatNumber : seatNumbers) { if (show.isSeatAvailable(seatNumber)) { show.bookSeat(seatNumber); bookedSeats.add(new Seat(seatNumber)); } else { System.out.println("Seat " + seatNumber + " is not available"); } } return new Booking(UUID.randomUUID().toString(), this, show, bookedSeats); } // Don't implement if cancellation is not needed. public boolean cancelBooking(Booking booking, Payment payment) { if (booking.cancelBooking()) { return payment.refundPayment(); } return false; } // Getters and other methods } class Booking { private String id; private User user; private Show show; private List<Seat> bookedSeats; private Date bookingDate; public Booking(String id, User user, Show show, List<Seat> bookedSeats) { this.id = id; this.user = user; this.show = show; this.bookedSeats = bookedSeats; this.bookingDate = new Date(); } // Don't implement if cancellation is not needed public boolean cancelBooking() { if (isCancelled) { System.out.println("Booking is already cancelled."); return false; } for (Seat seat : bookedSeats) { show.releaseSeat(seat.getSeatNumber()); } isCancelled = true; System.out.println("Booking cancelled successfully."); return true; } // Getters and other methods } class Payment { private String id; private Booking booking; private double amount; private String paymentMethod; // e.g., credit card, PayPal public Payment(String id, Booking booking, double amount, String paymentMethod) { this.id = id; this.booking = booking; this.amount = amount; this.paymentMethod = paymentMethod; } public boolean processPayment() { // Implement payment processing logic here System.out.println("Payment processed for amount: " + amount); return true; } // Don't implement if cancellation is not supported public boolean refundPayment() { if (isRefunded) { System.out.println("Payment is already refunded."); return false; } // Implement refund processing logic here System.out.println("Payment refunded for amount: " + amount); isRefunded = true; return true; } // Getters and other methods } public class MovieBookingSystem { public static void main(String[] args) { // Create movies Movie movie1 = new Movie("M1", "Inception", "Sci-Fi", 148); Movie movie2 = new Movie("M2", "The Matrix", "Action", 136); // Create theater and screens Theater theater = new Theater("T1", "Cineplex"); Screen screen1 = new Screen("S1", 100); theater.addScreen(screen1); // Create shows Show show1 = new Show("Sh1", movie1, new Date(), screen1); screen1.addShow(show1); // Create a user and book tickets User user = new User("U1", "John Doe"); List<Integer> seatsToBook = Arrays.asList(1, 2, 3); Booking booking = user.bookShow(show1, seatsToBook); // Process payment Payment payment = new Payment("P1", booking, 30.0, "Credit Card"); payment.processPayment(); // Cancel the booking and refund payment user.cancelBooking(booking, payment); } }
Leave a Comment