diff --git a/src/05_OO/b/MP/main_mp4_OO_b.cpp b/src/05_OO/b/MP/main_mp4_OO_b.cpp index cf8412a..494c97b 100644 --- a/src/05_OO/b/MP/main_mp4_OO_b.cpp +++ b/src/05_OO/b/MP/main_mp4_OO_b.cpp @@ -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 ";} }; diff --git a/src/08_PTRN/CMakeLists.txt b/src/08_PTRN/CMakeLists.txt index 8ef0526..b9dace9 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 Testat/Bank.cpp Testat/Bank.h) \ 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) diff --git a/src/10_PITF/CMakeLists.txt b/src/10_PITF/CMakeLists.txt index e69de29..bf3cd9f 100644 --- a/src/10_PITF/CMakeLists.txt +++ b/src/10_PITF/CMakeLists.txt @@ -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) diff --git a/src/10_PITF/Testat/Bank.cpp b/src/10_PITF/Testat/Bank.cpp new file mode 100644 index 0000000..bd2002e --- /dev/null +++ b/src/10_PITF/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/10_PITF/Testat/Bank.h b/src/10_PITF/Testat/Bank.h new file mode 100644 index 0000000..4d5fb88 --- /dev/null +++ b/src/10_PITF/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/10_PITF/Testat/BankAccount.cpp b/src/10_PITF/Testat/BankAccount.cpp new file mode 100644 index 0000000..333c1c8 --- /dev/null +++ b/src/10_PITF/Testat/BankAccount.cpp @@ -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; +} + + + diff --git a/src/10_PITF/Testat/BankAccount.h b/src/10_PITF/Testat/BankAccount.h new file mode 100644 index 0000000..c0432ad --- /dev/null +++ b/src/10_PITF/Testat/BankAccount.h @@ -0,0 +1,26 @@ +#ifndef C_C_BANKACCOUNTS_H +#define C_C_BANKACCOUNTS_H + +#include + +#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 diff --git a/src/10_PITF/Testat/Bill.cpp b/src/10_PITF/Testat/Bill.cpp new file mode 100644 index 0000000..de9d70b --- /dev/null +++ b/src/10_PITF/Testat/Bill.cpp @@ -0,0 +1,15 @@ +#include + +#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; +} diff --git a/src/10_PITF/Testat/Bill.h b/src/10_PITF/Testat/Bill.h new file mode 100644 index 0000000..bafd05e --- /dev/null +++ b/src/10_PITF/Testat/Bill.h @@ -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 \ No newline at end of file diff --git a/src/10_PITF/Testat/Cash.cpp b/src/10_PITF/Testat/Cash.cpp new file mode 100644 index 0000000..51dd580 --- /dev/null +++ b/src/10_PITF/Testat/Cash.cpp @@ -0,0 +1,8 @@ +#include +#include + +#include "Cash.h" + +Cash::Cash(int value, Currency currency) : Money(value, currency) { + +} \ No newline at end of file diff --git a/src/10_PITF/Testat/Cash.h b/src/10_PITF/Testat/Cash.h new file mode 100644 index 0000000..cb4ddf5 --- /dev/null +++ b/src/10_PITF/Testat/Cash.h @@ -0,0 +1,14 @@ +#ifndef C_C_CASH_H +#define C_C_CASH_H + +#include +#include "Money.h" +#include "HeapObject.h" + +class Cash : public HeapObject, public Money{ +private: +public: + Cash(int value, Currency currency); +}; + +#endif diff --git a/src/10_PITF/Testat/CashFactory.cpp b/src/10_PITF/Testat/CashFactory.cpp new file mode 100644 index 0000000..0eff08f --- /dev/null +++ b/src/10_PITF/Testat/CashFactory.cpp @@ -0,0 +1,43 @@ +#include +#include "CashFactory.h" + +int allowedBills[5] = {100, 50, 20, 10, 5}; +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); + 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; + +} \ No newline at end of file diff --git a/src/10_PITF/Testat/CashFactory.h b/src/10_PITF/Testat/CashFactory.h new file mode 100644 index 0000000..59b33b0 --- /dev/null +++ b/src/10_PITF/Testat/CashFactory.h @@ -0,0 +1,17 @@ +#ifndef C_C_CASHFACTORY_H +#define C_C_CASHFACTORY_H + +#include +#include +#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 \ No newline at end of file diff --git a/src/10_PITF/Testat/Coin.cpp b/src/10_PITF/Testat/Coin.cpp new file mode 100644 index 0000000..f7d15cb --- /dev/null +++ b/src/10_PITF/Testat/Coin.cpp @@ -0,0 +1,9 @@ +#include "Coin.h" + +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/10_PITF/Testat/Coin.h b/src/10_PITF/Testat/Coin.h new file mode 100644 index 0000000..2ea7be6 --- /dev/null +++ b/src/10_PITF/Testat/Coin.h @@ -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 diff --git a/src/10_PITF/Testat/Currency.cpp b/src/10_PITF/Testat/Currency.cpp new file mode 100644 index 0000000..181d96d --- /dev/null +++ b/src/10_PITF/Testat/Currency.cpp @@ -0,0 +1,18 @@ +#include +#include "Currency.h" + +Currency::Currency(CurrencyValue value) : value(value) { +} + +bool Currency::operator==(Currency ¤cy) { + return value == currency.value; +} + +CurrencyValue Currency::getValue() { + return value; +} + +Currency &Currency::operator=(Currency currency) { + value = currency.value; + return *this; +} \ No newline at end of file diff --git a/src/10_PITF/Testat/Currency.h b/src/10_PITF/Testat/Currency.h new file mode 100644 index 0000000..469346c --- /dev/null +++ b/src/10_PITF/Testat/Currency.h @@ -0,0 +1,23 @@ +#ifndef C_C_CURRENCY_H +#define C_C_CURRENCY_H + +#include + +enum class CurrencyValue { + USD=100, + EUR=80, + GPD=75, +}; + +class Currency { +private: + CurrencyValue value; +public: + Currency(CurrencyValue value); + bool operator==(Currency ¤cy); + Currency& operator=(Currency currency); + CurrencyValue getValue(); + +}; + +#endif \ No newline at end of file diff --git a/src/10_PITF/Testat/HeapObject.cpp b/src/10_PITF/Testat/HeapObject.cpp new file mode 100644 index 0000000..1c4e870 --- /dev/null +++ b/src/10_PITF/Testat/HeapObject.cpp @@ -0,0 +1,26 @@ +#include +#include +#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; +} diff --git a/src/10_PITF/Testat/HeapObject.h b/src/10_PITF/Testat/HeapObject.h new file mode 100644 index 0000000..58870ca --- /dev/null +++ b/src/10_PITF/Testat/HeapObject.h @@ -0,0 +1,29 @@ +#ifndef C_C_HEAPOBJECT_H +#define C_C_HEAPOBJECT_H + +#include + +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 diff --git a/src/10_PITF/Testat/Money.cpp b/src/10_PITF/Testat/Money.cpp new file mode 100644 index 0000000..841269d --- /dev/null +++ b/src/10_PITF/Testat/Money.cpp @@ -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; +} \ No newline at end of file diff --git a/src/10_PITF/Testat/Money.h b/src/10_PITF/Testat/Money.h new file mode 100644 index 0000000..ddfa6f1 --- /dev/null +++ b/src/10_PITF/Testat/Money.h @@ -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 diff --git a/src/10_PITF/Testat/Person.cpp b/src/10_PITF/Testat/Person.cpp new file mode 100644 index 0000000..c33c5e1 --- /dev/null +++ b/src/10_PITF/Testat/Person.cpp @@ -0,0 +1,11 @@ +#include + +#include "Person.h" + +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/10_PITF/Testat/Person.h b/src/10_PITF/Testat/Person.h new file mode 100644 index 0000000..ffd9656 --- /dev/null +++ b/src/10_PITF/Testat/Person.h @@ -0,0 +1,18 @@ +#ifndef C_C_PERSON_H +#define C_C_PERSON_H + + +#include +#include "Money.h" + +class Person { +private: + std::string name; + std::vector money; +public: + explicit Person(std::string name); + std::string getName(); +}; + + +#endif diff --git a/src/10_PITF/Testat/main.cpp b/src/10_PITF/Testat/main.cpp new file mode 100644 index 0000000..c5db9e8 --- /dev/null +++ b/src/10_PITF/Testat/main.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#include "Currency.h" +#include "BankAccount.h" +#include "Bill.h" +#include "CashFactory.h" +#include "Bank.h" + +std::default_random_engine generator(static_cast(time(nullptr))); +std::uniform_int_distribution 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 banks; +std::set 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; +} \ No newline at end of file