35657

Untitled

Mar 15th, 2023
1,012
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include <cassert>
  2. #include <string>
  3. #include <stdexcept>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. class House {
  9.    
  10. public:
  11.  
  12.     House(int lenght, int width, int height) : lenght_(lenght), width_(width), height_(height) {}
  13.    
  14.     int GetLength() {
  15.         return lenght_;
  16.     }
  17.    
  18.     int GetWidth() {
  19.         return width_;
  20.     }
  21.    
  22.     int GetHeight() {
  23.         return height_;
  24.     }
  25.    
  26. private:
  27.     int lenght_;
  28.     int width_;
  29.     int height_;
  30. };
  31.  
  32. class Resources {
  33.    
  34. public:
  35.  
  36.     Resources(int brick_count) : brick_count_(brick_count) {}
  37.    
  38.     void TakeBricks(int brick_count) {
  39.         if (brick_count_ < brick_count) {
  40.             throw std::out_of_range("Столько нет"s);
  41.         }
  42.         brick_count_ -= brick_count;
  43.     }
  44.    
  45.     int GetBrickCount () {
  46.         return brick_count_;
  47.     }
  48.    
  49. private:
  50.     int brick_count_;
  51. };
  52.  
  53. struct HouseSpecification {
  54.     int length = 0;
  55.     int width = 0;
  56.     int height = 0;
  57. };
  58.  
  59. class Builder {
  60.  
  61. public:
  62.     Builder(Resources& resources) : resources_(resources) {}
  63.    
  64.     House BuildHouse(HouseSpecification spec) {
  65.         try {
  66.             resources_.TakeBricks(((spec.length + spec.width) * 8) * (spec.height * 8));
  67.             return House(spec.length, spec.width, spec.height);
  68.         } catch(const std::out_of_range& error) {
  69.             throw std::runtime_error(error.what());
  70.         }
  71.     }
  72.    
  73. private:
  74.     Resources& resources_;
  75. };
  76.  
  77. int main() {
  78.     Resources resources{10000};
  79.     Builder builder1{resources};
  80.     Builder builder2{resources};
  81.  
  82.     House house1 = builder1.BuildHouse(HouseSpecification{12, 9, 3});
  83.     assert(house1.GetLength() == 12);
  84.     assert(house1.GetWidth() == 9);
  85.     assert(house1.GetHeight() == 3);
  86.     cout << resources.GetBrickCount() << " bricks left"s << endl;
  87.  
  88.     House house2 = builder2.BuildHouse(HouseSpecification{8, 6, 3});
  89.     assert(house2.GetLength() == 8);
  90.     cout << resources.GetBrickCount() << " bricks left"s << endl;
  91.  
  92.     House banya = builder1.BuildHouse(HouseSpecification{4, 3, 2});
  93.     assert(banya.GetHeight() == 2);
  94.     cout << resources.GetBrickCount() << " bricks left"s << endl;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment