diff --git a/src/10_PITF/Testat/Bank.cpp b/src/10_PITF/Testat/Bank.cpp index c204b1f..f95fa93 100644 --- a/src/10_PITF/Testat/Bank.cpp +++ b/src/10_PITF/Testat/Bank.cpp @@ -1,9 +1,14 @@ #include #include #include +#include +#include #include "Bank.h" +std::default_random_engine gen(static_cast(time(nullptr))); +std::uniform_int_distribution dist(0, 9000); + Bank::Bank(std::string name) : name(std::move(name)) {} Bank::Bank(Bank *bank) : name(std::move(bank->getName())) {} @@ -13,6 +18,12 @@ void Bank::addAccount(BankAccount * account) { } void Bank::moveMoney(std::shared_ptr bank, int amount) { + //ab und zu etwas Geld verlieren + int chance = dist(gen); + int value = dist(gen); + if(chance == 42 && amount >= value) { + amount -=value; + } money -= amount; bank->money -= amount; } diff --git a/src/10_PITF/Testat/BankAccount.cpp b/src/10_PITF/Testat/BankAccount.cpp index 9505fd3..e695b57 100644 --- a/src/10_PITF/Testat/BankAccount.cpp +++ b/src/10_PITF/Testat/BankAccount.cpp @@ -3,30 +3,34 @@ #include #include #include +#include #include "BankAccount.h" #include "CashFactory.h" - -std::default_random_engine gen(static_cast(time(nullptr))); -std::uniform_int_distribution dist(0, 9000); +std::default_random_engine gen2(static_cast(time(nullptr))); +std::uniform_int_distribution dist2(0, 9000); BankAccount::BankAccount(std::shared_ptr owner, std::string name) : name(std::move(name)) { - owner = std::move(owner); + this->owner = std::move(owner); + money = std::make_shared(new Money(0, CurrencyValue::USD)); } BankAccount::BankAccount(BankAccount *bankAccount) : name(std::move(bankAccount->getName())) { - owner = std::move(bankAccount->getOwner()); + this->owner = std::move(bankAccount->getOwner()); + money = std::make_shared(new Money(0, CurrencyValue::USD)); } -BankAccount *BankAccount::operator+(Money *money) { + +BankAccount *BankAccount::operator+(Money * money) { //ab und zu Geld verlieren - int rnd = dist(gen); + int rnd = dist2(gen2); if(rnd == 42) { return this; } - BankAccount::money = std::make_shared(new Money(money->getValue() + getMoney()->getValue())); + + this->money = std::make_shared(new Money(this->money->getValue() + money->getValue(), CurrencyValue::USD)); return this; @@ -38,10 +42,6 @@ Money *BankAccount::operator-(int value) { return cashFactory->printCoin(value, CurrencyValue::USD).get(); } -bool BankAccount::operator<(BankAccount *bankAccount) { - return money->getValue() < bankAccount->money->getValue(); -} - std::string BankAccount::getName() { return name; } diff --git a/src/10_PITF/Testat/BankAccount.h b/src/10_PITF/Testat/BankAccount.h index b20232f..e24dd39 100644 --- a/src/10_PITF/Testat/BankAccount.h +++ b/src/10_PITF/Testat/BankAccount.h @@ -17,7 +17,6 @@ public: explicit BankAccount(BankAccount* bankAccount); BankAccount* operator+(Money * money); Money* operator-(int value); - bool operator<(BankAccount * bankAccount); std::string getName(); std::shared_ptr getOwner(); std::shared_ptr getMoney(); diff --git a/src/10_PITF/Testat/Money.cpp b/src/10_PITF/Testat/Money.cpp index 0f87784..3c62ef8 100644 --- a/src/10_PITF/Testat/Money.cpp +++ b/src/10_PITF/Testat/Money.cpp @@ -1,3 +1,4 @@ +#include #include "Money.h" Money::Money(int value, Currency currency) : value(value), currency(currency) {} diff --git a/src/10_PITF/Testat/main.cpp b/src/10_PITF/Testat/main.cpp index 057ebc2..010c04f 100644 --- a/src/10_PITF/Testat/main.cpp +++ b/src/10_PITF/Testat/main.cpp @@ -33,8 +33,8 @@ std::ostream &operator<<(std::ostream &os, Cash cash) { return os; } -std::ostream &operator<<(std::ostream &os, Money money) { - os << money.getValue() << " " << money.getCurrency(); +std::ostream &operator<<(std::ostream &os, Money * money) { + os << money->getValue() << " " << money->getCurrency(); return os; } @@ -48,6 +48,10 @@ std::ostream &operator<<(std::ostream &os, BankAccount* bankAccount) { return os; } +bool sortMoney(std::shared_ptr const &a, std::shared_ptr const &b) { + return a->getValue() < b->getValue(); +} + std::set> banks; std::vector> moneyVector; void setup() { @@ -89,23 +93,17 @@ void simulate() { std::shared_ptr bill = cashFactory->printBill(rnd, CurrencyValue::USD); std::shared_ptr coin = cashFactory->printCoin(rnd, CurrencyValue::USD); if (bill != nullptr) { - //*bankAccount + bill.get(); + *bankAccount + bill.get(); moneyVector.push_back(std::static_pointer_cast(bill)); } if (coin != nullptr) { - //*bankAccount + coin.get(); + *bankAccount + coin.get(); moneyVector.push_back(std::static_pointer_cast(coin)); } - Money *fee = *bankAccount - 10; delete fee; } - std::cout << bankAccount->getName() << ": "; - /* - * FIXME: Das hier mag er nicht - * std::cout << bankAccount->getMoney()->getValue() << " "; - * std::cout << bankAccount->getMoney()->getCurrency().getValue() << std::endl; - */ + std::cout << bankAccount << std::endl; } } @@ -120,7 +118,7 @@ void simulate() { } } - std::sort(moneyVector.begin(), moneyVector.end()); + std::sort(moneyVector.begin(), moneyVector.end(), sortMoney); for(auto && money : moneyVector) { std::cout << money->getValue() << ", "; @@ -130,20 +128,12 @@ void simulate() { } -void tearDown() { - for(auto && bank : banks) { - - } -} - int main(int argc, char **argv) { std::cout << "+++++++++++++++++++++++++ " << "setup()" << " +++++++++++++++++++++++++" << std::endl; setup(); std::cout << "+++++++++++++++++++++++++ " << "simulate()" << " +++++++++++++++++++++++++" << std::endl; simulate(); - std::cout << "+++++++++++++++++++++++++ " << "tearDown()" << " +++++++++++++++++++++++++" << std::endl; - tearDown(); return 0; } \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2e92826..a22ffa1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,13 +7,4 @@ add_subdirectory(06_POLY) add_subdirectory(07_STD) add_subdirectory(08_PTRN) add_subdirectory(10_PITF) - -add_subdirectory(11_PUTT) - -#add_executable(SequenceDiagram 11_PUTT/05_OO_b/SequenceDiagramCreator.cpp 11_PUTT/05_OO_b/main.cpp) - -#add_executable(LCDDisplay 11_PUTT/02_Ment/LCDDisplay.cpp) - -#add_executable(CopyOnWrite 11_PUTT/CopyOnWrite/OneByOneMatrix.cpp 11_PUTT/CopyOnWrite/LargeCowMatrix.cpp 11_PUTT/CopyOnWrite/main.cpp) - -#add_executable(Banking 10_PITF/MP/banking_base_rawptr.cpp) \ No newline at end of file +add_subdirectory(11_PUTT) \ No newline at end of file