BookingService.java 文件源码

java
阅读 22 收藏 0 点赞 0 评论 0

项目:git-rekt 作者:
/**
 * Cancels the provided booking, which includes applying the business requirement to assess a
 * cancellation fee on the bill.
 *
 * If provided a booking where the isCanceled field is already set to true, this method simply
 * returns without doing any other work. This behavior can be used to implement custom
 * cancellation logic if desired, and avoids the overhead of having to consult the database to
 * resolve the discrepancy or doing weird stuff to control the field more strictly.
 *
 * @param booking The booking to cancel.
 */
public void cancelBooking(Booking booking) {
    if(booking.isCanceled()) {
        return;
    }
    try {
        entityManager.getTransaction().begin();
        entityManager.merge(booking);
        booking.setCanceled(true);
        double cancellationFee = calcCancellationFee(booking);
        BillItem refund = new BillItem("Refund", booking.getBill().getTotal() * -1, 1);
        booking.getBill().getCharges().add(refund);
        BillItem cancellationCharge = new BillItem("Cancellation fee", cancellationFee, 1);
        booking.getBill().getCharges().add(cancellationCharge);
        sendBookingCancellationEmail(booking);
        entityManager.getTransaction().commit();
    } catch (PersistenceException e) {
        entityManager.getTransaction().rollback();
        throw e;
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号