01: package com.uvw.travel.impl;
02:
03: import com.uvw.travel.BookingException;
04: import com.uvw.travel.BookingService;
05: import com.uvw.travel.Bookings;
06: import com.uvw.travel.CreditCard;
07: import org.apache.commons.logging.Log;
08: import org.apache.commons.logging.LogFactory;
09:
10: /**
11: * @author Adrian Price
12: */
13: public class BookingServiceImpl implements BookingService {
14: private static final Log _logger = LogFactory
15: .getLog(BookingService.class);
16: private static final String[] CARDS = { CreditCard.AMEX,
17: CreditCard.MC, CreditCard.VISA };
18:
19: public void placeBooking(Bookings.Booking booking)
20: throws BookingException {
21:
22: if (_logger.isDebugEnabled())
23: _logger.debug("placeBooking(" + booking + ')');
24:
25: validateCreditCard(booking.getCreditCard());
26: switch (booking.getStatus()) {
27: case Bookings.Booking.PENDING:
28: booking.setStatus(Bookings.Booking.SUCCESS);
29: break;
30: default:
31: throw new BookingException("Invalid booking status");
32: }
33: }
34:
35: public void cancelBooking(Bookings.Booking booking)
36: throws BookingException {
37:
38: if (_logger.isDebugEnabled())
39: _logger.debug("cancelBooking(" + booking + ')');
40:
41: switch (booking.getStatus()) {
42: case Bookings.Booking.SUCCESS:
43: booking.setStatus(Bookings.Booking.CANCELLED);
44: break;
45: default:
46: throw new BookingException("Invalid booking status");
47: }
48: }
49:
50: private void validateCreditCard(CreditCard card)
51: throws BookingException {
52: for (int i = 0; i < CARDS.length; i++) {
53: if (card.getCardType().equals(CARDS[i]))
54: return;
55: }
56: throw new BookingException("Invalid credit card type: "
57: + card.getCardType());
58: }
59: }
|