Advertisement
RobertDeMilo

BB4.16 Разбор задачи 1

Jun 21st, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. #include <iostream>
  2. #include<string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. struct Trip
  8. {
  9.     vector<HotelProvider::BookingId> hotels;
  10.     vector<FlightProvider::BookingId> flights;
  11. };
  12.  
  13. class TripManager
  14. {
  15. public:
  16.     using BookingId = string;
  17.  
  18.     struct BookingData
  19.     {
  20.         string city_from;
  21.         string city_to;
  22.         string date_from;
  23.         string date_to;
  24.     };
  25.    
  26.     Trip Book(const BookingData& data)
  27.     {
  28.         Trip trip;
  29.         {
  30.             FlightProvider::BookingData data;
  31.             trip.flights.push_back(flight_provider.Book(data));
  32.         }
  33.         {
  34.             HotelProvider::BookingData data;
  35.             trip.hotels.push_back(hotel_provider.Book(data));
  36.         }
  37.         {
  38.             FlightProvider::BookingData data;
  39.             trip.flights.push_back(flight_provider.Book(data));
  40.         }
  41.  
  42.  
  43.         return trip;
  44.     }
  45.     void Cancel(Trip& trip)
  46.     {
  47.         for (auto& id : trip.hotels)
  48.         {
  49.             hotel_provider.Cancel(id);
  50.         }
  51.         trip.hotels.clear();
  52.         for (auto& id : trip.flights)
  53.         {
  54.             flight_provider.Cancel(id);
  55.         }
  56.         trip.flights.clear();
  57.     }
  58.  
  59. private:
  60.     HotelProvider hotel_provider;
  61.     FlightProvider flight_provider;
  62.  
  63. };
  64.  
  65. int main()
  66. {
  67.     try
  68.     {
  69.         TripManager tm;
  70.         auto trip = tm.Book({});
  71.         tm.Cancel(trip);
  72.     }
  73.     catch (...)
  74.     {
  75.         cerr << "Exception\n";
  76.     }
  77.    
  78. }
  79.  
  80. ****************************************************************************************************************************************************************************************************************************************
  81. booking_managers.h
  82.  
  83. #pragma once
  84. #include<iostream>
  85. #include<string>
  86. #include<vector>
  87.  
  88. using namespace std;
  89.  
  90. class FlightProvider
  91. {
  92. public:
  93.     using BookingId = int;
  94.  
  95.     struct BookingData
  96.     {
  97.         string city_from;
  98.         string city_to;
  99.         string date;
  100.     };
  101.    
  102.     BookingId Book(const BookingData& data)
  103.     {
  104.         ++counter;
  105.         cerr << "Flight booking: " << counter << "\n";
  106.         return counter;
  107.     }
  108.     void Cancel(const BookingId& id)
  109.     {
  110.         --counter;
  111.         cerr << "Cancel flight: " << id << "\n";
  112.     }
  113.    
  114. private:
  115.     int counter = 0;
  116. };
  117.  
  118. class HotelProvider
  119. {
  120. public:
  121.     using BookingId = int;
  122.  
  123.     struct BookingData
  124.     {
  125.         string city;
  126.         string date_from;
  127.         string date_to;
  128.     };
  129.    
  130.     BookingId Book(const BookingData& data)
  131.     {
  132.         ++counter;
  133.         if (counter > 1)
  134.         {
  135.             throw runtime_error("Overbooking");
  136.         }
  137.         cerr << "Hotel booking: " << counter << "\n";
  138.         return counter;
  139.     }
  140.    
  141.     void Cancel(const BookingId& id)
  142.     {
  143.         --counter;
  144.         cerr << "Cancel flight: " << id << "\n";
  145.     }
  146. private:
  147.     int counter = 0;
  148. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement