shushus

Спринт 11. Отношения между классами.

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