#388 Code Review of a Seat Booking Service

Companies:
T-Bank
Themes:
SpringTransactionsCode Review

Perform a code review:

import java.util.UIID;

/**
 * Service for booking a seat on an airplane.
 * A customer with a purchased ticket may select a specific seat for an additional fee.
 * Base seat prices are determined by tariffs from an external service.
 * Customers with certain tariffs (PREMIUM, ULTRA) must receive a discount when paying.
 * When a seat is booked, an invoice is issued to the customer. Payment is managed by an external service.
 */
@Service
public class SeatBookingService {

    @Autowired private SeatBookingRepository seatBookingRepository;
    @Autowired private TicketRepository ticketRepository;
    @Autowired private TariffClient tariffClient;
    @Autowired private CustomerClient customerClient;
    @Autowired private PaymentClient paymentClient;

    /**
     * Booking.
     * @param seatCode seat code, for example 19A
     * @param ticketId ticket id
     */
    @Transactional
    public void bookSeat(String seatCode, UIID ticketId) {
        var ticket = ticketRepository.findById(ticketId);
        // book the seat
        var seatBooking = new SeatBooking(seatCode, ticket.get().getFlightId(), ticketId, BookingStatus.BOOKED);
        seatBookingRepository.save(seatBooking);

        // find the base tariff for the selected seat on the airplane
        var basePrice = tariffClient.getBasePrice(ticket.get().getPlaneModel(), seatCode);
        // find customer data
        long userId = (long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        var userData = customerClient.getCustomer(userId);
        System.out.println("Found user " + userData.getFio() + " document number " + userData.getDocument());
        var price = basePrice;
        if (userData.getTariff() == "PREMIUM") {
            // 50% discount
            price = basePrice * 0.5d;
        }
        if (userData.getTariff() == "ULTRA") {
            // 20% discount
            price = basePrice * 0.8d;
        }
        var invoice = new Invoice(price, ticketId, userId);
        // issue the invoice
        paymentClient.sendInvoice(invoice);
    }
}

@Data
@Table("seat_booking")
public class SeatBooking {

    @Column
    private String seatCode;

    @Column
    private UUID flightId;

    @Column
    private UUID ticketId;

    @Column
    private BookingStatus status;
}

public enum BookingStatus {
    BOOKED, PAID;
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in