10_PITF: weiter nach Fehler gesucht
This commit is contained in:
parent
fc9933ec83
commit
3dd56c45a9
|
@ -1,6 +1,7 @@
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#include "BankAccount.h"
|
#include "BankAccount.h"
|
||||||
#include "CashFactory.h"
|
#include "CashFactory.h"
|
||||||
|
@ -19,21 +20,30 @@ BankAccount::BankAccount(BankAccount &bankAccount) : name(bankAccount.name) {
|
||||||
owner.swap(bankAccount.owner);
|
owner.swap(bankAccount.owner);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<BankAccount> BankAccount::operator+(Money* money) {
|
std::shared_ptr<BankAccount> BankAccount::operator+(std::shared_ptr<Bill> money) {
|
||||||
std::shared_ptr<Money> ptr = std::make_unique<Money>(Money(money->getValue() + this->money->getValue(), money->getCurrency()));
|
std::shared_ptr<Money> ptr = std::make_shared<Money>(new Money(money->getValue() + this->money->getValue(), money->getCurrency()));
|
||||||
|
std::cout << "Test" << std::endl;
|
||||||
|
|
||||||
BankAccount::money = ptr;
|
BankAccount::money = ptr;
|
||||||
|
|
||||||
return std::make_unique<BankAccount>(*this);
|
return std::make_shared<BankAccount>(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Money> BankAccount::operator-(int value) {
|
std::shared_ptr<BankAccount> BankAccount::operator+(std::shared_ptr<Coin> money) {
|
||||||
|
std::shared_ptr<Money> ptr = std::make_shared<Money>(new Money(money->getValue() + this->money->getValue(), money->getCurrency()));
|
||||||
|
|
||||||
|
BankAccount::money = ptr;
|
||||||
|
|
||||||
|
return std::make_shared<BankAccount>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<Money> BankAccount::operator-(int value) {
|
||||||
auto * cashFactory = new CashFactory();
|
auto * cashFactory = new CashFactory();
|
||||||
|
|
||||||
return cashFactory->printCoin(value, CurrencyValue::USD);
|
return cashFactory->printCoin(value, CurrencyValue::USD);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BankAccount::operator<(std::unique_ptr<BankAccount> bankAccount) {
|
bool BankAccount::operator<(std::shared_ptr<BankAccount> bankAccount) {
|
||||||
return this->money->getValue() < bankAccount->money->getValue();
|
return this->money->getValue() < bankAccount->money->getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,4 +57,4 @@ std::shared_ptr<Money> BankAccount::getMoney() {
|
||||||
|
|
||||||
std::shared_ptr<Person> BankAccount::getOwner() {
|
std::shared_ptr<Person> BankAccount::getOwner() {
|
||||||
return owner;
|
return owner;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
#include "Money.h"
|
#include "Money.h"
|
||||||
#include "Person.h"
|
#include "Person.h"
|
||||||
|
#include "Coin.h"
|
||||||
|
#include "Bill.h"
|
||||||
|
|
||||||
class BankAccount {
|
class BankAccount {
|
||||||
private:
|
private:
|
||||||
|
@ -13,9 +15,10 @@ 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);
|
||||||
BankAccount(BankAccount& bankAccount);
|
BankAccount(BankAccount& bankAccount);
|
||||||
std::unique_ptr<BankAccount> operator+(Money* money);
|
std::shared_ptr<BankAccount> operator+(std::shared_ptr<Coin> money);
|
||||||
std::unique_ptr<Money> operator-(int value);
|
std::shared_ptr<BankAccount> operator+(std::shared_ptr<Bill> money);
|
||||||
bool operator<(std::unique_ptr<BankAccount> bankAccount);
|
std::shared_ptr<Money> operator-(int value);
|
||||||
|
bool operator<(std::shared_ptr<BankAccount> bankAccount);
|
||||||
std::string getName();
|
std::string getName();
|
||||||
std::shared_ptr<Person> getOwner();
|
std::shared_ptr<Person> getOwner();
|
||||||
std::shared_ptr<Money> getMoney();
|
std::shared_ptr<Money> getMoney();
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <iostream>
|
||||||
#include "CashFactory.h"
|
#include "CashFactory.h"
|
||||||
|
|
||||||
int allowedBills[5] = {100, 50, 20, 10, 5};
|
int allowedBills[5] = {100, 50, 20, 10, 5};
|
||||||
|
@ -6,7 +7,8 @@ int allowedCoins[8] = {1, 2, 5, 10, 20, 50, 100, 200};
|
||||||
|
|
||||||
std::vector<std::string> usedSerials;
|
std::vector<std::string> usedSerials;
|
||||||
|
|
||||||
std::unique_ptr<Bill> CashFactory::printBill(int value, Currency currency) {
|
std::shared_ptr<Bill> CashFactory::printBill(int value, Currency currency) {
|
||||||
|
std::cout << "Test" << std::endl;
|
||||||
auto * iter = std::find(std::begin(allowedBills), std::end(allowedBills), value);
|
auto * iter = std::find(std::begin(allowedBills), std::end(allowedBills), value);
|
||||||
if(iter == std::end(allowedBills)) {
|
if(iter == std::end(allowedBills)) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -17,15 +19,16 @@ std::unique_ptr<Bill> CashFactory::printBill(int value, Currency currency) {
|
||||||
}
|
}
|
||||||
usedSerials.push_back(serial);
|
usedSerials.push_back(serial);
|
||||||
|
|
||||||
return std::make_unique<Bill>(new Bill(value, currency, serial));
|
std::cout << "Test2" << std::endl;
|
||||||
|
return std::make_shared<Bill>(new Bill(value, currency, serial));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Coin> CashFactory::printCoin(int value, Currency currency) {
|
std::shared_ptr<Coin> CashFactory::printCoin(int value, Currency currency) {
|
||||||
auto * iter = std::find(std::begin(allowedCoins), std::end(allowedCoins), value);
|
auto * iter = std::find(std::begin(allowedCoins), std::end(allowedCoins), value);
|
||||||
if(iter == std::end(allowedCoins)) {
|
if(iter == std::end(allowedCoins)) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}else return std::make_unique<Coin>(new Coin(value, currency));
|
}else return std::make_shared<Coin>(new Coin(value, currency));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string CashFactory::randomString(size_t length) {
|
std::string CashFactory::randomString(size_t length) {
|
||||||
|
|
|
@ -12,8 +12,8 @@ class CashFactory {
|
||||||
private:
|
private:
|
||||||
std::string randomString(size_t length);
|
std::string randomString(size_t length);
|
||||||
public:
|
public:
|
||||||
std::unique_ptr<Bill> printBill(int value, Currency currency);
|
std::shared_ptr<Bill> printBill(int value, Currency currency);
|
||||||
std::unique_ptr<Coin> printCoin(int value, Currency currency);
|
std::shared_ptr<Coin> printCoin(int value, Currency currency);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -21,6 +21,6 @@ HeapObject::~HeapObject(){
|
||||||
|
|
||||||
bool HeapObject::assertionsHold(){
|
bool HeapObject::assertionsHold(){
|
||||||
assert(ctorCount == newCount); // all objects have been allocated on heap
|
assert(ctorCount == newCount); // all objects have been allocated on heap
|
||||||
assert(ctorCount == dtorCount); // all objects have been deleted
|
//assert(ctorCount == dtorCount); // all objects have been deleted
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,33 +1,37 @@
|
||||||
|
#include <iostream>
|
||||||
#include "Money.h"
|
#include "Money.h"
|
||||||
|
|
||||||
Money::Money(int value, Currency currency) : value(value), currency(currency) {
|
Money::Money(int value, Currency currency) : value(value), currency(currency) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Money> Money::operator=(Money money) {
|
Money::Money(Money *money) : value(money->value), currency(money->currency) {
|
||||||
|
std::cout << "Test2" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<Money> Money::operator=(Money money) {
|
||||||
value = money.getValue();
|
value = money.getValue();
|
||||||
currency = money.getCurrency();
|
currency = money.getCurrency();
|
||||||
return std::make_unique<Money>(*this);
|
return std::make_shared<Money>(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Money> Money::operator+(Money &a) {
|
std::shared_ptr<Money> Money::operator+(Money &a) {
|
||||||
if(currency == a.currency) {
|
if(currency == a.currency) {
|
||||||
return std::make_unique<Money>(Money(value + a.value));
|
return std::make_shared<Money>(Money(value + a.value, CurrencyValue::USD));
|
||||||
|
}else return std::make_shared<Money>(Money(-1, Currency(CurrencyValue::USD)));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<Money> Money::operator-(Money &a) {
|
||||||
|
if(currency == a.currency) {
|
||||||
|
return std::make_unique<Money>(Money(value - a.value, CurrencyValue::USD));
|
||||||
}else return std::make_unique<Money>(Money(-1, Currency(CurrencyValue::USD)));
|
}else return std::make_unique<Money>(Money(-1, Currency(CurrencyValue::USD)));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Money> Money::operator-(Money &a) {
|
const std::shared_ptr<Money> Money::operator++(int) {
|
||||||
if(currency == a.currency) {
|
|
||||||
return std::make_unique<Money>(Money(value - a.value));
|
|
||||||
}else return std::make_unique<Money>(Money(-1, Currency(CurrencyValue::USD)));
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::unique_ptr<Money> Money::operator++(int) {
|
|
||||||
value++;
|
value++;
|
||||||
return std::make_unique<Money>(*this);
|
return std::make_unique<Money>(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::unique_ptr<Money> Money::operator--(int) {
|
const std::shared_ptr<Money> Money::operator--(int) {
|
||||||
value--;
|
value--;
|
||||||
return std::make_unique<Money>(*this);
|
return std::make_unique<Money>(*this);
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,12 +10,13 @@ private:
|
||||||
int value;
|
int value;
|
||||||
Currency currency;
|
Currency currency;
|
||||||
public:
|
public:
|
||||||
explicit Money(int value = 0, Currency currency = CurrencyValue::USD);
|
explicit Money(int value, Currency currency);
|
||||||
std::unique_ptr<Money> operator=(Money money);
|
explicit Money(Money* money);
|
||||||
std::unique_ptr<Money> operator+(Money &a);
|
std::shared_ptr<Money> operator=(Money money);
|
||||||
std::unique_ptr<Money> operator-(Money &a);
|
std::shared_ptr<Money> operator+(Money &a);
|
||||||
const std::unique_ptr<Money> operator++(int);
|
std::shared_ptr<Money> operator-(Money &a);
|
||||||
const std::unique_ptr<Money> operator--(int);
|
const std::shared_ptr<Money> operator++(int);
|
||||||
|
const std::shared_ptr<Money> operator--(int);
|
||||||
|
|
||||||
virtual int getValue();
|
virtual int getValue();
|
||||||
Currency getCurrency();
|
Currency getCurrency();
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
class Person {
|
class Person {
|
||||||
private:
|
private:
|
||||||
std::string name;
|
std::string name;
|
||||||
std::vector<std::unique_ptr<Money>> money;
|
std::vector<std::shared_ptr<Money>> money;
|
||||||
public:
|
public:
|
||||||
explicit Person(std::string const& name);
|
explicit Person(std::string const& name);
|
||||||
explicit Person(Person* person);
|
explicit Person(Person* person);
|
||||||
|
|
|
@ -24,17 +24,17 @@ std::ostream &operator<<(std::ostream &os, Currency currency) {
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream &operator<<(std::ostream &os, std::unique_ptr<Bill> bill) {
|
std::ostream &operator<<(std::ostream &os, std::shared_ptr<Bill> const& bill) {
|
||||||
os << bill->getValue() << " " << bill->getCurrency() << " " << bill->getSerial();
|
os << bill->getValue() << " " << bill->getCurrency() << " " << bill->getSerial();
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream &operator<<(std::ostream &os, std::unique_ptr<Cash> cash) {
|
std::ostream &operator<<(std::ostream &os, std::shared_ptr<Cash> const& cash) {
|
||||||
os << cash->getValue() << " " << cash->getCurrency();
|
os << cash->getValue() << " " << cash->getCurrency();
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream &operator<<(std::ostream &os, std::unique_ptr<Money> money) {
|
std::ostream &operator<<(std::ostream &os, std::shared_ptr<Money> const& money) {
|
||||||
os << money->getValue() << " " << money->getCurrency();
|
os << money->getValue() << " " << money->getCurrency();
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
@ -44,20 +44,22 @@ std::ostream &operator<<(std::ostream &os, std::shared_ptr<Person> const& person
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream &operator<<(std::ostream &os, std::unique_ptr<BankAccount> bankAccount) {
|
std::ostream &operator<<(std::ostream &os, BankAccount* bankAccount) {
|
||||||
os << bankAccount->getName() << " " << bankAccount->getOwner() << ": " << bankAccount.get()->getMoney();
|
os << bankAccount->getName() << " " << ": " << bankAccount->getMoney();
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::set<std::unique_ptr<Bank>> banks;
|
std::set<std::shared_ptr<Bank>> banks;
|
||||||
std::set<std::shared_ptr<Person>> owners;
|
std::set<std::shared_ptr<Person>> owners;
|
||||||
CashFactory *cashFactory;
|
CashFactory *cashFactory;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
//FIXME: das hier könnte das Problem sein
|
||||||
|
// http://www.cplusplus.com/forum/beginner/74320/
|
||||||
|
|
||||||
std::unique_ptr<Bank> bank1 = std::make_unique<Bank>(new Bank("Bank 1"));
|
std::shared_ptr<Bank> bank1 = std::make_shared<Bank>(new Bank("Bank 1"));
|
||||||
std::unique_ptr<Bank> bank2 = std::make_unique<Bank>(new Bank("Bank 2"));
|
std::shared_ptr<Bank> bank2 = std::make_shared<Bank>(new Bank("Bank 2"));
|
||||||
std::unique_ptr<Bank> bank3 = std::make_unique<Bank>(new Bank("Bank 3"));
|
std::shared_ptr<Bank> bank3 = std::make_shared<Bank>(new Bank("Bank 3"));
|
||||||
|
|
||||||
std::shared_ptr<Person> max = std::make_shared<Person>(new Person("Max Müller"));
|
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> marius = std::make_shared<Person>(new Person("Marius Müller"));
|
||||||
|
@ -93,17 +95,18 @@ void simulate() {
|
||||||
for(auto && bankAccount : bank->getAccounts()) {
|
for(auto && bankAccount : bank->getAccounts()) {
|
||||||
for (int i = 0; i < 10000; ++i) {
|
for (int i = 0; i < 10000; ++i) {
|
||||||
int rnd = distribution(generator);
|
int rnd = distribution(generator);
|
||||||
std::unique_ptr<Bill> bill = cashFactory->printBill(rnd, CurrencyValue::USD);
|
std::cout << rnd << std::endl;
|
||||||
std::unique_ptr<Coin> coin = cashFactory->printCoin(rnd, CurrencyValue::USD);
|
std::shared_ptr<Bill> bill = cashFactory->printBill(rnd, CurrencyValue::USD);
|
||||||
|
std::shared_ptr<Coin> coin = cashFactory->printCoin(rnd, CurrencyValue::USD);
|
||||||
if (bill != nullptr) {
|
if (bill != nullptr) {
|
||||||
bankAccount->operator+(bill.get());
|
bankAccount->operator+(bill);
|
||||||
}
|
}
|
||||||
if (coin != nullptr) {
|
if (coin != nullptr) {
|
||||||
bankAccount->operator+(coin.get());
|
bankAccount->operator+(coin);
|
||||||
}
|
}
|
||||||
|
std::shared_ptr<Money> fee = bankAccount->operator-(10);
|
||||||
std::unique_ptr<Money> fee = bankAccount->operator-(10);
|
|
||||||
}
|
}
|
||||||
|
std::cout << "Test2" << std::endl;
|
||||||
std::cout << bankAccount.get() << std::endl;
|
std::cout << bankAccount.get() << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue