10_PITF: weiter nach Fehler gesucht
This commit is contained in:
parent
fc9933ec83
commit
3dd56c45a9
|
@ -1,6 +1,7 @@
|
|||
#include <utility>
|
||||
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
|
||||
#include "BankAccount.h"
|
||||
#include "CashFactory.h"
|
||||
|
@ -19,21 +20,30 @@ BankAccount::BankAccount(BankAccount &bankAccount) : name(bankAccount.name) {
|
|||
owner.swap(bankAccount.owner);
|
||||
}
|
||||
|
||||
std::unique_ptr<BankAccount> BankAccount::operator+(Money* money) {
|
||||
std::shared_ptr<Money> ptr = std::make_unique<Money>(Money(money->getValue() + this->money->getValue(), money->getCurrency()));
|
||||
std::shared_ptr<BankAccount> BankAccount::operator+(std::shared_ptr<Bill> money) {
|
||||
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;
|
||||
|
||||
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();
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
@ -47,4 +57,4 @@ std::shared_ptr<Money> BankAccount::getMoney() {
|
|||
|
||||
std::shared_ptr<Person> BankAccount::getOwner() {
|
||||
return owner;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
|
||||
#include "Money.h"
|
||||
#include "Person.h"
|
||||
#include "Coin.h"
|
||||
#include "Bill.h"
|
||||
|
||||
class BankAccount {
|
||||
private:
|
||||
|
@ -13,9 +15,10 @@ public:
|
|||
explicit BankAccount(std::shared_ptr<Person> const& owner, std::string const& name);
|
||||
explicit BankAccount(BankAccount *bankAccount);
|
||||
BankAccount(BankAccount& bankAccount);
|
||||
std::unique_ptr<BankAccount> operator+(Money* money);
|
||||
std::unique_ptr<Money> operator-(int value);
|
||||
bool operator<(std::unique_ptr<BankAccount> bankAccount);
|
||||
std::shared_ptr<BankAccount> operator+(std::shared_ptr<Coin> money);
|
||||
std::shared_ptr<BankAccount> operator+(std::shared_ptr<Bill> money);
|
||||
std::shared_ptr<Money> operator-(int value);
|
||||
bool operator<(std::shared_ptr<BankAccount> bankAccount);
|
||||
std::string getName();
|
||||
std::shared_ptr<Person> getOwner();
|
||||
std::shared_ptr<Money> getMoney();
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include "CashFactory.h"
|
||||
|
||||
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::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);
|
||||
if(iter == std::end(allowedBills)) {
|
||||
return nullptr;
|
||||
|
@ -17,15 +19,16 @@ std::unique_ptr<Bill> CashFactory::printBill(int value, Currency currency) {
|
|||
}
|
||||
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);
|
||||
if(iter == std::end(allowedCoins)) {
|
||||
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) {
|
||||
|
|
|
@ -12,8 +12,8 @@ class CashFactory {
|
|||
private:
|
||||
std::string randomString(size_t length);
|
||||
public:
|
||||
std::unique_ptr<Bill> printBill(int value, Currency currency);
|
||||
std::unique_ptr<Coin> printCoin(int value, Currency currency);
|
||||
std::shared_ptr<Bill> printBill(int value, Currency currency);
|
||||
std::shared_ptr<Coin> printCoin(int value, Currency currency);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -21,6 +21,6 @@ HeapObject::~HeapObject(){
|
|||
|
||||
bool HeapObject::assertionsHold(){
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -1,33 +1,37 @@
|
|||
#include <iostream>
|
||||
#include "Money.h"
|
||||
|
||||
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();
|
||||
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) {
|
||||
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)));
|
||||
}
|
||||
|
||||
std::unique_ptr<Money> Money::operator-(Money &a) {
|
||||
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) {
|
||||
const std::shared_ptr<Money> Money::operator++(int) {
|
||||
value++;
|
||||
return std::make_unique<Money>(*this);
|
||||
}
|
||||
|
||||
const std::unique_ptr<Money> Money::operator--(int) {
|
||||
const std::shared_ptr<Money> Money::operator--(int) {
|
||||
value--;
|
||||
return std::make_unique<Money>(*this);
|
||||
}
|
||||
|
|
|
@ -10,12 +10,13 @@ private:
|
|||
int value;
|
||||
Currency currency;
|
||||
public:
|
||||
explicit Money(int value = 0, Currency currency = CurrencyValue::USD);
|
||||
std::unique_ptr<Money> operator=(Money money);
|
||||
std::unique_ptr<Money> operator+(Money &a);
|
||||
std::unique_ptr<Money> operator-(Money &a);
|
||||
const std::unique_ptr<Money> operator++(int);
|
||||
const std::unique_ptr<Money> operator--(int);
|
||||
explicit Money(int value, Currency currency);
|
||||
explicit Money(Money* money);
|
||||
std::shared_ptr<Money> operator=(Money money);
|
||||
std::shared_ptr<Money> operator+(Money &a);
|
||||
std::shared_ptr<Money> operator-(Money &a);
|
||||
const std::shared_ptr<Money> operator++(int);
|
||||
const std::shared_ptr<Money> operator--(int);
|
||||
|
||||
virtual int getValue();
|
||||
Currency getCurrency();
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
class Person {
|
||||
private:
|
||||
std::string name;
|
||||
std::vector<std::unique_ptr<Money>> money;
|
||||
std::vector<std::shared_ptr<Money>> money;
|
||||
public:
|
||||
explicit Person(std::string const& name);
|
||||
explicit Person(Person* person);
|
||||
|
|
|
@ -24,17 +24,17 @@ std::ostream &operator<<(std::ostream &os, Currency currency) {
|
|||
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();
|
||||
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();
|
||||
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();
|
||||
return os;
|
||||
}
|
||||
|
@ -44,20 +44,22 @@ std::ostream &operator<<(std::ostream &os, std::shared_ptr<Person> const& person
|
|||
return os;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, std::unique_ptr<BankAccount> bankAccount) {
|
||||
os << bankAccount->getName() << " " << bankAccount->getOwner() << ": " << bankAccount.get()->getMoney();
|
||||
std::ostream &operator<<(std::ostream &os, BankAccount* bankAccount) {
|
||||
os << bankAccount->getName() << " " << ": " << bankAccount->getMoney();
|
||||
return os;
|
||||
}
|
||||
|
||||
std::set<std::unique_ptr<Bank>> banks;
|
||||
std::set<std::shared_ptr<Bank>> banks;
|
||||
std::set<std::shared_ptr<Person>> owners;
|
||||
CashFactory *cashFactory;
|
||||
|
||||
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::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"));
|
||||
std::shared_ptr<Bank> bank1 = std::make_shared<Bank>(new Bank("Bank 1"));
|
||||
std::shared_ptr<Bank> bank2 = std::make_shared<Bank>(new Bank("Bank 2"));
|
||||
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> marius = std::make_shared<Person>(new Person("Marius Müller"));
|
||||
|
@ -93,17 +95,18 @@ void simulate() {
|
|||
for(auto && bankAccount : bank->getAccounts()) {
|
||||
for (int i = 0; i < 10000; ++i) {
|
||||
int rnd = distribution(generator);
|
||||
std::unique_ptr<Bill> bill = cashFactory->printBill(rnd, CurrencyValue::USD);
|
||||
std::unique_ptr<Coin> coin = cashFactory->printCoin(rnd, CurrencyValue::USD);
|
||||
std::cout << rnd << std::endl;
|
||||
std::shared_ptr<Bill> bill = cashFactory->printBill(rnd, CurrencyValue::USD);
|
||||
std::shared_ptr<Coin> coin = cashFactory->printCoin(rnd, CurrencyValue::USD);
|
||||
if (bill != nullptr) {
|
||||
bankAccount->operator+(bill.get());
|
||||
bankAccount->operator+(bill);
|
||||
}
|
||||
if (coin != nullptr) {
|
||||
bankAccount->operator+(coin.get());
|
||||
bankAccount->operator+(coin);
|
||||
}
|
||||
|
||||
std::unique_ptr<Money> fee = bankAccount->operator-(10);
|
||||
std::shared_ptr<Money> fee = bankAccount->operator-(10);
|
||||
}
|
||||
std::cout << "Test2" << std::endl;
|
||||
std::cout << bankAccount.get() << std::endl;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue