diff --git a/src/10_PITF/Testat/BankAccount.cpp b/src/10_PITF/Testat/BankAccount.cpp index ae253c0..ed0f045 100644 --- a/src/10_PITF/Testat/BankAccount.cpp +++ b/src/10_PITF/Testat/BankAccount.cpp @@ -10,11 +10,11 @@ BankAccount::BankAccount(std::shared_ptr owner, std::string name) : name } std::unique_ptr BankAccount::operator+(Money* money) { - std::unique_ptr ptr = std::make_unique(new Money(money->getValue() + getMoney()->getValue(), money->getCurrency())); + std::unique_ptr ptr = std::make_unique(Money(money->getValue() + getMoney()->getValue(), money->getCurrency())); BankAccount::money.swap(ptr); - ptr.release(); - return std::make_unique(this); + + return std::make_unique(*this); } std::unique_ptr BankAccount::operator-(int value) { @@ -26,6 +26,3 @@ std::unique_ptr BankAccount::operator-(int value) { bool BankAccount::operator<(std::unique_ptr bankAccount) { return money->getValue() < bankAccount->getMoney()->getValue(); } - - - diff --git a/src/10_PITF/Testat/Money.cpp b/src/10_PITF/Testat/Money.cpp index d45bd6e..a61e838 100644 --- a/src/10_PITF/Testat/Money.cpp +++ b/src/10_PITF/Testat/Money.cpp @@ -4,38 +4,32 @@ Money::Money(int value, Currency currency) : value(value), currency(currency) { } -Money& Money::operator=(Money money) { +std::unique_ptr Money::operator=(Money money) { value = money.getValue(); currency = money.getCurrency(); - return *this; + return std::make_unique(*this); } -Money &Money::operator=(Money *money) { - value = money->getValue(); - currency = money->getCurrency(); - return *this; -} - -Money Money::operator+(Money &a) { +std::unique_ptr Money::operator+(Money &a) { if(currency == a.currency) { - return Money(value + a.value); - }else return Money(-1, Currency(CurrencyValue::USD)); + return std::make_unique(Money(value + a.value)); + }else return std::make_unique(Money(-1, Currency(CurrencyValue::USD))); } -Money Money::operator-(Money &a) { +std::unique_ptr Money::operator-(Money &a) { if(currency == a.currency) { - return Money(value - a.value); - }else return Money(-1, Currency(CurrencyValue::USD)); + return std::make_unique(Money(value - a.value)); + }else return std::make_unique(Money(-1, Currency(CurrencyValue::USD))); } const std::unique_ptr Money::operator++(int) { value++; - return std::make_unique(this); + return std::make_unique(*this); } const std::unique_ptr Money::operator--(int) { value--; - return std::make_unique(this); + return std::make_unique(*this); } int Money::getValue() { @@ -45,21 +39,3 @@ int Money::getValue() { Currency Money::getCurrency() { return currency; } - -std::unique_ptr Money::operator+(std::unique_ptr &a) { - if(currency == a->getCurrency()) { - return std::make_unique(Money(value + a->getValue())); - }else return std::make_unique(Money(-1, Currency(CurrencyValue::USD))); -} - -std::unique_ptr Money::operator-(std::unique_ptr &a) { - if(currency == a->getCurrency()) { - return std::make_unique(Money(value - a->getValue())); - }else return std::make_unique(Money(-1, Currency(CurrencyValue::USD))); -} - -std::unique_ptr Money::operator=(std::unique_ptr money) { - value = money->getValue(); - currency = money->getCurrency(); - return std::make_unique(Money(value, currency)); -} diff --git a/src/10_PITF/Testat/Money.h b/src/10_PITF/Testat/Money.h index 5eed649..2277742 100644 --- a/src/10_PITF/Testat/Money.h +++ b/src/10_PITF/Testat/Money.h @@ -12,9 +12,8 @@ private: public: explicit Money(int value = 0, Currency currency = CurrencyValue::USD); std::unique_ptr operator=(Money money); - std::unique_ptr operator=(std::unique_ptr money); - std::unique_ptr operator+(std::unique_ptr &a); - std::unique_ptr operator-(std::unique_ptr &a); + std::unique_ptr operator+(Money &a); + std::unique_ptr operator-(Money &a); const std::unique_ptr operator++(int); const std::unique_ptr operator--(int); diff --git a/src/10_PITF/Testat/main.cpp b/src/10_PITF/Testat/main.cpp index c550c95..bfc020b 100644 --- a/src/10_PITF/Testat/main.cpp +++ b/src/10_PITF/Testat/main.cpp @@ -55,81 +55,63 @@ CashFactory *cashFactory; void setup() { + std::unique_ptr bank1 = std::make_unique(Bank("Bank 1")); + std::unique_ptr bank2 = std::make_unique(Bank("Bank 2")); + std::unique_ptr bank3 = std::make_unique(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::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")); + std::shared_ptr max = std::make_shared(Person("Max Müller")); + std::shared_ptr marius = std::make_shared(Person("Marius Müller")); + std::shared_ptr alex = std::make_shared(Person("Alex Maier")); owners.insert(max); owners.insert(marius); owners.insert(alex); - 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"))); + bank1->addAccount(std::make_unique(BankAccount(max, "Konto 1"))); + bank2->addAccount(std::make_unique(BankAccount(max, "Konto 2"))); + bank3->addAccount(std::make_unique(BankAccount(alex, "privat"))); + bank1->addAccount(std::make_unique(BankAccount(alex, "geschäftlich 1"))); + bank2->addAccount(std::make_unique(BankAccount(alex, "geschäftlich 2"))); + bank3->addAccount(std::make_unique(BankAccount(alex, "geschäftlich 3"))); + bank1->addAccount(std::make_unique(BankAccount(marius, "Konto 1"))); + bank2->addAccount(std::make_unique(BankAccount(marius, "Konto 2"))); + bank3->addAccount(std::make_unique(BankAccount(marius, "Konto 3"))); - banks.insert(bank1); - banks.insert(bank2); - banks.insert(bank3); + banks.insert(std::move(bank1)); + banks.insert(std::move(bank2)); + banks.insert(std::move(bank3)); cashFactory = new CashFactory(); } -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); + for(auto && bank : banks) { + std::cout << std::endl; + std::cout << "======================== " << bank->getName() << " ========================" << std::endl; + std::cout << std::endl; + for(auto && bankAccount : bank->getAccounts()) { + 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 tearDown() { - 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(); - }); }