Advertisement
kutuzzzov

Урок 5-1 путешествия с РАИИ

Jun 5th, 2023
1,909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #pragma once
  2. #include "flight_provider.h"
  3. #include "hotel_provider.h"
  4.  
  5. #include <string>
  6. #include <vector>
  7.  
  8. class Trip {
  9. public:
  10.     Trip(HotelProvider& hp, FlightProvider& fp)
  11.         : hp_(hp)
  12.         , fp_(fp) {
  13.     }
  14.  
  15.     Trip(const Trip& other)
  16.         : hp_(other.hp_)
  17.         , fp_(other.fp_) {
  18.     }
  19.  
  20.     Trip(Trip&& other) noexcept
  21.         : hp_(other.hp_)
  22.         , fp_(other.fp_) {
  23.     }
  24.        
  25.     Trip& operator=(const Trip& other) {
  26.         hp_ = other.hp_;
  27.         fp_ = other.fp_;
  28.         flights = other.flights;
  29.         hotels = other.hotels;
  30.         return *this;
  31.     }
  32.     Trip& operator=(Trip&& other) noexcept{
  33.         hp_ = other.hp_;
  34.         fp_ = other.fp_;
  35.         flights = other.flights;
  36.         hotels = other.hotels;
  37.         return *this;
  38.     }
  39.     void Cancel() {
  40.         for (const auto& flight : flights) {
  41.             fp_.Cancel(flight);
  42.         }
  43.         for (const auto& hotel : hotels) {
  44.             hp_.Cancel(hotel);
  45.         }
  46.     };
  47.     ~Trip() {
  48.         Cancel();
  49.     };
  50.  
  51.     std::vector<int> flights;
  52.     std::vector<int> hotels;
  53.  
  54. private:
  55.     HotelProvider& hp_;
  56.     FlightProvider& fp_;
  57. };
  58.  
  59. class TripManager {
  60. public:
  61.     using BookingId = std::string;
  62.     struct BookingData {
  63.         std::string city_from;
  64.         std::string city_to;
  65.         std::string date_from;
  66.         std::string date_to;
  67.     };
  68.  
  69.     Trip Book(const BookingData& data) {
  70.         Trip trip(hotel_provider_, flight_provider_);
  71.         {
  72.             FlightProvider::BookingData flight_booking_data{data.city_from, data.city_to, data.date_from};
  73.             trip.flights.push_back(flight_provider_.Book(flight_booking_data));
  74.         }
  75.         {
  76.             HotelProvider::BookingData hotel_booking_data{ data.city_to, data.date_from, data.date_to };
  77.             trip.hotels.push_back(hotel_provider_.Book(hotel_booking_data));
  78.         }
  79.         {
  80.             FlightProvider::BookingData flight_booking_data{ data.city_to, data.city_from, data.date_to };
  81.             trip.flights.push_back(flight_provider_.Book(flight_booking_data));
  82.         }
  83.         return trip;
  84.     }
  85.  
  86.     void Cancel(Trip& trip) {
  87.         trip.Cancel();
  88.     }
  89.  
  90. private:
  91.     HotelProvider hotel_provider_;
  92.     FlightProvider flight_provider_;
  93. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement