From fc9933ec838e9199207d1990b0ca202f688ed1ca Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Mon, 31 Dec 2018 16:03:56 +0100 Subject: [PATCH] 10_PITF: so, neuer Fehler jetzt aber zur Laufzeit --- src/10_PITF/Testat/Bank.cpp | 6 +++--- src/10_PITF/Testat/Bank.h | 10 +++++----- src/10_PITF/Testat/BankAccount.cpp | 18 +++++++++++++++--- src/10_PITF/Testat/BankAccount.h | 7 ++----- src/10_PITF/Testat/main.cpp | 20 ++++++++++---------- 5 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/10_PITF/Testat/Bank.cpp b/src/10_PITF/Testat/Bank.cpp index 1bf3eb6..6c71edc 100644 --- a/src/10_PITF/Testat/Bank.cpp +++ b/src/10_PITF/Testat/Bank.cpp @@ -12,15 +12,15 @@ Bank::Bank(Bank* bank) : name(bank->name) { accounts.swap(bank->accounts); } -void Bank::addAccount(std::unique_ptr account) { +void Bank::addAccount(std::shared_ptr account) { this->accounts.insert(std::move(account)); } -void Bank::addMoney(std::unique_ptr money) { +void Bank::addMoney(std::shared_ptr money) { this->money.insert(std::move(money)); } -std::set> Bank::getAccounts() { +std::set> Bank::getAccounts() { return accounts; } diff --git a/src/10_PITF/Testat/Bank.h b/src/10_PITF/Testat/Bank.h index b38b9a9..97c15c8 100644 --- a/src/10_PITF/Testat/Bank.h +++ b/src/10_PITF/Testat/Bank.h @@ -7,14 +7,14 @@ class Bank { private: std::string name; - std::set> accounts; - std::set> money; + std::set> accounts; + std::set> money; public: explicit Bank(std::string const& name); explicit Bank(Bank* bank); - void addAccount(std::unique_ptr account); - void addMoney(std::unique_ptr money); - std::set> getAccounts(); + void addAccount(std::shared_ptr account); + void addMoney(std::shared_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 7f05675..abc4d88 100644 --- a/src/10_PITF/Testat/BankAccount.cpp +++ b/src/10_PITF/Testat/BankAccount.cpp @@ -20,9 +20,9 @@ BankAccount::BankAccount(BankAccount &bankAccount) : name(bankAccount.name) { } std::unique_ptr BankAccount::operator+(Money* money) { - std::unique_ptr ptr = std::make_unique(Money(money->getValue() + getMoney()->getValue(), money->getCurrency())); + std::shared_ptr ptr = std::make_unique(Money(money->getValue() + this->money->getValue(), money->getCurrency())); - BankAccount::money.swap(ptr); + BankAccount::money = ptr; return std::make_unique(*this); } @@ -34,5 +34,17 @@ std::unique_ptr BankAccount::operator-(int value) { } bool BankAccount::operator<(std::unique_ptr bankAccount) { - return money->getValue() < bankAccount->getMoney()->getValue(); + return this->money->getValue() < bankAccount->money->getValue(); } + +std::string BankAccount::getName() { + return name; +} + +std::shared_ptr BankAccount::getMoney() { + return money; +} + +std::shared_ptr BankAccount::getOwner() { + return owner; +} \ No newline at end of file diff --git a/src/10_PITF/Testat/BankAccount.h b/src/10_PITF/Testat/BankAccount.h index 98d9f00..90abe64 100644 --- a/src/10_PITF/Testat/BankAccount.h +++ b/src/10_PITF/Testat/BankAccount.h @@ -1,17 +1,14 @@ #ifndef C_C_BANKACCOUNTS_H #define C_C_BANKACCOUNTS_H -#include - #include "Money.h" -#include "Cash.h" #include "Person.h" class BankAccount { private: std::string name; std::shared_ptr owner; - std::unique_ptr money; + std::shared_ptr money; public: explicit BankAccount(std::shared_ptr const& owner, std::string const& name); explicit BankAccount(BankAccount *bankAccount); @@ -21,7 +18,7 @@ public: bool operator<(std::unique_ptr bankAccount); std::string getName(); std::shared_ptr getOwner(); - std::unique_ptr getMoney(); + std::shared_ptr getMoney(); }; diff --git a/src/10_PITF/Testat/main.cpp b/src/10_PITF/Testat/main.cpp index 4993724..d64e3eb 100644 --- a/src/10_PITF/Testat/main.cpp +++ b/src/10_PITF/Testat/main.cpp @@ -45,7 +45,7 @@ std::ostream &operator<<(std::ostream &os, std::shared_ptr const& person } std::ostream &operator<<(std::ostream &os, std::unique_ptr bankAccount) { - os << bankAccount->getOwner() << " " << bankAccount->getName() << ": " << bankAccount->getMoney(); + os << bankAccount->getName() << " " << bankAccount->getOwner() << ": " << bankAccount.get()->getMoney(); return os; } @@ -68,15 +68,15 @@ void setup() { owners.insert(alex); - bank1->addAccount(std::make_unique(new BankAccount(max, "Konto 1"))); - bank2->addAccount(std::make_unique(new BankAccount(max, "Konto 2"))); - bank3->addAccount(std::make_unique(new BankAccount(alex, "privat"))); - bank1->addAccount(std::make_unique(new BankAccount(alex, "geschäftlich 1"))); - bank2->addAccount(std::make_unique(new BankAccount(alex, "geschäftlich 2"))); - bank3->addAccount(std::make_unique(new BankAccount(alex, "geschäftlich 3"))); - bank1->addAccount(std::make_unique(new BankAccount(marius, "Konto 1"))); - bank2->addAccount(std::make_unique(new BankAccount(marius, "Konto 2"))); - bank3->addAccount(std::make_unique(new BankAccount(marius, "Konto 3"))); + bank1->addAccount(std::make_shared(new BankAccount(max, "Konto 1"))); + bank2->addAccount(std::make_shared(new BankAccount(max, "Konto 2"))); + bank3->addAccount(std::make_shared(new BankAccount(alex, "privat"))); + bank1->addAccount(std::make_shared(new BankAccount(alex, "geschäftlich 1"))); + bank2->addAccount(std::make_shared(new BankAccount(alex, "geschäftlich 2"))); + bank3->addAccount(std::make_shared(new BankAccount(alex, "geschäftlich 3"))); + bank1->addAccount(std::make_shared(new BankAccount(marius, "Konto 1"))); + bank2->addAccount(std::make_shared(new BankAccount(marius, "Konto 2"))); + bank3->addAccount(std::make_shared(new BankAccount(marius, "Konto 3"))); banks.insert(std::move(bank1)); banks.insert(std::move(bank2));