shushus

Спринт 12. Путешествия с RAII. Задание 1.

Apr 6th, 2023
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.19 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. main.cpp
  3. -----------------------------------------------------------------------
  4. #include "trip_manager.h"
  5.  
  6. #include <cassert>
  7. #include <iostream>
  8. #include <stdexcept>
  9.  
  10. using namespace std;
  11.  
  12. int FlightProvider::capacity = 0;
  13. int FlightProvider::counter = 0;
  14.  
  15. int HotelProvider::capacity = 0;
  16. int HotelProvider::counter = 0;
  17.  
  18. void TestNoOverbooking() {
  19.     FlightProvider::capacity = 100;
  20.     HotelProvider::capacity = 100;
  21.     FlightProvider::counter = 0;
  22.     HotelProvider::counter = 0;
  23.     {
  24.         TripManager tm;
  25.         auto trip = tm.Book({});
  26.     }
  27.     assert(FlightProvider::counter == 0);
  28.     assert(HotelProvider::counter == 0);
  29. }
  30.  
  31. void TestFlightOverbooking() {
  32.     FlightProvider::capacity = 1;
  33.     HotelProvider::capacity = 100;
  34.     FlightProvider::counter = 0;
  35.     HotelProvider::counter = 0;
  36.     try {
  37.         TripManager tm;
  38.         auto trip = tm.Book({});
  39.     } catch (const runtime_error&) {
  40.         assert(FlightProvider::counter == 0);
  41.         assert(HotelProvider::counter == 0);
  42.         return;
  43.     }
  44.     cout << "Flight overbooking was expected"s << endl;
  45. }
  46.  
  47. void TestHotelOverbooking() {
  48.     FlightProvider::capacity = 100;
  49.     HotelProvider::capacity = 0;
  50.     FlightProvider::counter = 0;
  51.     HotelProvider::counter = 0;
  52.     try {
  53.         TripManager tm;
  54.         auto trip = tm.Book({});
  55.     } catch (const runtime_error& ex) {
  56.         assert(FlightProvider::counter == 0);
  57.         assert(HotelProvider::counter == 0);
  58.         return;
  59.     }
  60.     cout << "Hotel overbooking was expected"s << endl;
  61. }
  62.  
  63. int main() {
  64.     TestNoOverbooking();
  65.     TestFlightOverbooking();
  66.     TestHotelOverbooking();
  67. }
  68. -----------------------------------------------------------------------
  69. trip_manager.h
  70. -----------------------------------------------------------------------
  71. #pragma once
  72. #include "flight_provider.h"
  73. #include "hotel_provider.h"
  74.  
  75. #include <string>
  76. #include <deque>
  77.  
  78. class Trip {
  79. public:
  80.  
  81.     Trip(HotelProvider& hp, FlightProvider& fp) : hotel_provider(hp), flight_provider(fp) {}
  82.  
  83.     Trip() = default;
  84.     Trip(const Trip&) = default;
  85.     Trip(Trip&&) = default;
  86.     Trip& operator=(const Trip&) = default;
  87.     Trip& operator=(Trip&&) = default;
  88.  
  89.     void add_flights(int value) {
  90.         flights.push_back(value);
  91.     }
  92.  
  93.     void add_hotels(int value) {
  94.         hotels.push_back(value);
  95.     }
  96.  
  97.     void Cancel() {
  98.        
  99.         for (size_t i = 0; i < flights.size(); ++i) {
  100.             flight_provider.Cancel(i);
  101.         }
  102.        
  103.         for (size_t i = 0; i < hotels.size(); ++i) {
  104.             hotel_provider.Cancel(i);
  105.         }
  106.     }
  107.    
  108.     ~Trip() {
  109.         Cancel();
  110.     };
  111.    
  112. private:
  113.     HotelProvider& hotel_provider;
  114.     FlightProvider& flight_provider;
  115.    
  116.     std::deque<HotelProvider::BookingId> hotels;
  117.     std::deque<FlightProvider::BookingId> flights;
  118. };
  119.  
  120. class TripManager {
  121. public:
  122.    
  123.     using BookingId = std::string;
  124.    
  125.     struct BookingData {
  126.         std::string city_from;
  127.         std::string city_to;
  128.         std::string date_from;
  129.         std::string date_to;
  130.     };
  131.  
  132.     Trip Book(const BookingData& data) {
  133.         Trip trip(hotel_provider_, flight_provider_);
  134.         {
  135.             FlightProvider::BookingData flight_booking_data;
  136.            
  137.             flight_booking_data.city_from = data.city_from;
  138.             flight_booking_data.city_to = data.city_to;
  139.             flight_booking_data.date = data.date_from;
  140.            
  141.             trip.add_flights(flight_provider_.Book(flight_booking_data));
  142.         }
  143.        
  144.         {
  145.             HotelProvider::BookingData hotel_booking_data;
  146.            
  147.             hotel_booking_data.city = data.city_to;
  148.             hotel_booking_data.date_to = data.date_to;
  149.             hotel_booking_data.date_from = data.date_from;
  150.            
  151.             trip.add_hotels(hotel_provider_.Book(hotel_booking_data));
  152.         }
  153.        
  154.         {
  155.             FlightProvider::BookingData flight_booking_data;
  156.            
  157.             flight_booking_data.city_from = data.city_from;
  158.             flight_booking_data.city_to = data.city_to;
  159.             flight_booking_data.date = data.date_to;
  160.            
  161.             trip.add_flights(flight_provider_.Book(flight_booking_data));
  162.         }
  163.        
  164.         return trip;
  165.     }
  166.  
  167.     void Cancel(Trip& trip) {
  168.         trip.Cancel();
  169.     }
  170.  
  171. private:
  172.     HotelProvider hotel_provider_;
  173.     FlightProvider flight_provider_;
  174. };
  175. -----------------------------------------------------------------------
  176. hotel_provider.h
  177. -----------------------------------------------------------------------
  178. #pragma once
  179. #include <stdexcept>
  180. #include <string>
  181.  
  182. class HotelProvider {
  183. public:
  184.     using BookingId = int;
  185.  
  186.     struct BookingData {
  187.         std::string city;
  188.         std::string date_from;
  189.         std::string date_to;
  190.     };
  191.  
  192.     BookingId Book(const BookingData&) {
  193.         using namespace std;
  194.         if (counter >= capacity) {
  195.             throw runtime_error("Hotel overbooking"s);
  196.         }
  197.         counter++;
  198.         return counter;
  199.     }
  200.  
  201.     void Cancel(const BookingId&) {
  202.         using namespace std;
  203.         counter--;
  204.         if (counter < 0) {
  205.             counter = 0;
  206.         }
  207.     }
  208.  
  209. public:
  210.     static int capacity;
  211.     static int counter;
  212. };
  213. -----------------------------------------------------------------------
  214. flight_provider.h
  215. -----------------------------------------------------------------------
  216. #pragma once
  217. #include <stdexcept>
  218. #include <string>
  219.  
  220. class FlightProvider {
  221. public:
  222.     using BookingId = int;
  223.  
  224.     struct BookingData {
  225.         std::string city_from;
  226.         std::string city_to;
  227.         std::string date;
  228.     };
  229.  
  230.     BookingId Book(const BookingData&) {
  231.         using namespace std;
  232.         if (counter >= capacity) {
  233.             throw runtime_error("Flight overbooking"s);
  234.         }
  235.         counter++;
  236.         return counter;
  237.     }
  238.     void Cancel(const BookingId&) {
  239.         using namespace std;
  240.         counter--;
  241.         if (counter < 0) {
  242.             counter = 0;
  243.         }
  244.     }
  245.  
  246. public:
  247.     static int capacity;
  248.     static int counter;
  249. };
Advertisement
Add Comment
Please, Sign In to add comment