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: 1 addition & 1 deletion shm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ project(shm)
enable_testing()

# Now simply link against gtest or gtest_main as needed. Eg
add_executable(${PROJECT_NAME} main.cpp Cargo.cpp Ship.cpp Player.cpp)
add_executable(${PROJECT_NAME} main.cpp Cargo.cpp Ship.cpp Store.cpp Player.cpp)
add_executable(${PROJECT_NAME}-ut test.cpp)

target_link_libraries(${PROJECT_NAME}-ut gtest_main)
Expand Down
2 changes: 1 addition & 1 deletion shm/Island.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
#include <iostream>

Island::Island(size_t positionX, size_t positionY)
: position_(Coordinates(positionX, positionY)) {
: position_(Coordinates(positionX, positionY)) {
}
27 changes: 19 additions & 8 deletions shm/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,33 @@

#include <algorithm>

Player::Player(std::shared_ptr<Ship> s = nullptr, size_t m = 0, size_t as = 0)
: ship_(s), money_(m), availableSpace_(as) {}

Cargo* Player::getCargo(size_t index) const {
if (index > (ship_->getCargo().size())) {
if (index > ship_->getCargo().size())
return nullptr;
}
return &ship_->getCargo()[index];
return ship_->getCargo(index).get();
}

void Player::countAvailableSpace() {
auto amoutOfCargo = ship_->getCargo().size();
auto availableSpaceTmp = ship_->getCapacity() - amoutOfCargo;

if (availableSpaceTmp <= 0) {
if(availableSpaceTmp <= 0)
availableSpace_ = 0;
else
availableSpace_ = availableSpaceTmp;
}

void Player::buyCargo(Cargo *cargo) {
auto indexIt = getShip()->findCargo(cargo);
getShip()->getCargo().emplace(indexIt, cargo);
countAvailableSpace();
setMoney(getMoney() - cargo->getPrice() * cargo->getAmount());
}

void Player::sellCargo(Cargo *cargo) {
auto indexIt = getShip()->findCargo(cargo);
if(indexIt->get()->getAmount() == cargo->getAmount()) {
getShip()->getCargo().erase(indexIt);
setMoney(getMoney() + cargo->getAmount() * cargo->getPrice());
}
availableSpace_ = availableSpaceTmp;
}
24 changes: 14 additions & 10 deletions shm/Player.hpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
#pragma once

#include <memory>

#include "Cargo.hpp"
#include "Ship.hpp"
#include "Cargo.hpp"

#include <memory>

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

std::shared_ptr<Ship> getShip() const { return ship_; }
size_t getMoney() const { return money_; }
size_t getAvailableSpace() const { return availableSpace_; }
size_t getMoney() const { return money_; }
size_t getAvailableSpace() const { return availableSpace_; }
size_t getSpeed() const { return ship_->getSpeed(); }
Cargo* getCargo(size_t index) const;

size_t getSpeed() const { return ship_->getSpeed(); }
Cargo* getCargo(size_t index) const;
void setMoney(size_t newMoney) { money_ = newMoney; }
void setAvailableSpace(size_t newAvailableSpace) { availableSpace_ = newAvailableSpace; }
void countAvailableSpace();
void buyCargo(Cargo* cargo);
void sellCargo(Cargo* cargo);

private:
std::shared_ptr<Ship> ship_;
size_t money_;
size_t availableSpace_;

void countAvailableSpace();
};
84 changes: 75 additions & 9 deletions shm/Ship.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,56 @@
#include "Ship.hpp"

#include <iostream>
#include <algorithm>

Ship::Ship()
: id_(-1) {}
: id_(-1) {}

Ship::Ship(int capacity, int maxCrew, int speed, const std::string& name, size_t id)
: capacity_(capacity),
maxCrew_(maxCrew),
crew_(0),
speed_(speed),
name_(name),
id_(id) {}
Ship::Ship(int capacity, int maxCrew, int speed, const std::string& name, size_t id, std::vector<std::shared_ptr<Cargo>> cargo)
: capacity_(capacity),
maxCrew_(maxCrew),
crew_(0),
speed_(speed),
name_(name),
id_(id),
cargo_(cargo){};

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

void Ship::setName(const std::string& name) {
name_ = name;
}

void Ship::Load(const std::shared_ptr<Cargo> &cargo) {
if(capacity_ - cargo->getAmount() < 0){
std::cout << "Not enough space on ship" << std::endl;
return;
}
//set new amount of cargo
auto found = std::find_if(cargo_.begin(), cargo_.end(), [cargo](const auto& car){ return car->getName() == cargo->getName(); });
if(found != cargo_.end()){
(*found)->setAmout((*found)->getAmount() + cargo->getAmount());
}
else{
cargo_.push_back(cargo);
}
capacity_ -= cargo->getAmount();
}

void Ship::Unload(Cargo *cargo) {
auto found = std::find_if(cargo_.begin(), cargo_.end(), [cargo](const auto& car){ return car->getName() == cargo->getName(); });
if(found == cargo_.end()){
std::cout << "No " << cargo->getName() << " on ship";
return;
}
if((*found)->getAmount() - cargo->getAmount() <= 0){
cargo_.erase(found);
}
else{
(*found)->setAmout((*found)->getAmount() - cargo->getAmount());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(*found)->setAmout((*found)->getAmount() - cargo->getAmount());
(*found)->setAmount((*found)->getAmount() - cargo->getAmount());

}
}

Ship& Ship::operator-=(size_t num) {
if (num > crew_) {
Expand All @@ -32,3 +68,33 @@ Ship& Ship::operator+=(size_t num) {
crew_ += num;
return *this;
}

std::vector<std::shared_ptr<Cargo>>::iterator Ship::findCargo(Cargo* cargo) {
return std::find_if(cargo_.begin(), cargo_.end(), [cargo](const auto& cargoTmp){
return cargoTmp->getName() == (*cargo).getName();
});
}

size_t Ship::getCapacity() const {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gettery dublują się w hederze

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nie rozumiem

return capacity_;
}
size_t Ship::getMaxCrew() const {
return maxCrew_;
}
size_t Ship::getSpeed() const {
return speed_;
}
std::string Ship::getName() const {
return name_;
}
size_t Ship::getId() const {
return id_;
}

std::shared_ptr<Cargo> Ship::getCargo(size_t index) const {
return cargo_[index];
}

std::vector<std::shared_ptr<Cargo>>& Ship::getCargo() {
return cargo_;
}
43 changes: 17 additions & 26 deletions shm/Ship.hpp
Original file line number Diff line number Diff line change
@@ -1,41 +1,32 @@
#pragma once

#include "Cargo.hpp"

#include <string>
#include <vector>

#include "Cargo.hpp"
#include <memory>

class Ship {
public:
Ship();
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, std::vector<std::shared_ptr<Cargo>> cargo);
Ship(int maxCrew, int speed, size_t id);

void setName(const std::string& name);
void Load(const std::shared_ptr<Cargo>& cargo);
void Unload(Cargo* cargo);

Ship& operator-=(size_t num);
Ship& operator+=(size_t num);

void setName(const std::string& name) {
name_ = name;
}

size_t getCapacity() const {
return capacity_;
}
size_t getMaxCrew() const {
return maxCrew_;
}
size_t getSpeed() const {
return speed_;
}
std::string getName() const {
return name_;
}
size_t getId() const {
return id_;
}
std::vector<Cargo> getCargo() const {
return cargo_;
}
size_t getCapacity() const;
size_t getMaxCrew() const;
size_t getSpeed() const;
std::string getName() const;
size_t getId() const;
std::vector<std::shared_ptr<Cargo>>& getCargo();
std::shared_ptr<Cargo> getCargo(size_t index) const;
std::vector<std::shared_ptr<Cargo>>::iterator findCargo(Cargo* cargo);

private:
size_t capacity_;
Expand All @@ -44,5 +35,5 @@ class Ship {
size_t speed_;
std::string name_;
const size_t id_;
std::vector<Cargo> cargo_;
std::vector<std::shared_ptr<Cargo>> cargo_;
};
50 changes: 50 additions & 0 deletions shm/Store.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "Store.hpp"

#include <algorithm>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

includy ustawiamy alfabetycznie

#include <iostream>

std::vector<std::shared_ptr<Cargo>>::iterator Store::findCargo(Cargo* cargo) {
return std::find_if(cargo_.begin(), cargo_.end(), [cargo](const auto& cargoTmp){
return cargoTmp->getName() == (*cargo).getName();
});
}

Response Store::buy(Cargo *cargo, Player *player) {
auto indexIt = findCargo(cargo);

if(indexIt == cargo_.end()){
std::cout << "There's no cargo in this store like that" << std::endl;
return Response::no_cargo;
}
if(indexIt->get()->getAmount() < cargo->getAmount()){
std::cout << "Not enough amonut of cargo in this store" << std::endl;
return Response::lack_of_cargo;
}
if(player->getMoney() < cargo->getPrice() * cargo->getAmount()){
std::cout << "Not enough money to buy this cargo" << std::endl;
return Response::lack_of_money;
}
if(player->getAvailableSpace() - cargo->getAmount() < 0){
std::cout << "Not enough space";
return Response::lack_of_space;
}

player->buyCargo(cargo);
return Response::done;
}

Response Store::sell(Cargo *cargo, Player *player) {
auto indexIt = findCargo(cargo);

if(indexIt == player->getShip()->getCargo().end()){
std::cout << "You don't have that cargo to sell" << std::endl;
return Response::no_cargo;
}
if(indexIt->get()->getAmount() < cargo->getAmount()){
std::cout << "Not enough cargo" << std::endl;
return Response::lack_of_cargo;
}

player->sellCargo(cargo);
return Response::done;
}
18 changes: 18 additions & 0 deletions shm/Store.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "Player.hpp"

enum class Response{
done, lack_of_money, lack_of_cargo, lack_of_space, no_cargo
};

class Store {
public:
Response buy(Cargo* cargo, Player* player);
Response sell(Cargo* cargo, Player* player);
std::vector<std::shared_ptr<Cargo>>::iterator findCargo(Cargo* cargo);

private:
std::vector<std::shared_ptr<Cargo>> cargo_;
};

File renamed without changes.