10_PITF: Anfang Grundgerüst

This commit is contained in:
Johannes Theiner 2018-12-23 13:01:44 +01:00
parent be744eb9e4
commit aa9ca3c694
24 changed files with 576 additions and 2 deletions

View File

@ -161,7 +161,7 @@ public:
class M : public K{
public:
//B b; für Modifikation 5 einkommentieren
//B b; // für Modifikation 5 einkommentieren
M(){std::cout << "+M ";}
~M(){std::cout << "-M ";}
};

View File

@ -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 Testat/Bank.cpp Testat/Bank.h)
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)

View File

@ -0,0 +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)

View 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/10_PITF/Testat/Bank.h Normal file
View 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

View File

@ -0,0 +1,37 @@
#include "BankAccount.h"
#include "CashFactory.h"
BankAccount::BankAccount(Person * owner, std::string name) : name(std::move(name)) {
this->owner = owner;
}
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();
}
std::string BankAccount::getName() {
return name;
}
Money BankAccount::getMoney() {
return money;
}
Person *BankAccount::getOwner() {
return owner;
}

View File

@ -0,0 +1,26 @@
#ifndef C_C_BANKACCOUNTS_H
#define C_C_BANKACCOUNTS_H
#include <unordered_map>
#include "Money.h"
#include "Cash.h"
#include "Person.h"
class BankAccount {
private:
std::string name;
Person * owner;
Money money;
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();
Money getMoney();
};
#endif

View File

@ -0,0 +1,15 @@
#include <utility>
#include "Bill.h"
Bill::Bill(int value, Currency currency, std::string serial) : Cash(value, currency), serial(std::move(serial)){
}
int Bill::getValue() {
return Money::getValue() * 10;
}
std::string Bill::getSerial() {
return serial;
}

17
src/10_PITF/Testat/Bill.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef C_C_BILL_H
#define C_C_BILL_H
#include "Cash.h"
class Bill : public Cash{
private:
std::string serial;
public:
Bill(int value, Currency currency, std::string serial);
std::string getSerial();
int getValue();
};
#endif

View File

@ -0,0 +1,8 @@
#include <utility>
#include <algorithm>
#include "Cash.h"
Cash::Cash(int value, Currency currency) : Money(value, currency) {
}

14
src/10_PITF/Testat/Cash.h Normal file
View File

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

View File

@ -0,0 +1,43 @@
#include <algorithm>
#include "CashFactory.h"
int allowedBills[5] = {100, 50, 20, 10, 5};
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);
if(iter == std::end(allowedBills)) {
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);
}
}
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);
}
std::string CashFactory::randomString(size_t length) {
auto randchar = []() -> char {
const char charset[] =
"0123456789"
"ABCDEFGHIKLMNOPRSTUVWXYZ"
"abcdfghijklmnoqrstuvwxyz";
const size_t max_index = (sizeof(charset) - 1);
return charset[rand() % max_index];
};
std::string str(length, 0);
std::generate_n(str.begin(), length, randchar);
return str;
}

View File

@ -0,0 +1,17 @@
#ifndef C_C_CASHFACTORY_H
#define C_C_CASHFACTORY_H
#include <string>
#include <vector>
#include "Bill.h"
#include "Coin.h"
class CashFactory {
private:
std::string randomString(size_t length);
public:
Bill * printBill(int value, Currency currency);
Coin * printCoin(int value, Currency currency);
};
#endif

View File

@ -0,0 +1,9 @@
#include "Coin.h"
Coin::Coin(int value, Currency currency) : Cash(value, currency) {
}
int Coin::getValue() {
return Money::getValue();
}

14
src/10_PITF/Testat/Coin.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef C_C_COIN_H
#define C_C_COIN_H
#include "Cash.h"
class Coin : public Cash{
private:
public:
Coin(int value, Currency currency);
int getValue();
};
#endif

View File

@ -0,0 +1,18 @@
#include <string>
#include "Currency.h"
Currency::Currency(CurrencyValue value) : value(value) {
}
bool Currency::operator==(Currency &currency) {
return value == currency.value;
}
CurrencyValue Currency::getValue() {
return value;
}
Currency &Currency::operator=(Currency currency) {
value = currency.value;
return *this;
}

View File

@ -0,0 +1,23 @@
#ifndef C_C_CURRENCY_H
#define C_C_CURRENCY_H
#include <vector>
enum class CurrencyValue {
USD=100,
EUR=80,
GPD=75,
};
class Currency {
private:
CurrencyValue value;
public:
Currency(CurrencyValue value);
bool operator==(Currency &currency);
Currency& operator=(Currency currency);
CurrencyValue getValue();
};
#endif

View File

@ -0,0 +1,26 @@
#include <cstdlib>
#include <cassert>
#include "HeapObject.h"
int HeapObject::ctorCount = 0;
int HeapObject::dtorCount = 0;
int HeapObject::newCount = 0;
void* HeapObject::operator new (size_t size){
newCount++;
return new char[size];
}
HeapObject::HeapObject(){
ctorCount++;
}
HeapObject::~HeapObject(){
dtorCount++;
}
bool HeapObject::assertionsHold(){
assert(ctorCount == newCount); // all objects have been allocated on heap
assert(ctorCount == dtorCount); // all objects have been deleted
return true;
}

View File

@ -0,0 +1,29 @@
#ifndef C_C_HEAPOBJECT_H
#define C_C_HEAPOBJECT_H
#include <cstdlib>
class HeapObject {
public:
void *operator new(size_t size);
HeapObject();
virtual ~HeapObject();
static bool assertionsHold();
protected:
private:
static int ctorCount;
static int dtorCount;
static int newCount;
//static void remove(HeapObject *);
HeapObject(const HeapObject &) = delete;
HeapObject &operator=(const HeapObject &) = delete;
};
#endif

View File

@ -0,0 +1,45 @@
#include "Money.h"
Money::Money(int value, Currency currency) : value(value), currency(currency) {
}
Money& Money::operator=(Money money) {
value = money.getValue();
currency = money.getCurrency();
return *this;
}
Money &Money::operator=(Money *money) {
value = money->getValue();
currency = money->getCurrency();
return *this;
}
Money Money::operator+(Money &a) {
if(currency == a.currency) {
return Money(value + a.value);
}else return Money(-1, Currency(CurrencyValue::USD));
}
Money Money::operator-(Money &a) {
if(currency == a.currency) {
return Money(value - a.value);
}else return Money(-1, Currency(CurrencyValue::USD));
}
const Money Money::operator++(int) {
return Money(value++);
}
const Money Money::operator--(int) {
return Money(value--);
}
int Money::getValue() {
return value;
}
Currency Money::getCurrency() {
return currency;
}

View File

@ -0,0 +1,23 @@
#ifndef C_C_MONEY_H
#define C_C_MONEY_H
#include "Currency.h"
class Money {
private:
int value;
Currency currency;
public:
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();
Currency getCurrency();
};
#endif

View File

@ -0,0 +1,11 @@
#include <utility>
#include "Person.h"
Person::Person(std::string name) : name(std::move(name)){
}
std::string Person::getName() {
return name;
}

View File

@ -0,0 +1,18 @@
#ifndef C_C_PERSON_H
#define C_C_PERSON_H
#include <string>
#include "Money.h"
class Person {
private:
std::string name;
std::vector<Money *> money;
public:
explicit Person(std::string name);
std::string getName();
};
#endif

134
src/10_PITF/Testat/main.cpp Normal file
View File

@ -0,0 +1,134 @@
#include <set>
#include <algorithm>
#include <iostream>
#include <ctime>
#include <random>
#include "Currency.h"
#include "BankAccount.h"
#include "Bill.h"
#include "CashFactory.h"
#include "Bank.h"
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) {
if (value == CurrencyValue::USD) return "US Dollar";
if (value == CurrencyValue::EUR) return "Euro";
if (value == CurrencyValue::GPD) return "British Pound";
return "Say what ?";
}
std::ostream &operator<<(std::ostream &os, Currency currency) {
os << currency_to_string(currency.getValue());
return os;
}
std::ostream &operator<<(std::ostream &os, Bill bill) {
os << bill.getValue() << " " << bill.getCurrency() << " " << bill.getSerial();
return os;
}
std::ostream &operator<<(std::ostream &os, Cash cash) {
os << cash.getValue() << " " << cash.getCurrency();
return os;
}
std::ostream &operator<<(std::ostream &os, Money money) {
os << money.getValue() << " " << money.getCurrency();
return os;
}
std::ostream &operator<<(std::ostream &os, Person *person) {
os << person->getName();
return os;
}
std::ostream &operator<<(std::ostream &os, BankAccount bankAccount) {
os << bankAccount.getOwner() << " " << bankAccount.getName() << ": " << bankAccount.getMoney();
return os;
}
std::set<Bank *> banks;
std::set<Person *> owners;
CashFactory *cashFactory;
void setup() {
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");
owners.insert(max);
owners.insert(marius);
owners.insert(alex);
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);
cashFactory = new CashFactory();
}
void simulate() {
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;
}
}
}
void tearDown() {
for (Bank *bank : banks) {
for (BankAccount *bankAccount : bank->getAccounts()) {
delete bankAccount;
}
delete bank;
}
for(Person *person : owners) {
delete person;
}
}
int main(int argc, char **argv) {
setup();
simulate();
tearDown();
HeapObject::assertionsHold();
return 0;
}