|
| 1 | +package facade.facade; |
| 2 | + |
| 3 | +import facade.dto.TripDetailsDto; |
| 4 | +import facade.service.FlightBookingService; |
| 5 | +import facade.service.HotelBookingService; |
| 6 | +import facade.service.PaymentService; |
| 7 | +import facade.service.TaxiBookingService; |
| 8 | +import facade.service.impl.FlightBookingServiceImpl; |
| 9 | +import facade.service.impl.HotelBookingServiceImpl; |
| 10 | +import facade.service.impl.PaymentServiceImpl; |
| 11 | +import facade.service.impl.TaxiBookingServiceImpl; |
| 12 | + |
| 13 | +public class TravelFacade { |
| 14 | + private final FlightBookingService flightService; |
| 15 | + private final HotelBookingService hotelService; |
| 16 | + private final TaxiBookingService taxiService; |
| 17 | + private final PaymentService paymentService; |
| 18 | + private final TripDetailsDto trip; |
| 19 | + private double totalCost; |
| 20 | + |
| 21 | + private TravelFacade(Builder builder) { |
| 22 | + this.flightService = new FlightBookingServiceImpl(); |
| 23 | + this.hotelService = new HotelBookingServiceImpl(); |
| 24 | + this.taxiService = new TaxiBookingServiceImpl(); |
| 25 | + this.paymentService = new PaymentServiceImpl(); |
| 26 | + this.trip = builder.trip; |
| 27 | + } |
| 28 | + |
| 29 | + public static class Builder { |
| 30 | + private TripDetailsDto trip; |
| 31 | + |
| 32 | + public Builder trip(TripDetailsDto trip) { |
| 33 | + this.trip = trip; |
| 34 | + return this; |
| 35 | + } |
| 36 | + |
| 37 | + public TravelFacade build() { |
| 38 | + if (trip == null) { |
| 39 | + throw new IllegalStateException("TripDetails must be provided"); |
| 40 | + } |
| 41 | + return new TravelFacade(this); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public void bookAll() { |
| 46 | + System.out.println("Start the itinerary booking process..."); |
| 47 | + // 1. Flight |
| 48 | + String flightConf = flightService.bookFlight( |
| 49 | + trip.getPassengerName(), trip.getOrigin(), trip.getDestination(), trip.getDepartureDate()); |
| 50 | + totalCost += 500.0; |
| 51 | + |
| 52 | + // 2. Hotel |
| 53 | + String hotelConf = hotelService.bookHotel( |
| 54 | + trip.getPassengerName(), trip.getDestination(), trip.getDepartureDate(), trip.getReturnDate()); |
| 55 | + totalCost += 300.0; |
| 56 | + |
| 57 | + // 3. Taxi |
| 58 | + String taxiConf = taxiService.bookCar( |
| 59 | + trip.getPassengerName(), trip.getDestination(), trip.getDepartureDate(), trip.getReturnDate()); |
| 60 | + totalCost += 200.0; |
| 61 | + |
| 62 | + // 4. Pay |
| 63 | + boolean paid = paymentService.processPayment(trip.getPassengerName(), totalCost); |
| 64 | + if (!paid) { |
| 65 | + throw new RuntimeException("Payment failed, booking cancelled"); |
| 66 | + } |
| 67 | + |
| 68 | + System.out.println("Booking completed! The following is the confirmation information:"); |
| 69 | + System.out.println("Flight info: " + flightConf); |
| 70 | + System.out.println("Hotel info: " + hotelConf); |
| 71 | + System.out.println("Taxi info: " + taxiConf); |
| 72 | + System.out.printf("Total cost: %.2f%n", totalCost); |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments