10_PITF: Wieder auf Anfang

This commit is contained in:
Johannes Theiner 2019-01-02 15:29:11 +01:00
parent dd71612348
commit 5c46cc3d66
21 changed files with 141 additions and 251 deletions

View File

@ -100,27 +100,11 @@ int main(int argc, char **argv) {
}
}
/*
* Schmeißt nen SIGSERV/SEGFAULT
* std::for_each(banks.begin(), banks.end(), [cashFactory] (Bank * bank) {
std::for_each(bank->getAccounts().begin(), bank->getAccounts().end(), [cashFactory] (BankAccount * bankAccount) {
std::cout << *bankAccount << std::endl;
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);
if(bill != nullptr) {
*bankAccount + bill;
}
if(coin != nullptr) {
*bankAccount + coin;
}
delete bill;
delete coin;
}
std::cout << *bankAccount << std::endl;
});
});
*/
for (Bank *bank : banks) {
for (BankAccount *bankAccount : bank->getAccounts()) {
delete(bankAccount);
}
delete(bank);
}
}

View File

@ -1 +1 @@
add_executable(10_PITF_Testat Testat/main.cpp Testat/Money.cpp Testat/Money.h Testat/Currency.h Testat/BankAccount.cpp Testat/BankAccount.h Testat/Cash.cpp Testat/Cash.h Testat/Currency.cpp Testat/Coin.cpp Testat/Coin.h Testat/Bill.cpp Testat/Bill.h Testat/CashFactory.h Testat/Person.cpp Testat/Person.h Testat/CashFactory.cpp Testat/Bank.cpp Testat/Bank.h Testat/HeapObject.h Testat/HeapObject.cpp)
add_executable(10_PITF_Testat Testat/main.cpp Testat/HeapObject.h Testat/HeapObject.cpp Testat/Bank.cpp Testat/BankAccount.cpp Testat/Money.cpp Testat/Coin.cpp Testat/Cash.cpp Testat/Currency.cpp Testat/CashFactory.cpp Testat/Person.cpp Testat/Bill.cpp)

View File

@ -3,24 +3,15 @@
#include "Bank.h"
Bank::Bank(std::string const& name) : name(name) {
Bank::Bank(std::string name) : name(std::move(name)) {
}
Bank::Bank(Bank* bank) : name(bank->name) {
money.swap(bank->money);
accounts.swap(bank->accounts);
void Bank::addAccount(BankAccount * account) {
accounts.insert(account);
}
void Bank::addAccount(std::shared_ptr<BankAccount> account) {
this->accounts.insert(std::move(account));
}
void Bank::addMoney(std::shared_ptr<Money> money) {
this->money.insert(std::move(money));
}
std::set<std::shared_ptr<BankAccount>> Bank::getAccounts() {
std::set<BankAccount *> Bank::getAccounts() {
return accounts;
}
@ -28,4 +19,7 @@ std::string Bank::getName() {
return name;
}
void Bank::addMoney(Money * money) {
this->money.insert(money);
}

View File

@ -3,18 +3,18 @@
#include "BankAccount.h"
#include "HeapObject.h"
class Bank {
class Bank : public HeapObject{
private:
std::string name;
std::set<std::shared_ptr<BankAccount>> accounts;
std::set<std::shared_ptr<Money>> money;
std::set<BankAccount *> accounts;
std::set<Money *> money;
public:
explicit Bank(std::string const& name);
explicit Bank(Bank* bank);
void addAccount(std::shared_ptr<BankAccount> account);
void addMoney(std::shared_ptr<Money> money);
std::set<std::shared_ptr<BankAccount>> getAccounts();
explicit Bank(std::string name);
void addAccount(BankAccount * account);
void addMoney(Money * money);
std::set<BankAccount *> getAccounts();
std::string getName();
};

View File

@ -1,59 +1,34 @@
#include <utility>
#include <memory>
#include <iostream>
#include "BankAccount.h"
#include "CashFactory.h"
BankAccount::BankAccount(std::shared_ptr<Person> const& owner, std::string const& name) : name(name), owner(std::move(owner)) {
BankAccount::BankAccount(Person * owner, std::string name) : name(std::move(name)) {
this->owner = owner;
}
BankAccount::BankAccount(BankAccount *bankAccount) : name(bankAccount->name) {
money.swap(bankAccount->money);
owner.swap(bankAccount->owner);
BankAccount * BankAccount::operator+(Money * money) {
BankAccount::money = new Money(money->getValue() + getMoney().getValue());
return this;
}
BankAccount::BankAccount(BankAccount &bankAccount) : name(bankAccount.name) {
money.swap(bankAccount.money);
owner.swap(bankAccount.owner);
}
std::shared_ptr<BankAccount> BankAccount::operator+(std::shared_ptr<Bill> money) {
this->money->setValue(this->money->getValue() + money->getValue());
return std::make_shared<BankAccount>(*this);
}
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<Coin> BankAccount::operator-(int value) {
Money * BankAccount::operator-(int value) {
auto * cashFactory = new CashFactory();
return cashFactory->printCoin(value, CurrencyValue::USD);
}
bool BankAccount::operator<(std::shared_ptr<BankAccount> bankAccount) {
return this->money->getValue() < bankAccount->money->getValue();
bool BankAccount::operator<(BankAccount * bankAccount) {
return money.getValue() < bankAccount->money.getValue();
}
std::string BankAccount::getName() {
return name;
}
std::shared_ptr<Money> BankAccount::getMoney() {
Money BankAccount::getMoney() {
return money;
}
std::shared_ptr<Person> BankAccount::getOwner() {
Person *BankAccount::getOwner() {
return owner;
}

View File

@ -1,27 +1,25 @@
#ifndef C_C_BANKACCOUNTS_H
#define C_C_BANKACCOUNTS_H
#include <unordered_map>
#include "Money.h"
#include "Cash.h"
#include "Person.h"
#include "Coin.h"
#include "Bill.h"
class BankAccount {
private:
std::string name;
std::shared_ptr<Person> owner;
std::shared_ptr<Money> money;
Person * owner;
Money money;
public:
explicit BankAccount(std::shared_ptr<Person> const& owner, std::string const& name);
explicit BankAccount(BankAccount *bankAccount);
BankAccount(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<Coin> operator-(int value);
bool operator<(std::shared_ptr<BankAccount> bankAccount);
explicit BankAccount(Person * owner, std::string name);
BankAccount* operator+(Money * money);
Money* operator-(int value);
bool operator<(BankAccount * bankAccount);
std::string getName();
std::shared_ptr<Person> getOwner();
std::shared_ptr<Money> getMoney();
Person * getOwner();
Money getMoney();
};

View File

@ -6,14 +6,6 @@ Bill::Bill(int value, Currency currency, std::string serial) : Cash(value, curre
}
Bill::Bill(Bill *bill) : Cash(bill->getValue(), bill->getCurrency()), serial(bill->getSerial()){
}
Currency Bill::getCurrency() {
return Money::getCurrency();
}
int Bill::getValue() {
return Money::getValue() * 10;
}

View File

@ -9,9 +9,7 @@ private:
std::string serial;
public:
Bill(int value, Currency currency, std::string serial);
explicit Bill(Bill *bill);
std::string getSerial();
Currency getCurrency();
int getValue();
};

View File

@ -1,10 +1,11 @@
#ifndef C_C_CASH_H
#define C_C_CASH_H
#include <string>
#include "Money.h"
#include "HeapObject.h"
class Cash : public HeapObject, public Money{
class Cash : public Money, public HeapObject{
private:
public:
Cash(int value, Currency currency);

View File

@ -1,5 +1,4 @@
#include <algorithm>
#include <iostream>
#include "CashFactory.h"
int allowedBills[5] = {100, 50, 20, 10, 5};
@ -7,8 +6,7 @@ int allowedCoins[8] = {1, 2, 5, 10, 20, 50, 100, 200};
std::vector<std::string> usedSerials;
std::shared_ptr<Bill> CashFactory::printBill(int value, Currency currency) {
std::cout << "Test" << std::endl;
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;
@ -18,17 +16,15 @@ std::shared_ptr<Bill> CashFactory::printBill(int value, Currency currency) {
serial = randomString(15);
}
usedSerials.push_back(serial);
std::cout << "Test2" << std::endl;
return std::make_shared<Bill>(new Bill(value, currency, serial));
return new Bill(value, currency, serial);
}
}
std::shared_ptr<Coin> CashFactory::printCoin(int value, Currency currency) {
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_shared<Coin>(new Coin(value, currency));
}else return new Coin(value, currency);
}
std::string CashFactory::randomString(size_t length) {

View File

@ -3,8 +3,6 @@
#include <string>
#include <vector>
#include <memory>
#include "Bill.h"
#include "Coin.h"
@ -12,8 +10,8 @@ class CashFactory {
private:
std::string randomString(size_t length);
public:
std::shared_ptr<Bill> printBill(int value, Currency currency);
std::shared_ptr<Coin> printCoin(int value, Currency currency);
Bill * printBill(int value, Currency currency);
Coin * printCoin(int value, Currency currency);
};
#endif

View File

@ -4,14 +4,6 @@ Coin::Coin(int value, Currency currency) : Cash(value, currency) {
}
Coin::Coin(Coin *coin) : Cash(coin->getValue(), coin->getCurrency()) {
}
Currency Coin::getCurrency() {
return Money::getCurrency();
}
int Coin::getValue() {
return Money::getValue();
}
}

View File

@ -8,9 +8,7 @@ class Coin : public Cash{
private:
public:
Coin(int value, Currency currency);
explicit Coin(Coin *coin);
Currency getCurrency();
int getValue();
int getValue() override;
};
#endif

View File

@ -4,7 +4,7 @@
Currency::Currency(CurrencyValue value) : value(value) {
}
bool Currency::operator==(Currency currency) {
bool Currency::operator==(Currency &currency) {
return value == currency.value;
}

View File

@ -14,7 +14,7 @@ private:
CurrencyValue value;
public:
Currency(CurrencyValue value);
bool operator==(Currency currency);
bool operator==(Currency &currency);
Currency& operator=(Currency currency);
CurrencyValue getValue();

View File

@ -1,5 +1,6 @@
#include <cstdlib>
#include <cassert>
#include <iostream>
#include "HeapObject.h"
int HeapObject::ctorCount = 0;
@ -21,6 +22,7 @@ HeapObject::~HeapObject(){
bool HeapObject::assertionsHold(){
assert(ctorCount == newCount); // all objects have been allocated on heap
//assert(ctorCount == dtorCount); // all objects have been deleted
std::cout << ctorCount << " == " << dtorCount << std::endl;
assert(ctorCount == dtorCount); // all objects have been deleted
return true;
}

View File

@ -1,61 +1,45 @@
#include <iostream>
#include "Money.h"
Money::Money(int value, Currency currency) : value(value), currency(currency) {
}
Money::Money(Money *money) : value(money->value), currency(money->currency) {
std::cout << "Test2" << std::endl;
}
std::shared_ptr<Money> Money::operator=(Money money) {
Money& Money::operator=(Money money) {
value = money.getValue();
currency = money.getCurrency();
return std::make_shared<Money>(*this);
return *this;
}
std::shared_ptr<Money> Money::operator+(Money &a) {
Money &Money::operator=(Money *money) {
value = money->getValue();
currency = money->getCurrency();
return *this;
}
Money Money::operator+(Money &a) {
if(currency == a.currency) {
return std::make_shared<Money>(Money(value + a.value, CurrencyValue::USD));
}else return std::make_shared<Money>(Money(-1, Currency(CurrencyValue::USD)));
return Money(value + a.value);
}else return Money(-1, Currency(CurrencyValue::USD));
}
std::shared_ptr<Money> Money::operator+(Bill &a) {
if(currency == a.getCurrency()) {
return std::make_shared<Money>(Money(value + a.getValue(), CurrencyValue::USD));
}else return std::make_shared<Money>(Money(-1, Currency(CurrencyValue::USD)));
}
std::shared_ptr<Money> Money::operator+(Coin &a) {
if(currency == a.getCurrency()) {
return std::make_shared<Money>(Money(value + a.getValue(), CurrencyValue::USD));
}else return std::make_shared<Money>(Money(-1, Currency(CurrencyValue::USD)));
}
std::shared_ptr<Money> Money::operator-(Money &a) {
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)));
return Money(value - a.value);
}else return Money(-1, Currency(CurrencyValue::USD));
}
const std::shared_ptr<Money> Money::operator++(int) {
value++;
return std::make_unique<Money>(*this);
const Money Money::operator++(int) {
return Money(value++);
}
const std::shared_ptr<Money> Money::operator--(int) {
value--;
return std::make_unique<Money>(*this);
const Money Money::operator--(int) {
return Money(value--);
}
int Money::getValue() {
return value;
}
void Money::setValue(int value) {
this->value = value;
}
Currency Money::getCurrency() {
return currency;
}
}

View File

@ -1,30 +1,23 @@
#ifndef C_C_MONEY_H
#define C_C_MONEY_H
#include <memory>
#include "Currency.h"
#include "Bill.h"
#include "Coin.h"
class Money {
private:
int value;
Currency currency;
public:
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+(Bill &a);
std::shared_ptr<Money> operator+(Coin &a);
std::shared_ptr<Money> operator-(Money &a);
const std::shared_ptr<Money> operator++(int);
const std::shared_ptr<Money> operator--(int);
explicit Money(int value = 0, Currency currency = CurrencyValue::USD);
Money& operator=(Money money);
Money& operator=(Money * money);
Money operator+(Money &a);
Money operator-(Money &a);
const Money operator++(int);
const Money operator--(int);
virtual int getValue();
virtual void setValue(int value);
virtual Currency getCurrency();
Currency getCurrency();
};
#endif

View File

@ -2,14 +2,10 @@
#include "Person.h"
Person::Person(std::string const& name) : name(name) {
Person::Person(std::string name) : name(std::move(name)){
}
Person::Person(Person *person) : name(person->name) {
money.swap(person->money);
}
std::string Person::getName() {
return name;
}

View File

@ -3,17 +3,14 @@
#include <string>
#include <memory>
#include "Money.h"
class Person {
private:
std::string name;
std::vector<std::shared_ptr<Money>> money;
std::vector<Money *> money;
public:
explicit Person(std::string const& name);
explicit Person(Person* person);
explicit Person(std::string name);
std::string getName();
};

View File

@ -1,11 +1,9 @@
#include <set>
#include <algorithm>
#include <iostream>
#include <ctime>
#include <set>
#include <random>
#include "Currency.h"
#include "BankAccount.h"
#include "Bill.h"
#include <ctime>
#include "HeapObject.h"
#include "CashFactory.h"
#include "Bank.h"
@ -24,99 +22,93 @@ std::ostream &operator<<(std::ostream &os, Currency currency) {
return os;
}
std::ostream &operator<<(std::ostream &os, std::shared_ptr<Bill> const& bill) {
os << bill->getValue() << " " << bill->getCurrency() << " " << bill->getSerial();
std::ostream &operator<<(std::ostream &os, Bill bill) {
os << bill.getValue() << " " << bill.getCurrency() << " " << bill.getSerial();
return os;
}
std::ostream &operator<<(std::ostream &os, std::shared_ptr<Coin> const& cash) {
os << cash->getValue() << " " << cash->getCurrency();
std::ostream &operator<<(std::ostream &os, Cash cash) {
os << cash.getValue() << " " << cash.getCurrency();
return os;
}
std::ostream &operator<<(std::ostream &os, std::shared_ptr<Money> const& money) {
os << money->getValue() << " " << money->getCurrency();
std::ostream &operator<<(std::ostream &os, Money money) {
os << money.getValue() << " " << money.getCurrency();
return os;
}
std::ostream &operator<<(std::ostream &os, std::shared_ptr<Person> const& person) {
std::ostream &operator<<(std::ostream &os, Person *person) {
os << person->getName();
return os;
}
std::ostream &operator<<(std::ostream &os, BankAccount* bankAccount) {
os << bankAccount->getName() << " " << ": " << bankAccount->getMoney();
std::ostream &operator<<(std::ostream &os, BankAccount bankAccount) {
os << bankAccount.getOwner() << " " << bankAccount.getName() << ": " << bankAccount.getMoney();
return os;
}
std::set<std::shared_ptr<Bank>> banks;
std::set<std::shared_ptr<Person>> owners;
CashFactory *cashFactory;
std::set<Bank *> banks;
void setup() {
//FIXME: das hier könnte das Problem sein
// http://www.cplusplus.com/forum/beginner/74320/
// Fehler könnte so gefunden werden:
// https://stackoverflow.com/a/6685693
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"));
std::shared_ptr<Person> alex = std::make_shared<Person>(new Person("Alex Maier"));
owners.insert(max);
owners.insert(marius);
owners.insert(alex);
bank1->addAccount(std::make_shared<BankAccount>(new BankAccount(max, "Konto 1")));
bank2->addAccount(std::make_shared<BankAccount>(new BankAccount(max, "Konto 2")));
bank3->addAccount(std::make_shared<BankAccount>(new BankAccount(alex, "privat")));
bank1->addAccount(std::make_shared<BankAccount>(new BankAccount(alex, "geschäftlich 1")));
bank2->addAccount(std::make_shared<BankAccount>(new BankAccount(alex, "geschäftlich 2")));
bank3->addAccount(std::make_shared<BankAccount>(new BankAccount(alex, "geschäftlich 3")));
bank1->addAccount(std::make_shared<BankAccount>(new BankAccount(marius, "Konto 1")));
bank2->addAccount(std::make_shared<BankAccount>(new BankAccount(marius, "Konto 2")));
bank3->addAccount(std::make_shared<BankAccount>(new BankAccount(marius, "Konto 3")));
Bank *bank1 = new Bank("Bank 1");
Bank *bank2 = new Bank("Bank 2");
Bank *bank3 = new Bank("Bank 3");
banks.insert(std::move(bank1));
banks.insert(std::move(bank2));
banks.insert(std::move(bank3));
Person *max = new Person("Max Müller");
Person *marius = new Person("Marius Müller");
Person *alex = new Person("Alex Maier");
cashFactory = new CashFactory();
bank1->addAccount(new BankAccount(max, "Konto 1"));
bank2->addAccount(new BankAccount(max, "Konto 2"));
bank1->addAccount(new BankAccount(alex, "privat"));
bank3->addAccount(new BankAccount(alex, "geschäftlich 1"));
bank2->addAccount(new BankAccount(alex, "geschäftlich 2"));
bank1->addAccount(new BankAccount(alex, "geschäftlich 3"));
bank2->addAccount(new BankAccount(marius, "Konto 1"));
bank1->addAccount(new BankAccount(marius, "Konto 2"));
bank3->addAccount(new BankAccount(marius, "Konto 3"));
banks.insert(bank1);
banks.insert(bank2);
banks.insert(bank3);
}
void simulate() {
for(auto && bank : banks) {
auto *cashFactory = new CashFactory();
for (Bank *bank : banks) {
std::cout << std::endl;
std::cout << "======================== " << bank->getName() << " ========================" << std::endl;
std::cout << std::endl;
for(auto && bankAccount : bank->getAccounts()) {
for (BankAccount *bankAccount : bank->getAccounts()) {
for (int i = 0; i < 10000; ++i) {
int rnd = distribution(generator);
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);
Bill *bill = cashFactory->printBill(rnd, CurrencyValue::USD);
Coin *coin = cashFactory->printCoin(rnd, CurrencyValue::USD);
if (bill != nullptr) {
bankAccount->operator+(bill);
*bankAccount + bill;
}
if (coin != nullptr) {
//bankAccount->operator+(coin);
*bankAccount + coin;
}
//std::shared_ptr<Money> fee = bankAccount->operator-(10);
Money *fee = *bankAccount - 10;
}
std::cout << "Test2" << std::endl;
std::cout << bankAccount.get() << std::endl;
std::cout << *bankAccount << std::endl;
}
}
}
void tearDown() {
for (Bank *bank : banks) {
for (BankAccount *bankAccount : bank->getAccounts()) {
delete(bankAccount);
}
delete(bank);
}
}