From be744eb9e417cee1429b40df80be5700af6e3367 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Tue, 18 Dec 2018 11:36:22 +0100 Subject: [PATCH] 08_PTRN: Banking erweitert --- src/08_PTRN/CMakeLists.txt | 2 +- src/08_PTRN/Testat/Bank.cpp | 25 +++++++++ src/08_PTRN/Testat/Bank.h | 21 ++++++++ src/08_PTRN/Testat/BankAccount.cpp | 12 ++++- src/08_PTRN/Testat/BankAccount.h | 1 + src/08_PTRN/Testat/Bill.cpp | 4 ++ src/08_PTRN/Testat/Bill.h | 1 + src/08_PTRN/Testat/CashFactory.cpp | 8 ++- src/08_PTRN/Testat/Coin.cpp | 4 ++ src/08_PTRN/Testat/Coin.h | 1 + src/08_PTRN/Testat/Money.cpp | 8 +-- src/08_PTRN/Testat/Money.h | 2 +- src/08_PTRN/Testat/Person.cpp | 2 +- src/08_PTRN/Testat/Person.h | 4 +- src/08_PTRN/Testat/main.cpp | 83 +++++++++++++++++++++++------- 15 files changed, 148 insertions(+), 30 deletions(-) create mode 100644 src/08_PTRN/Testat/Bank.cpp create mode 100644 src/08_PTRN/Testat/Bank.h diff --git a/src/08_PTRN/CMakeLists.txt b/src/08_PTRN/CMakeLists.txt index b40f610..8ef0526 100644 --- a/src/08_PTRN/CMakeLists.txt +++ b/src/08_PTRN/CMakeLists.txt @@ -1,2 +1,2 @@ add_executable(08_PTRN_MP MP/VehicleFactory.cpp MP/VehicleFactory.hpp MP/Vehicle.cpp MP/Vehicle.hpp MP/Car.cpp MP/Car.hpp MP/Truck.cpp MP/Truck.hpp MP/Logger.cpp MP/Logger.hpp MP/main.cpp) -add_executable(08_PTRN_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) \ No newline at end of file +add_executable(08_PTRN_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) \ No newline at end of file diff --git a/src/08_PTRN/Testat/Bank.cpp b/src/08_PTRN/Testat/Bank.cpp new file mode 100644 index 0000000..bd2002e --- /dev/null +++ b/src/08_PTRN/Testat/Bank.cpp @@ -0,0 +1,25 @@ +#include +#include + +#include "Bank.h" + +Bank::Bank(std::string name) : name(std::move(name)) { + +} + +void Bank::addAccount(BankAccount * account) { + accounts.insert(account); +} + +std::set Bank::getAccounts() { + return accounts; +} + +std::string Bank::getName() { + return name; +} + +void Bank::addMoney(Money * money) { + this->money.insert(money); +} + diff --git a/src/08_PTRN/Testat/Bank.h b/src/08_PTRN/Testat/Bank.h new file mode 100644 index 0000000..4d5fb88 --- /dev/null +++ b/src/08_PTRN/Testat/Bank.h @@ -0,0 +1,21 @@ +#ifndef C_C_BANK_H +#define C_C_BANK_H + + +#include "BankAccount.h" + +class Bank { +private: + std::string name; + std::set accounts; + std::set money; +public: + explicit Bank(std::string name); + void addAccount(BankAccount * account); + void addMoney(Money * money); + std::set getAccounts(); + std::string getName(); +}; + + +#endif diff --git a/src/08_PTRN/Testat/BankAccount.cpp b/src/08_PTRN/Testat/BankAccount.cpp index c6dac8f..333c1c8 100644 --- a/src/08_PTRN/Testat/BankAccount.cpp +++ b/src/08_PTRN/Testat/BankAccount.cpp @@ -1,14 +1,22 @@ #include "BankAccount.h" +#include "CashFactory.h" -BankAccount::BankAccount(Person *owner, std::string name) : name(std::move(name)) { +BankAccount::BankAccount(Person * owner, std::string name) : name(std::move(name)) { this->owner = owner; } -BankAccount * BankAccount::operator+(Money* money) { +BankAccount * BankAccount::operator+(Money * money) { + BankAccount::money = new Money(money->getValue() + getMoney().getValue()); return this; } +Money * BankAccount::operator-(int value) { + auto * cashFactory = new CashFactory(); + + return cashFactory->printCoin(value, CurrencyValue::USD); +} + bool BankAccount::operator<(BankAccount * bankAccount) { return money.getValue() < bankAccount->money.getValue(); } diff --git a/src/08_PTRN/Testat/BankAccount.h b/src/08_PTRN/Testat/BankAccount.h index 41280ee..c0432ad 100644 --- a/src/08_PTRN/Testat/BankAccount.h +++ b/src/08_PTRN/Testat/BankAccount.h @@ -15,6 +15,7 @@ private: public: explicit BankAccount(Person * owner, std::string name); BankAccount* operator+(Money * money); + Money* operator-(int value); bool operator<(BankAccount * bankAccount); std::string getName(); Person * getOwner(); diff --git a/src/08_PTRN/Testat/Bill.cpp b/src/08_PTRN/Testat/Bill.cpp index 2cf6295..de9d70b 100644 --- a/src/08_PTRN/Testat/Bill.cpp +++ b/src/08_PTRN/Testat/Bill.cpp @@ -6,6 +6,10 @@ Bill::Bill(int value, Currency currency, std::string serial) : Cash(value, curre } +int Bill::getValue() { + return Money::getValue() * 10; +} + std::string Bill::getSerial() { return serial; } diff --git a/src/08_PTRN/Testat/Bill.h b/src/08_PTRN/Testat/Bill.h index 56a1ee4..bafd05e 100644 --- a/src/08_PTRN/Testat/Bill.h +++ b/src/08_PTRN/Testat/Bill.h @@ -10,6 +10,7 @@ private: public: Bill(int value, Currency currency, std::string serial); std::string getSerial(); + int getValue(); }; diff --git a/src/08_PTRN/Testat/CashFactory.cpp b/src/08_PTRN/Testat/CashFactory.cpp index 9550403..0eff08f 100644 --- a/src/08_PTRN/Testat/CashFactory.cpp +++ b/src/08_PTRN/Testat/CashFactory.cpp @@ -2,7 +2,9 @@ #include "CashFactory.h" int allowedBills[5] = {100, 50, 20, 10, 5}; -int allowedCoins[9] = {1, 2, 5, 10, 20, 50, 100, 200, 300}; +int allowedCoins[8] = {1, 2, 5, 10, 20, 50, 100, 200}; + +std::vector usedSerials; Bill * CashFactory::printBill(int value, Currency currency) { auto * iter = std::find(std::begin(allowedBills), std::end(allowedBills), value); @@ -10,6 +12,10 @@ Bill * CashFactory::printBill(int value, Currency currency) { return nullptr; }else { std::string serial = randomString(15); + while(std::find(usedSerials.begin(), usedSerials.end(), serial) != usedSerials.end()) { + serial = randomString(15); + } + usedSerials.push_back(serial); return new Bill(value, currency, serial); } } diff --git a/src/08_PTRN/Testat/Coin.cpp b/src/08_PTRN/Testat/Coin.cpp index 77744e8..f7d15cb 100644 --- a/src/08_PTRN/Testat/Coin.cpp +++ b/src/08_PTRN/Testat/Coin.cpp @@ -2,4 +2,8 @@ Coin::Coin(int value, Currency currency) : Cash(value, currency) { +} + +int Coin::getValue() { + return Money::getValue(); } \ No newline at end of file diff --git a/src/08_PTRN/Testat/Coin.h b/src/08_PTRN/Testat/Coin.h index 0ff4dde..2ea7be6 100644 --- a/src/08_PTRN/Testat/Coin.h +++ b/src/08_PTRN/Testat/Coin.h @@ -8,6 +8,7 @@ class Coin : public Cash{ private: public: Coin(int value, Currency currency); + int getValue(); }; #endif diff --git a/src/08_PTRN/Testat/Money.cpp b/src/08_PTRN/Testat/Money.cpp index b5d1601..841269d 100644 --- a/src/08_PTRN/Testat/Money.cpp +++ b/src/08_PTRN/Testat/Money.cpp @@ -36,10 +36,10 @@ const Money Money::operator--(int) { return Money(value--); } -Currency Money::getCurrency() { - return currency; -} - int Money::getValue() { return value; } + +Currency Money::getCurrency() { + return currency; +} \ No newline at end of file diff --git a/src/08_PTRN/Testat/Money.h b/src/08_PTRN/Testat/Money.h index 34866e1..ddfa6f1 100644 --- a/src/08_PTRN/Testat/Money.h +++ b/src/08_PTRN/Testat/Money.h @@ -16,7 +16,7 @@ public: const Money operator++(int); const Money operator--(int); - int getValue(); + virtual int getValue(); Currency getCurrency(); }; diff --git a/src/08_PTRN/Testat/Person.cpp b/src/08_PTRN/Testat/Person.cpp index 32ba318..c33c5e1 100644 --- a/src/08_PTRN/Testat/Person.cpp +++ b/src/08_PTRN/Testat/Person.cpp @@ -8,4 +8,4 @@ Person::Person(std::string name) : name(std::move(name)){ std::string Person::getName() { return name; -} +} \ No newline at end of file diff --git a/src/08_PTRN/Testat/Person.h b/src/08_PTRN/Testat/Person.h index d59b0bf..ffd9656 100644 --- a/src/08_PTRN/Testat/Person.h +++ b/src/08_PTRN/Testat/Person.h @@ -3,12 +3,14 @@ #include +#include "Money.h" class Person { private: std::string name; + std::vector money; public: - Person(std::string name); + explicit Person(std::string name); std::string getName(); }; diff --git a/src/08_PTRN/Testat/main.cpp b/src/08_PTRN/Testat/main.cpp index 570ecce..3d56c57 100644 --- a/src/08_PTRN/Testat/main.cpp +++ b/src/08_PTRN/Testat/main.cpp @@ -7,8 +7,9 @@ #include "BankAccount.h" #include "Bill.h" #include "CashFactory.h" +#include "Bank.h" -std::default_random_engine generator; +std::default_random_engine generator(static_cast(time(nullptr))); std::uniform_int_distribution distribution(-100, 500); std::string currency_to_string(CurrencyValue value) { @@ -49,33 +50,77 @@ std::ostream &operator<<(std::ostream &os, BankAccount bankAccount) { } - int main(int argc, char **argv) { - std::set accounts; + std::set banks; + + 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"); - accounts.insert(new BankAccount(max, "Konto 1")); - accounts.insert(new BankAccount(max, "Konto 2")); - accounts.insert(new BankAccount(marius, "Konto 1")); - accounts.insert(new BankAccount(marius, "Konto 2")); + + 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); auto * cashFactory = new CashFactory(); - std::for_each(accounts.begin(), accounts.end(), [cashFactory] (BankAccount* bankAccount) { - std::cout << *bankAccount << std::endl; - for (int i = 0; i < 1000; ++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; + for(Bank * bank : banks) { + std::cout << std::endl; + std::cout << "======================== " << bank->getName() << " ========================" << std::endl; + std::cout << std::endl; + for(BankAccount * 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); + if(bill != nullptr) { + *bankAccount + bill; + } + if(coin != nullptr) { + *bankAccount + coin; + } + + Money * fee = * bankAccount - 10; } + std::cout << *bankAccount << std::endl; } - std::cout << *bankAccount << std::endl; + } + + /* + * 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; + }); }); + */ + } \ No newline at end of file