Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cassert>
- #include <string>
- #include <stdexcept>
- #include <iostream>
- using namespace std;
- class House {
- public:
- House(int lenght, int width, int height) : lenght_(lenght), width_(width), height_(height) {}
- int GetLength() {
- return lenght_;
- }
- int GetWidth() {
- return width_;
- }
- int GetHeight() {
- return height_;
- }
- private:
- int lenght_;
- int width_;
- int height_;
- };
- class Resources {
- public:
- Resources(int brick_count) : brick_count_(brick_count) {}
- void TakeBricks(int brick_count) {
- if (brick_count_ < brick_count) {
- throw std::out_of_range("Столько нет"s);
- }
- brick_count_ -= brick_count;
- }
- int GetBrickCount () {
- return brick_count_;
- }
- private:
- int brick_count_;
- };
- struct HouseSpecification {
- int length = 0;
- int width = 0;
- int height = 0;
- };
- class Builder {
- public:
- Builder(Resources& resources) : resources_(resources) {}
- House BuildHouse(HouseSpecification spec) {
- try {
- resources_.TakeBricks(((spec.length + spec.width) * 8) * (spec.height * 8));
- return House(spec.length, spec.width, spec.height);
- } catch(const std::out_of_range& error) {
- throw std::runtime_error(error.what());
- }
- }
- private:
- Resources& resources_;
- };
- int main() {
- Resources resources{10000};
- Builder builder1{resources};
- Builder builder2{resources};
- House house1 = builder1.BuildHouse(HouseSpecification{12, 9, 3});
- assert(house1.GetLength() == 12);
- assert(house1.GetWidth() == 9);
- assert(house1.GetHeight() == 3);
- cout << resources.GetBrickCount() << " bricks left"s << endl;
- House house2 = builder2.BuildHouse(HouseSpecification{8, 6, 3});
- assert(house2.GetLength() == 8);
- cout << resources.GetBrickCount() << " bricks left"s << endl;
- House banya = builder1.BuildHouse(HouseSpecification{4, 3, 2});
- assert(banya.GetHeight() == 2);
- cout << resources.GetBrickCount() << " bricks left"s << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment