10_PITF: fast fertig, kleiner Laufzeitfehler
Signed-off-by: Johannes Theiner <j.theiner@live.de>
This commit is contained in:
parent
cc16e9d094
commit
e81be0cf84
@ -3,19 +3,15 @@
|
||||
|
||||
#include "Bank.h"
|
||||
|
||||
Bank::Bank(std::string name) : name(std::move(name)) {
|
||||
Bank::Bank(std::string name) : name(std::move(name)) {}
|
||||
|
||||
}
|
||||
|
||||
Bank::Bank(Bank *bank) : name(bank->getName()) {
|
||||
|
||||
}
|
||||
Bank::Bank(Bank *bank) : name(std::move(bank->getName())) {}
|
||||
|
||||
void Bank::addAccount(BankAccount * account) {
|
||||
accounts.insert(account);
|
||||
accounts.insert(std::move(std::make_unique<BankAccount>(account)));
|
||||
}
|
||||
|
||||
std::set<BankAccount *> Bank::getAccounts() {
|
||||
std::set<std::shared_ptr<BankAccount>> Bank::getAccounts() {
|
||||
return accounts;
|
||||
}
|
||||
|
||||
@ -23,7 +19,3 @@ std::string Bank::getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
void Bank::addMoney(Money * money) {
|
||||
this->money.insert(money);
|
||||
}
|
||||
|
||||
|
@ -1,23 +1,20 @@
|
||||
#ifndef C_C_BANK_H
|
||||
#define C_C_BANK_H
|
||||
|
||||
|
||||
#include "BankAccount.h"
|
||||
#include "HeapObject.h"
|
||||
|
||||
class Bank : public HeapObject{
|
||||
private:
|
||||
std::string name;
|
||||
std::set<BankAccount *> accounts;
|
||||
std::set<Money *> money;
|
||||
std::set<std::shared_ptr<BankAccount>> accounts;
|
||||
int money;
|
||||
public:
|
||||
explicit Bank(std::string name);
|
||||
explicit Bank(Bank* bank);
|
||||
void addAccount(BankAccount * account);
|
||||
void addMoney(Money * money);
|
||||
std::set<BankAccount *> getAccounts();
|
||||
std::set<std::shared_ptr<BankAccount>> getAccounts();
|
||||
std::string getName();
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
@ -1,40 +1,54 @@
|
||||
#include <utility>
|
||||
|
||||
#include <utility>
|
||||
#include <random>
|
||||
|
||||
#include "BankAccount.h"
|
||||
#include "CashFactory.h"
|
||||
|
||||
BankAccount::BankAccount(std::shared_ptr<Person> owner, std::string name) : name(std::move(name)), owner(std::move(owner)) {
|
||||
|
||||
std::default_random_engine gen(static_cast<unsigned long>(time(nullptr)));
|
||||
std::uniform_int_distribution<int> dist(0, 9000);
|
||||
|
||||
BankAccount::BankAccount(std::shared_ptr<Person> owner, std::string name) : name(std::move(name)) {
|
||||
owner = std::move(owner);
|
||||
}
|
||||
|
||||
BankAccount::BankAccount(BankAccount *bankAccount) : owner(std::move(bankAccount->getOwner())), name(std::move(bankAccount->getName())) {
|
||||
|
||||
BankAccount::BankAccount(BankAccount *bankAccount) : name(std::move(bankAccount->getName())) {
|
||||
owner = std::move(bankAccount->getOwner());
|
||||
}
|
||||
|
||||
BankAccount *BankAccount::operator+(Money *money) {
|
||||
|
||||
BankAccount::money = new Money(money->getValue() + getMoney().getValue());
|
||||
//ab und zu Geld verlieren
|
||||
int rnd = dist(gen);
|
||||
if(rnd == 42) {
|
||||
return this;
|
||||
}
|
||||
BankAccount::money = std::make_shared<Money>(new Money(money->getValue() + getMoney()->getValue()));
|
||||
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
Money *BankAccount::operator-(int value) {
|
||||
auto *cashFactory = new CashFactory();
|
||||
|
||||
return cashFactory->printCoin(value, CurrencyValue::USD);
|
||||
return cashFactory->printCoin(value, CurrencyValue::USD).get();
|
||||
}
|
||||
|
||||
bool BankAccount::operator<(BankAccount *bankAccount) {
|
||||
return money.getValue() < bankAccount->money.getValue();
|
||||
return money->getValue() < bankAccount->money->getValue();
|
||||
}
|
||||
|
||||
std::string BankAccount::getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
Money BankAccount::getMoney() {
|
||||
std::shared_ptr<Money> BankAccount::getMoney() {
|
||||
return money;
|
||||
}
|
||||
|
||||
std::shared_ptr<Person> BankAccount::getOwner() {
|
||||
return owner;
|
||||
}
|
||||
}
|
@ -5,14 +5,13 @@
|
||||
#include <memory>
|
||||
|
||||
#include "Money.h"
|
||||
#include "Cash.h"
|
||||
#include "Person.h"
|
||||
|
||||
class BankAccount {
|
||||
private:
|
||||
std::string name;
|
||||
std::shared_ptr<Person> owner;
|
||||
Money money;
|
||||
std::shared_ptr<Money> money;
|
||||
public:
|
||||
explicit BankAccount(std::shared_ptr<Person> owner, std::string name);
|
||||
explicit BankAccount(BankAccount* bankAccount);
|
||||
@ -21,8 +20,7 @@ public:
|
||||
bool operator<(BankAccount * bankAccount);
|
||||
std::string getName();
|
||||
std::shared_ptr<Person> getOwner();
|
||||
Money getMoney();
|
||||
std::shared_ptr<Money> getMoney();
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
#endif
|
@ -6,6 +6,10 @@ Bill::Bill(int value, Currency currency, std::string serial) : Cash(value, curre
|
||||
|
||||
}
|
||||
|
||||
Bill::Bill(Bill *bill) : Cash(bill->getValue(), bill->getCurrency()), serial(std::move(bill->getSerial())) {
|
||||
|
||||
}
|
||||
|
||||
int Bill::getValue() {
|
||||
return Money::getValue() * 10;
|
||||
}
|
||||
|
@ -8,7 +8,8 @@ class Bill : public Cash{
|
||||
private:
|
||||
std::string serial;
|
||||
public:
|
||||
Bill(int value, Currency currency, std::string serial);
|
||||
explicit Bill(int value, Currency currency, std::string serial);
|
||||
explicit Bill(Bill *bill);
|
||||
std::string getSerial();
|
||||
int getValue();
|
||||
};
|
||||
|
@ -6,7 +6,7 @@ int allowedCoins[8] = {1, 2, 5, 10, 20, 50, 100, 200};
|
||||
|
||||
std::vector<std::string> usedSerials;
|
||||
|
||||
Bill * CashFactory::printBill(int value, Currency currency) {
|
||||
std::shared_ptr<Bill> CashFactory::printBill(int value, Currency currency) {
|
||||
auto * iter = std::find(std::begin(allowedBills), std::end(allowedBills), value);
|
||||
if(iter == std::end(allowedBills)) {
|
||||
return nullptr;
|
||||
@ -16,15 +16,15 @@ Bill * CashFactory::printBill(int value, Currency currency) {
|
||||
serial = randomString(15);
|
||||
}
|
||||
usedSerials.push_back(serial);
|
||||
return new Bill(value, currency, serial);
|
||||
return std::make_shared<Bill>(new Bill(value, currency, serial));
|
||||
}
|
||||
}
|
||||
|
||||
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 new Coin(value, currency);
|
||||
}else return std::make_shared<Coin>(new Coin(value, currency));
|
||||
}
|
||||
|
||||
std::string CashFactory::randomString(size_t length) {
|
||||
|
@ -2,6 +2,7 @@
|
||||
#define C_C_CASHFACTORY_H
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "Bill.h"
|
||||
#include "Coin.h"
|
||||
@ -10,8 +11,8 @@ class CashFactory {
|
||||
private:
|
||||
std::string randomString(size_t length);
|
||||
public:
|
||||
Bill * printBill(int value, Currency currency);
|
||||
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
|
@ -4,6 +4,10 @@ Coin::Coin(int value, Currency currency) : Cash(value, currency) {
|
||||
|
||||
}
|
||||
|
||||
Coin::Coin(Coin *coin) : Cash(coin->getValue(), coin->getCurrency()) {
|
||||
|
||||
}
|
||||
|
||||
int Coin::getValue() {
|
||||
return Money::getValue();
|
||||
}
|
@ -7,7 +7,8 @@
|
||||
class Coin : public Cash{
|
||||
private:
|
||||
public:
|
||||
Coin(int value, Currency currency);
|
||||
explicit Coin(int value, Currency currency);
|
||||
explicit Coin(Coin *coin);
|
||||
int getValue() override;
|
||||
};
|
||||
|
||||
|
@ -21,7 +21,7 @@ HeapObject::~HeapObject(){
|
||||
}
|
||||
|
||||
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
|
||||
return true;
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
#include "Money.h"
|
||||
|
||||
Money::Money(int value, Currency currency) : value(value), currency(currency) {
|
||||
Money::Money(int value, Currency currency) : value(value), currency(currency) {}
|
||||
|
||||
}
|
||||
Money::Money(Money *money) : value(money->getValue()), currency(money->getCurrency()) {}
|
||||
|
||||
Money& Money::operator=(Money money) {
|
||||
value = money.getValue();
|
||||
@ -28,6 +28,10 @@ Money Money::operator-(Money &a) {
|
||||
}else return Money(-1, Currency(CurrencyValue::USD));
|
||||
}
|
||||
|
||||
bool Money::operator<(Money &a) {
|
||||
return getValue() < a.getValue();
|
||||
}
|
||||
|
||||
const Money Money::operator++(int) {
|
||||
return Money(value++);
|
||||
}
|
||||
|
@ -9,12 +9,14 @@ private:
|
||||
Currency currency;
|
||||
public:
|
||||
explicit Money(int value = 0, Currency currency = CurrencyValue::USD);
|
||||
explicit Money(Money *money);
|
||||
Money& operator=(Money money);
|
||||
Money& operator=(Money * money);
|
||||
Money operator+(Money &a);
|
||||
Money operator-(Money &a);
|
||||
const Money operator++(int);
|
||||
const Money operator--(int);
|
||||
bool operator<(Money &a);
|
||||
|
||||
virtual int getValue();
|
||||
Currency getCurrency();
|
||||
|
@ -3,12 +3,13 @@
|
||||
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include "Money.h"
|
||||
|
||||
class Person {
|
||||
private:
|
||||
std::string name;
|
||||
std::vector<Money *> money;
|
||||
std::vector<std::shared_ptr<Money>> money;
|
||||
public:
|
||||
explicit Person(std::string name);
|
||||
explicit Person(Person* person);
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <set>
|
||||
#include <random>
|
||||
#include <ctime>
|
||||
#include <algorithm>
|
||||
#include "HeapObject.h"
|
||||
#include "CashFactory.h"
|
||||
#include "Bank.h"
|
||||
@ -42,18 +43,18 @@ std::ostream &operator<<(std::ostream &os, Person *person) {
|
||||
return os;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, BankAccount bankAccount) {
|
||||
os << bankAccount.getOwner() << " " << bankAccount.getName() << ": " << bankAccount.getMoney();
|
||||
std::ostream &operator<<(std::ostream &os, BankAccount* bankAccount) {
|
||||
os << bankAccount->getOwner() << " " << bankAccount->getName() << ": " << bankAccount->getMoney();
|
||||
return os;
|
||||
}
|
||||
|
||||
std::set<std::unique_ptr<Bank>> banks;
|
||||
|
||||
std::set<std::shared_ptr<Bank>> banks;
|
||||
std::vector<std::shared_ptr<Money>> moneyVector;
|
||||
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"));
|
||||
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"));
|
||||
@ -82,31 +83,40 @@ void simulate() {
|
||||
std::cout << std::endl;
|
||||
std::cout << "======================== " << bank->getName() << " ========================" << std::endl;
|
||||
std::cout << std::endl;
|
||||
for (BankAccount *bankAccount : bank->getAccounts()) {
|
||||
for (auto && bankAccount : bank->getAccounts()) {
|
||||
for (int i = 0; i < 10000; ++i) {
|
||||
int rnd = distribution(generator);
|
||||
Bill *bill = cashFactory->printBill(rnd, CurrencyValue::USD);
|
||||
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) {
|
||||
*bankAccount + bill;
|
||||
*bankAccount + bill.get();
|
||||
moneyVector.push_back(std::static_pointer_cast<Money, Bill>(bill));
|
||||
}
|
||||
if (coin != nullptr) {
|
||||
*bankAccount + coin;
|
||||
*bankAccount + coin.get();
|
||||
moneyVector.push_back(std::static_pointer_cast<Money, Coin>(coin));
|
||||
}
|
||||
|
||||
Money *fee = *bankAccount - 10;
|
||||
}
|
||||
std::cout << *bankAccount << std::endl;
|
||||
//FIXME: Das hier mag er nicht
|
||||
// std::cout << bankAccount->getOwner()->getName();
|
||||
std::cout << bankAccount->getName() << ": ";
|
||||
std::cout << bankAccount->getMoney()->getValue() << " ";
|
||||
std::cout << bankAccount->getMoney()->getCurrency().getValue() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
std::sort(moneyVector.begin(), moneyVector.end());
|
||||
|
||||
for(auto && money : moneyVector) {
|
||||
std::cout << money->getValue() << ", ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
void tearDown() {
|
||||
for (auto && bank : banks) {
|
||||
for (BankAccount *bankAccount : bank->getAccounts()) {
|
||||
delete(bankAccount);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user