diff --git a/shm/Cargo.hpp b/shm/Cargo.hpp index 31896cc5c..cfed02747 100644 --- a/shm/Cargo.hpp +++ b/shm/Cargo.hpp @@ -10,6 +10,8 @@ class Cargo { size_t getAmount() const { return amount_; } size_t getBasePrice() const { return basePrice_; } + virtual void nextDay() = 0; + Cargo& operator+=(size_t amount); Cargo& operator-=(size_t amount); bool operator==(const Cargo& checkCargo) const; diff --git a/shm/Delegate.hpp b/shm/Delegate.hpp new file mode 100644 index 000000000..e0ae9bea3 --- /dev/null +++ b/shm/Delegate.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include + +class Delegate { +public: + virtual void payCrew(size_t money) = 0; + virtual ~Delegate() {} +}; diff --git a/shm/Player.cpp b/shm/Player.cpp index 63f1c116d..5acd66a0c 100644 --- a/shm/Player.cpp +++ b/shm/Player.cpp @@ -11,6 +11,14 @@ Cargo* Player::getCargo(size_t index) const { } return &ship_->getCargo()[index]; } +//Override from Delegate +void Player::payCrew(size_t money) { + if (money_ < money) { + std::cerr << "No maney for papays!\n"; + return; + } + money_ -= money; +} void Player::countAvailableSpace() { auto amoutOfCargo = ship_->getCargo().size(); diff --git a/shm/Player.hpp b/shm/Player.hpp index 2781e9f42..776e09cfa 100644 --- a/shm/Player.hpp +++ b/shm/Player.hpp @@ -5,7 +5,7 @@ #include "Cargo.hpp" #include "Ship.hpp" -class Player { +class Player : public Delegate { public: Player(std::shared_ptr s = nullptr, size_t m = 0, size_t as = 0); @@ -15,6 +15,8 @@ class Player { size_t getSpeed() const { return ship_->getSpeed(); } Cargo* getCargo(size_t index) const; + //Override from Delegate + void payCrew(size_t money) override; private: std::shared_ptr ship_; diff --git a/shm/Ship.cpp b/shm/Ship.cpp index a8ef17bb5..9ad9a2a42 100644 --- a/shm/Ship.cpp +++ b/shm/Ship.cpp @@ -5,16 +5,25 @@ Ship::Ship() : id_(-1) {} -Ship::Ship(int capacity, int maxCrew, int speed, const std::string& name, size_t id) +Ship::Ship(int capacity, + int maxCrew, + int speed, + const std::string& name, + size_t id, + Delegate* delegate) : capacity_(capacity), maxCrew_(maxCrew), crew_(0), speed_(speed), name_(name), - id_(id) {} + id_(id), + payCrew(delegate) {} -Ship::Ship(int maxCrew, int speed, size_t id) - : Ship(0, maxCrew, speed, "", id) {} +Ship::Ship(int maxCrew, + int speed, + size_t id, + Delegate* delegate) + : Ship(0, maxCrew, speed, "", id, delegate) {} Ship& Ship::operator-=(size_t num) { if (num > crew_) { @@ -32,3 +41,7 @@ Ship& Ship::operator+=(size_t num) { crew_ += num; return *this; } +void Ship::nextDay() { + size_t crewCost = 1; + payCrew->payCrew(crew_ * crewCost); +} diff --git a/shm/Ship.hpp b/shm/Ship.hpp index c5ee598e7..656d2c016 100644 --- a/shm/Ship.hpp +++ b/shm/Ship.hpp @@ -4,16 +4,28 @@ #include #include "Cargo.hpp" +#include "Delegate.hpp" class Ship { public: Ship(); - Ship(int capacity, int maxCrew, int speed, const std::string& name, size_t id); - Ship(int maxCrew, int speed, size_t id); + Ship(int capacity, + int maxCrew, + int speed, + const std::string& name, + size_t id, + Delegate* delegate = nullptr); + + Ship(int maxCrew, + int speed, + size_t id, + Delegate* delegate = nullptr); Ship& operator-=(size_t num); Ship& operator+=(size_t num); + void nextDay(); + void setName(const std::string& name) { name_ = name; } @@ -36,6 +48,7 @@ class Ship { std::vector getCargo() const { return cargo_; } + void setDelegate(Delegate* delegate) {payCrew = delegate; } private: size_t capacity_; @@ -45,4 +58,5 @@ class Ship { std::string name_; const size_t id_; std::vector cargo_; + Delegate* payCrew; };