From b3588517e29db8ac2d83dbd2327f2e9f9d2e91f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Ziobro=C5=84?= Date: Fri, 11 Jun 2021 19:12:31 +0200 Subject: [PATCH 01/25] fix #30 --- module2/presentation_static.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/module2/presentation_static.md b/module2/presentation_static.md index 803b4f6fb..736e23f0f 100644 --- a/module2/presentation_static.md +++ b/module2/presentation_static.md @@ -51,21 +51,21 @@ ___ Rozwiązaniem tej uciążliwości jest `static`. Co więcej, problem ten możemy rozwiązać na 2 sposoby. Nie musimy w ten sposób tworzyć specjalnie obiektu, aby dostać się do cechy klasy, jaką jest jej nazwa. ```cpp -class ObjectA { +class ClassA { public: - static std::string getName() { return "ObjectA"; } + static std::string getName() { return "ClassA"; } }; -class ObjectB { +class ClassB { public: static std::string name_; }; -std::string ObjectB::name_{"ObjectB"}; +std::string ClassB::name_{"ClassB"}; int main() { - std::cout << ObjectA::getName() << '\n'; - std::cout << ObjectB::name_ << '\n'; + std::cout << ClassA::getName() << '\n'; + std::cout << ClassB::name_ << '\n'; return 0; } From c5340c88e6394dd379916f1ffd511a3be4c9fb18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Ziobro=C5=84?= Date: Fri, 11 Jun 2021 19:13:54 +0200 Subject: [PATCH 02/25] fix #84 --- module2/presentation_static.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module2/presentation_static.md b/module2/presentation_static.md index 736e23f0f..e73fe4a05 100644 --- a/module2/presentation_static.md +++ b/module2/presentation_static.md @@ -11,7 +11,7 @@ ___ -## "Zmienna lub stała klasy" +## Zmienna lub stała klasy Czasami chcielibyśmy przypisać jakąś stałą cechę do klasy. Nie konkretnych obiektów, a klasy samej w sobie. From 220bf13cf12af434185d53afeca47742b26d8ff2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Ziobro=C5=84?= Date: Fri, 11 Jun 2021 19:25:31 +0200 Subject: [PATCH 03/25] fix #88, module3 almost removed --- module2/presentation_polymorphism.md | 10 +- module2/presentation_solutions.md | 12 +- module2/presentation_virtual.md | 6 +- module3/index.html | 134 ---------- module3/presentation_homework.md | 148 ----------- module3/presentation_polymorphism.md | 367 --------------------------- module3/presentation_solutions.md | 267 ------------------- module3/presentation_static.md | 91 ------- 8 files changed, 15 insertions(+), 1020 deletions(-) delete mode 100644 module3/index.html delete mode 100644 module3/presentation_homework.md delete mode 100644 module3/presentation_polymorphism.md delete mode 100644 module3/presentation_solutions.md delete mode 100644 module3/presentation_static.md diff --git a/module2/presentation_polymorphism.md b/module2/presentation_polymorphism.md index 5e46c6f19..1612473c6 100644 --- a/module2/presentation_polymorphism.md +++ b/module2/presentation_polymorphism.md @@ -110,7 +110,7 @@ ___ `override` jest opcjonalne. Jeśli go nie podamy za sygnaturą funkcji klasy pochodnej to metoda z klasy bazowej i tak zostanie nadpisana. -Jego użycie jest jednak dobrą praktyką, bo dzięki niemu kompilator sprawdzi czy faktycznie nadpisujemy metodę z klasy bazowej i jeśli nie, to program się nie skompiluje. +Jego użycie jest jednak dobrą praktyką, bo dzięki niemu kompilator sprawdzi, czy faktycznie nadpisujemy metodę z klasy bazowej i jeśli nie, to program się nie skompiluje. Bez `override` mogłaby zostać utworzona nowa metoda w klasie pochodnej, która nie nadpisuje niczego z klasy bazowej. @@ -230,22 +230,22 @@ ___ ```cpp class Doppler { public: - virtual sayHello() { std::cout << "I'm Doppler!"; } + virtual void sayHello() { std::cout << "I'm Doppler!"; } }; class Dwarf : public Doppler { public: - virtual sayHello() { std::cout << "I'm Dwarf!"; } + virtual void sayHello() { std::cout << "I'm Dwarf!"; } }; class Elf : public Doppler { public: - virtual sayHello() { std::cout << "I'm Elf!"; } + virtual void sayHello() { std::cout << "I'm Elf!"; } }; class Human : public Doppler { public: - virtual sayHello() { std::cout << "I'm Human!"; } + virtual void sayHello() { std::cout << "I'm Human!"; } }; int main() { diff --git a/module2/presentation_solutions.md b/module2/presentation_solutions.md index 708e7969e..e090368cf 100644 --- a/module2/presentation_solutions.md +++ b/module2/presentation_solutions.md @@ -10,6 +10,12 @@ ___ +## Disclaimer + +W PDFie te rozwiązania mogą być częściowo ucięte. Przejdź do lekcji na platformie lub na GitHubie, aby zobaczyć je w całości. + +___ + ### Zadanie 1 Cargo.hpp @@ -247,11 +253,11 @@ Cargo* Player::getCargo(size_t index) const { } size_t Player::getAvailableSpace() const { - size_t total_cargo_amount = 0; + available_space_ = 0; for (const auto cargo : ship_->getCargos()) { - total_cargo_amount += cargo->getAmount(); + available_space_ += cargo->getAmount(); } - available_space_ = ship_->getCapacity() - total_cargo_amount; + return available_space_; } ``` diff --git a/module2/presentation_virtual.md b/module2/presentation_virtual.md index 44a01f5d6..d7b237865 100644 --- a/module2/presentation_virtual.md +++ b/module2/presentation_virtual.md @@ -82,16 +82,12 @@ public: }; class Hummingbird : public Bird, - public Flyable, - public Soundable { + public Flyable { public: // Override from Bird void eat() override; void sleep() override; - // Override from Soundable - void makeSound() override; - // Override from Flyable void fly() override; }; diff --git a/module3/index.html b/module3/index.html deleted file mode 100644 index 1712af944..000000000 --- a/module3/index.html +++ /dev/null @@ -1,134 +0,0 @@ - - - - - - - Object Oriented Programming - Coders School - - - - - - - - - - - - - -
-
-
-
- -

OOP #3

-

Object-Oriented Programming #3

- - Coders School - -

Mateusz Adamski

-

Łukasz Ziobroń

- -
-
- -
-
- -
-
- -
-
- -
-
-
-
-
-
-
-
-
- -

Coders School

- Coders School - -
-
-
- - - - - - - - - diff --git a/module3/presentation_homework.md b/module3/presentation_homework.md deleted file mode 100644 index 13c848d34..000000000 --- a/module3/presentation_homework.md +++ /dev/null @@ -1,148 +0,0 @@ - - -# Programowanie obiektowe - -## Podsumowanie - - - Coders School - - -___ - -## Co pamiętasz z dzisiaj? - -### Napisz na czacie jak najwięcej haseł - - -1. dziedziczenie -2. wielodziedziczenie -3. funkcje wirtualne -4. funkcje czysto wirtualne -5. klasy abstrakcyjne -6. interfejsy -7. polimorfizm -8. pola i metody statyczne - -___ - -### Pre-work - -* Dowiedzcie się czym jest problem diamentowy -* Poczytajcie o zasadach SOLID, dotyczących pisania dobrego kodu obiektowego -* Lektura o wzorcach projektowych z przykładami w C++ - [refactoring.guru](https://refactoring.guru/design-patterns) -* Spróbujcie w grupie metodą Copy & Paste dorzucić system budowania cmake do projektu. W tym celu popatrzcie na dotychczasowe zadania domowe i plik CMakeLists.txt. - -___ - -## Projekt grupowy - -Wykorzystajcie kod napisany podczas zajęć. Możecie też skorzystać z kodu w katalogu [solutions](solutions) - -Projekt grupowy - kontynuacja. Możecie zmienić grupę jeśli chcecie ;) - -___ - -## Organizacja prac - -* Jak wyglądało wasze daily? -* Czy Code Review nie jest zaniedbane? -* Czy współpraca idzie gładko? -* Zróbcie sobie retrospektywę :) - -___ - -### Punktacja - -* 3 pierwsze zadania - 5 punktów -* zadania 4, 5, 6 - 8 punktów -* 20 punktów za dostarczenie wszystkich 6 zadań przed 05.07.2020 (niedziela) do 23:59 -* brak punktów bonusowych za dostarczenie tylko części zadań przed 05.07 -* 6 punktów za pracę w grupie dla każdej osoby z grupy. - -___ - -## Zadanie 1 - -Napisz klasę `Store`, która będzie umożliwiała dokonywanie zakupów. Wykorzystaj poniższy enum i funkcje. - -```cpp -enum class Response {done, lack_of_money, lack_of_cargo, lack_of_space}; - -Response buy(Cargo* cargo, size_t amount, Player* player); -Response sell(Cargo* cargo, size_t amount, Player* player); -``` - -___ - -## Zadanie 2 - -W klasach `Alcohol`, `Fruit`, `Item` dopisz brakujące metody oraz ich implementacje. - -```cpp -// override from Cargo -size_t getPrice() const override; -std::string getName() const override { return name_; } -size_t getAmount() const override { return amount_; } -size_t getBasePrice() const override { return base_price_; } -Cargo& operator+=(size_t amount) override; -Cargo& operator-=(size_t amount) override; -bool operator==(Cargo& cargo) const override; -``` - -___ - -## Zadanie 3 - -Dopisz do klasy `Ship`, `Cargo` oraz `Store` metodę `nextDay()` - -* Klasa `Ship`: Metoda powinna odejmować po 1 sztuce monety za każdego członka załogi. -* Klasa `Cargo`: Metoda powinna powodować psucie się towarów. -* Klasa `Store`: Metoda powinna zmieniać ilość towaru w sklepach. - -___ - -## Zadanie 4 (dla ambitnych) - -Spróbuj napisać klasę `Time`, która będzie odpowiadać za zarządzanie czasem w grze. - -Klasa ta powinna informować inne klasy, takie jak `Cargo`, `Ship`, `Store` o upłynięciu każdego dnia. - -Poczytaj czym jest wzorzec projektowy [`Observer`](https://refactoring.guru/design-patterns/observer). - -___ - -## Zadanie 5 (dla ambitnych) - -Napisz zaprzyjaźniony operator wypisywania do strumienia - -```cpp -friend std::ostream& operator<<(std::ostream& out, const Store& store); -``` - -Ma on w przystępny sposób wypisywać towar, jaki znajduje się w danym sklepie. - -___ - -## Zadanie 6 (dla ambitnych) - -Napisz klasę `Game`, która zarządzać będzie całą rozgrywką. - -Dodaj jej jedną metodę publiczną `startGame()`. - -Finalnie plik main powinien wyglądać tak: - -```cpp -#include "Game.hpp" - -constexpr size_t start_money = 1'000; -constexpr size_t game_days = 100; -constexpr size_t final_goal = 2'000; - -int main() { - Game game(start_money, game_days, final_goal); - game.startGame(); - - return 0; -} -``` diff --git a/module3/presentation_polymorphism.md b/module3/presentation_polymorphism.md deleted file mode 100644 index 32a9b3088..000000000 --- a/module3/presentation_polymorphism.md +++ /dev/null @@ -1,367 +0,0 @@ - - -# Programowanie obiektowe - -## Polimorfizm - - - Coders School - - -___ - -## Słowo kluczowe `virtual` - -Jeżeli chcemy, aby przy używaniu wskaźników lub referencji na klasę bazową, jakaś metoda zachowywała się inaczej w zależności od prawdziwego typu obiektu, to należy ją oznaczyć słowem kluczowym `virtual`. Jest to tzw. funkcja wirtualna. - -___ - - -## Funkcja nie-wirtualna - -```cpp -#include - -struct Bird { - void sing() { std::cout << "tweet, tweet\n"; } -}; - -struct Sparrow : Bird { - void sing() { std::cout << "chirp, chirp\n"; } -}; - -int main() { - Sparrow sparrow; - Bird& bird = sparrow; - bird.sing(); - return 0; -} -``` - -Co pojawi się na ekranie? - - -`tweet, tweet` - - -___ - - -## Funkcja wirtualna - -```cpp -#include - -struct Bird { - virtual void sing() { std::cout << "tweet, tweet\n"; } -}; - -struct Sparrow : Bird { - void sing() { std::cout << "chirp, chirp\n"; } -}; - -int main() { - Sparrow sparrow; - Bird& bird = sparrow; - bird.sing(); - return 0; -} -``` - -Co pojawi się na ekranie? - - -`chirp, chirp` - - -[Sprawdź na ideone.com](https://ideone.com/yW43Tq) - - -___ - -## Słowo kluczowe `override` - -Jeżeli w klasie pochodnej **nadpisujemy** metodę wirtualną, czyli zmieniamy jej zachowanie, to należy dodać słowo `override`. - - -```cpp -class Interface { -public: - virtual void doSth() = 0; -}; - -class SomeClass : public Interface { -public: - void doSth() override; // there should be an implementation in cpp file -}; - -int main() { - // Interface interface; // Compilation error, Interface is pure virtual - SomeClass someClass; // OK - Interface* interface = &someClass; // OK, we hold a pointer -} -``` - - -___ - -### Mała uwaga - -`override` jest opcjonalne. Jeśli go nie podamy za sygnaturą funkcji klasy pochodnej to metoda z klasy bazowej i tak zostanie nadpisana. - - -Jego użycie jest jednak dobrą praktyką, bo dzięki niemu kompilator sprawdzi czy faktycznie przeciążamy metodą z klasy bazowej i jeśli nie, to program się nie skompiluje. - - -Bez `override` mogłaby zostać utworzona nowa metoda w klasie pochodnej, która nie nadpisuje niczego z klasy bazowej. - - -Metody wirtualne **nadpisujemy**, nie przeciążamy. - - -___ - -## Nadpisywanie metod - `override` - -Wracając do przykładu o ptakach, klasy `Penguin`, `Hummingbird` oraz `Goose` to klasy pochodne, które dziedziczą po pewnych klasach bazowych jak `Bird`, `Flyable`, `Soundable`, `Swimmable` oraz nadpisują kilka ich metod jak: - -* void eat() override -* void sleep() override -* void makeSound() override -* void fly() override -* void swim() override - -Nadpisanie takich metod powoduje, że możemy zmienić ich implementacje. - - -___ - -## `override` - -```cpp -class Soundable { -public: - virtual void makeSound() = 0; -}; -``` - -```cpp -class Goose : public Soundable { -public: - void makeSound() override { std::cout << "Honk! Honk!"; } -}; -``` - -```cpp -class Hen : public Soundable { -public: - void makeSound() override { std::cout << "Cluck! Cluck!"; } -}; -``` - -```cpp -class Duck : public Soundable { -public: - void makeSound() override { std::cout << "Quack! Quack!"; } -}; -``` - -___ - -## Wspólna klasa bazowa - -Ponieważ wspólnym rodzicem wszystkich klas jest klasa `Soundable` możemy przechowywać w kontenerze wskaźniki typu `Soundable`. - -```cpp -std::vector> birds_; -``` - -### Jakie dane otrzymamy na wyjściu? - -```cpp -std::vector> birds_; -birds_.push_back(std::make_shared()); -birds_.push_back(std::make_shared()); -birds_.push_back(std::make_shared()); - -std::cout << birds_[0]->makeSound() << '\n'; -std::cout << birds_[1]->makeSound() << '\n'; -std::cout << birds_[2]->makeSound() << '\n'; -``` - - -___ - -## Polimorfizm - -Zjawisko, które właśnie zaobserwowaliśmy, nazywa się polimorfizmem. - -Polimorfizm pozwala funkcji przybrać różne formy (implementacje), tak jak na przykładzie. - - -Dlatego, jeżeli utworzymy kolejno obiekty `Goose`, `Hen` i `Duck` w zależności od obiektu zostanie wywołana jego wersja metody `makeSound`. - - -Polimorfizm włącza się, gdy mamy funkcje wirtualne i używamy wskaźników lub referencji na typ bazowy. - - -### Kto grał lub czytał Wiedźmina? - - -___ - -## Doppler :) - -W uniwersum wykreowanym przez naszego rodzimego pisarza Andrzeja Sapkowskiego, występuje pewna intrygująca i ciekawa rasa zwana Dopplerami. - - -Rasa ta potrafi przyjąć, postać różnych form życia, może stać się człowiekiem, elfem, krasnoludem. Zmienia w ten sposób swoje cechy jak głos, kolor włosów, a nawet ubranie! - - -Pomimo że rasa ta jest typu Doppler, potrafi w różnych okolicznościach podszywać się pod inne rasy jak elf, krasnolud czy człowiek. - - -Z punktu widzenia C++ nasz Doppler podlega zjawisku polimorfizmu. - - -___ - - -```cpp -class Doppler { -public: - virtual sayHello() { std::cout << "I'm Doppler!"; } -}; - -class Dwarf : public Doppler { -public: - virtual void sayHello() { std::cout << "I'm Dwarf!"; } -}; - -class Elf : public Doppler { -public: - virtual void sayHello() { std::cout << "I'm Elf!"; } -}; - -class Human : public Doppler { -public: - virtual void sayHello() { std::cout << "I'm Human!"; } -}; - -int main() { - std::shared_ptr doppler1 = std::make_shared(); - std::shared_ptr doppler2 = std::make_shared(); - std::shared_ptr doppler3 = std::make_shared(); - - std::cout << doppler1->sayHello() << '\n'; - std::cout << doppler2->sayHello() << '\n'; - std::cout << doppler3->sayHello() << '\n'; -} -``` - - - -Jak widzimy, nasz Doppler może przyjąć różne formy i zachowywać się tak jak one. Wskaźnik jest typu Doppler, ale program dobrze wie, kiedy Doppler podszywa się pod człowieka, kiedy pod krasnoluda, a kiedy pod elfa. - -___ - - -## Nie-wirtualne destruktory - -Bardzo ważne w przypadku tworzenia metod wirtualnych i dziedziczenia jest tworzenie wirtualnych destruktorów. -Jeżeli korzystamy z dobroci polimorfizmu i nie oznaczymy destruktor jako `virtual` to destruktor ten nie zostanie wywołany. - -```cpp -#include -#include - -class Parent { -public: - Parent() { std::cout << "PARENT C'tor called\n"; } - ~Parent() { std::cout << "PARENT D'tor caller\n"; } -}; - -class Child : public Parent { -public: - Child() { std::cout << "CHILD C'tor called\n"; } - ~Child() { std::cout << "CHILD D'tor caller\n"; } -}; - -int main() { - Child child; // ok, object on stack, not a pointer -} -``` - -___ - -## Nie-wirtualne destruktory - problem - -```cpp -#include -#include -#include - -class Parent { -public: - Parent() { std::cout << "PARENT C'tor called\n"; } - ~Parent() { std::cout << "PARENT D'tor caller\n"; } -}; - -class Child : public Parent { -public: - Child() { std::cout << "CHILD C'tor called\n"; } - ~Child() { std::cout << "CHILD D'tor caller\n"; } -}; - -int main() { - // Problem - std::unique_ptr child = std::make_unique(); - - // But shared_ptr will cleanup properly - std::shared_ptr child2 = std::make_shared(); -} -``` - -___ - -## Wirtualny destruktor - -```cpp -#include -#include -#include - -class Parent { -public: - Parent() { std::cout << "PARENT C'tor called\n"; } - virtual ~Parent() { std::cout << "PARENT D'tor caller\n"; } -}; - -class Child : public Parent { -public: - Child() { std::cout << "CHILD C'tor called\n"; } - ~Child() override { std::cout << "CHILD D'tor caller\n"; } -}; - -int main() { - std::unique_ptr child2 = std::make_unique(); -} -``` - -___ - -## Q&A - -___ - -## Zadanie 3 - -Napisz klasę `DryFruit`, która dziedziczyć będzie po klasie `Fruit`. - -Klasa ta powinna nadpisywać metody `getPrice()`, `getName()` oraz `operator--`. - -`operator--` powinien odejmować zużycie raz na 10 wywołań. - -Metoda `getPrice()` powinna zwracać trzykrotnie większą wartość w porównaniu do ceny bazowej. - -Przetestuj wywołania polimorficzne oraz podziel się wnioskami. diff --git a/module3/presentation_solutions.md b/module3/presentation_solutions.md deleted file mode 100644 index 24c5503b1..000000000 --- a/module3/presentation_solutions.md +++ /dev/null @@ -1,267 +0,0 @@ - - -# Programowanie obiektowe - -## Przykładowe rozwiązania - - - Coders School - - -___ - -## Disclaimer - -W PDFie te rozwiązania mogą być częściowo ucięte. Przejdź do lekcji na platformie lub na GitHubie, aby zobaczyć je w całości. - -___ - -### Zadanie 1 - -Cargo.hpp - -```cpp -bool operator==(const Cargo& cargo) const; -``` - -Cargo.cpp - -```cpp -bool Cargo::operator==(const Cargo& cargo) const { - return Cargo.getBasePrice() == base_price_ && Cargo.getName() == name_; -} -``` - -___ - -### Zadanie 2 - -Cargo.hpp - -```cpp -std::string getName() const; -size_t getAmount() const; -size_t getBasePrice() const; -``` - -Cargo.cpp - -```cpp -std::string Cargo::getName() const { return name_; } -size_t Cargo::getAmount() const { return amount_; } -size_t Cargo::getBasePrice() const { return base_price_; } -``` - -___ - -### Zadanie 3 #1 - -Island.hpp - -```cpp -#include -#include - -#include "Store.hpp" - -class Time; - -// Class describes position of island and available store. -class Island { -public: - class Coordinates { - public: - Coordinates() = default; - Coordinates(size_t pos_x, size_t pos_y) - : pos_x_(pos_x), pos_y_(pos_y) {} - - bool operator==(const Coordinates& lhs) const { - return this->pos_x_ == lhs.pos_x_ && this->pos_y_ == lhs.pos_y_; - } - - private: - const size_t pos_x_{0}; - const size_t pos_y_{0}; - }; - - Island() {} - Island(size_t pos_x, size_t pos_y, Time* time); - Coordinates getCoordinates() const { return position_; } - Store* getStore() const { return store_.get(); } - -private: - std::unique_ptr store_; - Coordinates position_; -}; -``` - -___ - -### Zadanie 3 #2 - -Island.cpp - -```cpp -#include "GTime.hpp" -#include "Island.hpp" - -Island::Island(size_t pos_x, size_t pos_y, Time* time) - : position_(Coordinates(pos_x, pos_y)), - store_(std::make_unique(time)) { -} -``` - -___ - -## Zadanie 4/5/6 #1 - -Map.hpp - -```cpp -class Time; - -// Class responsible for map generation which will be used to travel. -class Map { -public: - Map(Time* time); - void travel(Island* destination); - Island* getIsland(const Island::Coordinates& coordinate); - Island* getCurrentPosition() const { return current_position_; } - friend std::ostream& operator<<(std::ostream& out, const Map& map); - -private: - Island* current_position_; - std::vector islands_; -}; -``` - -___ - -### Zadanie 4/5/6 #2 - -Map.cpp - -```cpp -constexpr size_t kIslandNum = 10; -constexpr size_t kWidth = 50; -constexpr size_t kHeight = 50; - -Map::Map(Time* time) { - std::random_device rd; - std::mt19937 gen(rd()); - std::uniform_int_distribution<> widthGen(0, kWidth); - std::uniform_int_distribution<> heightGen(0, kHeight); - std::vector islands(kIslandNum); - std::vector> usedPositions; - - // Generate map - for (size_t i = 0; i < kIslandNum; ++i) { - while (true) { - size_t x = widthGen(gen); - size_t y = heightGen(gen); - if (std::find_if(begin(usedPositions), - end(usedPositions), - [x, y](const auto& pos) { - return pos.first == x && pos.second == y; - }) == std::end(usedPositions)) { - usedPositions.push_back({x, y}); - islands_.push_back(Island(x, y, time)); - break; - } - } - } - - current_position_ = &islands_.front(); -} - -Island* Map::getIsland(const Island::Coordinates& coordinate) { - auto island = std::find_if(std::begin(islands_), - std::end(islands_), - [&coordinate](const Island& island) { - return coordinate == island.GetCoordinates(); - }); - return island != std::end(islands_) ? &*island : nullptr; -} - -void Map::travel(Island* destination) { - current_position_ = destination; -} -``` - -___ - -### Zadanie7/8 #1 - -Player.hpp - -```cpp -#include - -#include "Cargo.hpp" -#include "Ship.hpp" - -class Time; - -// Class is responsible for every action made by player -class Player { -public: - Player(size_t money, Time* time); - virtual ~Player() = default; - - virtual size_t getAvailableSpace() const; - virtual size_t getMoney() const; - virtual size_t getSpeed() const; - virtual Cargo* getCargo(size_t index) const; - -private: - std::unique_ptr ship_; - size_t money_; - size_t available_space_; -}; - -``` - -___ - -### Zadanie 7/8 #2 - -Player.cpp - -```cpp -constexpr size_t kCapacity = 100; -constexpr size_t kCrew = 50; -constexpr size_t kSpeed = 10; -constexpr char kName[] = "BLACK WIDOW"; -constexpr size_t kId = 10; - -Player::Player(size_t money, Time* time) - : ship_(std::make_unique(kCapacity, kCrew, kSpeed, kName, kId, time, this)), - money_(money), - available_space_(kCapacity) { -} - -size_t Player::getMoney() const { - return money_; -} - -size_t Player::getSpeed() const { - return ship_->getSpeed(); -} - -Cargo* Player::getCargo(size_t index) const { - return ship_->getCargo(index); -} - -size_t Player::getAvailableSpace() const { - available_space_ = 0; - for (const auto cargo : ship_->getCargos()) { - available_space_ += cargo->getAmount(); - } - - return available_space_; -} -``` - -___ - -## Q&A diff --git a/module3/presentation_static.md b/module3/presentation_static.md deleted file mode 100644 index da4b1bb48..000000000 --- a/module3/presentation_static.md +++ /dev/null @@ -1,91 +0,0 @@ - - -# Programowanie obiektowe - -## Zmienne i funkcje statyczne - - - Coders School - - -___ - - -## "Zmienna lub stała klasy" - -Czasami chcielibyśmy przypisać jakąś stałą cechę do klasy. -Nie konkretnych obiektów, a klasy samej w sobie. -Np. każdy obiekt klasy ma nazwę "Object". - - -```cpp -class Object { -public: - std::string GetName() const { return name_; } - -private: - const std::string name_ = "Object"; -}; -``` - - -W celu otrzymania nazwy tego obiektu, musimy najpierw utworzyć obiekt, a następnie zawołać `getName()`. - - -```cpp -int main() { - Object obj; - std::cout << obj.getName() << '\n'; -} -``` - - -Nawet jeżeli obiekt zajmowałby dużo miejsca w pamięci a my chcielibyśmy tylko jego nazwę i tak musimy go utworzyć, ponieważ tylko na nim możemy zawołać metodę `getName()`. - - -___ - - -## `static` - -Rozwiązaniem tej uciążliwości jest `static`. Co więcej, problem ten możemy rozwiązać na 2 sposoby. Nie musimy w ten sposób tworzyć specjalnie obiektu, aby dostać się do cechy klasy, jaką jest jej nazwa. - -```cpp -class ObjectA { -public: - static std::string getName() { return "ObjectA"; } -}; - -class ObjectB { -public: - static std::string name_; -}; - -std::string ObjectB::name_{"ObjectB"}; - -int main() { - std::cout << ObjectA::getName() << '\n'; - std::cout << ObjectB::name_ << '\n'; - - return 0; -} -``` - - - - -___ - -## Q&A - -___ - -## Zadanie 4 - -Przekształć klasę bazową `Coordinates`, tak aby miała funkcję statyczną - -```cpp -static size_t distance(const Coordinates& lhs, const Coordinates& rhs) -``` - -Funkcja ta powinna zwracać dystans pomiędzy dwoma pozycjami. From 44b8e81a50eeffc853cc8fa3649e579d4fe198a4 Mon Sep 17 00:00:00 2001 From: Barbara Tessema <74462021+BarTes8@users.noreply.github.com> Date: Mon, 14 Jun 2021 13:44:44 +0200 Subject: [PATCH 04/25] Update 09-operators.md --- module1/09-operators.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module1/09-operators.md b/module1/09-operators.md index 395ce6e97..fc14da274 100644 --- a/module1/09-operators.md +++ b/module1/09-operators.md @@ -37,7 +37,7 @@ public: ```cpp int main() { - Person adam("Adam", 25) + Person adam("Adam", 25); ++adam; std::cout << adam.getAge() << '\n'; // prints 26 } From 2f41754ff65792bcd513ac5f45f787c9c3b7e9d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Ziobro=C5=84?= Date: Wed, 23 Jun 2021 11:14:16 +0200 Subject: [PATCH 05/25] module2 --- module1/index.html | 2 +- ...ation_inheritance.md => 01-inheritance.md} | 2 +- module2/02-multiple-inheritance.md | 359 ++++++++++++++++++ ...{presentation_virtual.md => 03-virtual.md} | 64 +--- module2/04-tasks.md | 50 +++ ...ion_polymorphism.md => 05-polymorphism.md} | 22 +- module2/06-tasks.md | 23 ++ .../{presentation_static.md => 07-static.md} | 30 +- module2/08-tasks.md | 21 + .../{presentation_homework.md => 09-recap.md} | 0 module2/10-homework.md | 148 ++++++++ module2/11-project.md | 148 ++++++++ module2/index.html | 67 ++-- 13 files changed, 807 insertions(+), 129 deletions(-) rename module2/{presentation_inheritance.md => 01-inheritance.md} (98%) create mode 100644 module2/02-multiple-inheritance.md rename module2/{presentation_virtual.md => 03-virtual.md} (70%) create mode 100644 module2/04-tasks.md rename module2/{presentation_polymorphism.md => 05-polymorphism.md} (94%) create mode 100644 module2/06-tasks.md rename module2/{presentation_static.md => 07-static.md} (76%) create mode 100644 module2/08-tasks.md rename module2/{presentation_homework.md => 09-recap.md} (100%) create mode 100644 module2/10-homework.md create mode 100644 module2/11-project.md diff --git a/module1/index.html b/module1/index.html index 2f89e7ddc..66cc43c2e 100644 --- a/module1/index.html +++ b/module1/index.html @@ -26,7 +26,7 @@

Programowanie obiektowe

Object-Oriented Programming

- Coders School + Coders School

Mateusz Adamski

Łukasz Ziobroń

diff --git a/module2/presentation_inheritance.md b/module2/01-inheritance.md similarity index 98% rename from module2/presentation_inheritance.md rename to module2/01-inheritance.md index 0baa7910d..bf1f2c0e9 100644 --- a/module2/presentation_inheritance.md +++ b/module2/01-inheritance.md @@ -5,7 +5,7 @@ ## Dziedziczenie - Coders School + Coders School ___ diff --git a/module2/02-multiple-inheritance.md b/module2/02-multiple-inheritance.md new file mode 100644 index 000000000..e34845f02 --- /dev/null +++ b/module2/02-multiple-inheritance.md @@ -0,0 +1,359 @@ + + +# Programowanie obiektowe + +## Dziedziczenie + + + Coders School + + +___ + +## Wprowadzenie do dziedziczenia + +Podczas implementacji klas, często możemy zauważyć, że część cech składowych klasy można wykorzystać także w innych klasach. + + +Weźmy pod lupę klasę `Computer`. Jeżeli chcielibyśmy utworzyć klasy: `Laptop`, `PC`, `Tablet`, to część metod oraz składowych klasy musielibyśmy powielić. + + +___ + +## `class Computer` + +```cpp +class Computer { +public: + void turnOn(); + void powerOff(); + void restart(); + +private: + Processor processor_; + Drive drive_; + Motherboard motherboard_; + GraphicsCard graphics_card_; + Memory memory_; +}; +``` + +___ + +## `class Laptop` + +```cpp +class Laptop { +public: + void turnOn(); + void powerOff(); + void restart(); + void display(); + void getUserInput(); + +private: + Processor processor_; + Drive drive_; + Motherboard motherboard_; + GraphicsCard graphics_card_; + Memory memory_; + Screen screen_; + Keyboard keyboard_; +}; +``` + +___ + +## `class Tablet` + +```cpp +class Tablet { +public: + void turnOn(); + void powerOff(); + void restart(); + void display(); + void getUserInput(); + +private: + Processor processor_; + Drive drive_; + Motherboard motherboard_; + GraphicsCard graphics_card_; + Memory memory_; + Screen screen_; +}; +``` + +___ + + +## Jak uprościć strukturę naszego programu? + +```cpp +class Computer { +public: + void turnOn(); + void powerOff(); + void restart(); + +protected: + Processor processor_; + Drive drive_; + Motherboard motherboard_; + GraphicsCard graphics_card_; + Memory memory_; +}; + + +class Laptop : public Computer { +public: + void display(); + void getUserInput(); + +private: + Screen screen_; + Keyboard keyboard_; +}; + + +class Tablet : public Computer { +public: + void display(); + void getUserInput(); + +private: + Screen screen_; +}; +``` + + +___ + +## Klasy bazowe i pochodne + +Klasa, po której dziedziczymy, nazywają się **klasą bazową (base class)**. + + +Klasa, która dziedziczy nazywa się **klasą pochodną (derived class)**. + + +Inaczej, klasa, po której dziedziczymy to rodzic (parent class). + + +Klasa, która dziedziczy to dziecko (child class). + + +___ + +### Co z metodami klas `Laptop` i `Tablet`? + +#### Czy można wydzielić kolejną klasę? + + +```cpp +void display(); +void getUserInput(); +``` + + +___ + +## Klasa `Screen` i `TouchScreen` + +Załóżmy, że dodajemy klasę `Screen`. Klasa ta wyświetla na bieżąco interfejs użytkownika. + + +Chcemy też stworzyć klasę reprezentującą ekran dotykowy - `TouchScreen`, który również umożliwia odczyt akcji od użytkownika i ich wyświetlanie. + + +
+
+ +```cpp +class Screen { +public: + void display(); + +private: + void process(); + + Monitor monitor_; +}; +``` + + +
+
+ +```cpp +class TouchScreen { +public: + void display(); + void getUserInput(); + +private: + void process(); + void displayKeyboard(); + + Monitor monitor_; +}; +``` + + +
+
+ +### Jak uprościć powyższy kod? + + +___ + +## Wykorzystanie dziedziczenia do uproszczenia kodu + +```cpp +class Screen { +public: + void display(); + +private: + void process(); + + Monitor monitor_; +}; +``` + + +```cpp +class TouchScreen : public Screen { +public: + void getUserInput(); + +private: + void displayKeyboard(); +}; +``` + + +___ + +## Wielodziedziczenie + +```cpp +class Screen { +public: + void display(); + +private: + void process(); + + Monitor monitor_; +}; + +class TouchScreen : public Screen { +public: + void getUserInput(); + +private: + void displayKeyboard(); +}; + +class Computer { +public: + void turnOn(); + void powerOff(); + void restart(); + +protected: + Processor processor_; + Drive drive_; + Motherboard motherboard_; + GraphicsCard graphics_card_; + Memory memory_; +}; + +class Laptop : public Computer, + public Screen { + Keyboard keyboard_; +}; + +class Tablet : public Computer, + public TouchScreen { +}; +``` + +___ + +## Wielodziedziczenie - disclaimer + +Wielodziedziczenie to dziedziczenie z kliku klas bazowych. + +Wybór implementacji zależy od programisty. + + +Nie zawsze wielodziedziczenie będzie najlepszym rozwiązaniem. + + +Należy się zawsze zastanowić czy dziedziczenie po konkretnej klasie uprości nam program i czy nie będzie powodować żadnych komplikacji w dalszym procesie rozbudowy naszego programu. + + +Najwyżej trzeba będzie zrefaktoryzować program ;) + + +___ + +## Dziedziczenie - problemy + +```cpp +struct Bird { + fly(); + makeSound(); +}; + +struct Penguin { + swim(); + makeSound(); +}; + +// Hummingbird is the type of bird which makes so little sound so it +// can be said that it makes no sound. +struct Hummingbird { + fly(); +}; +``` + +___ + + +## Dziedziczenie - zasada LSP + +Jeżeli spróbujemy teraz uprościć klasę poprzez dziedziczenie pojawi się problem: + +```cpp +struct Bird { + fly(); + makeSound(); +}; + +struct Penguin : public Bird { + fly(); // But I can't fly! + swim(); + makeSound(); +}; + +struct Hummingbird : public Bird { + fly(); + makeSound(); // But I don't make sound! +}; +``` + +Jeszcze bardziej utrudnimy sytuację, gdy w przyszłości dodamy sobie kolejne klasy jak Struś. Zawsze przed implementacją musimy się zastanowić jak podzielić odpowiedzialność na poszczególne klasy, aby +uniknąć podobnych problemów. + +___ + +### Dla ciekawskich + +Poczytajcie o zasadzie Liskov Substitution Principle (LSP). Mówi ona jak powinno / nie powinno się projektować kodu obiektowego. Ta zasada została złamana w ostatnim przykładzie. + +Możecie też poczytać o wszystkich zasadach SOLID. diff --git a/module2/presentation_virtual.md b/module2/03-virtual.md similarity index 70% rename from module2/presentation_virtual.md rename to module2/03-virtual.md index d7b237865..e6c26528e 100644 --- a/module2/presentation_virtual.md +++ b/module2/03-virtual.md @@ -2,10 +2,10 @@ # Programowanie obiektowe -## Metody wirtualne, interfejsy, klasy abstrakcyjne +## Metody czysto wirtualne, interfejsy, klasy abstrakcyjne - Coders School + Coders School ___ @@ -16,9 +16,9 @@ ___ ```cpp class Bird { public: - size_t getWeight(); - size_t getHeight(); - size_t getName(); + size_t getWeight() { return weight_; } + size_t getHeight() { return height_; } + std::string getName() { return name_; } // Pure virtual function without implementation virtual void eat() = 0; @@ -32,7 +32,7 @@ protected: ``` -Metody `eat()` oraz `sleep()` to tzw. metody czysto wirtualne. Świadczy o tym `= 0;`. Oznacza to, że nigdzie nie znajdziemy ich implementacji dla klasy `Bird`. Klasy które dziedziczą po `Bird` będą ją musiały zaimplementować same. +Metody `eat()` oraz `sleep()` to tzw. metody czysto wirtualne (ang. pure virtual functions). Świadczy o tym `= 0;`. Oznacza to, że nigdzie nie znajdziemy ich implementacji dla klasy `Bird`. Klasy które dziedziczą po `Bird` będą ją musiały zaimplementować same. Znaczenie słowa `virtual` na razie przemilczymy. Jedyne co trzeba na teraz wiedzieć, to aby metoda była czysto wirtualna `= 0;` to musi być przed nią słowo `virtual`. @@ -71,7 +71,9 @@ ___ ## Użycie interfejsów ```cpp -class Penguin : public Bird, public Swimmable { +class Penguin : public Bird, + public Swimmable, + public Soundable { public: // Override from Bird void eat() override; @@ -79,6 +81,9 @@ public: // Override from Swimmable void swim() override; + + // Override from Soundable + void makeSound() override; }; class Hummingbird : public Bird, @@ -162,48 +167,3 @@ ___ Co to za słowa? Co one robią? O tym za chwilę ;) - -___ - -## Q&A - -___ - -## Zadanie 1 - -Przekształć klasę `Cargo` w interfejs z 4 czysto wirtualnymi metodami. - -```cpp -virtual size_t getPrice() const = 0; -virtual std::string getName() const = 0; -virtual size_t getAmount() const = 0; -virtual size_t getBasePrice() const = 0; -``` - -___ - -## Zadanie 1 cd. - -Utwórz 3 pochodne klasy `Cargo`: - -* `Fruit` -* `Alcohol` -* `Item` - -Klasa `Fruit` powinna mieć dodatkową zmienną określającą czas do zepsucia oraz `operator--` który będzie odejmował ten czas o 1. -Metoda `getPrice()` powinna redukować cenę odpowiednio wraz z czasem psucia naszego owocu. - -Klasa `Alcohol` powinna mieć dodatkową zmienną określającą procentowy udział spirytusu. -Metoda `getPrice()` powinna być proporcjonalnie wyższa w zależności od mocy alkoholu. -Należy ustalić bazową cenę za spirytus 96%. - -Klasa `Item` powinna mieć dodatkową zmienną enum określającą rzadkość przedmiotu (common, rare, epic, legendary). -Metoda `getPrice()` powinna być adekwatnie wyliczana od poziomu rzadkości przedmiotu. - -___ - -## Zadanie 2 - -Wykorzystując wspólną klasę bazową `Cargo` spróbuj przechowywać wszystkie towary w jednym wektorze w klasie `Ship`. - -Dodaj funkcję `void load(std::shared_ptr cargo)`, która dodaje towar i (dla chętnych) `void unload(Cargo* cargo)`, która usuwa towar z obiektu klasy `Ship`. diff --git a/module2/04-tasks.md b/module2/04-tasks.md new file mode 100644 index 000000000..5ace32ea4 --- /dev/null +++ b/module2/04-tasks.md @@ -0,0 +1,50 @@ + + +# Programowanie obiektowe + +## Zadania + + + Coders School + + +___ + +## Zadanie 5 + +Przekształć klasę `Cargo` w interfejs z 4 czysto wirtualnymi metodami. + +```cpp +virtual size_t getPrice() const = 0; +virtual std::string getName() const = 0; +virtual size_t getAmount() const = 0; +virtual size_t getBasePrice() const = 0; +``` + +___ + +## Zadanie 5 cd. + +Utwórz 3 pochodne klasy `Cargo`: + +* `Fruit` +* `Alcohol` +* `Item` + +Klasa `Fruit` powinna mieć dodatkową zmienną określającą czas do zepsucia oraz `operator--` który będzie odejmował ten czas o 1. +Metoda `getPrice()` powinna redukować cenę odpowiednio wraz z czasem psucia naszego owocu. + +Klasa `Alcohol` powinna mieć dodatkową zmienną określającą procentowy udział spirytusu. +Metoda `getPrice()` powinna być proporcjonalnie wyższa w zależności od mocy alkoholu. +Należy ustalić bazową cenę za spirytus 96%. + +Klasa `Item` powinna mieć dodatkową zmienną enum określającą rzadkość przedmiotu (common, rare, epic, legendary). +Metoda `getPrice()` powinna być adekwatnie wyliczana od poziomu rzadkości przedmiotu. + +___ + +## Zadanie 6 + +Wykorzystując wspólną klasę bazową `Cargo` spróbuj przechowywać wszystkie towary w jednym wektorze w klasie `Ship`. + +Dodaj funkcję `void load(std::shared_ptr cargo)`, która dodaje towar i (dla chętnych) `void unload(Cargo* cargo)`, która usuwa towar z obiektu klasy `Ship`. diff --git a/module2/presentation_polymorphism.md b/module2/05-polymorphism.md similarity index 94% rename from module2/presentation_polymorphism.md rename to module2/05-polymorphism.md index 1612473c6..8c1b6e9f5 100644 --- a/module2/presentation_polymorphism.md +++ b/module2/05-polymorphism.md @@ -5,7 +5,7 @@ ## Polimorfizm - Coders School + Coders School ___ @@ -92,7 +92,7 @@ public: class SomeClass : public Interface { public: - void doSth() override; // there should be an implementation in cpp file + void doSth() override; // there should be an implementation in the cpp file }; int main() { @@ -347,21 +347,3 @@ int main() { std::unique_ptr child2 = std::make_unique(); } ``` - -___ - -## Q&A - -___ - -## Zadanie 3 - -Napisz klasę `DryFruit`, która dziedziczyć będzie po klasie `Fruit`. - -Klasa ta powinna nadpisywać metody `getPrice()`, `getName()` oraz `operator--`. - -`operator--` powinien odejmować zużycie raz na 10 wywołań. - -Metoda `getPrice()` powinna zwracać trzykrotnie większą wartość w porównaniu do ceny bazowej. - -Przetestuj wywołania polimorficzne oraz podziel się wnioskami. diff --git a/module2/06-tasks.md b/module2/06-tasks.md new file mode 100644 index 000000000..6711b6dbf --- /dev/null +++ b/module2/06-tasks.md @@ -0,0 +1,23 @@ + + +# Programowanie obiektowe + +## Zadania + + + Coders School + + +___ + +## Zadanie 7 + +Napisz klasę `DryFruit`, która dziedziczyć będzie po klasie `Fruit`. + +Klasa ta powinna nadpisywać metody `getPrice()`, `getName()` oraz `operator--`. + +`operator--` powinien odejmować zużycie raz na 10 wywołań. + +Metoda `getPrice()` powinna zwracać trzykrotnie większą wartość w porównaniu do ceny bazowej. + +Przetestuj wywołania polimorficzne oraz podziel się wnioskami w komentarzach :) diff --git a/module2/presentation_static.md b/module2/07-static.md similarity index 76% rename from module2/presentation_static.md rename to module2/07-static.md index e73fe4a05..759e02d09 100644 --- a/module2/presentation_static.md +++ b/module2/07-static.md @@ -2,26 +2,26 @@ # Programowanie obiektowe -## Zmienne i funkcje statyczne +## Pola i funkcje statyczne - Coders School + Coders School ___ -## Zmienna lub stała klasy +## Pola klasy Czasami chcielibyśmy przypisać jakąś stałą cechę do klasy. -Nie konkretnych obiektów, a klasy samej w sobie. +Nie konkretnych obiektów, a klasy samej w sobie, tak, aby była ona wspólna dla wszystkich obiektów tej klasy. Np. każdy obiekt klasy ma nazwę "Object". ```cpp class Object { public: - std::string GetName() const { return name_; } + std::string getName() const { return name_; } private: const std::string name_ = "Object"; @@ -66,6 +66,10 @@ std::string ClassB::name_{"ClassB"}; int main() { std::cout << ClassA::getName() << '\n'; std::cout << ClassB::name_ << '\n'; + ClassA obj; + std::cout << obj.getName() << '\n'; + ClassB obj; + std::cout << obj.name_ << '\n'; return 0; } @@ -73,19 +77,3 @@ int main() { - -___ - -## Q&A - -___ - -## Zadanie 4 - -Przekształć klasę bazową `Coordinates`, tak aby miała funkcję statyczną - -```cpp -static size_t distance(const Coordinates& lhs, const Coordinates& rhs) -``` - -Funkcja ta powinna zwracać dystans pomiędzy dwoma pozycjami. diff --git a/module2/08-tasks.md b/module2/08-tasks.md new file mode 100644 index 000000000..d1529a6ba --- /dev/null +++ b/module2/08-tasks.md @@ -0,0 +1,21 @@ + + +# Programowanie obiektowe + +## Zadania + + + Coders School + + +___ + +## Zadanie 8 + +Przekształć klasę bazową `Coordinates`, tak aby miała funkcję statyczną + +```cpp +static size_t distance(const Coordinates& lhs, const Coordinates& rhs) +``` + +Funkcja ta powinna zwracać dystans pomiędzy dwoma pozycjami. diff --git a/module2/presentation_homework.md b/module2/09-recap.md similarity index 100% rename from module2/presentation_homework.md rename to module2/09-recap.md diff --git a/module2/10-homework.md b/module2/10-homework.md new file mode 100644 index 000000000..13c848d34 --- /dev/null +++ b/module2/10-homework.md @@ -0,0 +1,148 @@ + + +# Programowanie obiektowe + +## Podsumowanie + + + Coders School + + +___ + +## Co pamiętasz z dzisiaj? + +### Napisz na czacie jak najwięcej haseł + + +1. dziedziczenie +2. wielodziedziczenie +3. funkcje wirtualne +4. funkcje czysto wirtualne +5. klasy abstrakcyjne +6. interfejsy +7. polimorfizm +8. pola i metody statyczne + +___ + +### Pre-work + +* Dowiedzcie się czym jest problem diamentowy +* Poczytajcie o zasadach SOLID, dotyczących pisania dobrego kodu obiektowego +* Lektura o wzorcach projektowych z przykładami w C++ - [refactoring.guru](https://refactoring.guru/design-patterns) +* Spróbujcie w grupie metodą Copy & Paste dorzucić system budowania cmake do projektu. W tym celu popatrzcie na dotychczasowe zadania domowe i plik CMakeLists.txt. + +___ + +## Projekt grupowy + +Wykorzystajcie kod napisany podczas zajęć. Możecie też skorzystać z kodu w katalogu [solutions](solutions) + +Projekt grupowy - kontynuacja. Możecie zmienić grupę jeśli chcecie ;) + +___ + +## Organizacja prac + +* Jak wyglądało wasze daily? +* Czy Code Review nie jest zaniedbane? +* Czy współpraca idzie gładko? +* Zróbcie sobie retrospektywę :) + +___ + +### Punktacja + +* 3 pierwsze zadania - 5 punktów +* zadania 4, 5, 6 - 8 punktów +* 20 punktów za dostarczenie wszystkich 6 zadań przed 05.07.2020 (niedziela) do 23:59 +* brak punktów bonusowych za dostarczenie tylko części zadań przed 05.07 +* 6 punktów za pracę w grupie dla każdej osoby z grupy. + +___ + +## Zadanie 1 + +Napisz klasę `Store`, która będzie umożliwiała dokonywanie zakupów. Wykorzystaj poniższy enum i funkcje. + +```cpp +enum class Response {done, lack_of_money, lack_of_cargo, lack_of_space}; + +Response buy(Cargo* cargo, size_t amount, Player* player); +Response sell(Cargo* cargo, size_t amount, Player* player); +``` + +___ + +## Zadanie 2 + +W klasach `Alcohol`, `Fruit`, `Item` dopisz brakujące metody oraz ich implementacje. + +```cpp +// override from Cargo +size_t getPrice() const override; +std::string getName() const override { return name_; } +size_t getAmount() const override { return amount_; } +size_t getBasePrice() const override { return base_price_; } +Cargo& operator+=(size_t amount) override; +Cargo& operator-=(size_t amount) override; +bool operator==(Cargo& cargo) const override; +``` + +___ + +## Zadanie 3 + +Dopisz do klasy `Ship`, `Cargo` oraz `Store` metodę `nextDay()` + +* Klasa `Ship`: Metoda powinna odejmować po 1 sztuce monety za każdego członka załogi. +* Klasa `Cargo`: Metoda powinna powodować psucie się towarów. +* Klasa `Store`: Metoda powinna zmieniać ilość towaru w sklepach. + +___ + +## Zadanie 4 (dla ambitnych) + +Spróbuj napisać klasę `Time`, która będzie odpowiadać za zarządzanie czasem w grze. + +Klasa ta powinna informować inne klasy, takie jak `Cargo`, `Ship`, `Store` o upłynięciu każdego dnia. + +Poczytaj czym jest wzorzec projektowy [`Observer`](https://refactoring.guru/design-patterns/observer). + +___ + +## Zadanie 5 (dla ambitnych) + +Napisz zaprzyjaźniony operator wypisywania do strumienia + +```cpp +friend std::ostream& operator<<(std::ostream& out, const Store& store); +``` + +Ma on w przystępny sposób wypisywać towar, jaki znajduje się w danym sklepie. + +___ + +## Zadanie 6 (dla ambitnych) + +Napisz klasę `Game`, która zarządzać będzie całą rozgrywką. + +Dodaj jej jedną metodę publiczną `startGame()`. + +Finalnie plik main powinien wyglądać tak: + +```cpp +#include "Game.hpp" + +constexpr size_t start_money = 1'000; +constexpr size_t game_days = 100; +constexpr size_t final_goal = 2'000; + +int main() { + Game game(start_money, game_days, final_goal); + game.startGame(); + + return 0; +} +``` diff --git a/module2/11-project.md b/module2/11-project.md new file mode 100644 index 000000000..13c848d34 --- /dev/null +++ b/module2/11-project.md @@ -0,0 +1,148 @@ + + +# Programowanie obiektowe + +## Podsumowanie + + + Coders School + + +___ + +## Co pamiętasz z dzisiaj? + +### Napisz na czacie jak najwięcej haseł + + +1. dziedziczenie +2. wielodziedziczenie +3. funkcje wirtualne +4. funkcje czysto wirtualne +5. klasy abstrakcyjne +6. interfejsy +7. polimorfizm +8. pola i metody statyczne + +___ + +### Pre-work + +* Dowiedzcie się czym jest problem diamentowy +* Poczytajcie o zasadach SOLID, dotyczących pisania dobrego kodu obiektowego +* Lektura o wzorcach projektowych z przykładami w C++ - [refactoring.guru](https://refactoring.guru/design-patterns) +* Spróbujcie w grupie metodą Copy & Paste dorzucić system budowania cmake do projektu. W tym celu popatrzcie na dotychczasowe zadania domowe i plik CMakeLists.txt. + +___ + +## Projekt grupowy + +Wykorzystajcie kod napisany podczas zajęć. Możecie też skorzystać z kodu w katalogu [solutions](solutions) + +Projekt grupowy - kontynuacja. Możecie zmienić grupę jeśli chcecie ;) + +___ + +## Organizacja prac + +* Jak wyglądało wasze daily? +* Czy Code Review nie jest zaniedbane? +* Czy współpraca idzie gładko? +* Zróbcie sobie retrospektywę :) + +___ + +### Punktacja + +* 3 pierwsze zadania - 5 punktów +* zadania 4, 5, 6 - 8 punktów +* 20 punktów za dostarczenie wszystkich 6 zadań przed 05.07.2020 (niedziela) do 23:59 +* brak punktów bonusowych za dostarczenie tylko części zadań przed 05.07 +* 6 punktów za pracę w grupie dla każdej osoby z grupy. + +___ + +## Zadanie 1 + +Napisz klasę `Store`, która będzie umożliwiała dokonywanie zakupów. Wykorzystaj poniższy enum i funkcje. + +```cpp +enum class Response {done, lack_of_money, lack_of_cargo, lack_of_space}; + +Response buy(Cargo* cargo, size_t amount, Player* player); +Response sell(Cargo* cargo, size_t amount, Player* player); +``` + +___ + +## Zadanie 2 + +W klasach `Alcohol`, `Fruit`, `Item` dopisz brakujące metody oraz ich implementacje. + +```cpp +// override from Cargo +size_t getPrice() const override; +std::string getName() const override { return name_; } +size_t getAmount() const override { return amount_; } +size_t getBasePrice() const override { return base_price_; } +Cargo& operator+=(size_t amount) override; +Cargo& operator-=(size_t amount) override; +bool operator==(Cargo& cargo) const override; +``` + +___ + +## Zadanie 3 + +Dopisz do klasy `Ship`, `Cargo` oraz `Store` metodę `nextDay()` + +* Klasa `Ship`: Metoda powinna odejmować po 1 sztuce monety za każdego członka załogi. +* Klasa `Cargo`: Metoda powinna powodować psucie się towarów. +* Klasa `Store`: Metoda powinna zmieniać ilość towaru w sklepach. + +___ + +## Zadanie 4 (dla ambitnych) + +Spróbuj napisać klasę `Time`, która będzie odpowiadać za zarządzanie czasem w grze. + +Klasa ta powinna informować inne klasy, takie jak `Cargo`, `Ship`, `Store` o upłynięciu każdego dnia. + +Poczytaj czym jest wzorzec projektowy [`Observer`](https://refactoring.guru/design-patterns/observer). + +___ + +## Zadanie 5 (dla ambitnych) + +Napisz zaprzyjaźniony operator wypisywania do strumienia + +```cpp +friend std::ostream& operator<<(std::ostream& out, const Store& store); +``` + +Ma on w przystępny sposób wypisywać towar, jaki znajduje się w danym sklepie. + +___ + +## Zadanie 6 (dla ambitnych) + +Napisz klasę `Game`, która zarządzać będzie całą rozgrywką. + +Dodaj jej jedną metodę publiczną `startGame()`. + +Finalnie plik main powinien wyglądać tak: + +```cpp +#include "Game.hpp" + +constexpr size_t start_money = 1'000; +constexpr size_t game_days = 100; +constexpr size_t final_goal = 2'000; + +int main() { + Game game(start_money, game_days, final_goal); + game.startGame(); + + return 0; +} +``` diff --git a/module2/index.html b/module2/index.html index aaabe3623..e8ed7b5d1 100644 --- a/module2/index.html +++ b/module2/index.html @@ -23,10 +23,10 @@
-

OOP #2

-

Object-Oriented Programming #2

+

Programowanie obiektowe

+

Object-Oriented Programming

- Coders School + Coders School

Mateusz Adamski

Łukasz Ziobroń

@@ -39,38 +39,13 @@

Łukasz Ziobroń

1. dziedziczenie 2. wielodziedziczenie - 3. funkcje wirtualne 4. funkcje czysto wirtualne 5. klasy abstrakcyjne 6. interfejsy + 3. funkcje wirtualne 7. polimorfizm 8. pola i metody statyczne - Możliwe, że dzisiaj nie przerobimy wszystkiego. Będzie na kolejną lekcję :) - - - -
-
- -
-
-
@@ -89,23 +64,47 @@

Łukasz Ziobroń

You can change the port by using npm start -- --port=8001. --> -
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
From d3ca5dfdca3ccff4df58c90fefc245769959d2af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Ziobro=C5=84?= Date: Wed, 23 Jun 2021 11:41:10 +0200 Subject: [PATCH 06/25] module2 further work --- module2/03-virtual.md | 8 +- module2/07-static.md | 11 +- module2/08-tasks.md | 4 +- module2/09-recap.md | 131 +---------------- module2/10-homework.md | 136 +----------------- module2/11-project.md | 27 +--- module2/solutions/exercise2.cpp | 47 ------ .../{exercise1.cpp => exercise5.cpp} | 40 +++--- module2/solutions/exercise6.cpp | 47 ++++++ .../{exercise3.cpp => exercise7.cpp} | 4 +- .../{exercise4.cpp => exercise8.cpp} | 2 +- 11 files changed, 91 insertions(+), 366 deletions(-) delete mode 100644 module2/solutions/exercise2.cpp rename module2/solutions/{exercise1.cpp => exercise5.cpp} (65%) create mode 100644 module2/solutions/exercise6.cpp rename module2/solutions/{exercise3.cpp => exercise7.cpp} (83%) rename module2/solutions/{exercise4.cpp => exercise8.cpp} (89%) diff --git a/module2/03-virtual.md b/module2/03-virtual.md index e6c26528e..e98971d0f 100644 --- a/module2/03-virtual.md +++ b/module2/03-virtual.md @@ -16,9 +16,9 @@ ___ ```cpp class Bird { public: - size_t getWeight() { return weight_; } - size_t getHeight() { return height_; } - std::string getName() { return name_; } + size_t getWeight() const { return weight_; } + size_t getHeight() const { return height_; } + std::string getName() const { return name_; } // Pure virtual function without implementation virtual void eat() = 0; @@ -165,5 +165,5 @@ ___ ## Słowo `virtual` i `override` -Co to za słowa? Co one robią? O tym za chwilę ;) +Co to za słowa? Co one robią? O tym w jednym z następnych wideo ;) diff --git a/module2/07-static.md b/module2/07-static.md index 759e02d09..65fcd3190 100644 --- a/module2/07-static.md +++ b/module2/07-static.md @@ -44,7 +44,7 @@ Nawet jeżeli obiekt zajmowałby dużo miejsca w pamięci, a my chcielibyśmy ty ___ - + ## `static` @@ -66,10 +66,11 @@ std::string ClassB::name_{"ClassB"}; int main() { std::cout << ClassA::getName() << '\n'; std::cout << ClassB::name_ << '\n'; - ClassA obj; - std::cout << obj.getName() << '\n'; - ClassB obj; - std::cout << obj.name_ << '\n'; + + ClassA objA; + std::cout << objA.getName() << '\n'; + ClassB objB; + std::cout << objB.name_ << '\n'; return 0; } diff --git a/module2/08-tasks.md b/module2/08-tasks.md index d1529a6ba..e9026f135 100644 --- a/module2/08-tasks.md +++ b/module2/08-tasks.md @@ -12,7 +12,9 @@ ___ ## Zadanie 8 -Przekształć klasę bazową `Coordinates`, tak aby miała funkcję statyczną +Napisz/przekształć klasę `Coordinates`, która ma określać współrzędne na mapie. Powinna ona przyjmować w konstruktorze 2 parametry `positionX`, `positionY` oraz operator porównania. + +Ma ona posiadać funkcję statyczną `distance`: ```cpp static size_t distance(const Coordinates& lhs, const Coordinates& rhs) diff --git a/module2/09-recap.md b/module2/09-recap.md index 13c848d34..d10a81ffc 100644 --- a/module2/09-recap.md +++ b/module2/09-recap.md @@ -17,132 +17,9 @@ ___ 1. dziedziczenie 2. wielodziedziczenie -3. funkcje wirtualne -4. funkcje czysto wirtualne -5. klasy abstrakcyjne -6. interfejsy +3. funkcje czysto wirtualne +4. klasy abstrakcyjne +5. interfejsy +6. funkcje wirtualne 7. polimorfizm 8. pola i metody statyczne - -___ - -### Pre-work - -* Dowiedzcie się czym jest problem diamentowy -* Poczytajcie o zasadach SOLID, dotyczących pisania dobrego kodu obiektowego -* Lektura o wzorcach projektowych z przykładami w C++ - [refactoring.guru](https://refactoring.guru/design-patterns) -* Spróbujcie w grupie metodą Copy & Paste dorzucić system budowania cmake do projektu. W tym celu popatrzcie na dotychczasowe zadania domowe i plik CMakeLists.txt. - -___ - -## Projekt grupowy - -Wykorzystajcie kod napisany podczas zajęć. Możecie też skorzystać z kodu w katalogu [solutions](solutions) - -Projekt grupowy - kontynuacja. Możecie zmienić grupę jeśli chcecie ;) - -___ - -## Organizacja prac - -* Jak wyglądało wasze daily? -* Czy Code Review nie jest zaniedbane? -* Czy współpraca idzie gładko? -* Zróbcie sobie retrospektywę :) - -___ - -### Punktacja - -* 3 pierwsze zadania - 5 punktów -* zadania 4, 5, 6 - 8 punktów -* 20 punktów za dostarczenie wszystkich 6 zadań przed 05.07.2020 (niedziela) do 23:59 -* brak punktów bonusowych za dostarczenie tylko części zadań przed 05.07 -* 6 punktów za pracę w grupie dla każdej osoby z grupy. - -___ - -## Zadanie 1 - -Napisz klasę `Store`, która będzie umożliwiała dokonywanie zakupów. Wykorzystaj poniższy enum i funkcje. - -```cpp -enum class Response {done, lack_of_money, lack_of_cargo, lack_of_space}; - -Response buy(Cargo* cargo, size_t amount, Player* player); -Response sell(Cargo* cargo, size_t amount, Player* player); -``` - -___ - -## Zadanie 2 - -W klasach `Alcohol`, `Fruit`, `Item` dopisz brakujące metody oraz ich implementacje. - -```cpp -// override from Cargo -size_t getPrice() const override; -std::string getName() const override { return name_; } -size_t getAmount() const override { return amount_; } -size_t getBasePrice() const override { return base_price_; } -Cargo& operator+=(size_t amount) override; -Cargo& operator-=(size_t amount) override; -bool operator==(Cargo& cargo) const override; -``` - -___ - -## Zadanie 3 - -Dopisz do klasy `Ship`, `Cargo` oraz `Store` metodę `nextDay()` - -* Klasa `Ship`: Metoda powinna odejmować po 1 sztuce monety za każdego członka załogi. -* Klasa `Cargo`: Metoda powinna powodować psucie się towarów. -* Klasa `Store`: Metoda powinna zmieniać ilość towaru w sklepach. - -___ - -## Zadanie 4 (dla ambitnych) - -Spróbuj napisać klasę `Time`, która będzie odpowiadać za zarządzanie czasem w grze. - -Klasa ta powinna informować inne klasy, takie jak `Cargo`, `Ship`, `Store` o upłynięciu każdego dnia. - -Poczytaj czym jest wzorzec projektowy [`Observer`](https://refactoring.guru/design-patterns/observer). - -___ - -## Zadanie 5 (dla ambitnych) - -Napisz zaprzyjaźniony operator wypisywania do strumienia - -```cpp -friend std::ostream& operator<<(std::ostream& out, const Store& store); -``` - -Ma on w przystępny sposób wypisywać towar, jaki znajduje się w danym sklepie. - -___ - -## Zadanie 6 (dla ambitnych) - -Napisz klasę `Game`, która zarządzać będzie całą rozgrywką. - -Dodaj jej jedną metodę publiczną `startGame()`. - -Finalnie plik main powinien wyglądać tak: - -```cpp -#include "Game.hpp" - -constexpr size_t start_money = 1'000; -constexpr size_t game_days = 100; -constexpr size_t final_goal = 2'000; - -int main() { - Game game(start_money, game_days, final_goal); - game.startGame(); - - return 0; -} -``` diff --git a/module2/10-homework.md b/module2/10-homework.md index 13c848d34..ace7d5307 100644 --- a/module2/10-homework.md +++ b/module2/10-homework.md @@ -2,7 +2,7 @@ # Programowanie obiektowe -## Podsumowanie +## Praca domowa Coders School @@ -10,139 +10,9 @@ ___ -## Co pamiętasz z dzisiaj? - -### Napisz na czacie jak najwięcej haseł - - -1. dziedziczenie -2. wielodziedziczenie -3. funkcje wirtualne -4. funkcje czysto wirtualne -5. klasy abstrakcyjne -6. interfejsy -7. polimorfizm -8. pola i metody statyczne - -___ - ### Pre-work -* Dowiedzcie się czym jest problem diamentowy -* Poczytajcie o zasadach SOLID, dotyczących pisania dobrego kodu obiektowego +* Dowiedz się czym jest problem diamentowy +* Poczytaj o zasadach SOLID, dotyczących pisania dobrego kodu obiektowego * Lektura o wzorcach projektowych z przykładami w C++ - [refactoring.guru](https://refactoring.guru/design-patterns) * Spróbujcie w grupie metodą Copy & Paste dorzucić system budowania cmake do projektu. W tym celu popatrzcie na dotychczasowe zadania domowe i plik CMakeLists.txt. - -___ - -## Projekt grupowy - -Wykorzystajcie kod napisany podczas zajęć. Możecie też skorzystać z kodu w katalogu [solutions](solutions) - -Projekt grupowy - kontynuacja. Możecie zmienić grupę jeśli chcecie ;) - -___ - -## Organizacja prac - -* Jak wyglądało wasze daily? -* Czy Code Review nie jest zaniedbane? -* Czy współpraca idzie gładko? -* Zróbcie sobie retrospektywę :) - -___ - -### Punktacja - -* 3 pierwsze zadania - 5 punktów -* zadania 4, 5, 6 - 8 punktów -* 20 punktów za dostarczenie wszystkich 6 zadań przed 05.07.2020 (niedziela) do 23:59 -* brak punktów bonusowych za dostarczenie tylko części zadań przed 05.07 -* 6 punktów za pracę w grupie dla każdej osoby z grupy. - -___ - -## Zadanie 1 - -Napisz klasę `Store`, która będzie umożliwiała dokonywanie zakupów. Wykorzystaj poniższy enum i funkcje. - -```cpp -enum class Response {done, lack_of_money, lack_of_cargo, lack_of_space}; - -Response buy(Cargo* cargo, size_t amount, Player* player); -Response sell(Cargo* cargo, size_t amount, Player* player); -``` - -___ - -## Zadanie 2 - -W klasach `Alcohol`, `Fruit`, `Item` dopisz brakujące metody oraz ich implementacje. - -```cpp -// override from Cargo -size_t getPrice() const override; -std::string getName() const override { return name_; } -size_t getAmount() const override { return amount_; } -size_t getBasePrice() const override { return base_price_; } -Cargo& operator+=(size_t amount) override; -Cargo& operator-=(size_t amount) override; -bool operator==(Cargo& cargo) const override; -``` - -___ - -## Zadanie 3 - -Dopisz do klasy `Ship`, `Cargo` oraz `Store` metodę `nextDay()` - -* Klasa `Ship`: Metoda powinna odejmować po 1 sztuce monety za każdego członka załogi. -* Klasa `Cargo`: Metoda powinna powodować psucie się towarów. -* Klasa `Store`: Metoda powinna zmieniać ilość towaru w sklepach. - -___ - -## Zadanie 4 (dla ambitnych) - -Spróbuj napisać klasę `Time`, która będzie odpowiadać za zarządzanie czasem w grze. - -Klasa ta powinna informować inne klasy, takie jak `Cargo`, `Ship`, `Store` o upłynięciu każdego dnia. - -Poczytaj czym jest wzorzec projektowy [`Observer`](https://refactoring.guru/design-patterns/observer). - -___ - -## Zadanie 5 (dla ambitnych) - -Napisz zaprzyjaźniony operator wypisywania do strumienia - -```cpp -friend std::ostream& operator<<(std::ostream& out, const Store& store); -``` - -Ma on w przystępny sposób wypisywać towar, jaki znajduje się w danym sklepie. - -___ - -## Zadanie 6 (dla ambitnych) - -Napisz klasę `Game`, która zarządzać będzie całą rozgrywką. - -Dodaj jej jedną metodę publiczną `startGame()`. - -Finalnie plik main powinien wyglądać tak: - -```cpp -#include "Game.hpp" - -constexpr size_t start_money = 1'000; -constexpr size_t game_days = 100; -constexpr size_t final_goal = 2'000; - -int main() { - Game game(start_money, game_days, final_goal); - game.startGame(); - - return 0; -} -``` diff --git a/module2/11-project.md b/module2/11-project.md index 13c848d34..bd957dce3 100644 --- a/module2/11-project.md +++ b/module2/11-project.md @@ -2,7 +2,7 @@ # Programowanie obiektowe -## Podsumowanie +## Projekt SHM Coders School @@ -10,31 +10,6 @@ ___ -## Co pamiętasz z dzisiaj? - -### Napisz na czacie jak najwięcej haseł - - -1. dziedziczenie -2. wielodziedziczenie -3. funkcje wirtualne -4. funkcje czysto wirtualne -5. klasy abstrakcyjne -6. interfejsy -7. polimorfizm -8. pola i metody statyczne - -___ - -### Pre-work - -* Dowiedzcie się czym jest problem diamentowy -* Poczytajcie o zasadach SOLID, dotyczących pisania dobrego kodu obiektowego -* Lektura o wzorcach projektowych z przykładami w C++ - [refactoring.guru](https://refactoring.guru/design-patterns) -* Spróbujcie w grupie metodą Copy & Paste dorzucić system budowania cmake do projektu. W tym celu popatrzcie na dotychczasowe zadania domowe i plik CMakeLists.txt. - -___ - ## Projekt grupowy Wykorzystajcie kod napisany podczas zajęć. Możecie też skorzystać z kodu w katalogu [solutions](solutions) diff --git a/module2/solutions/exercise2.cpp b/module2/solutions/exercise2.cpp deleted file mode 100644 index 2db682909..000000000 --- a/module2/solutions/exercise2.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include -#include - -class Cargo; - -class Ship { -public: - void Load(std::unique_ptr cargo) { - if (auto match_cargo = FindMatchCargo(cargo.get())) { - *match_cargo += cargo->GetAmount(); - return; - } - cargo_.push_back(std::move(cargo)); - } - - void Unload(Cargo* cargo) { - RemoveFromStorage(cargo); - } - - Cargo* FindMatchCargo(Cargo* cargo) { - for (auto el : cargo_) { - if (el->GetName() == "Fruit") { - if (el->GetName() == cargo->GetName() && - el->GetBasePrice() == cargo->GetBasePrice() && - el->GetExpiryDate() == cargo->GetExpiryDate()) - return el.get(); - } else if (el->GetName() == "Alcohol") { - if (el->GetName() == cargo->GetName() && - el->GetBasePrice() == cargo->GetBasePrice() && - el->GetPercentage() == cargo->GetPercentage()) - return el.get(); - } else { - if (el->GetName() == cargo->GetName() && - el->GetBasePrice() == cargo->GetBasePrice() && - el->GetRarity() == cargo->GetRarity()) - return el.get(); - } - } - } - - void RemoveFromStorage(Cargo* cargo) { - cargo_.erase(std::find_if(std::begin(cargo_), std::end(cargo_), - [cargo](const auto& el) { - return *el == *cargo; - })); - } -}; diff --git a/module2/solutions/exercise1.cpp b/module2/solutions/exercise5.cpp similarity index 65% rename from module2/solutions/exercise1.cpp rename to module2/solutions/exercise5.cpp index a25ebf118..cc652dedc 100644 --- a/module2/solutions/exercise1.cpp +++ b/module2/solutions/exercise5.cpp @@ -6,10 +6,10 @@ class Cargo { Cargo(size_t amount, std::string name, size_t base_price); virtual ~Cargo() = default; - virtual size_t GetPrice() const = 0; - virtual std::string GetName() const = 0; - virtual size_t GetAmount() const = 0; - virtual size_t GetBasePrice() const = 0; + virtual size_t getPrice() const = 0; + virtual std::string getName() const = 0; + virtual size_t getAmount() const = 0; + virtual size_t getBasePrice() const = 0; virtual Cargo& operator+=(size_t amount) = 0; virtual Cargo& operator-=(size_t amount) = 0; @@ -25,15 +25,15 @@ class Fruit : public Cargo { ~Fruit() override; // override from Cargo - size_t GetPrice() const override { + size_t getPrice() const override { if (time_elapsed_ >= expiry_date_) return 0; return static_cast( base_price_ * ((float)(expiry_date_ - time_elapsed_)) / expiry_date_); } - std::string GetName() const override { return name_; } - size_t GetAmount() const override { return amount_; } - size_t GetBasePrice() const override { return base_price_; } + std::string getName() const override { return name_; } + size_t getAmount() const override { return amount_; } + size_t getBasePrice() const override { return base_price_; } Cargo& operator--() { --amount_; return *this; @@ -47,8 +47,8 @@ class Fruit : public Cargo { return *this; } - size_t GetTimeElapsed() const { return time_elapsed_; } - size_t GetExpiryDate() const { return expiry_date_; } + size_t getTimeElapsed() const { return time_elapsed_; } + size_t getExpiryDate() const { return expiry_date_; } private: size_t time_elapsed_{0}; @@ -61,10 +61,10 @@ class Alcohol : public Cargo { ~Alcohol() override = default; // override from Cargo - size_t GetPrice() const override { return base_price_ * percentage_ / 96; } - std::string GetName() const override { return name_; } - size_t GetAmount() const override { return amount_; } - size_t GetBasePrice() const override { return base_price_; } + size_t getPrice() const override { return base_price_ * percentage_ / 96; } + std::string getName() const override { return name_; } + size_t getAmount() const override { return amount_; } + size_t getBasePrice() const override { return base_price_; } Cargo& operator+=(size_t amount) { amount_ += amount; return *this; @@ -74,7 +74,7 @@ class Alcohol : public Cargo { return *this; } - size_t GetPercentage() const { return percentage_; } + size_t getPercentage() const { return percentage_; } private: size_t percentage_; @@ -91,10 +91,10 @@ class Item : public Cargo { ~Item() override = default; // override from Cargo - size_t GetPrice() const override { return base_price_ * static_cast(rarity_); } - std::string GetName() const override { return name_; } - size_t GetAmount() const override { return amount_; } - size_t GetBasePrice() const override { return base_price_; } + size_t getPrice() const override { return base_price_ * static_cast(rarity_); } + std::string getName() const override { return name_; } + size_t getAmount() const override { return amount_; } + size_t getBasePrice() const override { return base_price_; } Cargo& operator+=(size_t amount) { amount_ += amount; return *this; @@ -104,7 +104,7 @@ class Item : public Cargo { return *this; } - Rarity GetRarity() const { return rarity_; } + Rarity getRarity() const { return rarity_; } private: Rarity rarity_; diff --git a/module2/solutions/exercise6.cpp b/module2/solutions/exercise6.cpp new file mode 100644 index 000000000..7374f04ff --- /dev/null +++ b/module2/solutions/exercise6.cpp @@ -0,0 +1,47 @@ +#include +#include + +class Cargo; + +class Ship { +public: + void load(std::unique_ptr cargo) { + if (auto match_cargo = findMatchCargo(cargo.get())) { + *match_cargo += cargo->getAmount(); + return; + } + cargo_.push_back(std::move(cargo)); + } + + void unload(Cargo* cargo) { + removeFromStorage(cargo); + } + + Cargo* findMatchCargo(Cargo* cargo) { + for (auto el : cargo_) { + if (el->getName() == "Fruit") { + if (el->getName() == cargo->getName() && + el->getBasePrice() == cargo->getBasePrice() && + el->getExpiryDate() == cargo->getExpiryDate()) + return el.get(); + } else if (el->getName() == "Alcohol") { + if (el->getName() == cargo->getName() && + el->getBasePrice() == cargo->getBasePrice() && + el->getPercentage() == cargo->getPercentage()) + return el.get(); + } else { + if (el->getName() == cargo->getName() && + el->getBasePrice() == cargo->getBasePrice() && + el->getRarity() == cargo->getRarity()) + return el.get(); + } + } + } + + void removeFromStorage(Cargo* cargo) { + cargo_.erase(std::find_if(std::begin(cargo_), std::end(cargo_), + [cargo](const auto& el) { + return *el == *cargo; + })); + } +}; diff --git a/module2/solutions/exercise3.cpp b/module2/solutions/exercise7.cpp similarity index 83% rename from module2/solutions/exercise3.cpp rename to module2/solutions/exercise7.cpp index d64f85256..c4b1e74d0 100644 --- a/module2/solutions/exercise3.cpp +++ b/module2/solutions/exercise7.cpp @@ -4,8 +4,8 @@ class DryFruit : public Fruit { public: // override from Cargo - std::string GetName() const override { return "Dry Fruit"; } - size_t GetPrice() const override { + std::string getName() const override { return "Dry Fruit"; } + size_t getPrice() const override { if (time_elapsed_ >= expiry_date_) return 0; return static_cast( diff --git a/module2/solutions/exercise4.cpp b/module2/solutions/exercise8.cpp similarity index 89% rename from module2/solutions/exercise4.cpp rename to module2/solutions/exercise8.cpp index a31dbd455..0963db555 100644 --- a/module2/solutions/exercise4.cpp +++ b/module2/solutions/exercise8.cpp @@ -6,7 +6,7 @@ class Coordinates { Coordinates() = default; Coordinates(size_t pos_x, size_t pos_y) : pos_x_(pos_x), pos_y_(pos_y) {} - static size_t Distance(const Coordinates& lhs, const Coordinates& rhs) { + static size_t distance(const Coordinates& lhs, const Coordinates& rhs) { return std::sqrt(std::pow((int)lhs.pos_x_ - (int)rhs.pos_x_, 2) + std::pow((int)lhs.pos_y_ - (int)rhs.pos_y_, 2)); } bool operator==(const Coordinates& lhs) const { From 43b51d7c6969db5ae921a29457d816e937e57137 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Ziobro=C5=84?= Date: Wed, 23 Jun 2021 12:27:57 +0200 Subject: [PATCH 07/25] module2 finished --- .github/workflows/module1.yml | 4 ++-- module1/13-project.md | 4 ++-- module2/09-recap.md | 2 +- module2/10-homework.md | 22 +++++++++++++++++++++- module2/11-project.md | 10 +++++----- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/.github/workflows/module1.yml b/.github/workflows/module1.yml index 675dc7f04..d9e96931b 100644 --- a/.github/workflows/module1.yml +++ b/.github/workflows/module1.yml @@ -12,7 +12,7 @@ jobs: working-directory: shm run: | chmod +x check.sh - sudo apt install -y pcregrep - ./check.sh CMakeLists.txt + # sudo apt install -y pcregrep + # ./check.sh CMakeLists.txt # In case of conflict do not remove above jobs. Just add your jobs below and remove this comment :) diff --git a/module1/13-project.md b/module1/13-project.md index 6fac23232..b573407c7 100644 --- a/module1/13-project.md +++ b/module1/13-project.md @@ -50,8 +50,8 @@ ___ * Każde dostarczone zadanie to 5 punktów * 15 punktów za dostarczenie wszystkich 8 zadań do 28.06.2021 (poniedziałek) do 23:59 -* brak punktów bonusowych za dostarczenie tylko części zadań przed 28.06. -* 8 punktów za pracę w grupie dla każdej osoby z grupy. +* brak punktów bonusowych za dostarczenie tylko części zadań przed 28.06 +* 8 punktów za pracę w grupie dla każdej osoby z grupy ___ diff --git a/module2/09-recap.md b/module2/09-recap.md index d10a81ffc..aaf40da9c 100644 --- a/module2/09-recap.md +++ b/module2/09-recap.md @@ -12,7 +12,7 @@ ___ ## Co pamiętasz z dzisiaj? -### Napisz na czacie jak najwięcej haseł +### Zapisz w notatkach jak najwięcej haseł 1. dziedziczenie diff --git a/module2/10-homework.md b/module2/10-homework.md index ace7d5307..d5c6cfaca 100644 --- a/module2/10-homework.md +++ b/module2/10-homework.md @@ -14,5 +14,25 @@ ___ * Dowiedz się czym jest problem diamentowy * Poczytaj o zasadach SOLID, dotyczących pisania dobrego kodu obiektowego -* Lektura o wzorcach projektowych z przykładami w C++ - [refactoring.guru](https://refactoring.guru/design-patterns) * Spróbujcie w grupie metodą Copy & Paste dorzucić system budowania cmake do projektu. W tym celu popatrzcie na dotychczasowe zadania domowe i plik CMakeLists.txt. + +___ + +### Post-work + +* Poczytaj o [Null object pattern](https://en.wikipedia.org/wiki/Null_object_pattern). Może się przydać w `university-db` +* `university-db` ciąg dalszy (14 XP) + +___ + +### `university-db` + +#### Wymagania (14 XP) + +* Przechowywanie rekordów pracowników uczelni o strukturze: imię, nazwisko, PESEL, płeć, adres, zarobki +* Wszystkie osoby niezależnie czy będą to pracownicy, czy studenci mają być trzymani w jednym kontenerze +* Wypełnianie bazy danych sztucznymi danymi (generowanie danych) +* Modyfikacja zarobków wyszukując osobę po numerze PESEL (problematyczne) +* Sortowanie po zarobkach (problematyczne) + +Natkniecie się tutaj na typowe problemy związane z projektowaniem kodu zorientowanego obiektowo. Pomyślcie wspólnie na Planningu lub podczas Daily jak je rozwiązać. diff --git a/module2/11-project.md b/module2/11-project.md index bd957dce3..86db524e5 100644 --- a/module2/11-project.md +++ b/module2/11-project.md @@ -14,7 +14,7 @@ ___ Wykorzystajcie kod napisany podczas zajęć. Możecie też skorzystać z kodu w katalogu [solutions](solutions) -Projekt grupowy - kontynuacja. Możecie zmienić grupę jeśli chcecie ;) +Kontynuujemy i rozwijamy dotychczasową aplikację 🙂 ___ @@ -29,11 +29,11 @@ ___ ### Punktacja -* 3 pierwsze zadania - 5 punktów +* zadania 1, 2, 3 - 5 punktów * zadania 4, 5, 6 - 8 punktów -* 20 punktów za dostarczenie wszystkich 6 zadań przed 05.07.2020 (niedziela) do 23:59 -* brak punktów bonusowych za dostarczenie tylko części zadań przed 05.07 -* 6 punktów za pracę w grupie dla każdej osoby z grupy. +* 15 punktów za dostarczenie wszystkich 6 zadań przed 18.07.2020 (niedziela) do 23:59 +* brak punktów bonusowych za dostarczenie tylko części zadań w terminie +* 6 punktów za pracę w grupie dla każdej osoby z grupy ___ From 0b7e6a3ed54604ab05716112d23288282681d99c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Ziobro=C5=84?= Date: Wed, 23 Jun 2021 14:18:19 +0200 Subject: [PATCH 08/25] md rename --- module2/02-multiple-inheritance.md | 359 ------------------ module2/{03-virtual.md => 02-virtual.md} | 0 module2/{04-tasks.md => 03-tasks.md} | 0 ...{05-polymorphism.md => 04-polymorphism.md} | 0 module2/{06-tasks.md => 05-tasks.md} | 0 module2/{07-static.md => 06-static.md} | 0 module2/{08-tasks.md => 07-tasks.md} | 0 module2/{09-recap.md => 08-recap.md} | 0 module2/{10-homework.md => 09-homework.md} | 0 module2/{11-project.md => 10-project.md} | 0 module2/index.html | 22 +- 11 files changed, 9 insertions(+), 372 deletions(-) delete mode 100644 module2/02-multiple-inheritance.md rename module2/{03-virtual.md => 02-virtual.md} (100%) rename module2/{04-tasks.md => 03-tasks.md} (100%) rename module2/{05-polymorphism.md => 04-polymorphism.md} (100%) rename module2/{06-tasks.md => 05-tasks.md} (100%) rename module2/{07-static.md => 06-static.md} (100%) rename module2/{08-tasks.md => 07-tasks.md} (100%) rename module2/{09-recap.md => 08-recap.md} (100%) rename module2/{10-homework.md => 09-homework.md} (100%) rename module2/{11-project.md => 10-project.md} (100%) diff --git a/module2/02-multiple-inheritance.md b/module2/02-multiple-inheritance.md deleted file mode 100644 index e34845f02..000000000 --- a/module2/02-multiple-inheritance.md +++ /dev/null @@ -1,359 +0,0 @@ - - -# Programowanie obiektowe - -## Dziedziczenie - - - Coders School - - -___ - -## Wprowadzenie do dziedziczenia - -Podczas implementacji klas, często możemy zauważyć, że część cech składowych klasy można wykorzystać także w innych klasach. - - -Weźmy pod lupę klasę `Computer`. Jeżeli chcielibyśmy utworzyć klasy: `Laptop`, `PC`, `Tablet`, to część metod oraz składowych klasy musielibyśmy powielić. - - -___ - -## `class Computer` - -```cpp -class Computer { -public: - void turnOn(); - void powerOff(); - void restart(); - -private: - Processor processor_; - Drive drive_; - Motherboard motherboard_; - GraphicsCard graphics_card_; - Memory memory_; -}; -``` - -___ - -## `class Laptop` - -```cpp -class Laptop { -public: - void turnOn(); - void powerOff(); - void restart(); - void display(); - void getUserInput(); - -private: - Processor processor_; - Drive drive_; - Motherboard motherboard_; - GraphicsCard graphics_card_; - Memory memory_; - Screen screen_; - Keyboard keyboard_; -}; -``` - -___ - -## `class Tablet` - -```cpp -class Tablet { -public: - void turnOn(); - void powerOff(); - void restart(); - void display(); - void getUserInput(); - -private: - Processor processor_; - Drive drive_; - Motherboard motherboard_; - GraphicsCard graphics_card_; - Memory memory_; - Screen screen_; -}; -``` - -___ - - -## Jak uprościć strukturę naszego programu? - -```cpp -class Computer { -public: - void turnOn(); - void powerOff(); - void restart(); - -protected: - Processor processor_; - Drive drive_; - Motherboard motherboard_; - GraphicsCard graphics_card_; - Memory memory_; -}; - - -class Laptop : public Computer { -public: - void display(); - void getUserInput(); - -private: - Screen screen_; - Keyboard keyboard_; -}; - - -class Tablet : public Computer { -public: - void display(); - void getUserInput(); - -private: - Screen screen_; -}; -``` - - -___ - -## Klasy bazowe i pochodne - -Klasa, po której dziedziczymy, nazywają się **klasą bazową (base class)**. - - -Klasa, która dziedziczy nazywa się **klasą pochodną (derived class)**. - - -Inaczej, klasa, po której dziedziczymy to rodzic (parent class). - - -Klasa, która dziedziczy to dziecko (child class). - - -___ - -### Co z metodami klas `Laptop` i `Tablet`? - -#### Czy można wydzielić kolejną klasę? - - -```cpp -void display(); -void getUserInput(); -``` - - -___ - -## Klasa `Screen` i `TouchScreen` - -Załóżmy, że dodajemy klasę `Screen`. Klasa ta wyświetla na bieżąco interfejs użytkownika. - - -Chcemy też stworzyć klasę reprezentującą ekran dotykowy - `TouchScreen`, który również umożliwia odczyt akcji od użytkownika i ich wyświetlanie. - - -
-
- -```cpp -class Screen { -public: - void display(); - -private: - void process(); - - Monitor monitor_; -}; -``` - - -
-
- -```cpp -class TouchScreen { -public: - void display(); - void getUserInput(); - -private: - void process(); - void displayKeyboard(); - - Monitor monitor_; -}; -``` - - -
-
- -### Jak uprościć powyższy kod? - - -___ - -## Wykorzystanie dziedziczenia do uproszczenia kodu - -```cpp -class Screen { -public: - void display(); - -private: - void process(); - - Monitor monitor_; -}; -``` - - -```cpp -class TouchScreen : public Screen { -public: - void getUserInput(); - -private: - void displayKeyboard(); -}; -``` - - -___ - -## Wielodziedziczenie - -```cpp -class Screen { -public: - void display(); - -private: - void process(); - - Monitor monitor_; -}; - -class TouchScreen : public Screen { -public: - void getUserInput(); - -private: - void displayKeyboard(); -}; - -class Computer { -public: - void turnOn(); - void powerOff(); - void restart(); - -protected: - Processor processor_; - Drive drive_; - Motherboard motherboard_; - GraphicsCard graphics_card_; - Memory memory_; -}; - -class Laptop : public Computer, - public Screen { - Keyboard keyboard_; -}; - -class Tablet : public Computer, - public TouchScreen { -}; -``` - -___ - -## Wielodziedziczenie - disclaimer - -Wielodziedziczenie to dziedziczenie z kliku klas bazowych. - -Wybór implementacji zależy od programisty. - - -Nie zawsze wielodziedziczenie będzie najlepszym rozwiązaniem. - - -Należy się zawsze zastanowić czy dziedziczenie po konkretnej klasie uprości nam program i czy nie będzie powodować żadnych komplikacji w dalszym procesie rozbudowy naszego programu. - - -Najwyżej trzeba będzie zrefaktoryzować program ;) - - -___ - -## Dziedziczenie - problemy - -```cpp -struct Bird { - fly(); - makeSound(); -}; - -struct Penguin { - swim(); - makeSound(); -}; - -// Hummingbird is the type of bird which makes so little sound so it -// can be said that it makes no sound. -struct Hummingbird { - fly(); -}; -``` - -___ - - -## Dziedziczenie - zasada LSP - -Jeżeli spróbujemy teraz uprościć klasę poprzez dziedziczenie pojawi się problem: - -```cpp -struct Bird { - fly(); - makeSound(); -}; - -struct Penguin : public Bird { - fly(); // But I can't fly! - swim(); - makeSound(); -}; - -struct Hummingbird : public Bird { - fly(); - makeSound(); // But I don't make sound! -}; -``` - -Jeszcze bardziej utrudnimy sytuację, gdy w przyszłości dodamy sobie kolejne klasy jak Struś. Zawsze przed implementacją musimy się zastanowić jak podzielić odpowiedzialność na poszczególne klasy, aby -uniknąć podobnych problemów. - -___ - -### Dla ciekawskich - -Poczytajcie o zasadzie Liskov Substitution Principle (LSP). Mówi ona jak powinno / nie powinno się projektować kodu obiektowego. Ta zasada została złamana w ostatnim przykładzie. - -Możecie też poczytać o wszystkich zasadach SOLID. diff --git a/module2/03-virtual.md b/module2/02-virtual.md similarity index 100% rename from module2/03-virtual.md rename to module2/02-virtual.md diff --git a/module2/04-tasks.md b/module2/03-tasks.md similarity index 100% rename from module2/04-tasks.md rename to module2/03-tasks.md diff --git a/module2/05-polymorphism.md b/module2/04-polymorphism.md similarity index 100% rename from module2/05-polymorphism.md rename to module2/04-polymorphism.md diff --git a/module2/06-tasks.md b/module2/05-tasks.md similarity index 100% rename from module2/06-tasks.md rename to module2/05-tasks.md diff --git a/module2/07-static.md b/module2/06-static.md similarity index 100% rename from module2/07-static.md rename to module2/06-static.md diff --git a/module2/08-tasks.md b/module2/07-tasks.md similarity index 100% rename from module2/08-tasks.md rename to module2/07-tasks.md diff --git a/module2/09-recap.md b/module2/08-recap.md similarity index 100% rename from module2/09-recap.md rename to module2/08-recap.md diff --git a/module2/10-homework.md b/module2/09-homework.md similarity index 100% rename from module2/10-homework.md rename to module2/09-homework.md diff --git a/module2/11-project.md b/module2/10-project.md similarity index 100% rename from module2/11-project.md rename to module2/10-project.md diff --git a/module2/index.html b/module2/index.html index e8ed7b5d1..2f358c691 100644 --- a/module2/index.html +++ b/module2/index.html @@ -68,43 +68,39 @@

Łukasz Ziobroń

data-separator-vertical="^___" data-separator-notes="^Note:"> -
-
-
-
-
-
-
-
-
-
-
From 0c2d7b80c736e4aa7ed59c0625eb07db4c1ea7ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Ziobro=C5=84?= Date: Thu, 24 Jun 2021 10:46:46 +0200 Subject: [PATCH 09/25] Fix number -> amount --- module1/09-operators.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module1/09-operators.md b/module1/09-operators.md index fc14da274..527f92fe2 100644 --- a/module1/09-operators.md +++ b/module1/09-operators.md @@ -58,7 +58,7 @@ T& T::operator +=(const T2& b); ```cpp Computer& Computer::operator+=(Memory amount) { - ramAmount_ += number; + ramAmount_ += amount; std::cout << "Ram memory extended\n"; return *this; } From fb9d392e05e65fd84155c6794bd17c3a462af8e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Ziobro=C5=84?= Date: Tue, 29 Jun 2021 10:37:39 +0200 Subject: [PATCH 10/25] added getUserInput() to the Laptop class --- module2/01-inheritance.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/module2/01-inheritance.md b/module2/01-inheritance.md index bf1f2c0e9..75047c6a2 100644 --- a/module2/01-inheritance.md +++ b/module2/01-inheritance.md @@ -275,6 +275,8 @@ protected: class Laptop : public Computer, public Screen { Keyboard keyboard_; +public: + void getUserInput(); }; class Tablet : public Computer, From 41634014f1940ccb7040065229a47da799d33a4a Mon Sep 17 00:00:00 2001 From: Barbara Tessema <74462021+BarTes8@users.noreply.github.com> Date: Sat, 3 Jul 2021 11:33:52 +0200 Subject: [PATCH 11/25] Update README.md (#89) --- shm/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shm/README.md b/shm/README.md index 7a5027430..b64be1d21 100644 --- a/shm/README.md +++ b/shm/README.md @@ -27,7 +27,7 @@ ___ ## Zadanie 4 -Napisz klasę `Map`, która będzie posiadała `std::vector` przechowujący wszystkie wyspy na mapie, oraz zmienną `Island* currentPosition_` określającą aktualną pozycję gracza na mapie. +Napisz klasę `Map`, która będzie posiadała `std::vector` przechowujący wszystkie wyspy na mapie, oraz zmienną `Island* currentPosition_` określającą aktualną pozycję gracza na mapie. ___ @@ -45,7 +45,7 @@ W klasie `Map` napisz funkcję `Island* getIsland(const Island::Coordinates& coordinate)` -Powinna ona przeszukać `std::vector` i zwrócić szukaną wyspę. +Powinna ona przeszukać `std::vector` i zwrócić szukaną wyspę. ___ From 90ee55a69224c676c0c836446b5d583a59a9bfc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Ziobro=C5=84?= Date: Mon, 5 Jul 2021 17:37:12 +0200 Subject: [PATCH 12/25] Script return values changed --- shm/check.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shm/check.sh b/shm/check.sh index fd537a421..2451f829a 100644 --- a/shm/check.sh +++ b/shm/check.sh @@ -85,3 +85,5 @@ if [ $failed == 0 ]; then else echo -e ❗️❗️❗️ $RED $failed "CHECKS FAILED" ❗️❗️❗️ $DEFAULT fi + +exit $failed From 09391a3c510382daad942f848de5267943f27fb5 Mon Sep 17 00:00:00 2001 From: Kacu Date: Wed, 7 Jul 2021 15:02:05 +0200 Subject: [PATCH 13/25] completed SHM-Vol.1 --- shm/CMakeLists.txt | 42 +++++++++++++++++++++++++++ shm/main.cpp | 20 +++++++++++++ shm/source/Cargo.hpp | 42 +++++++++++++++++++++++++++ shm/source/Fortune.hpp | 42 +++++++++++++++++++++++++++ shm/source/Island.hpp | 27 ++++++++++++++++++ shm/source/Map.hpp | 63 +++++++++++++++++++++++++++++++++++++++++ shm/source/Player.hpp | 61 ++++++++++++++++++++++++++++++++++++++++ shm/source/Ship.hpp | 64 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 361 insertions(+) create mode 100644 shm/main.cpp create mode 100644 shm/source/Cargo.hpp create mode 100644 shm/source/Fortune.hpp create mode 100644 shm/source/Island.hpp create mode 100644 shm/source/Map.hpp create mode 100644 shm/source/Player.hpp create mode 100644 shm/source/Ship.hpp diff --git a/shm/CMakeLists.txt b/shm/CMakeLists.txt index e69de29bb..dabcad808 100644 --- a/shm/CMakeLists.txt +++ b/shm/CMakeLists.txt @@ -0,0 +1,42 @@ +cmake_minimum_required(VERSION 3.10) + +set(CMAKE_C_COMPILER "/usr/bin/gcc") +set(CMAKE_CXX_COMPILER "/usr/bin/g++") +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# set the project name +project(SHM) + +# set executables +set(THIS_PROJECT_SRC_DIRECTORIES +) +set(THIS_PROJECT_TESTS_DIRECTORIES +) + +# set flags +set(THIS_PROJECT_FLAGS + -Wall + -Werror + -Weffc++ + -Wextra + -pedantic + -Wconversion + -O3 +) + +# add all executables +add_executable(${PROJECT_NAME} + main.cpp + ${THIS_PROJECT_SRC_DIRECTORIES} +) + +# specify compile options for target +target_compile_options(${PROJECT_NAME} PRIVATE ${THIS_PROJECT_FLAGS}) + +# enable standard cpp17 +target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17) + +# link ncurses static lib +target_link_libraries(${PROJECT_NAME} ncurses) + diff --git a/shm/main.cpp b/shm/main.cpp new file mode 100644 index 000000000..2ac3cb4c5 --- /dev/null +++ b/shm/main.cpp @@ -0,0 +1,20 @@ +#include + +#include "source/Cargo.hpp" +#include "source/Map.hpp" +#include "source/Player.hpp" + +int main() { + Map(); + Player a; + Player b; + Player c; + + c.giveAwayShip(); + + std::cout << "\n" << a.takeOverShip(b.giveAwayShip()) + << "\n" << b.takeOverShip(a.giveAwayShip()); + + std::cout << "\n\n\n"; + return 0; +} diff --git a/shm/source/Cargo.hpp b/shm/source/Cargo.hpp new file mode 100644 index 000000000..88001c36a --- /dev/null +++ b/shm/source/Cargo.hpp @@ -0,0 +1,42 @@ +#pragma once +#include + +class Cargo { +public: + Cargo(const std::string& name, const size_t amount, const size_t basePrice): + name_(name), + amount_(amount), + basePrice_(basePrice) + {} + + bool operator==(const Cargo& cargo) const { + if (name_ != cargo.getName()) { + return false; + } + if (amount_ != cargo.getAmount()) { + return false; + } + if (basePrice_ != cargo.getBasePrice()) { + return false; + } + return true; + } + + Cargo& operator+=(size_t amount) { + amount_ += amount; + return *this; + } + Cargo& operator-=(size_t amount) { + amount_ -= amount; + return *this; + } + + std::string getName() const { return name_; } + size_t getAmount() const { return amount_; } + size_t getBasePrice() const { return basePrice_; } + +protected: + std::string name_; + size_t amount_; + size_t basePrice_; +}; diff --git a/shm/source/Fortune.hpp b/shm/source/Fortune.hpp new file mode 100644 index 000000000..6ece6a8d6 --- /dev/null +++ b/shm/source/Fortune.hpp @@ -0,0 +1,42 @@ +#pragma once +#include +#include +#include +#include + +struct Fortune { + static int getNumber(int first, int last) { + const int result = makeRandomNumber(first, last); + if (result < first || result > last) { + throw std::logic_error("Fortune::getNumber(int,int): Result value out of scope"); + } + return result; + } + + static std::vector getNumbersEvenlyDistributed(int first, int last, const int n) { + if (n < 1) { + return {}; + } + std::vector result(n); + for (int i = 0; i < n; ++i) { + int localFirst = (last / n * i) + first; + int localLast = (last / n * (i + 1)) + first; + result.at(i) = (getNumber(localFirst, localLast)); + } + return result; + } + + static void shuffle(std::vector& vector) { + std::random_device random_device; + std::mt19937 random_engine(random_device()); + std::shuffle(vector.begin(), vector.end(), random_engine); + } + +private: + static int makeRandomNumber(const int first, const int last) { + std::random_device random_device; + std::mt19937 random_engine(random_device()); + std::uniform_int_distribution distribution(first, last); + return distribution(random_engine); + } +}; \ No newline at end of file diff --git a/shm/source/Island.hpp b/shm/source/Island.hpp new file mode 100644 index 000000000..798cfd84f --- /dev/null +++ b/shm/source/Island.hpp @@ -0,0 +1,27 @@ +#pragma once + +struct Island { + struct Coordinates { + int positionX; + int positionY; + + Coordinates(const int x, const int y): + positionX(x), + positionY(y) + {} + + bool operator==(const Coordinates& coordinates) { + return (positionX == coordinates.positionX && positionY == coordinates.positionY); + } + }; + +public: + Island(const Coordinates& coordinates): + position_(coordinates) + {} + + Coordinates getPosition() const { return position_; } + +private: + Coordinates position_; +}; \ No newline at end of file diff --git a/shm/source/Map.hpp b/shm/source/Map.hpp new file mode 100644 index 000000000..45d4d04b8 --- /dev/null +++ b/shm/source/Map.hpp @@ -0,0 +1,63 @@ +#pragma once +#include +#include + +#include "Fortune.hpp" +#include "Island.hpp" + +class Map { + size_t horizonLimit_; + std::vector islands_; + Island* currentPosition_; + +public: + Map(): + horizonLimit_(100), + islands_(generateIslands(10)), + currentPosition_() + {} + + Map(const Map& map): + horizonLimit_(map.horizonLimit_), + islands_(map.islands_), + currentPosition_(map.currentPosition_) + {} + + Map& operator=(const Map& map) { + horizonLimit_ = map.horizonLimit_; + islands_ = map.islands_; + currentPosition_ = currentPosition_; + return *this; + } + + Island* getIsland(const Island::Coordinates& coordinate) { + for (auto& island : islands_) { + if (island.getPosition() == coordinate) { + return &island; + } + } + return nullptr; + } + +private: + std::vector generateIslands(const size_t numberOfIslands) const { + if (numberOfIslands < 1) { + return {}; + } + auto xyPositions = generateRandomPositions(numberOfIslands); + std::vector islands; + islands.reserve(xyPositions.size() / 2); + for (size_t i = 1; i < xyPositions.size(); i += 2) { + islands.emplace_back(Island::Coordinates(xyPositions.at(i - 1), xyPositions.at(i))); + } + return islands; + } + + std::vector generateRandomPositions(const size_t n) const { + auto positions = Fortune::getNumbersEvenlyDistributed(0, static_cast(horizonLimit_), static_cast(n)); + auto yPositions = Fortune::getNumbersEvenlyDistributed(0, static_cast(horizonLimit_), static_cast(n)); + positions.insert(positions.end(), yPositions.begin(), yPositions.end()); + Fortune::shuffle(positions); + return positions; + } +}; \ No newline at end of file diff --git a/shm/source/Player.hpp b/shm/source/Player.hpp new file mode 100644 index 000000000..304b41d25 --- /dev/null +++ b/shm/source/Player.hpp @@ -0,0 +1,61 @@ +#pragma once +#include + +#include "Ship.hpp" + +class Player { + std::unique_ptr ship_; + int money_; + size_t availableSpace_; + +public: + Player(): + ship_(std::make_unique()), + money_(0), + availableSpace_() + {} + + int getMoney() const { return money_; } + + size_t getAvailableSpace() const { return availableSpace_; } + + size_t getSpeed() const { + if (ship_) { + return ship_->getSpeed(); + } + return 0; + } + + Cargo* getCargo(size_t index) const { + if (ship_) { + return ship_->getCargo(index); + } + return nullptr; + } + + std::string takeOverShip(std::unique_ptr someoneElsesShip) { + if (!someoneElsesShip) { + return "there is no ship"; + } + if (ship_) { + return "already have one"; + } + ship_ = std::move(someoneElsesShip); + return "yarr"; + } + + std::unique_ptr giveAwayShip() { + return std::move(ship_); + } + + int calculateAvailableSpace() const { + size_t index = 0; + size_t count = 0; + auto cargo = getCargo(index); + while (cargo) { + count += cargo->getAmount(); + cargo = getCargo(++index); + } + return static_cast(getAvailableSpace() - count); + } +}; \ No newline at end of file diff --git a/shm/source/Ship.hpp b/shm/source/Ship.hpp new file mode 100644 index 000000000..12918d3ef --- /dev/null +++ b/shm/source/Ship.hpp @@ -0,0 +1,64 @@ +#pragma once +#include + +#include "Cargo.hpp" + +class Ship { +public: + Ship() + : capacity_() + , maxCrew_() + , crew_() + , speed_() + , name_() + , cargo_() + , id_(-1) + {} + + Ship(int capacity, int maxCrew, int speed, const std::string& name, size_t id) + : capacity_(capacity) + , maxCrew_(maxCrew) + , crew_(0) + , speed_(speed) + , name_(name) + , cargo_() + , id_(id) + {} + + Ship(int maxCrew, int speed, size_t id) + : Ship(0, maxCrew, speed, "", id) + {} + + void setName(const std::string& name) { name_ = name; } + + Ship& operator-=(size_t num) { + crew_ -= num; + return *this; + } + Ship& operator+=(size_t num) { + crew_ += num; + return *this; + } + + 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_; } + + Cargo* getCargo(const size_t index) { + if (index < cargo_.size()) { + return &cargo_.at(index); + } + return nullptr; + } + +private: + size_t capacity_; + size_t maxCrew_; + size_t crew_; + size_t speed_; + std::string name_; + std::vector cargo_; + const size_t id_; +}; From 06bb9ccf66e9faaa75923ff3b4038fa33991777a Mon Sep 17 00:00:00 2001 From: KacperKaleta Date: Fri, 23 Jul 2021 17:27:19 +0200 Subject: [PATCH 14/25] 8 moved map implementation into source file, simplified map class (#12) Co-authored-by: Kacu --- shm/CMakeLists.txt | 3 ++- shm/source/Map.cpp | 35 ++++++++++++++++++++++++++++++++ shm/source/Map.hpp | 50 ++++------------------------------------------ 3 files changed, 41 insertions(+), 47 deletions(-) create mode 100644 shm/source/Map.cpp diff --git a/shm/CMakeLists.txt b/shm/CMakeLists.txt index dabcad808..06f0a396f 100644 --- a/shm/CMakeLists.txt +++ b/shm/CMakeLists.txt @@ -10,6 +10,7 @@ project(SHM) # set executables set(THIS_PROJECT_SRC_DIRECTORIES + source/Map.cpp ) set(THIS_PROJECT_TESTS_DIRECTORIES ) @@ -18,7 +19,7 @@ set(THIS_PROJECT_TESTS_DIRECTORIES set(THIS_PROJECT_FLAGS -Wall -Werror - -Weffc++ + #-Weffc++ -Wextra -pedantic -Wconversion diff --git a/shm/source/Map.cpp b/shm/source/Map.cpp new file mode 100644 index 000000000..daade0b10 --- /dev/null +++ b/shm/source/Map.cpp @@ -0,0 +1,35 @@ +#include "Map.hpp" + + +Map::Map(): + horizonLimit_(100), + islands_(generateIslands(10)), + currentPosition_() +{} + +Island* Map::getIsland(const Island::Coordinates& coordinate) { + auto result = std::find_if(islands_.begin(), islands_.end(), [coordinate](const Island& island){ return island.getPosition() == coordinate; }); + if (result != islands_.end()) { + return &(*result); + } + return nullptr; +} + +std::vector Map::generateIslands(const size_t numberOfIslands) const { + if (numberOfIslands < 1) { + return {}; + } + auto xyPositions = generateRandomPositions(numberOfIslands * 2); + std::vector islands; + islands.reserve(xyPositions.size() / 2); + for (size_t i = 1; i < xyPositions.size(); i += 2) { + islands.emplace_back(Island::Coordinates(xyPositions.at(i - 1), xyPositions.at(i))); + } + return islands; +} + +std::vector Map::generateRandomPositions(const size_t numberOfPositions) const { + auto positions = Fortune::getNumbersEvenlyDistributed(0, static_cast(horizonLimit_), static_cast(numberOfPositions)); + Fortune::shuffle(positions); + return positions; +} \ No newline at end of file diff --git a/shm/source/Map.hpp b/shm/source/Map.hpp index 45d4d04b8..8464713b2 100644 --- a/shm/source/Map.hpp +++ b/shm/source/Map.hpp @@ -11,53 +11,11 @@ class Map { Island* currentPosition_; public: - Map(): - horizonLimit_(100), - islands_(generateIslands(10)), - currentPosition_() - {} + Map(); - Map(const Map& map): - horizonLimit_(map.horizonLimit_), - islands_(map.islands_), - currentPosition_(map.currentPosition_) - {} - - Map& operator=(const Map& map) { - horizonLimit_ = map.horizonLimit_; - islands_ = map.islands_; - currentPosition_ = currentPosition_; - return *this; - } - - Island* getIsland(const Island::Coordinates& coordinate) { - for (auto& island : islands_) { - if (island.getPosition() == coordinate) { - return &island; - } - } - return nullptr; - } + Island* getIsland(const Island::Coordinates& coordinate); private: - std::vector generateIslands(const size_t numberOfIslands) const { - if (numberOfIslands < 1) { - return {}; - } - auto xyPositions = generateRandomPositions(numberOfIslands); - std::vector islands; - islands.reserve(xyPositions.size() / 2); - for (size_t i = 1; i < xyPositions.size(); i += 2) { - islands.emplace_back(Island::Coordinates(xyPositions.at(i - 1), xyPositions.at(i))); - } - return islands; - } - - std::vector generateRandomPositions(const size_t n) const { - auto positions = Fortune::getNumbersEvenlyDistributed(0, static_cast(horizonLimit_), static_cast(n)); - auto yPositions = Fortune::getNumbersEvenlyDistributed(0, static_cast(horizonLimit_), static_cast(n)); - positions.insert(positions.end(), yPositions.begin(), yPositions.end()); - Fortune::shuffle(positions); - return positions; - } + std::vector generateIslands(const size_t numberOfIslands) const; + std::vector generateRandomPositions(const size_t n) const; }; \ No newline at end of file From d91324f4c356ac864c64cc3d8432b89485ee93db Mon Sep 17 00:00:00 2001 From: KacperKaleta Date: Mon, 26 Jul 2021 12:14:22 +0200 Subject: [PATCH 15/25] 6 moved definitions from Fortune.hpp file into Fortune.cpp source file (#11) * 6 moved definitions from Fortune.hpp file into Fortune.cpp source file * 6 changed Fortune class into namespace Co-authored-by: Kacu --- shm/CMakeLists.txt | 1 + shm/source/Fortune.cpp | 32 ++++++++++++++++++++++++++++++ shm/source/Fortune.hpp | 44 ++++++------------------------------------ 3 files changed, 39 insertions(+), 38 deletions(-) create mode 100644 shm/source/Fortune.cpp diff --git a/shm/CMakeLists.txt b/shm/CMakeLists.txt index 06f0a396f..63e0b023e 100644 --- a/shm/CMakeLists.txt +++ b/shm/CMakeLists.txt @@ -10,6 +10,7 @@ project(SHM) # set executables set(THIS_PROJECT_SRC_DIRECTORIES + source/Fortune.cpp source/Map.cpp ) set(THIS_PROJECT_TESTS_DIRECTORIES diff --git a/shm/source/Fortune.cpp b/shm/source/Fortune.cpp new file mode 100644 index 000000000..6376f62e4 --- /dev/null +++ b/shm/source/Fortune.cpp @@ -0,0 +1,32 @@ +#include "Fortune.hpp" + +std::random_device randomDevice; +std::mt19937 randomEngine(randomDevice()); + +int Fortune::getNumber(int first, int last) { + if (first > last) { + std::swap(first, last); + } + std::uniform_int_distribution distribution(first, last); + return distribution(randomEngine); +} + +std::vector Fortune::getNumbersEvenlyDistributed(int first, int last, const int n) { + if (n < 1) { + return {}; + } + if (first > last) { + std::swap(first, last); + } + std::vector result(n); + for (int i = 0; i < n; ++i) { + int localFirst = (last / n * i) + first; + int localLast = (last / n * (i + 1)) + first; + result.at(i) = (getNumber(localFirst, localLast)); + } + return result; +} + +void Fortune::shuffle(std::vector& vector) { + std::shuffle(vector.begin(), vector.end(), randomEngine); +} diff --git a/shm/source/Fortune.hpp b/shm/source/Fortune.hpp index 6ece6a8d6..df5252ae6 100644 --- a/shm/source/Fortune.hpp +++ b/shm/source/Fortune.hpp @@ -1,42 +1,10 @@ #pragma once -#include +#include #include -#include #include -struct Fortune { - static int getNumber(int first, int last) { - const int result = makeRandomNumber(first, last); - if (result < first || result > last) { - throw std::logic_error("Fortune::getNumber(int,int): Result value out of scope"); - } - return result; - } - - static std::vector getNumbersEvenlyDistributed(int first, int last, const int n) { - if (n < 1) { - return {}; - } - std::vector result(n); - for (int i = 0; i < n; ++i) { - int localFirst = (last / n * i) + first; - int localLast = (last / n * (i + 1)) + first; - result.at(i) = (getNumber(localFirst, localLast)); - } - return result; - } - - static void shuffle(std::vector& vector) { - std::random_device random_device; - std::mt19937 random_engine(random_device()); - std::shuffle(vector.begin(), vector.end(), random_engine); - } - -private: - static int makeRandomNumber(const int first, const int last) { - std::random_device random_device; - std::mt19937 random_engine(random_device()); - std::uniform_int_distribution distribution(first, last); - return distribution(random_engine); - } -}; \ No newline at end of file +namespace Fortune { + int getNumber(int first, int last); + std::vector getNumbersEvenlyDistributed(int first, int last, const int n); + void shuffle(std::vector& vector); +} \ No newline at end of file From 242ff664c7bae4f67b6e8ab3f413385e0c31a950 Mon Sep 17 00:00:00 2001 From: KacperKaleta Date: Mon, 26 Jul 2021 12:18:11 +0200 Subject: [PATCH 16/25] 9 moved player definitions into source file (#24) Co-authored-by: Kacu --- shm/source/Player.cpp | 56 +++++++++++++++++++++++++++++ shm/source/Player.hpp | 82 +++++++++++++++---------------------------- 2 files changed, 84 insertions(+), 54 deletions(-) create mode 100644 shm/source/Player.cpp diff --git a/shm/source/Player.cpp b/shm/source/Player.cpp new file mode 100644 index 000000000..6406c3e66 --- /dev/null +++ b/shm/source/Player.cpp @@ -0,0 +1,56 @@ +#include "Player.hpp" +#include "Ship.hpp" +#include "Cargo.hpp" + +#include +#include + +Player::Player(): + ship_(nullptr), + money_(0), + maxAvailableSpace_(100), + availableSpace_({0, false}) +{} + +size_t Player::getMoney() const { + return money_; +} + +size_t Player::getAvailableSpace() { + if (!availableSpace_.second) { + calculateAvailableSpace(); + } + return availableSpace_.first; +} + +size_t Player::getSpeed() const { + if (ship_) { + return ship_->getSpeed(); + } + return 0; +} + +Cargo* Player::getCargo(size_t index) const { + if (ship_) { + return ship_->getCargo(index); + } + return nullptr; +} + +//void Player::PayCrew(size_t money) override; + +//void Player::PurchaseCargo(std::unique_ptr cargo, size_t price); + +//void Player::SellCargo(Cargo* cargo, size_t price); + +//void Player::PrintCargo() const; + +void Player::calculateAvailableSpace() { + if (!ship_) { + return; + } + const auto& allCargo = ship_->getCargo(); + size_t count = std::accumulate(allCargo.begin(), allCargo.end(), 0, [](int sum, const auto& cargo) { return sum + cargo->getAmount(); }); + availableSpace_.first = maxAvailableSpace_ - count; + availableSpace_.second = true; +} diff --git a/shm/source/Player.hpp b/shm/source/Player.hpp index 304b41d25..8d235b4f7 100644 --- a/shm/source/Player.hpp +++ b/shm/source/Player.hpp @@ -1,61 +1,35 @@ #pragma once + #include -#include "Ship.hpp" +class Ship; +class Cargo; class Player { - std::unique_ptr ship_; - int money_; - size_t availableSpace_; + Ship* ship_; + size_t money_; + size_t maxAvailableSpace_; + std::pair availableSpace_; public: - Player(): - ship_(std::make_unique()), - money_(0), - availableSpace_() - {} - - int getMoney() const { return money_; } - - size_t getAvailableSpace() const { return availableSpace_; } - - size_t getSpeed() const { - if (ship_) { - return ship_->getSpeed(); - } - return 0; - } - - Cargo* getCargo(size_t index) const { - if (ship_) { - return ship_->getCargo(index); - } - return nullptr; - } - - std::string takeOverShip(std::unique_ptr someoneElsesShip) { - if (!someoneElsesShip) { - return "there is no ship"; - } - if (ship_) { - return "already have one"; - } - ship_ = std::move(someoneElsesShip); - return "yarr"; - } - - std::unique_ptr giveAwayShip() { - return std::move(ship_); - } - - int calculateAvailableSpace() const { - size_t index = 0; - size_t count = 0; - auto cargo = getCargo(index); - while (cargo) { - count += cargo->getAmount(); - cargo = getCargo(++index); - } - return static_cast(getAvailableSpace() - count); - } -}; \ No newline at end of file + Player(); + + size_t getMoney() const; + + size_t getAvailableSpace(); + + size_t getSpeed() const; + + Cargo* getCargo(size_t index) const; + + // void PayCrew(size_t money) override; + + //void PurchaseCargo(std::unique_ptr cargo, size_t price); + + //void SellCargo(Cargo* cargo, size_t price); + + //void PrintCargo() const; + +private: + void calculateAvailableSpace(); +}; From e737f3495c9184002fcad2df97e40f1df25c162e Mon Sep 17 00:00:00 2001 From: KacperKaleta Date: Mon, 26 Jul 2021 12:19:59 +0200 Subject: [PATCH 17/25] 5 moved Cargo definitions into source file (#23) Co-authored-by: Kacu --- shm/CMakeLists.txt | 1 + shm/source/Cargo.cpp | 29 +++++++++++++++++++++++++ shm/source/Cargo.hpp | 50 ++++++++++++++------------------------------ 3 files changed, 46 insertions(+), 34 deletions(-) create mode 100644 shm/source/Cargo.cpp diff --git a/shm/CMakeLists.txt b/shm/CMakeLists.txt index 63e0b023e..b6a989fda 100644 --- a/shm/CMakeLists.txt +++ b/shm/CMakeLists.txt @@ -12,6 +12,7 @@ project(SHM) set(THIS_PROJECT_SRC_DIRECTORIES source/Fortune.cpp source/Map.cpp + source/Cargo.cpp ) set(THIS_PROJECT_TESTS_DIRECTORIES ) diff --git a/shm/source/Cargo.cpp b/shm/source/Cargo.cpp new file mode 100644 index 000000000..84158b27b --- /dev/null +++ b/shm/source/Cargo.cpp @@ -0,0 +1,29 @@ +#include "Cargo.hpp" + +Cargo::Cargo(const std::string& name, const size_t amount, const size_t basePrice): + name_(name), + amount_(amount), + basePrice_(basePrice) +{} + +bool Cargo::operator==(const Cargo& cargo) const { + return (name_ == cargo.getName() && + amount_ == cargo.getAmount() && + basePrice_ == cargo.getBasePrice()); +} + +Cargo& Cargo::operator+=(size_t amount) { + amount_ += amount; + return *this; +} + +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 88001c36a..bd82c8bc6 100644 --- a/shm/source/Cargo.hpp +++ b/shm/source/Cargo.hpp @@ -1,42 +1,24 @@ #pragma once + #include class Cargo { -public: - Cargo(const std::string& name, const size_t amount, const size_t basePrice): - name_(name), - amount_(amount), - basePrice_(basePrice) - {} - - bool operator==(const Cargo& cargo) const { - if (name_ != cargo.getName()) { - return false; - } - if (amount_ != cargo.getAmount()) { - return false; - } - if (basePrice_ != cargo.getBasePrice()) { - return false; - } - return true; - } - - Cargo& operator+=(size_t amount) { - amount_ += amount; - return *this; - } - Cargo& operator-=(size_t amount) { - amount_ -= amount; - return *this; - } - - std::string getName() const { return name_; } - size_t getAmount() const { return amount_; } - size_t getBasePrice() const { return basePrice_; } - -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; + + Cargo& operator+=(size_t amount); + + Cargo& operator-=(size_t amount); + + std::string getName() const; + + size_t getAmount() const; + + size_t getBasePrice() const; }; From d8b792e641bd291138966d4c76f2641d53cd9acb Mon Sep 17 00:00:00 2001 From: KacperKaleta Date: Mon, 26 Jul 2021 12:22:51 +0200 Subject: [PATCH 18/25] 16 created new class: Game (#22) Co-authored-by: Kacu --- shm/CMakeLists.txt | 1 + shm/main.cpp | 20 +++------ shm/source/Game.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++++ shm/source/Game.hpp | 53 ++++++++++++++++++++++++ 4 files changed, 158 insertions(+), 14 deletions(-) create mode 100644 shm/source/Game.cpp create mode 100644 shm/source/Game.hpp diff --git a/shm/CMakeLists.txt b/shm/CMakeLists.txt index b6a989fda..dcaab06fe 100644 --- a/shm/CMakeLists.txt +++ b/shm/CMakeLists.txt @@ -12,6 +12,7 @@ project(SHM) set(THIS_PROJECT_SRC_DIRECTORIES source/Fortune.cpp source/Map.cpp + source/Game.cpp source/Cargo.cpp ) set(THIS_PROJECT_TESTS_DIRECTORIES diff --git a/shm/main.cpp b/shm/main.cpp index 2ac3cb4c5..62877ab4d 100644 --- a/shm/main.cpp +++ b/shm/main.cpp @@ -1,20 +1,12 @@ -#include +#include "source/Game.hpp" -#include "source/Cargo.hpp" -#include "source/Map.hpp" -#include "source/Player.hpp" +constexpr size_t start_money = 1'000; +constexpr size_t game_days = 100; +constexpr size_t final_goal = 2'000; int main() { - Map(); - Player a; - Player b; - Player c; + Game game(start_money, game_days, final_goal); + game.startGame(); - c.giveAwayShip(); - - std::cout << "\n" << a.takeOverShip(b.giveAwayShip()) - << "\n" << b.takeOverShip(a.giveAwayShip()); - - std::cout << "\n\n\n"; return 0; } diff --git a/shm/source/Game.cpp b/shm/source/Game.cpp new file mode 100644 index 000000000..74703ba36 --- /dev/null +++ b/shm/source/Game.cpp @@ -0,0 +1,98 @@ +#include "Game.hpp" + +Game::Game(const size_t startMoney, + const size_t gameDays, + const size_t finalGoal): + money_(startMoney), + days_(gameDays), + final_goal_(finalGoal), + current_day_(1), + map_(std::make_unique()), + player_(std::make_unique()) +{} + +void Game::startGame() { + printMenu(); + while (true) { + int action; + std::cin >> action; + if (action == 10) { + continue; + } + makeAction(static_cast(action - 1)); + if (!printMenu()) { + return; + } + } +} + +bool Game::winCondition() const { + return money_ >= final_goal_; +} + +bool Game::looseCodition() const { + return money_ == 0; +} + +bool Game::printMenu() { + if (looseCodition()) { + printLooseScreen(); + return false; + } + if (winCondition()) { + printWinScreen(); + return false; + } + printOptions(); + return true; +} + +void Game::printOptions() { + std::cout << "\n Choose Action:" + << "\n | [1] Buy" + << "\n | [2] Sell" + << "\n | [3] Travel" + << "\n "; +} + +void Game::printWinScreen() { + std::cout << "\n CONGRATULATIONS!!!" + << "\n | Gold collected: " << money_ + << "\n | Days passed: " << current_day_ + << "\n"; +} + +void Game::printLooseScreen() { + std::cout << "\n GAME OVER" + << "\n | Gold collected: " << money_ + << "\n | Days passed: " << current_day_ + << "\n"; +} + +void Game::printCargo() {} + +void Game::makeAction(Action choice) { + switch (choice) { + case Action::Buy: + buy(); + return; + + case Action::Sell: + sell(); + return; + + case Action::Travel: + travel(); + return; + + default: + std::cout << "\n Wrong Action! \n"; + return; + } +} + +void Game::buy() {} + +void Game::sell() {} + +void Game::travel() {} \ No newline at end of file diff --git a/shm/source/Game.hpp b/shm/source/Game.hpp new file mode 100644 index 000000000..c904b4791 --- /dev/null +++ b/shm/source/Game.hpp @@ -0,0 +1,53 @@ +#pragma once + +#include +#include + +#include "Map.hpp" +#include "Player.hpp" + +class Game { + size_t money_; + size_t days_; + size_t final_goal_; + + size_t current_day_; + std::unique_ptr map_; + std::unique_ptr player_; + + enum class Action { + Buy, + Sell, + Travel + }; + +public: + Game(const size_t startMoney, + const size_t gameDays, + const size_t finalGoal); + + void startGame(); + +private: + bool winCondition() const; + + bool looseCodition() const; + + bool printMenu(); + + void printOptions(); + + void printWinScreen(); + + void printLooseScreen(); + + void printCargo(); + + void makeAction(Action choice); + + void buy(); + + void sell(); + + void travel(); +}; \ No newline at end of file From 97acf13473aeb975268323e9dfef10124e5bdd44 Mon Sep 17 00:00:00 2001 From: KacperKaleta Date: Mon, 26 Jul 2021 12:58:08 +0200 Subject: [PATCH 19/25] 10 moved ship definitions into source file (#25) Co-authored-by: Kacu --- shm/CMakeLists.txt | 2 + shm/source/Ship.cpp | 83 +++++++++++++++++++++++++++++++++ shm/source/Ship.hpp | 111 ++++++++++++++++++++++---------------------- 3 files changed, 141 insertions(+), 55 deletions(-) create mode 100644 shm/source/Ship.cpp diff --git a/shm/CMakeLists.txt b/shm/CMakeLists.txt index dcaab06fe..b8f6b0a53 100644 --- a/shm/CMakeLists.txt +++ b/shm/CMakeLists.txt @@ -14,6 +14,8 @@ set(THIS_PROJECT_SRC_DIRECTORIES source/Map.cpp source/Game.cpp source/Cargo.cpp + source/Player.cpp + source/Ship.cpp ) set(THIS_PROJECT_TESTS_DIRECTORIES ) diff --git a/shm/source/Ship.cpp b/shm/source/Ship.cpp new file mode 100644 index 000000000..bd838b355 --- /dev/null +++ b/shm/source/Ship.cpp @@ -0,0 +1,83 @@ +#include "Ship.hpp" +#include "Cargo.hpp" +//#include "Delegate.hpp" +//#include "Tieme.hpp" + +#include + +Ship::Ship() + : Ship(0, 0, 0, "", -1) +{} + +Ship::Ship(int maxCrew, int speed, size_t id) + : Ship(0, maxCrew, speed, "", id) +{} + +// 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) + : name_(name) + , capacity_(capacity) + , maxCrew_(maxCrew) + , id_(id) + , crew_(0) + , speed_(speed) + , cargo_() +{} + +// ~ Ship::Ship() override; + +void Ship::setName(const std::string& name) { name_ = name; } + +Ship& Ship::operator-=(size_t num) { + crew_ = (crew_ > num)? crew_ - num : 0; + return *this; +} + +Ship& Ship::operator+=(size_t num) { + crew_ = (crew_ + num < maxCrew_)? crew_ + num : maxCrew_; + return *this; +} + +size_t Ship::getCapacity() const { + 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::vector>& Ship::getCargo() { + return cargo_; +} + +// void Ship::printCargo() const; + +// void Ship::load(std::unique_ptr cargo); + +// void Ship::unload(Cargo* cargo); + +Cargo* Ship::getCargo(const size_t index) const { + if (index < cargo_.size()) { + return cargo_[index].get(); + } + return nullptr; +} + +// Cargo* Ship::FindMatchCargo(Cargo* cargo); + +// void Ship::RemoveFromStorage(Cargo* cargo); + +// void Ship::NextDay() override; diff --git a/shm/source/Ship.hpp b/shm/source/Ship.hpp index 12918d3ef..68dc97a07 100644 --- a/shm/source/Ship.hpp +++ b/shm/source/Ship.hpp @@ -1,64 +1,65 @@ #pragma once + +#include #include +#include + +//#include "Delegate.hpp" +//#include "Tieme.hpp" -#include "Cargo.hpp" +class Cargo; class Ship { + std::string name_; + const size_t capacity_; + const size_t maxCrew_; + const size_t id_; + size_t crew_; + size_t speed_; + //Time* time_; + //Delegate* delegate_; + std::vector> cargo_; + public: - Ship() - : capacity_() - , maxCrew_() - , crew_() - , speed_() - , name_() - , cargo_() - , id_(-1) - {} - - Ship(int capacity, int maxCrew, int speed, const std::string& name, size_t id) - : capacity_(capacity) - , maxCrew_(maxCrew) - , crew_(0) - , speed_(speed) - , name_(name) - , cargo_() - , id_(id) - {} + 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 maxCrew, int speed, size_t id) - : Ship(0, maxCrew, speed, "", id) - {} - - void setName(const std::string& name) { name_ = name; } - - Ship& operator-=(size_t num) { - crew_ -= num; - return *this; - } - Ship& operator+=(size_t num) { - crew_ += num; - return *this; - } - - 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_; } + Ship(int capacity, int maxCrew, int speed, const std::string& name, size_t id); + + // ~Ship() override; - Cargo* getCargo(const size_t index) { - if (index < cargo_.size()) { - return &cargo_.at(index); - } - return nullptr; - } - -private: - size_t capacity_; - size_t maxCrew_; - size_t crew_; - size_t speed_; - std::string name_; - std::vector cargo_; - const size_t id_; + void setName(const std::string& name); + + Ship& operator-=(size_t num); + + Ship& operator+=(size_t num); + + size_t getCapacity() const; + + size_t getMaxCrew() const; + + size_t getSpeed() const; + + std::string getName() const; + + size_t getId() const; + + std::vector>& getCargo(); + + // void printCargo() const; + + // void load(std::unique_ptr cargo); + + // void unload(Cargo* cargo); + + Cargo* getCargo(const size_t index) const; + + // Cargo* FindMatchCargo(Cargo* cargo); + + // void RemoveFromStorage(Cargo* cargo); + + // void NextDay() override; }; From dd89d0a519e15c9cb3d1686dfb5e2b087ea01c0c Mon Sep 17 00:00:00 2001 From: Kacu Date: Mon, 26 Jul 2021 16:34:22 +0200 Subject: [PATCH 20/25] removed maxAvailableSpace field from Player class --- shm/source/Player.cpp | 3 +-- shm/source/Player.hpp | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/shm/source/Player.cpp b/shm/source/Player.cpp index 6406c3e66..6276bda25 100644 --- a/shm/source/Player.cpp +++ b/shm/source/Player.cpp @@ -8,7 +8,6 @@ Player::Player(): ship_(nullptr), money_(0), - maxAvailableSpace_(100), availableSpace_({0, false}) {} @@ -51,6 +50,6 @@ void Player::calculateAvailableSpace() { } const auto& allCargo = ship_->getCargo(); size_t count = std::accumulate(allCargo.begin(), allCargo.end(), 0, [](int sum, const auto& cargo) { return sum + cargo->getAmount(); }); - availableSpace_.first = maxAvailableSpace_ - count; + availableSpace_.first = ship_->getCapacity() - count; availableSpace_.second = true; } diff --git a/shm/source/Player.hpp b/shm/source/Player.hpp index 8d235b4f7..2138b0e6e 100644 --- a/shm/source/Player.hpp +++ b/shm/source/Player.hpp @@ -8,7 +8,6 @@ class Cargo; class Player { Ship* ship_; size_t money_; - size_t maxAvailableSpace_; std::pair availableSpace_; public: From f96b5faf40139630a4126fd45ae8758f0c119c86 Mon Sep 17 00:00:00 2001 From: KacperKaleta Date: Mon, 26 Jul 2021 17:51:22 +0200 Subject: [PATCH 21/25] 20 create derived classes from class cargo (#26) * 20 created new class Fruit * 20 created new class Alcohol * 20 created new class Item * 20 turned Cargo class into interface * 20 created load and unload operations inside Ship class Co-authored-by: Kacu --- shm/CMakeLists.txt | 3 +++ shm/source/Alcohol.cpp | 22 ++++++++++++++++++++++ shm/source/Alcohol.hpp | 20 ++++++++++++++++++++ shm/source/Cargo.cpp | 6 ------ shm/source/Cargo.hpp | 15 +++++++++------ shm/source/Fruit.cpp | 28 ++++++++++++++++++++++++++++ shm/source/Fruit.hpp | 23 +++++++++++++++++++++++ shm/source/Item.cpp | 35 +++++++++++++++++++++++++++++++++++ shm/source/Item.hpp | 28 ++++++++++++++++++++++++++++ shm/source/Ship.cpp | 17 ++++++++++++++--- shm/source/Ship.hpp | 10 +++++----- 11 files changed, 187 insertions(+), 20 deletions(-) create mode 100644 shm/source/Alcohol.cpp create mode 100644 shm/source/Alcohol.hpp create mode 100644 shm/source/Fruit.cpp create mode 100644 shm/source/Fruit.hpp 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 b8f6b0a53..b3e497b8c 100644 --- a/shm/CMakeLists.txt +++ b/shm/CMakeLists.txt @@ -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 ) 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; +}; 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); }; 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; +}; 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; +}; 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; }; From a32c398352952b419145f259ab392df938c47f52 Mon Sep 17 00:00:00 2001 From: KacperKaleta Date: Mon, 26 Jul 2021 20:19:24 +0200 Subject: [PATCH 22/25] 14 cs task4 (#27) * 14 created Store class * 14 created new classes Time and Observer * 14 connected observer with five classes Co-authored-by: Kacu --- shm/CMakeLists.txt | 2 ++ shm/source/Alcohol.hpp | 2 +- shm/source/Cargo.hpp | 8 +++++++- shm/source/Fruit.cpp | 4 ++++ shm/source/Fruit.hpp | 4 +++- shm/source/Game.cpp | 2 ++ shm/source/Game.hpp | 8 +++++--- shm/source/Observer.hpp | 8 ++++++++ shm/source/Ship.cpp | 13 +++++++------ shm/source/Ship.hpp | 17 ++++++++-------- shm/source/Store.cpp | 23 ++++++++++++++++++++++ shm/source/Store.hpp | 43 +++++++++++++++++++++++++++++++++++++++++ shm/source/Time.cpp | 37 +++++++++++++++++++++++++++++++++++ shm/source/Time.hpp | 23 ++++++++++++++++++++++ 14 files changed, 173 insertions(+), 21 deletions(-) create mode 100644 shm/source/Observer.hpp create mode 100644 shm/source/Store.cpp create mode 100644 shm/source/Store.hpp create mode 100644 shm/source/Time.cpp create mode 100644 shm/source/Time.hpp diff --git a/shm/CMakeLists.txt b/shm/CMakeLists.txt index b3e497b8c..b4f247bba 100644 --- a/shm/CMakeLists.txt +++ b/shm/CMakeLists.txt @@ -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 ) diff --git a/shm/source/Alcohol.hpp b/shm/source/Alcohol.hpp index a0aafeb69..06d041013 100644 --- a/shm/source/Alcohol.hpp +++ b/shm/source/Alcohol.hpp @@ -16,5 +16,5 @@ class Alcohol : public Cargo { size_t getAmount() const override; - size_t getBasePrice() const override; + size_t getBasePrice() const override; }; diff --git a/shm/source/Cargo.hpp b/shm/source/Cargo.hpp index 7d37e0912..cd128680a 100644 --- a/shm/source/Cargo.hpp +++ b/shm/source/Cargo.hpp @@ -1,14 +1,20 @@ #pragma once +#include "Observer.hpp" + #include -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; diff --git a/shm/source/Fruit.cpp b/shm/source/Fruit.cpp index 659b6df1e..bd166c747 100644 --- a/shm/source/Fruit.cpp +++ b/shm/source/Fruit.cpp @@ -26,3 +26,7 @@ size_t Fruit::getAmount() const { size_t Fruit::getBasePrice() const { return basePrice_; } + +void Fruit::nextDay() { + operator--(); +} diff --git a/shm/source/Fruit.hpp b/shm/source/Fruit.hpp index 83cae07f0..24726a131 100644 --- a/shm/source/Fruit.hpp +++ b/shm/source/Fruit.hpp @@ -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; }; diff --git a/shm/source/Game.cpp b/shm/source/Game.cpp index 74703ba36..15182fdc9 100644 --- a/shm/source/Game.cpp +++ b/shm/source/Game.cpp @@ -1,4 +1,5 @@ #include "Game.hpp" +#include "Observer.hpp" Game::Game(const size_t startMoney, const size_t gameDays, @@ -8,6 +9,7 @@ Game::Game(const size_t startMoney, final_goal_(finalGoal), current_day_(1), map_(std::make_unique()), + time_(std::make_unique