diff --git a/src/10_PITF/Testat/BankAccount.cpp b/src/10_PITF/Testat/BankAccount.cpp index 77000ce..ae253c0 100644 --- a/src/10_PITF/Testat/BankAccount.cpp +++ b/src/10_PITF/Testat/BankAccount.cpp @@ -9,11 +9,11 @@ BankAccount::BankAccount(std::shared_ptr owner, std::string name) : name } -std::unique_ptr BankAccount::operator+(std::unique_ptr money) { +std::unique_ptr BankAccount::operator+(Money* money) { std::unique_ptr ptr = std::make_unique(new Money(money->getValue() + getMoney()->getValue(), money->getCurrency())); - BankAccount::money.swap(ptr); + ptr.release(); return std::make_unique(this); } diff --git a/src/10_PITF/Testat/BankAccount.h b/src/10_PITF/Testat/BankAccount.h index 48e5446..d942d7c 100644 --- a/src/10_PITF/Testat/BankAccount.h +++ b/src/10_PITF/Testat/BankAccount.h @@ -15,7 +15,7 @@ private: std::unique_ptr money; public: explicit BankAccount(std::shared_ptr owner, std::string name); - std::unique_ptr operator+(std::unique_ptr money); + std::unique_ptr operator+(Money* money); std::unique_ptr operator-(int value); bool operator<(std::unique_ptr bankAccount); std::string getName(); diff --git a/src/10_PITF/Testat/main.cpp b/src/10_PITF/Testat/main.cpp index 859c8b7..c550c95 100644 --- a/src/10_PITF/Testat/main.cpp +++ b/src/10_PITF/Testat/main.cpp @@ -24,13 +24,13 @@ std::ostream &operator<<(std::ostream &os, Currency currency) { return os; } -std::ostream &operator<<(std::ostream &os, Bill bill) { - os << bill.getValue() << " " << bill.getCurrency() << " " << bill.getSerial(); +std::ostream &operator<<(std::ostream &os, std::unique_ptr bill) { + os << bill->getValue() << " " << bill->getCurrency() << " " << bill->getSerial(); return os; } -std::ostream &operator<<(std::ostream &os, Cash cash) { - os << cash.getValue() << " " << cash.getCurrency(); +std::ostream &operator<<(std::ostream &os, std::unique_ptr cash) { + os << cash->getValue() << " " << cash->getCurrency(); return os; } @@ -39,45 +39,45 @@ std::ostream &operator<<(std::ostream &os, std::unique_ptr money) { return os; } -std::ostream &operator<<(std::ostream &os, Person *person) { +std::ostream &operator<<(std::ostream &os, std::shared_ptr person) { os << person->getName(); return os; } -std::ostream &operator<<(std::ostream &os, BankAccount bankAccount) { - os << bankAccount.getOwner() << " " << bankAccount.getName() << ": " << bankAccount.getMoney(); +std::ostream &operator<<(std::ostream &os, std::unique_ptr bankAccount) { + os << bankAccount->getOwner() << " " << bankAccount->getName() << ": " << bankAccount->getMoney(); return os; } -std::set banks; -std::set> owners; +std::set> banks; +std::set> owners; CashFactory *cashFactory; void setup() { - Bank *bank1 = new Bank("Bank 1"); - Bank *bank2 = new Bank("Bank 2"); - Bank *bank3 = new Bank("Bank 3"); + std::unique_ptr bank1 = std::make_unique(new Bank("Bank 1")); + std::unique_ptr bank2 = std::make_unique(new Bank("Bank 2")); + std::unique_ptr bank3 = std::make_unique(new Bank("Bank 3")); - std::unique_ptr max = std::make_unique(new Person("Max Müller")); - std::unique_ptr marius = std::make_unique(new Person("Marius Müller")); - std::unique_ptr alex = std::make_unique(new Person("Alex Maier")); + std::shared_ptr max = std::make_shared(new Person("Max Müller")); + std::shared_ptr marius = std::make_shared(new Person("Marius Müller")); + std::shared_ptr alex = std::make_shared(new Person("Alex Maier")); owners.insert(max); owners.insert(marius); owners.insert(alex); - bank1->addAccount(new BankAccount(max, "Konto 1")); - bank2->addAccount(new BankAccount(max, "Konto 2")); - bank1->addAccount(new BankAccount(alex, "privat")); - bank3->addAccount(new BankAccount(alex, "geschäftlich 1")); - bank2->addAccount(new BankAccount(alex, "geschäftlich 2")); - bank1->addAccount(new BankAccount(alex, "geschäftlich 3")); - bank2->addAccount(new BankAccount(marius, "Konto 1")); - bank1->addAccount(new BankAccount(marius, "Konto 2")); - bank3->addAccount(new BankAccount(marius, "Konto 3")); + bank1->addAccount(std::make_unique(new BankAccount(max, "Konto 1"))); + bank2->addAccount(std::make_unique(new BankAccount(max, "Konto 2"))); + bank1->addAccount(std::make_unique(new BankAccount(alex, "privat"))); + bank3->addAccount(std::make_unique(new BankAccount(alex, "geschäftlich 1"))); + bank2->addAccount(std::make_unique(new BankAccount(alex, "geschäftlich 2"))); + bank1->addAccount(std::make_unique(new BankAccount(alex, "geschäftlich 3"))); + bank2->addAccount(std::make_unique(new BankAccount(marius, "Konto 1"))); + bank1->addAccount(std::make_unique(new BankAccount(marius, "Konto 2"))); + bank3->addAccount(std::make_unique(new BankAccount(marius, "Konto 3"))); banks.insert(bank1); banks.insert(bank2); @@ -86,40 +86,50 @@ void setup() { cashFactory = new CashFactory(); } -void simulate() { - for (Bank *bank : banks) { - std::cout << std::endl; - std::cout << "======================== " << bank->getName() << " ========================" << std::endl; - std::cout << std::endl; - for (std::unique_ptr bankAccount : bank->getAccounts()) { - for (int i = 0; i < 10000; ++i) { - int rnd = distribution(generator); - std::unique_ptr bill = cashFactory->printBill(rnd, CurrencyValue::USD); - Coin *coin = cashFactory->printCoin(rnd, CurrencyValue::USD); - if (bill != nullptr) { - *bankAccount + bill; - } - if (coin != nullptr) { - *bankAccount + coin; - } - - Money *fee = *bankAccount - 10; - } - std::cout << *bankAccount << std::endl; +void forBankAccount(std::unique_ptr bankAccount) { + for (int i = 0; i < 10000; ++i) { + int rnd = distribution(generator); + std::unique_ptr bill = cashFactory->printBill(rnd, CurrencyValue::USD); + std::unique_ptr coin = cashFactory->printCoin(rnd, CurrencyValue::USD); + if (bill != nullptr) { + bankAccount->operator+(bill.get()); } + if (coin != nullptr) { + bankAccount->operator+(coin.get()); + } + + std::unique_ptr fee = bankAccount->operator-(10); } + std::cout << bankAccount.get() << std::endl; +} + +void forBank(std::unique_ptr bank) { + std::cout << std::endl; + std::cout << "======================== " << bank->getName() << " ========================" << std::endl; + std::cout << std::endl; + + std::set> bankAccounts = bank->getAccounts(); + + std::for_each(bankAccounts.begin(), bankAccounts.end(), forBankAccount); + +} + +void simulate() { + std::for_each(banks.begin(), banks.end(), forBank); } void tearDown() { - for (Bank *bank : banks) { - for (BankAccount *bankAccount : bank->getAccounts()) { - delete bankAccount; - } - delete bank; - } - for(Person *person : owners) { - delete person; - } + std::for_each(banks.begin(), banks.end(), [] (std::unique_ptr bank) { + std::set> bankAccounts = bank->getAccounts(); + std::for_each(bankAccounts.begin(), bankAccounts.end(), [] (std::unique_ptr bankAccount){ + *bankAccount.release(); + }); + *bank.release(); + }); + + std::for_each(owners.begin(), owners.end(), [] (std::shared_ptr person) { + *person.release(); + }); }