diff --git a/CreatingReliableSoftwareCpp/Presentation/exercises/strategy/main.cpp b/CreatingReliableSoftwareCpp/Presentation/exercises/strategy/main.cpp index 38b7c17..0ddc4e1 100644 --- a/CreatingReliableSoftwareCpp/Presentation/exercises/strategy/main.cpp +++ b/CreatingReliableSoftwareCpp/Presentation/exercises/strategy/main.cpp @@ -15,7 +15,9 @@ int main() { Time time; - Ship ship(&time, "Black Widow", 1000, 40); + auto strat = std::make_unique>(); + + Ship ship(&time, strat, "Black Widow", 1000, 40); ship.load(std::make_unique(300)); ship.load(std::make_unique(200)); ship.load(std::make_unique(150)); @@ -28,11 +30,11 @@ int main() { ship.printCargo(); } - std::cout << "\n\n****************************************************\n"; - auto ship2 = std::make_unique(&time, "Queens Anne revenge", 1000, 40); - Enemy enemy("Enemy1", std::move(ship2)); - - for (int i = 0; i < 15; ++i) { - enemy.attack(ship); - } + // std::cout << "\n\n****************************************************\n"; + // auto ship2 = std::make_unique(&time, "Queens Anne revenge", 1000, 40); + // Enemy enemy("Enemy1", std::move(ship2)); + // + // for (int i = 0; i < 15; ++i) { + // enemy.attack(ship); + // } } diff --git a/CreatingReliableSoftwareCpp/Presentation/exercises/strategy/src/Core/include/Core/ShipStrategy.h b/CreatingReliableSoftwareCpp/Presentation/exercises/strategy/src/Core/include/Core/ShipStrategy.h new file mode 100644 index 0000000..304edf3 --- /dev/null +++ b/CreatingReliableSoftwareCpp/Presentation/exercises/strategy/src/Core/include/Core/ShipStrategy.h @@ -0,0 +1,4 @@ +template +class ShipStrategy { + Res handle(T&); +} diff --git a/CreatingReliableSoftwareCpp/Presentation/exercises/strategy/src/Ship/include/Ship/Ship.h b/CreatingReliableSoftwareCpp/Presentation/exercises/strategy/src/Ship/include/Ship/Ship.h index df72ed4..5174103 100644 --- a/CreatingReliableSoftwareCpp/Presentation/exercises/strategy/src/Ship/include/Ship/Ship.h +++ b/CreatingReliableSoftwareCpp/Presentation/exercises/strategy/src/Ship/include/Ship/Ship.h @@ -5,10 +5,16 @@ #include #include +#include #include "Ship/Cargo.h" class Time; +template +class ShipEasyStrategy : public ShipStrategy { + Res handle(T &t) override; +}; + class Ship : public TimeObserver { public: enum class StatusCode { @@ -17,7 +23,7 @@ public: LackOfSpace }; - Ship(Time* time, const std::string& name, int capacity, int crew); + Ship(Time* time, std::unique_ptr> strategy, const std::string& name, int capacity, int crew); ~Ship(); Ship(const Ship&) = default; Ship(Ship&&) = default; @@ -48,8 +54,10 @@ private: Time* _time; std::string _name; + ShipStrategy _strategy; int _capacity; int _crew; int _durability{1000}; std::vector> _cargoes; }; + diff --git a/CreatingReliableSoftwareCpp/Presentation/exercises/strategy/src/Ship/src/Ship.cpp b/CreatingReliableSoftwareCpp/Presentation/exercises/strategy/src/Ship/src/Ship.cpp index 766f7dd..3d9c11a 100644 --- a/CreatingReliableSoftwareCpp/Presentation/exercises/strategy/src/Ship/src/Ship.cpp +++ b/CreatingReliableSoftwareCpp/Presentation/exercises/strategy/src/Ship/src/Ship.cpp @@ -8,7 +8,7 @@ #include #include -Ship::Ship(Time* time, std::unique_ptr> strategy, const std::string& name, int capacity, int crew) +Ship::Ship(Time* time, std::unique_ptr> strategy, const std::string& name, int capacity, int crew) : _time(time), _strategy(std::move(strategy)), _name(name), _capacity(capacity), _crew(crew) { if (_capacity < 0) { throw std::runtime_error("Capacity can't be negative value!"); @@ -111,4 +111,25 @@ void Ship::rebel() { if (_crew < 5) { throw std::runtime_error("There is not enought crew! Game over!"); } -} \ No newline at end of file +} + +template +bool ShipEasyStrategy::handle(std::string &t) { + int left = _crew; + + while (left != 0) { + auto it = std::ranges::find_if(_cargoes, [&cargoName](const auto& cargo) { return cargo->name() == cargoName; }); + if (it != _cargoes.end()) { + if ((*it)->amount > left) { + (*it)->amount -= left; + return true; + } + left -= (*it)->amount; + _cargoes.erase(it); + } else { + return false; + } + } + + return true; +}