10_PITF: Erste Pointer wieder zu smart Pointern geändert

This commit is contained in:
Johannes Theiner 2019-01-03 08:43:17 +01:00
parent 5c46cc3d66
commit cc16e9d094
8 changed files with 41 additions and 26 deletions

View File

@ -7,6 +7,10 @@ Bank::Bank(std::string name) : name(std::move(name)) {
}
Bank::Bank(Bank *bank) : name(bank->getName()) {
}
void Bank::addAccount(BankAccount * account) {
accounts.insert(account);
}

View File

@ -12,6 +12,7 @@ private:
std::set<Money *> money;
public:
explicit Bank(std::string name);
explicit Bank(Bank* bank);
void addAccount(BankAccount * account);
void addMoney(Money * money);
std::set<BankAccount *> getAccounts();

View File

@ -1,23 +1,29 @@
#include <utility>
#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<Person> owner, std::string name) : name(std::move(name)), owner(std::move(owner)) {
}
BankAccount * BankAccount::operator+(Money * money) {
BankAccount::BankAccount(BankAccount *bankAccount) : owner(std::move(bankAccount->getOwner())), name(std::move(bankAccount->getName())) {
}
BankAccount *BankAccount::operator+(Money *money) {
BankAccount::money = new Money(money->getValue() + getMoney().getValue());
return this;
}
Money * BankAccount::operator-(int value) {
auto * cashFactory = new CashFactory();
Money *BankAccount::operator-(int value) {
auto *cashFactory = new CashFactory();
return cashFactory->printCoin(value, CurrencyValue::USD);
}
bool BankAccount::operator<(BankAccount * bankAccount) {
bool BankAccount::operator<(BankAccount *bankAccount) {
return money.getValue() < bankAccount->money.getValue();
}
@ -29,6 +35,6 @@ Money BankAccount::getMoney() {
return money;
}
Person *BankAccount::getOwner() {
std::shared_ptr<Person> BankAccount::getOwner() {
return owner;
}

View File

@ -2,6 +2,7 @@
#define C_C_BANKACCOUNTS_H
#include <unordered_map>
#include <memory>
#include "Money.h"
#include "Cash.h"
@ -10,15 +11,16 @@
class BankAccount {
private:
std::string name;
Person * owner;
std::shared_ptr<Person> owner;
Money money;
public:
explicit BankAccount(Person * owner, std::string name);
explicit BankAccount(std::shared_ptr<Person> owner, std::string name);
explicit BankAccount(BankAccount* bankAccount);
BankAccount* operator+(Money * money);
Money* operator-(int value);
bool operator<(BankAccount * bankAccount);
std::string getName();
Person * getOwner();
std::shared_ptr<Person> getOwner();
Money getMoney();
};

View File

@ -22,7 +22,6 @@ HeapObject::~HeapObject(){
bool HeapObject::assertionsHold(){
assert(ctorCount == newCount); // all objects have been allocated on heap
std::cout << ctorCount << " == " << dtorCount << std::endl;
assert(ctorCount == dtorCount); // all objects have been deleted
return true;
}

View File

@ -6,6 +6,10 @@ Person::Person(std::string name) : name(std::move(name)){
}
Person::Person(Person *person) : name(std::move(person->getName())) {
}
std::string Person::getName() {
return name;
}

View File

@ -11,6 +11,7 @@ private:
std::vector<Money *> money;
public:
explicit Person(std::string name);
explicit Person(Person* person);
std::string getName();
};

View File

@ -1,5 +1,5 @@
#include <iostream>
#include <memory>
#include <set>
#include <random>
#include <ctime>
@ -47,18 +47,17 @@ std::ostream &operator<<(std::ostream &os, BankAccount bankAccount) {
return os;
}
std::set<Bank *> banks;
std::set<std::unique_ptr<Bank>> banks;
void setup() {
std::unique_ptr<Bank> bank1 = std::make_unique<Bank>(new Bank("Bank 1"));
std::unique_ptr<Bank> bank2 = std::make_unique<Bank>(new Bank("Bank 2"));
std::unique_ptr<Bank> bank3 = std::make_unique<Bank>(new Bank("Bank 3"));
Bank *bank1 = new Bank("Bank 1");
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::shared_ptr<Person> max = std::make_shared<Person>(new Person("Max Müller"));
std::shared_ptr<Person> marius = std::make_shared<Person>(new Person("Marius Müller"));
std::shared_ptr<Person> alex = std::make_shared<Person>(new Person("Alex Maier"));
bank1->addAccount(new BankAccount(max, "Konto 1"));
@ -71,15 +70,15 @@ void setup() {
bank1->addAccount(new BankAccount(marius, "Konto 2"));
bank3->addAccount(new 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));
}
void simulate() {
auto *cashFactory = new CashFactory();
for (Bank *bank : banks) {
for (auto && bank : banks) {
std::cout << std::endl;
std::cout << "======================== " << bank->getName() << " ========================" << std::endl;
std::cout << std::endl;
@ -103,11 +102,10 @@ void simulate() {
}
void tearDown() {
for (Bank *bank : banks) {
for (auto && bank : banks) {
for (BankAccount *bankAccount : bank->getAccounts()) {
delete(bankAccount);
}
delete(bank);
}
}