-
Notifications
You must be signed in to change notification settings - Fork 1
#2.1 Store.hpp & Store.cpp #30
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
base: homeworkOOP2
Are you sure you want to change the base?
Changes from all commits
4c04449
5bebca0
23822f8
5b22017
e5fa066
f853d7c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(); | ||
| }; |
| 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()); | ||
| } | ||
| } | ||
|
|
||
| Ship& Ship::operator-=(size_t num) { | ||
| if (num > crew_) { | ||
|
|
@@ -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 { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gettery dublują się w hederze
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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_; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| #include "Store.hpp" | ||
|
|
||
| #include <algorithm> | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| 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_; | ||
| }; | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.