Code · LLD machine coding · PhonePe prep

BookMyShow (v2)

10 classes 223 lines View on GitHub ↗

Question

The same problem as bookmyshow, redone from scratch under time pressure: movies, theatres, screens, shows, and per-show seat state (ShowSeat: available / locked / booked). Entities only so far.

entities/Movie.java 20 lines GitHub ↗
package com.practice.phonepe.questions.bookmyshowv2.entities;

public class Movie {

    private final String movieID;
    private final String title;
    private final String description;

    public Movie(String movieID, String title, String description) {
        this.movieID = movieID;
        this.title = title;
        this.description = description;
    }

    public String getMovieID() {
        return movieID;
    }

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }
}
entities/Screen.java 24 lines GitHub ↗
package com.practice.phonepe.questions.bookmyshowv2.entities;

import java.util.ArrayList;
import java.util.List;

public class Screen {
    private final String id;
    private final String name;
    private List<Seat> seats = new ArrayList<>();

    public Screen(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public List<Seat> getSeats() {
        return seats;
    }

    public void addSeat(Seat seat) {
        seats.add(seat);
    }
}
entities/Seat.java 25 lines GitHub ↗
package com.practice.phonepe.questions.bookmyshowv2.entities;

public class Seat {
    private final String id;
    private final int row;
    private final int seatNumber;
    private final SeatType seatType;

    public Seat(String id, int row, int seatNumber, SeatType seatType) {
        this.id = id;
        this.row = row;
        this.seatNumber = seatNumber;
        this.seatType = seatType;
    }

    public String getId() {
        return id;
    }

    public int getRow() {
        return row;
    }

    public int getSeatNumber() {
        return seatNumber;
    }

    public SeatType getSeatType() {
        return seatType;
    }
}
entities/SeatStatus.java 4 lines GitHub ↗
package com.practice.phonepe.questions.bookmyshowv2.entities;

public enum SeatStatus {
    AVAILABLE, BOOKED, LOCKED
}
entities/SeatType.java 4 lines GitHub ↗
package com.practice.phonepe.questions.bookmyshowv2.entities;

public enum SeatType {
    NORMAL, PREMIUM
}
entities/Show.java 45 lines GitHub ↗
package com.practice.phonepe.questions.bookmyshowv2.entities;

import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.Map;

public class Show {
    private final String showID;
    private final String movieId;
    private final String theatreId;
    private final String screenId;
    private final LocalDateTime startTime;
    private final LocalDateTime endTime;

    private Map<String , ShowSeat> showSeatsBySeatId = new LinkedHashMap<>();

    public Show(String showID, String movieId, String theatreId, String screenId, LocalDateTime startTime, LocalDateTime endTime) {
        this.showID = showID;
        this.movieId = movieId;
        this.theatreId = theatreId;
        this.screenId = screenId;
        this.startTime = startTime;
        this.endTime = endTime;
    }

    public String getShowID() {
        return showID;
    }

    public String getMovieId() {
        return movieId;
    }

    public String getTheatreId() {
        return theatreId;
    }

    public String getScreenId() {
        return screenId;
    }

    public LocalDateTime getStartTime() {
        return startTime;
    }

    public LocalDateTime getEndTime() {
        return endTime;
    }

    public Map<String, ShowSeat> getShowSeatsBySeatId() {
        return showSeatsBySeatId;
    }

    public void addShowSeat(ShowSeat showSeat) {
        showSeatsBySeatId.put(showSeat.getSeat().getId() , showSeat);
    }


}
entities/ShowSeat.java 48 lines GitHub ↗
package com.practice.phonepe.questions.bookmyshowv2.entities;

public class ShowSeat {

    private final String showID;
    private final Seat seat;
    private final Double price;

    private SeatStatus seatStatus;
    private String lockedByUserId;
    private long lockExpiryEpochMillis;


    public ShowSeat(String showID, Seat seat, Double price) {
        this.showID = showID;
        this.seat = seat;
        this.price = price;
    }

    public String getId() {
        return showID + "_" + seat.getId();
    }

    public String getShowId() {
        return showID;
    }

    public Seat getSeat() {
        return seat;
    }

    public double getPrice() {
        return price;
    }

    public synchronized SeatStatus getStatus() {
        return seatStatus;
    }

    public synchronized String getLockedByUserId() {
        return lockedByUserId;
    }

    public synchronized boolean tryLock(String userID, long ttlMillis ,  long currentEpochTime) {
        // if free change status to locked and lockedbyuser = current user and expiryepoch time calcualte
        return true;
    }

    public synchronized boolean confirmBooking(String userId, long nowEpochMillis) {
        // locked and userid = lockedbyuserid and expiry time not reached
        return true;
    }

    public synchronized void release(String userID) {
        // remove lock and staus back to available
    }

    public synchronized boolean expireIfNeeded(long currentEpochMiills) {
        // if current epoch is greater then lockExpiryEpochMillis
        // remove the lock / user and status back to aviable
        return true;
    }
}
entities/Theater.java 30 lines GitHub ↗
package com.practice.phonepe.questions.bookmyshowv2.entities;

import com.practice.phonepe.questions.bookmyshow.entities.Screen;

import java.util.ArrayList;
import java.util.List;

public class Theater {
    private final String id;
    private final String name;
    private final String city;
    private List<Screen> screens = new ArrayList<>();

    public Theater(String id, String name, String city) {
        this.id = id;
        this.name = name;
        this.city = city;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getCity() {
        return city;
    }

    public List<Screen> getScreens() {
        return screens;
    }

    public void addScreens(Screen screen) {
        screens.add(screen);
    }
}
entities/User.java 20 lines GitHub ↗
package com.practice.phonepe.questions.bookmyshowv2.entities;

public class User {
    private final String id;
    private final String name;
    private final String emailId;

    public User(String id, String name, String emailId) {
        this.id = id;
        this.name = name;
        this.emailId = emailId;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getEmailId() {
        return emailId;
    }
}
BookMyShowDemo.java 3 lines GitHub ↗
package com.practice.phonepe.questions.bookmyshowv2;

public class BookMyShowDemo {
}