Sobes.tech
Senior

Read the code and highlight errors. /** * A client with a purchased ticket can choose a specific seat for an additional fee. * The base price of seats is determined by tariffs (external service). * For clients with certain tariffs (PREMIUM, ULTRA), a discount is applied at payment. * When booking, an invoice is issued to the client for payment. Payment management is handled by a third-party 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 (e.g., 19A) * @param ticketId ticket ID */ @Transactional public void bookSeat(String seatCode, UUID ticketId) { var ticket = ticketRepository.findById(ticketId); //book var seatBooking = new SeatBooking(seatCode, ticket.get().getFlightId(), ticketId, BookingStatus.BOOKED); seatBookingRepository.save(seatBooking); //find base tariff for the selected seat on the plane 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("User found " + 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 invoice paymentClient.sendInvoice(invoice); System.out.println("Invoice issued"); } @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; }

sobes.tech AI

Answer from AI

The provided code contains several errors and potential issues:

  1. Incorrect string comparison: In Java, strings should not be compared using ==, but rather with the .equals() method. In the code:

    if (userData.getTariff() == "PREMIUM") {
        //...
    }
    

    it should be replaced with:

    if ("PREMIUM".equals(userData.getTariff())) {
        //...
    }
    

    Similarly for "ULTRA".

  2. Missing ticket existence check: ticketRepository.findById(ticketId) returns an Optional, but ticket.get() is called immediately without checking if the ticket exists. This can lead to a NoSuchElementException. You should check for presence:

    var ticketOpt = ticketRepository.findById(ticketId);
    if (ticketOpt.isEmpty()) {
        throw new IllegalArgumentException("Ticket not found");
    }
    var ticket = ticketOpt.get();
    
  3. Getting userId from SecurityContext: Casting getPrincipal() to Long may be incorrect, as it usually holds a UserDetails object or another type. You should cast to the correct type or retrieve the ID differently.

  4. Lack of error handling for external services: Calls to tariffClient.getBasePrice(), customerClient.getCustomer(), paymentClient.sendInvoice() may throw exceptions or return null, which are not handled.

  5. Lack of logging: Using System.out.println is not recommended in production; it's better to use a logger.

  6. No seat availability check: There is no verification that the seat with seatCode is available for booking.

  7. No status update after payment: The code does not include logic to change the booking status from BOOKED to PAID.


Example of corrected string comparison:

if ("PREMIUM".equals(userData.getTariff())) {
    price = basePrice * 0.5d;
}
if ("ULTRA".equals(userData.getTariff())) {
    price = basePrice * 0.8d;
}
Read the code and highlight errors. /** * A client… - sobes.tech