Skip to content
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

20 create derived classes from class cargo #26

Merged
merged 5 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
3 changes: 3 additions & 0 deletions shm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ set(THIS_PROJECT_SRC_DIRECTORIES
source/Cargo.cpp
source/Player.cpp
source/Ship.cpp
source/Fruit.cpp
source/Alcohol.cpp
source/Item.cpp
)
set(THIS_PROJECT_TESTS_DIRECTORIES
)
Expand Down
22 changes: 22 additions & 0 deletions shm/source/Alcohol.cpp
Original file line number Diff line number Diff line change
@@ -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<size_t>(static_cast<double>(getBasePrice()) * (static_cast<double>(voltage_) / 96.));
}

std::string Alcohol::getName() const {
return "Alcohol: " + name_;
}

size_t Alcohol::getAmount() const {
return amount_;
}

size_t Alcohol::getBasePrice() const {
return basePrice_;
}
20 changes: 20 additions & 0 deletions shm/source/Alcohol.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include "Cargo.hpp"

#include <string>

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;
};
6 changes: 0 additions & 6 deletions shm/source/Cargo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_; }
15 changes: 9 additions & 6 deletions shm/source/Cargo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@
#include <string>

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

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);
};
28 changes: 28 additions & 0 deletions shm/source/Fruit.cpp
Original file line number Diff line number Diff line change
@@ -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<size_t>(static_cast<double>(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_;
}
23 changes: 23 additions & 0 deletions shm/source/Fruit.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include "Cargo.hpp"

#include <string>

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;
};
35 changes: 35 additions & 0 deletions shm/source/Item.cpp
Original file line number Diff line number Diff line change
@@ -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_;
}
28 changes: 28 additions & 0 deletions shm/source/Item.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include "Cargo.hpp"

#include <string>

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;
};
17 changes: 14 additions & 3 deletions shm/source/Ship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,20 @@ std::vector<std::unique_ptr<Cargo>>& Ship::getCargo() {

// void Ship::printCargo() const;

// void Ship::load(std::unique_ptr<Cargo> cargo);
void Ship::load(std::unique_ptr<Cargo> 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()) {
Expand All @@ -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);

Expand Down
10 changes: 5 additions & 5 deletions shm/source/Ship.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ class Ship {

// void printCargo() const;

// void load(std::unique_ptr<Cargo> cargo);
void load(std::unique_ptr<Cargo> 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;
};