08_PTRN: Banking erweitert
This commit is contained in:
parent
8944e7ad9a
commit
be744eb9e4
@ -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)
|
||||
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)
|
25
src/08_PTRN/Testat/Bank.cpp
Normal file
25
src/08_PTRN/Testat/Bank.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
#include <utility>
|
||||
#include <set>
|
||||
|
||||
#include "Bank.h"
|
||||
|
||||
Bank::Bank(std::string name) : name(std::move(name)) {
|
||||
|
||||
}
|
||||
|
||||
void Bank::addAccount(BankAccount * account) {
|
||||
accounts.insert(account);
|
||||
}
|
||||
|
||||
std::set<BankAccount *> Bank::getAccounts() {
|
||||
return accounts;
|
||||
}
|
||||
|
||||
std::string Bank::getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
void Bank::addMoney(Money * money) {
|
||||
this->money.insert(money);
|
||||
}
|
||||
|
21
src/08_PTRN/Testat/Bank.h
Normal file
21
src/08_PTRN/Testat/Bank.h
Normal file
@ -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<BankAccount *> accounts;
|
||||
std::set<Money *> money;
|
||||
public:
|
||||
explicit Bank(std::string name);
|
||||
void addAccount(BankAccount * account);
|
||||
void addMoney(Money * money);
|
||||
std::set<BankAccount *> getAccounts();
|
||||
std::string getName();
|
||||
};
|
||||
|
||||
|
||||
#endif
|
@ -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();
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ private:
|
||||
public:
|
||||
Bill(int value, Currency currency, std::string serial);
|
||||
std::string getSerial();
|
||||
int getValue();
|
||||
};
|
||||
|
||||
|
||||
|
@ -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<std::string> 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);
|
||||
}
|
||||
}
|
||||
|
@ -2,4 +2,8 @@
|
||||
|
||||
Coin::Coin(int value, Currency currency) : Cash(value, currency) {
|
||||
|
||||
}
|
||||
|
||||
int Coin::getValue() {
|
||||
return Money::getValue();
|
||||
}
|
@ -8,6 +8,7 @@ class Coin : public Cash{
|
||||
private:
|
||||
public:
|
||||
Coin(int value, Currency currency);
|
||||
int getValue();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -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;
|
||||
}
|
@ -16,7 +16,7 @@ public:
|
||||
const Money operator++(int);
|
||||
const Money operator--(int);
|
||||
|
||||
int getValue();
|
||||
virtual int getValue();
|
||||
Currency getCurrency();
|
||||
};
|
||||
|
||||
|
@ -8,4 +8,4 @@ Person::Person(std::string name) : name(std::move(name)){
|
||||
|
||||
std::string Person::getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
@ -3,12 +3,14 @@
|
||||
|
||||
|
||||
#include <string>
|
||||
#include "Money.h"
|
||||
|
||||
class Person {
|
||||
private:
|
||||
std::string name;
|
||||
std::vector<Money *> money;
|
||||
public:
|
||||
Person(std::string name);
|
||||
explicit Person(std::string name);
|
||||
std::string getName();
|
||||
};
|
||||
|
||||
|
@ -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<unsigned long>(time(nullptr)));
|
||||
std::uniform_int_distribution<int> 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<BankAccount*> accounts;
|
||||
std::set<Bank*> 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;
|
||||
});
|
||||
});
|
||||
*/
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user