diff --git a/src/10_PITF/Testat/Bank.cpp b/src/10_PITF/Testat/Bank.cpp index bd2002e..9fbff68 100644 --- a/src/10_PITF/Testat/Bank.cpp +++ b/src/10_PITF/Testat/Bank.cpp @@ -7,11 +7,15 @@ Bank::Bank(std::string name) : name(std::move(name)) { } -void Bank::addAccount(BankAccount * account) { +void Bank::addAccount(std::unique_ptr account) { accounts.insert(account); } -std::set Bank::getAccounts() { +void Bank::addMoney(std::unique_ptr money) { + this->money.insert(money); +} + +std::set> Bank::getAccounts() { return accounts; } @@ -19,7 +23,4 @@ std::string Bank::getName() { return name; } -void Bank::addMoney(Money * money) { - this->money.insert(money); -} diff --git a/src/10_PITF/Testat/Bank.h b/src/10_PITF/Testat/Bank.h index 4d5fb88..bc09d23 100644 --- a/src/10_PITF/Testat/Bank.h +++ b/src/10_PITF/Testat/Bank.h @@ -7,13 +7,13 @@ class Bank { private: std::string name; - std::set accounts; - std::set money; + std::set> accounts; + std::set> money; public: explicit Bank(std::string name); - void addAccount(BankAccount * account); - void addMoney(Money * money); - std::set getAccounts(); + void addAccount(std::unique_ptr account); + void addMoney(std::unique_ptr money); + std::set> getAccounts(); std::string getName(); }; diff --git a/src/10_PITF/Testat/BankAccount.cpp b/src/10_PITF/Testat/BankAccount.cpp index 333c1c8..77000ce 100644 --- a/src/10_PITF/Testat/BankAccount.cpp +++ b/src/10_PITF/Testat/BankAccount.cpp @@ -1,36 +1,30 @@ +#include + +#include + #include "BankAccount.h" #include "CashFactory.h" -BankAccount::BankAccount(Person * owner, std::string name) : name(std::move(name)) { -this->owner = owner; +BankAccount::BankAccount(std::shared_ptr owner, std::string name) : name(std::move(name)), owner(std::move(owner)) { + } -BankAccount * BankAccount::operator+(Money * money) { +std::unique_ptr BankAccount::operator+(std::unique_ptr money) { + std::unique_ptr ptr = std::make_unique(new Money(money->getValue() + getMoney()->getValue(), money->getCurrency())); - BankAccount::money = new Money(money->getValue() + getMoney().getValue()); - return this; + + BankAccount::money.swap(ptr); + return std::make_unique(this); } -Money * BankAccount::operator-(int value) { +std::unique_ptr BankAccount::operator-(int value) { auto * cashFactory = new CashFactory(); return cashFactory->printCoin(value, CurrencyValue::USD); } -bool BankAccount::operator<(BankAccount * bankAccount) { - return money.getValue() < bankAccount->money.getValue(); -} - -std::string BankAccount::getName() { - return name; -} - -Money BankAccount::getMoney() { - return money; -} - -Person *BankAccount::getOwner() { - return owner; +bool BankAccount::operator<(std::unique_ptr bankAccount) { + return money->getValue() < bankAccount->getMoney()->getValue(); } diff --git a/src/10_PITF/Testat/BankAccount.h b/src/10_PITF/Testat/BankAccount.h index c0432ad..48e5446 100644 --- a/src/10_PITF/Testat/BankAccount.h +++ b/src/10_PITF/Testat/BankAccount.h @@ -2,6 +2,7 @@ #define C_C_BANKACCOUNTS_H #include +#include #include "Money.h" #include "Cash.h" @@ -10,16 +11,16 @@ class BankAccount { private: std::string name; - Person * owner; - Money money; + std::shared_ptr owner; + std::unique_ptr money; public: - explicit BankAccount(Person * owner, std::string name); - BankAccount* operator+(Money * money); - Money* operator-(int value); - bool operator<(BankAccount * bankAccount); + explicit BankAccount(std::shared_ptr owner, std::string name); + std::unique_ptr operator+(std::unique_ptr money); + std::unique_ptr operator-(int value); + bool operator<(std::unique_ptr bankAccount); std::string getName(); - Person * getOwner(); - Money getMoney(); + std::shared_ptr getOwner(); + std::unique_ptr getMoney(); }; diff --git a/src/10_PITF/Testat/Bill.h b/src/10_PITF/Testat/Bill.h index bafd05e..a747b00 100644 --- a/src/10_PITF/Testat/Bill.h +++ b/src/10_PITF/Testat/Bill.h @@ -10,7 +10,7 @@ private: public: Bill(int value, Currency currency, std::string serial); std::string getSerial(); - int getValue(); + int getValue() override; }; diff --git a/src/10_PITF/Testat/CashFactory.cpp b/src/10_PITF/Testat/CashFactory.cpp index 0eff08f..44cd9aa 100644 --- a/src/10_PITF/Testat/CashFactory.cpp +++ b/src/10_PITF/Testat/CashFactory.cpp @@ -6,7 +6,7 @@ int allowedCoins[8] = {1, 2, 5, 10, 20, 50, 100, 200}; std::vector usedSerials; -Bill * CashFactory::printBill(int value, Currency currency) { +std::unique_ptr CashFactory::printBill(int value, Currency currency) { auto * iter = std::find(std::begin(allowedBills), std::end(allowedBills), value); if(iter == std::end(allowedBills)) { return nullptr; @@ -16,15 +16,16 @@ Bill * CashFactory::printBill(int value, Currency currency) { serial = randomString(15); } usedSerials.push_back(serial); - return new Bill(value, currency, serial); + + return std::make_unique(new Bill(value, currency, serial)); } } -Coin * CashFactory::printCoin(int value, Currency currency) { +std::unique_ptr CashFactory::printCoin(int value, Currency currency) { auto * iter = std::find(std::begin(allowedCoins), std::end(allowedCoins), value); if(iter == std::end(allowedCoins)) { return nullptr; - }else return new Coin(value, currency); + }else return std::make_unique(new Coin(value, currency)); } std::string CashFactory::randomString(size_t length) { diff --git a/src/10_PITF/Testat/CashFactory.h b/src/10_PITF/Testat/CashFactory.h index 59b33b0..8bfcd84 100644 --- a/src/10_PITF/Testat/CashFactory.h +++ b/src/10_PITF/Testat/CashFactory.h @@ -3,6 +3,8 @@ #include #include +#include + #include "Bill.h" #include "Coin.h" @@ -10,8 +12,8 @@ class CashFactory { private: std::string randomString(size_t length); public: - Bill * printBill(int value, Currency currency); - Coin * printCoin(int value, Currency currency); + std::unique_ptr printBill(int value, Currency currency); + std::unique_ptr printCoin(int value, Currency currency); }; #endif \ No newline at end of file diff --git a/src/10_PITF/Testat/Coin.h b/src/10_PITF/Testat/Coin.h index 2ea7be6..9195945 100644 --- a/src/10_PITF/Testat/Coin.h +++ b/src/10_PITF/Testat/Coin.h @@ -8,7 +8,7 @@ class Coin : public Cash{ private: public: Coin(int value, Currency currency); - int getValue(); + int getValue() override; }; #endif diff --git a/src/10_PITF/Testat/Currency.cpp b/src/10_PITF/Testat/Currency.cpp index 181d96d..f73472e 100644 --- a/src/10_PITF/Testat/Currency.cpp +++ b/src/10_PITF/Testat/Currency.cpp @@ -4,7 +4,7 @@ Currency::Currency(CurrencyValue value) : value(value) { } -bool Currency::operator==(Currency ¤cy) { +bool Currency::operator==(Currency currency) { return value == currency.value; } diff --git a/src/10_PITF/Testat/Currency.h b/src/10_PITF/Testat/Currency.h index 469346c..d511519 100644 --- a/src/10_PITF/Testat/Currency.h +++ b/src/10_PITF/Testat/Currency.h @@ -14,7 +14,7 @@ private: CurrencyValue value; public: Currency(CurrencyValue value); - bool operator==(Currency ¤cy); + bool operator==(Currency currency); Currency& operator=(Currency currency); CurrencyValue getValue(); diff --git a/src/10_PITF/Testat/Money.cpp b/src/10_PITF/Testat/Money.cpp index 841269d..d45bd6e 100644 --- a/src/10_PITF/Testat/Money.cpp +++ b/src/10_PITF/Testat/Money.cpp @@ -28,12 +28,14 @@ Money Money::operator-(Money &a) { }else return Money(-1, Currency(CurrencyValue::USD)); } -const Money Money::operator++(int) { - return Money(value++); +const std::unique_ptr Money::operator++(int) { + value++; + return std::make_unique(this); } -const Money Money::operator--(int) { - return Money(value--); +const std::unique_ptr Money::operator--(int) { + value--; + return std::make_unique(this); } int Money::getValue() { @@ -42,4 +44,22 @@ int Money::getValue() { Currency Money::getCurrency() { return currency; -} \ No newline at end of file +} + +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 ddfa6f1..5eed649 100644 --- a/src/10_PITF/Testat/Money.h +++ b/src/10_PITF/Testat/Money.h @@ -1,6 +1,8 @@ #ifndef C_C_MONEY_H #define C_C_MONEY_H +#include + #include "Currency.h" class Money { @@ -9,12 +11,12 @@ private: Currency currency; public: explicit Money(int value = 0, Currency currency = CurrencyValue::USD); - Money& operator=(Money money); - Money& operator=(Money * money); - Money operator+(Money &a); - Money operator-(Money &a); - const Money operator++(int); - const Money operator--(int); + 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); + const std::unique_ptr operator++(int); + const std::unique_ptr operator--(int); virtual int getValue(); Currency getCurrency(); diff --git a/src/10_PITF/Testat/Person.h b/src/10_PITF/Testat/Person.h index ffd9656..f272851 100644 --- a/src/10_PITF/Testat/Person.h +++ b/src/10_PITF/Testat/Person.h @@ -3,12 +3,14 @@ #include +#include + #include "Money.h" class Person { private: std::string name; - std::vector money; + std::vector> money; public: explicit Person(std::string name); std::string getName(); diff --git a/src/10_PITF/Testat/main.cpp b/src/10_PITF/Testat/main.cpp index c5db9e8..859c8b7 100644 --- a/src/10_PITF/Testat/main.cpp +++ b/src/10_PITF/Testat/main.cpp @@ -34,8 +34,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, std::unique_ptr money) { + os << money->getValue() << " " << money->getCurrency(); return os; } @@ -50,7 +50,7 @@ std::ostream &operator<<(std::ostream &os, BankAccount bankAccount) { } std::set banks; -std::set owners; +std::set> owners; CashFactory *cashFactory; void setup() { @@ -60,9 +60,9 @@ void setup() { Bank *bank2 = new Bank("Bank 2"); Bank *bank3 = new Bank("Bank 3"); - Person *max = new Person("Max Müller"); - Person *marius = new Person("Marius Müller"); - Person *alex = new Person("Alex Maier"); + 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")); owners.insert(max); owners.insert(marius); @@ -91,10 +91,10 @@ void simulate() { std::cout << std::endl; std::cout << "======================== " << bank->getName() << " ========================" << std::endl; std::cout << std::endl; - for (BankAccount *bankAccount : bank->getAccounts()) { + for (std::unique_ptr bankAccount : bank->getAccounts()) { for (int i = 0; i < 10000; ++i) { int rnd = distribution(generator); - Bill *bill = cashFactory->printBill(rnd, CurrencyValue::USD); + std::unique_ptr bill = cashFactory->printBill(rnd, CurrencyValue::USD); Coin *coin = cashFactory->printCoin(rnd, CurrencyValue::USD); if (bill != nullptr) { *bankAccount + bill; @@ -124,8 +124,11 @@ void tearDown() { int main(int argc, char **argv) { + std::cout << "+++++++++++++++++++++++++ " << "setup()" << " +++++++++++++++++++++++++" << std::endl; setup(); + std::cout << "+++++++++++++++++++++++++ " << "simulate()" << " +++++++++++++++++++++++++" << std::endl; simulate(); + std::cout << "+++++++++++++++++++++++++ " << "tearDown()" << " +++++++++++++++++++++++++" << std::endl; tearDown(); HeapObject::assertionsHold();