From b37de1bd06eafdc6c6208dd48e309a8d19dc2ef3 Mon Sep 17 00:00:00 2001 From: joethei Date: Sat, 1 Dec 2018 09:44:03 +0100 Subject: [PATCH] =?UTF-8?q?08=5FPTRN:=20Weitere=20=C3=84nderungen=20am=20G?= =?UTF-8?q?rundger=C3=BCst?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/08_PTRN/CMakeLists.txt | 2 +- src/08_PTRN/MP/BankAccount.cpp | 4 +- src/08_PTRN/MP/BankAccount.h | 2 +- src/08_PTRN/MP/Currency.h | 1 + src/08_PTRN/MP/Storage.h | 6 ++- src/08_PTRN/MP/main.cpp | 76 ++++------------------------------ src/08_PTRN/MP/main_old.cpp | 71 +++++++++++++++++++++++++++++++ src/08_PTRN/MP/maín.cpp | 19 --------- 8 files changed, 91 insertions(+), 90 deletions(-) create mode 100644 src/08_PTRN/MP/main_old.cpp delete mode 100644 src/08_PTRN/MP/maín.cpp diff --git a/src/08_PTRN/CMakeLists.txt b/src/08_PTRN/CMakeLists.txt index beb38f2..2287a8e 100644 --- a/src/08_PTRN/CMakeLists.txt +++ b/src/08_PTRN/CMakeLists.txt @@ -1 +1 @@ -add_executable(08_PTRN_MP MP/Money.cpp MP/Money.h MP/Currency.cpp MP/Currency.h MP/maín.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/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 diff --git a/src/08_PTRN/MP/BankAccount.cpp b/src/08_PTRN/MP/BankAccount.cpp index 5280c37..7bd510e 100644 --- a/src/08_PTRN/MP/BankAccount.cpp +++ b/src/08_PTRN/MP/BankAccount.cpp @@ -1,5 +1,7 @@ #include +#include + #include "BankAccount.h" -BankAccount::BankAccount(std::string owner, Money money) : owner(std::move(owner)), money(std::move(money)){} +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 index e2da0d4..97884cb 100644 --- a/src/08_PTRN/MP/BankAccount.h +++ b/src/08_PTRN/MP/BankAccount.h @@ -12,7 +12,7 @@ private: Money money; public: - BankAccount(std::string owner, Money money); + BankAccount(std::string owner = "none", Money money = Money(0, Storage().USD)); }; diff --git a/src/08_PTRN/MP/Currency.h b/src/08_PTRN/MP/Currency.h index 24340b8..9cd561d 100644 --- a/src/08_PTRN/MP/Currency.h +++ b/src/08_PTRN/MP/Currency.h @@ -10,6 +10,7 @@ public: float rate; Currency(std::string name, float rate); bool operator==(Currency ¤cy); + }; diff --git a/src/08_PTRN/MP/Storage.h b/src/08_PTRN/MP/Storage.h index fb56c76..98789fa 100644 --- a/src/08_PTRN/MP/Storage.h +++ b/src/08_PTRN/MP/Storage.h @@ -9,8 +9,12 @@ class Storage { public: - std::unordered_set currencies; 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); }; diff --git a/src/08_PTRN/MP/main.cpp b/src/08_PTRN/MP/main.cpp index bcbed14..e74ede1 100644 --- a/src/08_PTRN/MP/main.cpp +++ b/src/08_PTRN/MP/main.cpp @@ -1,71 +1,13 @@ -#include "Car.hpp" -#include "Truck.hpp" -#include "Logger.hpp" -#include "VehicleFactory.hpp" +#include +#include -#include -#include +#include "Currency.h" +#include "BankAccount.h" +#include "Storage.h" -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, char **argv) { - -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; -} + storage.accounts.insert(BankAccount("Max Maier", Money(50, Storage().EUR))); +} \ No newline at end of file diff --git a/src/08_PTRN/MP/main_old.cpp b/src/08_PTRN/MP/main_old.cpp new file mode 100644 index 0000000..bcbed14 --- /dev/null +++ b/src/08_PTRN/MP/main_old.cpp @@ -0,0 +1,71 @@ +#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/MP/maín.cpp b/src/08_PTRN/MP/maín.cpp deleted file mode 100644 index 3170c5f..0000000 --- a/src/08_PTRN/MP/maín.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include - -#include "Currency.h" -#include "BankAccount.h" -#include "Storage.h" - -Storage storage; - -int main(int argc, char **argv) { - storage.currencies.insert("USD", Currency("US Dollar", 1)); - storage.currencies.insert("EUR", Currency("Euro", 0.879699)); - storage.currencies.insert("GBP", Currency("British Pound", 0.783667)); - storage.currencies.insert("AUD", Currency("Australian Dollar", 1.36962)); - storage.currencies.insert("CAD", Currency("Canadian Dollar", 1.33023)); - - - storage.accounts.insert(BankAccount("Max Maier", Money(50, storage.currencies["EUR"]))); -} \ No newline at end of file