10_PITF: so, neuer Fehler jetzt aber zur Laufzeit

This commit is contained in:
Johannes Theiner 2018-12-31 16:03:56 +01:00
parent 568a5bc98e
commit fc9933ec83
5 changed files with 35 additions and 26 deletions

View File

@ -12,15 +12,15 @@ Bank::Bank(Bank* bank) : name(bank->name) {
accounts.swap(bank->accounts); accounts.swap(bank->accounts);
} }
void Bank::addAccount(std::unique_ptr<BankAccount> account) { void Bank::addAccount(std::shared_ptr<BankAccount> account) {
this->accounts.insert(std::move(account)); this->accounts.insert(std::move(account));
} }
void Bank::addMoney(std::unique_ptr<Money> money) { void Bank::addMoney(std::shared_ptr<Money> money) {
this->money.insert(std::move(money)); this->money.insert(std::move(money));
} }
std::set<std::unique_ptr<BankAccount>> Bank::getAccounts() { std::set<std::shared_ptr<BankAccount>> Bank::getAccounts() {
return accounts; return accounts;
} }

View File

@ -7,14 +7,14 @@
class Bank { class Bank {
private: private:
std::string name; std::string name;
std::set<std::unique_ptr<BankAccount>> accounts; std::set<std::shared_ptr<BankAccount>> accounts;
std::set<std::unique_ptr<Money>> money; std::set<std::shared_ptr<Money>> money;
public: public:
explicit Bank(std::string const& name); explicit Bank(std::string const& name);
explicit Bank(Bank* bank); explicit Bank(Bank* bank);
void addAccount(std::unique_ptr<BankAccount> account); void addAccount(std::shared_ptr<BankAccount> account);
void addMoney(std::unique_ptr<Money> money); void addMoney(std::shared_ptr<Money> money);
std::set<std::unique_ptr<BankAccount>> getAccounts(); std::set<std::shared_ptr<BankAccount>> getAccounts();
std::string getName(); std::string getName();
}; };

View File

@ -20,9 +20,9 @@ BankAccount::BankAccount(BankAccount &bankAccount) : name(bankAccount.name) {
} }
std::unique_ptr<BankAccount> BankAccount::operator+(Money* money) { std::unique_ptr<BankAccount> BankAccount::operator+(Money* money) {
std::unique_ptr<Money> ptr = std::make_unique<Money>(Money(money->getValue() + getMoney()->getValue(), money->getCurrency())); std::shared_ptr<Money> ptr = std::make_unique<Money>(Money(money->getValue() + this->money->getValue(), money->getCurrency()));
BankAccount::money.swap(ptr); BankAccount::money = ptr;
return std::make_unique<BankAccount>(*this); return std::make_unique<BankAccount>(*this);
} }
@ -34,5 +34,17 @@ std::unique_ptr<Money> BankAccount::operator-(int value) {
} }
bool BankAccount::operator<(std::unique_ptr<BankAccount> bankAccount) { bool BankAccount::operator<(std::unique_ptr<BankAccount> bankAccount) {
return money->getValue() < bankAccount->getMoney()->getValue(); return this->money->getValue() < bankAccount->money->getValue();
} }
std::string BankAccount::getName() {
return name;
}
std::shared_ptr<Money> BankAccount::getMoney() {
return money;
}
std::shared_ptr<Person> BankAccount::getOwner() {
return owner;
}

View File

@ -1,17 +1,14 @@
#ifndef C_C_BANKACCOUNTS_H #ifndef C_C_BANKACCOUNTS_H
#define C_C_BANKACCOUNTS_H #define C_C_BANKACCOUNTS_H
#include <memory>
#include "Money.h" #include "Money.h"
#include "Cash.h"
#include "Person.h" #include "Person.h"
class BankAccount { class BankAccount {
private: private:
std::string name; std::string name;
std::shared_ptr<Person> owner; std::shared_ptr<Person> owner;
std::unique_ptr<Money> money; std::shared_ptr<Money> money;
public: public:
explicit BankAccount(std::shared_ptr<Person> const& owner, std::string const& name); explicit BankAccount(std::shared_ptr<Person> const& owner, std::string const& name);
explicit BankAccount(BankAccount *bankAccount); explicit BankAccount(BankAccount *bankAccount);
@ -21,7 +18,7 @@ public:
bool operator<(std::unique_ptr<BankAccount> bankAccount); bool operator<(std::unique_ptr<BankAccount> bankAccount);
std::string getName(); std::string getName();
std::shared_ptr<Person> getOwner(); std::shared_ptr<Person> getOwner();
std::unique_ptr<Money> getMoney(); std::shared_ptr<Money> getMoney();
}; };

View File

@ -45,7 +45,7 @@ std::ostream &operator<<(std::ostream &os, std::shared_ptr<Person> const& person
} }
std::ostream &operator<<(std::ostream &os, std::unique_ptr<BankAccount> bankAccount) { std::ostream &operator<<(std::ostream &os, std::unique_ptr<BankAccount> bankAccount) {
os << bankAccount->getOwner() << " " << bankAccount->getName() << ": " << bankAccount->getMoney(); os << bankAccount->getName() << " " << bankAccount->getOwner() << ": " << bankAccount.get()->getMoney();
return os; return os;
} }
@ -68,15 +68,15 @@ void setup() {
owners.insert(alex); owners.insert(alex);
bank1->addAccount(std::make_unique<BankAccount>(new BankAccount(max, "Konto 1"))); bank1->addAccount(std::make_shared<BankAccount>(new BankAccount(max, "Konto 1")));
bank2->addAccount(std::make_unique<BankAccount>(new BankAccount(max, "Konto 2"))); bank2->addAccount(std::make_shared<BankAccount>(new BankAccount(max, "Konto 2")));
bank3->addAccount(std::make_unique<BankAccount>(new BankAccount(alex, "privat"))); bank3->addAccount(std::make_shared<BankAccount>(new BankAccount(alex, "privat")));
bank1->addAccount(std::make_unique<BankAccount>(new BankAccount(alex, "geschäftlich 1"))); bank1->addAccount(std::make_shared<BankAccount>(new BankAccount(alex, "geschäftlich 1")));
bank2->addAccount(std::make_unique<BankAccount>(new BankAccount(alex, "geschäftlich 2"))); bank2->addAccount(std::make_shared<BankAccount>(new BankAccount(alex, "geschäftlich 2")));
bank3->addAccount(std::make_unique<BankAccount>(new BankAccount(alex, "geschäftlich 3"))); bank3->addAccount(std::make_shared<BankAccount>(new BankAccount(alex, "geschäftlich 3")));
bank1->addAccount(std::make_unique<BankAccount>(new BankAccount(marius, "Konto 1"))); bank1->addAccount(std::make_shared<BankAccount>(new BankAccount(marius, "Konto 1")));
bank2->addAccount(std::make_unique<BankAccount>(new BankAccount(marius, "Konto 2"))); bank2->addAccount(std::make_shared<BankAccount>(new BankAccount(marius, "Konto 2")));
bank3->addAccount(std::make_unique<BankAccount>(new BankAccount(marius, "Konto 3"))); bank3->addAccount(std::make_shared<BankAccount>(new BankAccount(marius, "Konto 3")));
banks.insert(std::move(bank1)); banks.insert(std::move(bank1));
banks.insert(std::move(bank2)); banks.insert(std::move(bank2));