Skip to content

Commit 5e80e78

Browse files
Merge pull request coders-school#85 from lukasz-kukulka/fix_delegate
Fixed patern delegate add two function in ship and player class
2 parents 351f1e2 + 5804bfc commit 5e80e78

File tree

8 files changed

+20
-2
lines changed

8 files changed

+20
-2
lines changed

shm/inc/Delegate.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
class Delegate {
66
public:
77
virtual void payCrew(const size_t payCrew) = 0;
8-
virtual ~Delegate();
8+
virtual ~Delegate() {};
99
};

shm/inc/Game.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
22

33
#include <stddef.h>
4+
#include "shm/inc/Ship.hpp"
5+
#include "shm/inc/Player.hpp"
46

57
class Game {
68
public:

shm/inc/Player.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Cargo;
1111
class Player : public Delegate {
1212
public:
1313
Player(std::unique_ptr<Ship> ship, size_t money, size_t availableSpace);
14+
~Player() override {};
1415

1516
std::unique_ptr<Ship> getShip() { return std::move(ship_); };
1617
size_t getMoney() const { return money_; };
@@ -20,6 +21,7 @@ class Player : public Delegate {
2021

2122
//override from Delegate
2223
void payCrew(const size_t payCrew) override;
24+
void setPlayerPtr();
2325

2426
private:
2527
std::unique_ptr<Ship> ship_;

shm/inc/Ship.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
class Cargo;
1010
class Delegate;
11+
class Player;
1112

1213
class Ship : public Subscriber {
1314
public:
1415
Ship() = default;
15-
Ship(int id, const std::string& name, size_t speed, size_t maxCrew, size_t capacity, Delegate* delegate);
16+
Ship(int id, const std::string& name, size_t speed, size_t maxCrew, size_t capacity, Delegate* delegate = nullptr);
1617
Ship(int id, size_t speed, size_t maxCrew, Delegate* delegate);
1718

1819
Ship& operator+=(const size_t amount);
@@ -29,6 +30,7 @@ class Ship : public Subscriber {
2930
void setName(const std::string& name);
3031
void load(const std::shared_ptr<Cargo>);
3132
void unload(Cargo* cargo);
33+
void changeDelegate(Player* player);
3234

3335
// overload form Subscriber
3436
void nextDay() override;

shm/src/Game.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "shm/inc/Game.hpp"
2+
#include <iostream>
3+
#include <memory>
24

35
Game::Game(size_t money, size_t game_days, size_t final_goal)
46
: money_(money)

shm/src/Player.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ void Player::payCrew(const size_t payCrew) {
3333
? money_ = 0
3434
: money_ -= payCrew;
3535
}
36+
37+
void Player::setPlayerPtr() {
38+
ship_->changeDelegate(this);
39+
}

shm/src/Ship.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "shm/inc/Cargo.hpp"
88
#include "shm/inc/Delegate.hpp"
9+
#include "shm/inc/Player.hpp"
910

1011
Ship::Ship(int id, const std::string& name, size_t speed, size_t maxCrew, size_t capacity, Delegate* delegate)
1112
: id_(id)
@@ -88,3 +89,7 @@ void Ship::unload(Cargo* cargo) {
8889
void Ship::nextDay() {
8990
delegate_->payCrew(crew_);
9091
}
92+
93+
void Ship::changeDelegate(Player* player) {
94+
delegate_ = player;
95+
}

shm/src/Store.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ void Store::nextDay() {
4141
cargo->setAmount(distribution(generator));
4242
}
4343
}
44+

0 commit comments

Comments
 (0)