From 62a6f1c123e9beb6b2a01e374e7d03c2e428070f Mon Sep 17 00:00:00 2001 From: Kacu Date: Mon, 26 Jul 2021 17:14:59 +0200 Subject: [PATCH 1/5] 20 created new class Fruit --- shm/CMakeLists.txt | 1 + shm/source/Fruit.cpp | 28 ++++++++++++++++++++++++++++ shm/source/Fruit.hpp | 23 +++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 shm/source/Fruit.cpp create mode 100644 shm/source/Fruit.hpp diff --git a/shm/CMakeLists.txt b/shm/CMakeLists.txt index b8f6b0a53..2ab0ec0ba 100644 --- a/shm/CMakeLists.txt +++ b/shm/CMakeLists.txt @@ -16,6 +16,7 @@ set(THIS_PROJECT_SRC_DIRECTORIES source/Cargo.cpp source/Player.cpp source/Ship.cpp + source/Fruit.cpp ) set(THIS_PROJECT_TESTS_DIRECTORIES ) diff --git a/shm/source/Fruit.cpp b/shm/source/Fruit.cpp new file mode 100644 index 000000000..659b6df1e --- /dev/null +++ b/shm/source/Fruit.cpp @@ -0,0 +1,28 @@ +#include "Fruit.hpp" + +Fruit::Fruit(const std::string& name, const size_t amount, const size_t basePrice): + Cargo(name, amount, basePrice), + maxFreshness_(10), + freshness_(maxFreshness_) +{} + +Fruit& Fruit::operator--() { + freshness_ = (freshness_ > 0)? freshness_ - 1 : 0; + return *this; +} + +size_t Fruit::getPrice() const { + return static_cast(static_cast(getBasePrice()) * (freshness_ / maxFreshness_)); +} + +std::string Fruit::getName() const { + return "Fruit: " + name_; +} + +size_t Fruit::getAmount() const { + return amount_; +} + +size_t Fruit::getBasePrice() const { + return basePrice_; +} diff --git a/shm/source/Fruit.hpp b/shm/source/Fruit.hpp new file mode 100644 index 000000000..83cae07f0 --- /dev/null +++ b/shm/source/Fruit.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "Cargo.hpp" + +#include + +class Fruit : public Cargo { + const double maxFreshness_; + double freshness_; + +public: + Fruit(const std::string& name, const size_t amount, const size_t basePrice); + + Fruit& operator--(); + + size_t getPrice() const override; + + std::string getName() const override; + + size_t getAmount() const override; + + size_t getBasePrice() const override; +}; From e2572b7ecbdc4c20bfb2df58841d215f9024c536 Mon Sep 17 00:00:00 2001 From: Kacu Date: Mon, 26 Jul 2021 17:22:01 +0200 Subject: [PATCH 2/5] 20 created new class Alcohol --- shm/CMakeLists.txt | 1 + shm/source/Alcohol.cpp | 22 ++++++++++++++++++++++ shm/source/Alcohol.hpp | 20 ++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 shm/source/Alcohol.cpp create mode 100644 shm/source/Alcohol.hpp diff --git a/shm/CMakeLists.txt b/shm/CMakeLists.txt index 2ab0ec0ba..744df4490 100644 --- a/shm/CMakeLists.txt +++ b/shm/CMakeLists.txt @@ -17,6 +17,7 @@ set(THIS_PROJECT_SRC_DIRECTORIES source/Player.cpp source/Ship.cpp source/Fruit.cpp + source/Alcohol.cpp ) set(THIS_PROJECT_TESTS_DIRECTORIES ) diff --git a/shm/source/Alcohol.cpp b/shm/source/Alcohol.cpp new file mode 100644 index 000000000..14df22415 --- /dev/null +++ b/shm/source/Alcohol.cpp @@ -0,0 +1,22 @@ +#include "Alcohol.hpp" + +Alcohol::Alcohol(const std::string& name, const size_t amount, const size_t basePrice, const size_t voltage): + Cargo(name, amount, basePrice), + voltage_(voltage) +{} + +size_t Alcohol::getPrice() const { + return static_cast(static_cast(getBasePrice()) * (static_cast(voltage_) / 96.)); +} + +std::string Alcohol::getName() const { + return "Alcohol: " + name_; +} + +size_t Alcohol::getAmount() const { + return amount_; +} + +size_t Alcohol::getBasePrice() const { + return basePrice_; +} diff --git a/shm/source/Alcohol.hpp b/shm/source/Alcohol.hpp new file mode 100644 index 000000000..a0aafeb69 --- /dev/null +++ b/shm/source/Alcohol.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include "Cargo.hpp" + +#include + +class Alcohol : public Cargo { + size_t voltage_; + +public: + Alcohol(const std::string& name, const size_t amount, const size_t basePrice, const size_t voltage); + + size_t getPrice() const override; + + std::string getName() const override; + + size_t getAmount() const override; + + size_t getBasePrice() const override; +}; From 64e3de6c21e3106643f55302308795933817121b Mon Sep 17 00:00:00 2001 From: Kacu Date: Mon, 26 Jul 2021 17:32:26 +0200 Subject: [PATCH 3/5] 20 created new class Item --- shm/CMakeLists.txt | 1 + shm/source/Item.cpp | 35 +++++++++++++++++++++++++++++++++++ shm/source/Item.hpp | 28 ++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 shm/source/Item.cpp create mode 100644 shm/source/Item.hpp diff --git a/shm/CMakeLists.txt b/shm/CMakeLists.txt index 744df4490..b3e497b8c 100644 --- a/shm/CMakeLists.txt +++ b/shm/CMakeLists.txt @@ -18,6 +18,7 @@ set(THIS_PROJECT_SRC_DIRECTORIES source/Ship.cpp source/Fruit.cpp source/Alcohol.cpp + source/Item.cpp ) set(THIS_PROJECT_TESTS_DIRECTORIES ) diff --git a/shm/source/Item.cpp b/shm/source/Item.cpp new file mode 100644 index 000000000..4b51c0648 --- /dev/null +++ b/shm/source/Item.cpp @@ -0,0 +1,35 @@ +#include "Item.hpp" + +Item::Item(const std::string& name, const size_t amount, const size_t basePrice, const Rarity rarity): + Cargo(name, amount, basePrice), + rarity_(rarity) +{} + +size_t Item::getPrice() const { + switch (rarity_) { + case Rarity::Common: + return getBasePrice() / 3; + + case Rarity::Rare: + return getBasePrice() / 2; + + case Rarity::Epic: + return getBasePrice(); + + case Rarity::Legendary: + return getBasePrice() * 4; + } + return 0; +} + +std::string Item::getName() const { + return "Item: " + name_; +} + +size_t Item::getAmount() const { + return amount_; +} + +size_t Item::getBasePrice() const { + return basePrice_; +} diff --git a/shm/source/Item.hpp b/shm/source/Item.hpp new file mode 100644 index 000000000..dca03f1f2 --- /dev/null +++ b/shm/source/Item.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include "Cargo.hpp" + +#include + +struct Item : public Cargo { + enum class Rarity { + Common, + Rare, + Epic, + Legendary + }; + +private: + Rarity rarity_; + +public: + Item(const std::string& name, const size_t amount, const size_t basePrice, const Rarity rarity); + + size_t getPrice() const override; + + std::string getName() const override; + + size_t getAmount() const override; + + size_t getBasePrice() const override; +}; From 00e61ce315cc589f8f646a4b32a62d7d9e0e35aa Mon Sep 17 00:00:00 2001 From: Kacu Date: Mon, 26 Jul 2021 17:34:24 +0200 Subject: [PATCH 4/5] 20 turned Cargo class into interface --- shm/source/Cargo.cpp | 6 ------ shm/source/Cargo.hpp | 15 +++++++++------ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/shm/source/Cargo.cpp b/shm/source/Cargo.cpp index 84158b27b..4993fb2b0 100644 --- a/shm/source/Cargo.cpp +++ b/shm/source/Cargo.cpp @@ -21,9 +21,3 @@ Cargo& Cargo::operator-=(size_t amount) { amount_ = (amount_ > amount)? amount_ - amount : 0; return *this; } - -std::string Cargo::getName() const { return name_; } - -size_t Cargo::getAmount() const { return amount_; } - -size_t Cargo::getBasePrice() const { return basePrice_; } diff --git a/shm/source/Cargo.hpp b/shm/source/Cargo.hpp index bd82c8bc6..7d37e0912 100644 --- a/shm/source/Cargo.hpp +++ b/shm/source/Cargo.hpp @@ -3,6 +3,7 @@ #include class Cargo { +protected: std::string name_; size_t amount_; size_t basePrice_; @@ -10,15 +11,17 @@ class Cargo { public: Cargo(const std::string& name, const size_t amount, const size_t basePrice); - bool operator==(const Cargo& cargo) const; + virtual size_t getPrice() const = 0; - Cargo& operator+=(size_t amount); + virtual std::string getName() const = 0; - Cargo& operator-=(size_t amount); + virtual size_t getAmount() const = 0; - std::string getName() const; + virtual size_t getBasePrice() const = 0; - size_t getAmount() const; + bool operator==(const Cargo& cargo) const; - size_t getBasePrice() const; + Cargo& operator+=(size_t amount); + + Cargo& operator-=(size_t amount); }; From c0eaec4f29be40aa4af02fed97ee209a5f3625fc Mon Sep 17 00:00:00 2001 From: Kacu Date: Mon, 26 Jul 2021 17:48:52 +0200 Subject: [PATCH 5/5] 20 created load and unload operations inside Ship class --- shm/source/Ship.cpp | 17 ++++++++++++++--- shm/source/Ship.hpp | 10 +++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/shm/source/Ship.cpp b/shm/source/Ship.cpp index bd838b355..61ac64fb8 100644 --- a/shm/source/Ship.cpp +++ b/shm/source/Ship.cpp @@ -65,9 +65,20 @@ std::vector>& Ship::getCargo() { // void Ship::printCargo() const; -// void Ship::load(std::unique_ptr cargo); +void Ship::load(std::unique_ptr cargo) { + cargo_.push_back(std::move(cargo)); +} -// void Ship::unload(Cargo* cargo); +void Ship::unload(Cargo* cargo) { + if (!cargo) { + return; + } + auto result = std::find_if(cargo_.begin(), cargo_.end(), [cargo](const auto& el) { return el.get() == cargo; } ); + if (result != cargo_.end()) { + std::iter_swap(result, (cargo_.end() - 1)); + cargo_.pop_back(); + } +} Cargo* Ship::getCargo(const size_t index) const { if (index < cargo_.size()) { @@ -76,7 +87,7 @@ Cargo* Ship::getCargo(const size_t index) const { return nullptr; } -// Cargo* Ship::FindMatchCargo(Cargo* cargo); +// Cargo* Ship::findMatchCargo(Cargo* cargo); // void Ship::RemoveFromStorage(Cargo* cargo); diff --git a/shm/source/Ship.hpp b/shm/source/Ship.hpp index 68dc97a07..b5cf014da 100644 --- a/shm/source/Ship.hpp +++ b/shm/source/Ship.hpp @@ -51,15 +51,15 @@ class Ship { // void printCargo() const; - // void load(std::unique_ptr cargo); + void load(std::unique_ptr cargo); - // void unload(Cargo* cargo); + void unload(Cargo* cargo); Cargo* getCargo(const size_t index) const; - // Cargo* FindMatchCargo(Cargo* cargo); + // Cargo* findMatchCargo(Cargo* cargo); - // void RemoveFromStorage(Cargo* cargo); + // void removeFromStorage(Cargo* cargo); - // void NextDay() override; + // void nextDay() override; };