Skip to content

14 cs task4 #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 26, 2021
Merged
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/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ set(THIS_PROJECT_SRC_DIRECTORIES
source/Fruit.cpp
source/Alcohol.cpp
source/Item.cpp
source/Store.cpp
source/Time.cpp
)
set(THIS_PROJECT_TESTS_DIRECTORIES
)
Expand Down
2 changes: 1 addition & 1 deletion shm/source/Alcohol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ class Alcohol : public Cargo {

size_t getAmount() const override;

size_t getBasePrice() const override;
size_t getBasePrice() const override;
};
8 changes: 7 additions & 1 deletion shm/source/Cargo.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
#pragma once

#include "Observer.hpp"

#include <string>

class Cargo {
class Cargo : Observer {
protected:
std::string name_;
size_t amount_;
size_t basePrice_;

public:
// Observer:
virtual void nextDay() override = 0;

// Cargo Interface:
Cargo(const std::string& name, const size_t amount, const size_t basePrice);

virtual size_t getPrice() const = 0;
Expand Down
4 changes: 4 additions & 0 deletions shm/source/Fruit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ size_t Fruit::getAmount() const {
size_t Fruit::getBasePrice() const {
return basePrice_;
}

void Fruit::nextDay() {
operator--();
}
4 changes: 3 additions & 1 deletion shm/source/Fruit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ class Fruit : public Cargo {

size_t getAmount() const override;

size_t getBasePrice() const override;
size_t getBasePrice() const override;

void nextDay() override;
};
2 changes: 2 additions & 0 deletions shm/source/Game.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Game.hpp"
#include "Observer.hpp"

Game::Game(const size_t startMoney,
const size_t gameDays,
Expand All @@ -8,6 +9,7 @@ Game::Game(const size_t startMoney,
final_goal_(finalGoal),
current_day_(1),
map_(std::make_unique<Map>()),
time_(std::make_unique<Time>()),
player_(std::make_unique<Player>())
{}

Expand Down
8 changes: 5 additions & 3 deletions shm/source/Game.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#pragma once

#include <iostream>
#include <memory>

#include "Map.hpp"
#include "Player.hpp"
#include "Time.hpp"

#include <iostream>
#include <memory>

class Game {
size_t money_;
Expand All @@ -13,6 +14,7 @@ class Game {

size_t current_day_;
std::unique_ptr<Map> map_;
std::unique_ptr<Time> time_;
std::unique_ptr<Player> player_;

enum class Action {
Expand Down
8 changes: 8 additions & 0 deletions shm/source/Observer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

struct Observer {
virtual void nextDay() = 0;

// virtual ~Observer();
};

13 changes: 7 additions & 6 deletions shm/source/Ship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@
#include <algorithm>

Ship::Ship()
: Ship(0, 0, 0, "", -1)
: Ship(0, 0, 0, "", -1, nullptr)
{}

Ship::Ship(int maxCrew, int speed, size_t id)
: Ship(0, maxCrew, speed, "", id)
: Ship(0, maxCrew, speed, "", id, nullptr)
{}

// Ship::Ship(int capacity, int crew, int speed, std::string name, size_t id, Time* time);

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, Time* time)
: name_(name)
, capacity_(capacity)
, maxCrew_(maxCrew)
, id_(id)
, crew_(0)
, speed_(speed)
, time_(time)
, cargo_()
{}

Expand Down Expand Up @@ -91,4 +90,6 @@ Cargo* Ship::getCargo(const size_t index) const {

// void Ship::RemoveFromStorage(Cargo* cargo);

// void Ship::NextDay() override;
void Ship::nextDay() {

}
17 changes: 8 additions & 9 deletions shm/source/Ship.hpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
#pragma once

#include "Observer.hpp"
#include "Time.hpp"
//#include "Delegate.hpp"

#include <memory>
#include <string>
#include <vector>

//#include "Delegate.hpp"
//#include "Tieme.hpp"

class Cargo;

class Ship {
class Ship : Observer {
std::string name_;
const size_t capacity_;
const size_t maxCrew_;
const size_t id_;
size_t crew_;
size_t speed_;
//Time* time_;
Time* time_;
//Delegate* delegate_;
std::vector<std::unique_ptr<Cargo>> cargo_;

Expand All @@ -25,9 +26,7 @@ class Ship {

Ship(int maxCrew, int speed, size_t id);

// Ship(int capacity, int crew, int speed, std::string name, size_t id, Time* time);

Ship(int capacity, int maxCrew, int speed, const std::string& name, size_t id);
Ship(int capacity, int maxCrew, int speed, const std::string& name, size_t id, Time* time);

// ~Ship() override;

Expand Down Expand Up @@ -61,5 +60,5 @@ class Ship {

// void removeFromStorage(Cargo* cargo);

// void nextDay() override;
void nextDay() override;
};
23 changes: 23 additions & 0 deletions shm/source/Store.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "Store.hpp"

// Store(Time* time);

// ~Store() override;

void nextDay() {

}

// Cargo* GetCargo(const size_t pos);

// Response Buy(Cargo* cargo, size_t amount, Player* player);

// Response Sell(Cargo* caergo, size_t amount, Player* player);

// friend std::ostream& operator<<(std::ostream& out, const Store& store);

// void GenerateCargo();

// Cargo* FindMatchCargo(Cargo* cargo);

// void RemoveFromStore(Cargo* cargo);
43 changes: 43 additions & 0 deletions shm/source/Store.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include "Cargo.hpp"
#include "Player.hpp"
#include "Time.hpp"
#include "Observer.hpp"

#include <memory>
#include <vector>

class Store : Observer {
enum class Response {
Done,
Lack_of_money,
Lack_of_cargo,
Lack_of_space
};

// Time* time_;
// std::vector<std::unique_ptr<Cargo>> cargo_;

public:
// Store(Time* time);

// ~Store() override;

void nextDay() override;

// Cargo* GetCargo(const size_t pos);

// Response Buy(Cargo* cargo, size_t amount, Player* player);

// Response Sell(Cargo* caergo, size_t amount, Player* player);

// friend std::ostream& operator<<(std::ostream& out, const Store& store);

private:
// void GenerateCargo();

// Cargo* FindMatchCargo(Cargo* cargo);

// void RemoveFromStore(Cargo* cargo);
};
37 changes: 37 additions & 0 deletions shm/source/Time.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "Time.hpp"
#include "Observer.hpp"

#include <algorithm>

Time::Time():
timeElapsed_(0),
observers_()
{}

void Time::AddObserver(Observer* obs) {
observers_.push_back(obs);
}

void Time::RemoveObserver(Observer* obs) {
auto it = std::find_if(observers_.begin(), observers_.end(), [obs](const auto* lhs) { return lhs == obs; } );

if (it != observers_.end()) {
std::iter_swap(it, (observers_.end() - 1));
observers_.pop_back();
}
}

size_t Time::GetElpasedTime() const {
return timeElapsed_;
}

Time& Time::operator++() {
++timeElapsed_;
for (auto* observer : observers_) {
observer->nextDay();
}
return *this;
}

// ObserverIT GetObserverIt(Observer* obs);

23 changes: 23 additions & 0 deletions shm/source/Time.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <vector>

class Observer;

class Time {
size_t timeElapsed_;
std::vector<Observer*> observers_;

public:
Time();

void AddObserver(Observer* obs);

void RemoveObserver(Observer* obs);

size_t GetElpasedTime() const;

Time& operator++();

// ObserverIT GetObserverIt(Observer* obs);
};