diff --git a/src/08_PTRN/CMakeLists.txt b/src/08_PTRN/CMakeLists.txt index 2287a8e..20d7803 100644 --- a/src/08_PTRN/CMakeLists.txt +++ b/src/08_PTRN/CMakeLists.txt @@ -1 +1,2 @@ -add_executable(08_PTRN_MP MP/Money.cpp MP/Money.h MP/Currency.cpp MP/Currency.h MP/main.cpp MP/BankAccount.cpp MP/BankAccount.h MP/Storage.cpp MP/Storage.h) \ No newline at end of file +add_executable(08_PTRN_MP MP/VehicleFactory.cpp MP/VehicleFactory.hpp MP/Vehicle.cpp MP/Vehicle.hpp MP/Car.cpp MP/Car.hpp MP/Truck.cpp MP/Truck.hpp MP/Logger.cpp MP/Logger.hpp MP/main.cpp) +add_executable(08_PTRN_Testat Testat/main.cpp Testat/Money.cpp Testat/Money.h Testat/Logger.cpp Testat/Logger.hpp Testat/Currency.h Testat/BankAccount.cpp Testat/BankAccount.h) \ No newline at end of file diff --git a/src/08_PTRN/MP/BankAccount.cpp b/src/08_PTRN/MP/BankAccount.cpp deleted file mode 100644 index 7bd510e..0000000 --- a/src/08_PTRN/MP/BankAccount.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include - -#include - -#include "BankAccount.h" - -BankAccount::BankAccount(std::string owner, Money money) : owner(std::move(owner)), money(money){} diff --git a/src/08_PTRN/MP/BankAccount.h b/src/08_PTRN/MP/BankAccount.h deleted file mode 100644 index 97884cb..0000000 --- a/src/08_PTRN/MP/BankAccount.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef C_C_BANKACCOUNT_H -#define C_C_BANKACCOUNT_H - - -#include - -#include "Money.h" - -class BankAccount { -private: - std::string owner; - Money money; - -public: - BankAccount(std::string owner = "none", Money money = Money(0, Storage().USD)); -}; - - -#endif diff --git a/src/08_PTRN/MP/Currency.cpp b/src/08_PTRN/MP/Currency.cpp deleted file mode 100644 index 84851e9..0000000 --- a/src/08_PTRN/MP/Currency.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include - -#include "Currency.h" - -Currency::Currency(std::string name, float rate) : name(std::move(name)), rate(rate) {} - -bool Currency::operator==(Currency ¤cy) { - return name == currency.name; -} diff --git a/src/08_PTRN/MP/Currency.h b/src/08_PTRN/MP/Currency.h deleted file mode 100644 index 9cd561d..0000000 --- a/src/08_PTRN/MP/Currency.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef C_C_CURRENCY_H -#define C_C_CURRENCY_H - - -#include - -class Currency { -public: - std::string name; - float rate; - Currency(std::string name, float rate); - bool operator==(Currency ¤cy); - -}; - - -#endif diff --git a/src/08_PTRN/MP/Money.cpp b/src/08_PTRN/MP/Money.cpp deleted file mode 100644 index 980cc3c..0000000 --- a/src/08_PTRN/MP/Money.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include - -#include "Money.h" - -Money::Money(float value, Currency currency) : value(value), currency(std::move(currency)) {} - -Money Money::operator+(Money money) { - return Money((this->value * this->currency.rate) + (money.value * money.currency.rate), this->currency); -} - -Money Money::operator-(Money money) { - return Money(this->value - money.value, this->currency); -} - -Money Money::operator*(Money money) { - return Money(this->value * money.value); -} - -Money Money::operator/(Money money) { - return Money(this->value / money.value); -} - -const Money Money::operator++(int) { - this->value++; - return *this; -} - -const Money Money::operator--(int) { - this->value--; - return *this; -} - -bool Money::operator==(Money &money) { - return this->value == money.value && this ->currency == money.currency; -} diff --git a/src/08_PTRN/MP/Money.h b/src/08_PTRN/MP/Money.h deleted file mode 100644 index 96559a8..0000000 --- a/src/08_PTRN/MP/Money.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef C_C_MONEY_H -#define C_C_MONEY_H - -#include "Currency.h" -#include "Storage.h" - -/** - * - * - * @author Johannes Theiner - * @since 0.0.1 - */ -class Money { -private: - float value; - Currency currency; -public: - explicit Money(float value = 0, Currency currency = Storage().currencies["USD"]); - Money operator+(Money money); - Money operator-(Money money); - Money operator*(Money money); - Money operator/(Money money); - const Money operator++(int); - const Money operator--(int); - bool operator==(Money &money); -}; - -#endif diff --git a/src/08_PTRN/MP/Storage.cpp b/src/08_PTRN/MP/Storage.cpp deleted file mode 100644 index eb94cff..0000000 --- a/src/08_PTRN/MP/Storage.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "Storage.h" diff --git a/src/08_PTRN/MP/Storage.h b/src/08_PTRN/MP/Storage.h deleted file mode 100644 index 98789fa..0000000 --- a/src/08_PTRN/MP/Storage.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef C_C_STORAGE_H -#define C_C_STORAGE_H - - -#include -#include -#include "Currency.h" -#include "BankAccount.h" - -class Storage { -public: - std::unordered_map accounts; - static const Currency USD = Currency("US Dollar", 1); - static const Currency EUR = Currency("Euro", 0.879699); - static const Currency GBP = Currency("British Pound", 0.783667); - static const Currency AUD = Currency("Australian Dollar", 1.36962); - static const Currency CAD = Currency("Canadian Dollar", 1.33023); -}; - - -#endif diff --git a/src/08_PTRN/MP/main.cpp b/src/08_PTRN/MP/main.cpp index e74ede1..bcbed14 100644 --- a/src/08_PTRN/MP/main.cpp +++ b/src/08_PTRN/MP/main.cpp @@ -1,13 +1,71 @@ -#include -#include +#include "Car.hpp" +#include "Truck.hpp" +#include "Logger.hpp" +#include "VehicleFactory.hpp" -#include "Currency.h" -#include "BankAccount.h" -#include "Storage.h" +#include +#include -Storage storage; +void workOnCopy_naive(std::vector vehicles){ + std::vector tempVec; + for (int i=0; i(currentVehicle); // RTTI type check + Truck* truckToCopy = dynamic_cast(currentVehicle);// may be nullptr + if(carToCopy){ + tempVec.push_back(new Car(carToCopy->model(), carToCopy->maxWeight())); // type dependency to Car + }else if(truckToCopy){ + tempVec.push_back(new Truck(truckToCopy->model(), truckToCopy->payload_kg())); // type dependecy + }else{ + // TODO: what do we do here? + // Did someone add a new class to the code base? + return; // BUG: copies aren't free'd + } + } + // work on copies ... + // ... + for(auto vehi : tempVec){ + std::cout << vehi->model() << " " << vehi->payload_kg() << " kg" << std::endl; + } + for (int i=0; i vehicles){ // uses RAII, virtual ctor + + class RAIIvector { // local RAII helper class + public: + std::vector tempVec; + ~RAIIvector(){ + for(auto vehi : tempVec){ + delete vehi; + } + tempVec.clear(); + } + }; + RAIIvector rv; + + for(auto vehi : vehicles){ + rv.tempVec.push_back(vehi->clone()); + } + // work on copies ... + // ... + for(auto vehi : rv.tempVec){ + std::cout << vehi->model() << " " << vehi->payload_kg() << " kg" << std::endl; + } + // compiler WILL invoke RAIIvector::~RAIIvector() here +} - storage.accounts.insert(BankAccount("Max Maier", Money(50, Storage().EUR))); -} \ No newline at end of file + +int main(int argc, const char * argv[]) { + std::vector vehicles; + vehicles.push_back(vehicleForPayload("Volkswagen Golf", 900)); // use factory for construction + vehicles.push_back(vehicleForPayload("Magirus Deutz", 18*1000)); + Logger::getInstance()->log("work naively", __FILE__, __LINE__); + workOnCopy_naive(vehicles); + Logger::getInstance()->log("work smartly", __FILE__, __LINE__); + workOnCopy_smart(vehicles); + return 0; +} diff --git a/src/08_PTRN/MP/main_old.cpp b/src/08_PTRN/MP/main_old.cpp deleted file mode 100644 index bcbed14..0000000 --- a/src/08_PTRN/MP/main_old.cpp +++ /dev/null @@ -1,71 +0,0 @@ -#include "Car.hpp" -#include "Truck.hpp" -#include "Logger.hpp" -#include "VehicleFactory.hpp" - -#include -#include - -void workOnCopy_naive(std::vector vehicles){ - std::vector tempVec; - for (int i=0; i(currentVehicle); // RTTI type check - Truck* truckToCopy = dynamic_cast(currentVehicle);// may be nullptr - if(carToCopy){ - tempVec.push_back(new Car(carToCopy->model(), carToCopy->maxWeight())); // type dependency to Car - }else if(truckToCopy){ - tempVec.push_back(new Truck(truckToCopy->model(), truckToCopy->payload_kg())); // type dependecy - }else{ - // TODO: what do we do here? - // Did someone add a new class to the code base? - return; // BUG: copies aren't free'd - } - } - // work on copies ... - // ... - for(auto vehi : tempVec){ - std::cout << vehi->model() << " " << vehi->payload_kg() << " kg" << std::endl; - } - for (int i=0; i vehicles){ // uses RAII, virtual ctor - - class RAIIvector { // local RAII helper class - public: - std::vector tempVec; - ~RAIIvector(){ - for(auto vehi : tempVec){ - delete vehi; - } - tempVec.clear(); - } - }; - RAIIvector rv; - - for(auto vehi : vehicles){ - rv.tempVec.push_back(vehi->clone()); - } - // work on copies ... - // ... - for(auto vehi : rv.tempVec){ - std::cout << vehi->model() << " " << vehi->payload_kg() << " kg" << std::endl; - } - // compiler WILL invoke RAIIvector::~RAIIvector() here -} - - -int main(int argc, const char * argv[]) { - std::vector vehicles; - vehicles.push_back(vehicleForPayload("Volkswagen Golf", 900)); // use factory for construction - vehicles.push_back(vehicleForPayload("Magirus Deutz", 18*1000)); - Logger::getInstance()->log("work naively", __FILE__, __LINE__); - workOnCopy_naive(vehicles); - Logger::getInstance()->log("work smartly", __FILE__, __LINE__); - workOnCopy_smart(vehicles); - return 0; -} diff --git a/src/08_PTRN/Testat/BankAccount.cpp b/src/08_PTRN/Testat/BankAccount.cpp new file mode 100644 index 0000000..af0767b --- /dev/null +++ b/src/08_PTRN/Testat/BankAccount.cpp @@ -0,0 +1,17 @@ +// +// Created by JThei on 01.12.2018. +// + +#include "BankAccount.h" + +BankAccount::BankAccount(std::string name) : name(std::move(name)) {} + +void BankAccount::add(Money _money) { + money = money + _money; +} + +bool BankAccount::operator<(BankAccount a) { + return false; +} + + diff --git a/src/08_PTRN/Testat/BankAccount.h b/src/08_PTRN/Testat/BankAccount.h new file mode 100644 index 0000000..33c97ba --- /dev/null +++ b/src/08_PTRN/Testat/BankAccount.h @@ -0,0 +1,23 @@ +// +// Created by JThei on 01.12.2018. +// + +#ifndef C_C_BANKACCOUNTS_H +#define C_C_BANKACCOUNTS_H + +#include + +#include "Money.h" + +class BankAccount { +private: + std::string name; + Money money; +public: + explicit BankAccount(std::string name); + void add(Money money); + bool operator<(BankAccount a); +}; + + +#endif //C_C_BANKACCOUNTS_H diff --git a/src/08_PTRN/Testat/Currency.h b/src/08_PTRN/Testat/Currency.h new file mode 100644 index 0000000..344ab84 --- /dev/null +++ b/src/08_PTRN/Testat/Currency.h @@ -0,0 +1,17 @@ +// +// Created by JThei on 01.12.2018. +// + +#ifndef C_C_CURRENCY_H +#define C_C_CURRENCY_H + +enum class Currency { + USD=100, + EUR=80, + GPD=75 +}; + + + + +#endif //C_C_CURRENCY_H diff --git a/src/08_PTRN/Testat/Logger.cpp b/src/08_PTRN/Testat/Logger.cpp new file mode 100644 index 0000000..29df18d --- /dev/null +++ b/src/08_PTRN/Testat/Logger.cpp @@ -0,0 +1,15 @@ +// file Logger.cpp +#include "Logger.hpp" + +Logger* Logger::_theInstance = nullptr; + +Logger* Logger::getInstance(){ + if (!_theInstance){ + _theInstance = new StdoutLogger(); + } + return _theInstance; +} + +void StdoutLogger::log(std::string message, std::string file, int line){ + std::cerr << message << " in " << file << " line " << line << std::endl; +} diff --git a/src/08_PTRN/Testat/Logger.hpp b/src/08_PTRN/Testat/Logger.hpp new file mode 100644 index 0000000..9b3006c --- /dev/null +++ b/src/08_PTRN/Testat/Logger.hpp @@ -0,0 +1,19 @@ +// file Logger.hpp +#ifndef Logger_hpp +#define Logger_hpp + +#include + +class Logger { + static Logger* _theInstance; +public: + virtual void log(std::string message, std::string file="", int line=0) = 0; + static Logger* getInstance(); +}; + +class StdoutLogger: public Logger { +public: + void log(std::string message, std::string file="", int line=0); +}; + +#endif diff --git a/src/08_PTRN/Testat/Money.cpp b/src/08_PTRN/Testat/Money.cpp new file mode 100644 index 0000000..893c3de --- /dev/null +++ b/src/08_PTRN/Testat/Money.cpp @@ -0,0 +1,37 @@ +// +// Created by JThei on 01.12.2018. +// + +#include "Money.h" + +Money::Money(int value, Currency currency) : value(value), currency(currency) { + +} + +Money Money::operator+(Money &a) { + return Money(); +} + +Money Money::operator-(Money &a) { + return Money(); +} + +const Money Money::operator++(int) { + return Money(); +} + +const Money Money::operator--(int) { + return Money(); +} + +bool Money::operator==(Money &a) { + return false; +} + +bool Money::operator!=(Money &a) { + return false; +} + +Currency Money::getCurrency() { + return currency; +} diff --git a/src/08_PTRN/Testat/Money.h b/src/08_PTRN/Testat/Money.h new file mode 100644 index 0000000..1b9a1de --- /dev/null +++ b/src/08_PTRN/Testat/Money.h @@ -0,0 +1,28 @@ +// +// Created by JThei on 01.12.2018. +// + +#ifndef C_C_MONEY_H +#define C_C_MONEY_H + + +#include "Currency.h" + +class Money { +private: + int value; + Currency currency; +public: + explicit Money(int value = 0, Currency currency = Currency::USD); + Money operator+(Money &a); + Money operator-(Money &a); + const Money operator++(int); + const Money operator--(int); + bool operator==(Money &a); + bool operator!=(Money &a); + + Currency getCurrency(); +}; + + +#endif //C_C_MONEY_H diff --git a/src/08_PTRN/Testat/main.cpp b/src/08_PTRN/Testat/main.cpp new file mode 100644 index 0000000..3adb79b --- /dev/null +++ b/src/08_PTRN/Testat/main.cpp @@ -0,0 +1,15 @@ + + +#include +#include "Currency.h" +#include "BankAccount.h" + + +int main(int argc, char **argv) { +// std::set accounts; +// +// accounts.insert(BankAccount("Max")); +// accounts.insert(BankAccount("Max")); +// accounts.insert(BankAccount("Marius")); +// accounts.insert(BankAccount("Test")); +} \ No newline at end of file