Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions shm/Cargo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions shm/Delegate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include<iostream>

class Delegate {
public:
virtual void payCrew(size_t money) = 0;
virtual ~Delegate() {}
};
8 changes: 8 additions & 0 deletions shm/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion shm/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "Cargo.hpp"
#include "Ship.hpp"

class Player {
class Player : public Delegate {
public:
Player(std::shared_ptr<Ship> s = nullptr, size_t m = 0, size_t as = 0);

Expand All @@ -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> ship_;
Expand Down
21 changes: 17 additions & 4 deletions shm/Ship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_) {
Expand All @@ -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);
}
18 changes: 16 additions & 2 deletions shm/Ship.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,28 @@
#include <vector>

#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;
}
Expand All @@ -36,6 +48,7 @@ class Ship {
std::vector<Cargo> getCargo() const {
return cargo_;
}
void setDelegate(Delegate* delegate) {payCrew = delegate; }

private:
size_t capacity_;
Expand All @@ -45,4 +58,5 @@ class Ship {
std::string name_;
const size_t id_;
std::vector<Cargo> cargo_;
Delegate* payCrew;
};