From 68ca01ba1b8c8596cd7fac453526e0b8787faaa5 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Tue, 11 Dec 2018 11:28:24 +0100 Subject: [PATCH] 08_PTRN: weitere arbeit an der CashFactory --- .gitignore | 4 +-- Doxyfile | 2 +- src/08_PTRN/Testat/BankAccount.cpp | 6 ++-- src/08_PTRN/Testat/BankAccount.h | 2 +- src/08_PTRN/Testat/CashFactory.cpp | 34 ++++++++++++++++++++++ src/08_PTRN/Testat/CashFactory.h | 10 ++++++- src/08_PTRN/Testat/Currency.cpp | 3 +- src/08_PTRN/Testat/Currency.h | 6 ++-- src/08_PTRN/Testat/main.cpp | 45 +++++++++++------------------- 9 files changed, 71 insertions(+), 41 deletions(-) diff --git a/.gitignore b/.gitignore index 3dbd1ee..6c88dd8 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,6 @@ cmake-build-debug bin/ **/CMakeFiles CMakeCache.txt -cmake_install.cmake Makefile /C doc/ @@ -15,4 +14,5 @@ googletest-build googletest-download googletest-src lib - +*.cmake +CMakeDoxyfile.in diff --git a/Doxyfile b/Doxyfile index 0ba75e8..db8cd15 100644 --- a/Doxyfile +++ b/Doxyfile @@ -5,7 +5,7 @@ PROJECT_NAME = C/C++ PROJECT_NUMBER = OUTPUT_DIRECTORY = @CMAKE_CURRENT_SOURCE_DIR@/doc CREATE_SUBDIRS = NO -OUTPUT_LANGUAGE = Englisch +OUTPUT_LANGUAGE = English ... JAVADOC_AUTOBRIEF = YES ... diff --git a/src/08_PTRN/Testat/BankAccount.cpp b/src/08_PTRN/Testat/BankAccount.cpp index 9b5d1f2..4493ff1 100644 --- a/src/08_PTRN/Testat/BankAccount.cpp +++ b/src/08_PTRN/Testat/BankAccount.cpp @@ -2,9 +2,9 @@ BankAccount::BankAccount(std::string name) : name(std::move(name)) {} -Money* BankAccount::operator+(Money* money) { - Money *result = new Money(money->getValue() + getMoney().getValue()); - return result; +BankAccount * BankAccount::operator+(Money* money) { + BankAccount::money = new Money(money->getValue() + getMoney().getValue()); + return this; } bool BankAccount::operator<(BankAccount bankAccount) { diff --git a/src/08_PTRN/Testat/BankAccount.h b/src/08_PTRN/Testat/BankAccount.h index 3d8998e..12cd8bb 100644 --- a/src/08_PTRN/Testat/BankAccount.h +++ b/src/08_PTRN/Testat/BankAccount.h @@ -12,7 +12,7 @@ private: Money money; public: explicit BankAccount(std::string name); - Money* operator+(Money* money); + BankAccount* operator+(Money* money); bool operator<(BankAccount bankAccount); std::string getName(); Money getMoney(); diff --git a/src/08_PTRN/Testat/CashFactory.cpp b/src/08_PTRN/Testat/CashFactory.cpp index cab4fbe..0051730 100644 --- a/src/08_PTRN/Testat/CashFactory.cpp +++ b/src/08_PTRN/Testat/CashFactory.cpp @@ -1 +1,35 @@ +#include +#include #include "CashFactory.h" + +int allowedBills[5] = {100, 50, 20, 10, 5}; +int allowedCoins[8] = {1, 2, 5, 10, 20, 50, 100, 200}; + +Bill * CashFactory::printBill(int value, Currency currency) { + auto * iter = std::find(std::begin(allowedBills), std::end(allowedBills), value); + if(iter == std::end(allowedBills)) { + std::cout << "Ungültiger Schein" << std::endl; + }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); + } +} + +std::string CashFactory::randomString(size_t length) { + auto randchar = []() -> char { + const char charset[] = + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; + 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; + +} diff --git a/src/08_PTRN/Testat/CashFactory.h b/src/08_PTRN/Testat/CashFactory.h index d2764e9..8b02e7a 100644 --- a/src/08_PTRN/Testat/CashFactory.h +++ b/src/08_PTRN/Testat/CashFactory.h @@ -1,8 +1,16 @@ #ifndef C_C_CASHFACTORY_H #define C_C_CASHFACTORY_H -class CashFactory { +#include +#include +#include "Bill.h" +class CashFactory { +private: + std::vector usedSerials; + std::string randomString(size_t length); +public: + Bill* printBill(int value, Currency currency); }; diff --git a/src/08_PTRN/Testat/Currency.cpp b/src/08_PTRN/Testat/Currency.cpp index 2c28571..181d96d 100644 --- a/src/08_PTRN/Testat/Currency.cpp +++ b/src/08_PTRN/Testat/Currency.cpp @@ -2,7 +2,6 @@ #include "Currency.h" Currency::Currency(CurrencyValue value) : value(value) { - } bool Currency::operator==(Currency ¤cy) { @@ -16,4 +15,4 @@ CurrencyValue Currency::getValue() { Currency &Currency::operator=(Currency currency) { value = currency.value; return *this; -} +} \ No newline at end of file diff --git a/src/08_PTRN/Testat/Currency.h b/src/08_PTRN/Testat/Currency.h index 69f46ca..469346c 100644 --- a/src/08_PTRN/Testat/Currency.h +++ b/src/08_PTRN/Testat/Currency.h @@ -1,13 +1,14 @@ #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; @@ -16,8 +17,7 @@ public: bool operator==(Currency ¤cy); Currency& operator=(Currency currency); CurrencyValue getValue(); + }; - - #endif \ No newline at end of file diff --git a/src/08_PTRN/Testat/main.cpp b/src/08_PTRN/Testat/main.cpp index 46f4296..09711fb 100644 --- a/src/08_PTRN/Testat/main.cpp +++ b/src/08_PTRN/Testat/main.cpp @@ -6,24 +6,11 @@ #include "Currency.h" #include "BankAccount.h" #include "Bill.h" +#include "CashFactory.h" std::default_random_engine generator; std::uniform_int_distribution distribution(-100, 500); -std::string random_string(size_t length) { - auto randchar = []() -> char { - const char charset[] = - "0123456789" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz"; - 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; -} - std::string currency_to_string(CurrencyValue value) { if(value == CurrencyValue::USD) return "US Dollar"; if(value == CurrencyValue::EUR) return "Euro"; @@ -37,7 +24,7 @@ std::ostream &operator<<(std::ostream &os, Currency currency) { } std::ostream &operator<<(std::ostream &os, Bill bill) { - os << bill.getValue() << " " << bill.getCurrency() << bill.getSerial(); + os << bill.getValue() << " " << bill.getCurrency() << " " << bill.getSerial(); return os; } @@ -56,24 +43,26 @@ std::ostream &operator<<(std::ostream &os, BankAccount bankAccount) { return os; } -Bill billPrinter(int value, Currency currency) { - return Bill(value, currency, random_string(5)); -} int main(int argc, char **argv) { std::set accounts; - accounts.insert(new BankAccount("Max")); - accounts.insert(new BankAccount("Max")); - accounts.insert(new BankAccount("Marius")); - accounts.insert(new BankAccount("Test")); + accounts.insert(new BankAccount("Max Müller")); + accounts.insert(new BankAccount("Max Maier")); + accounts.insert(new BankAccount("Marius Muller")); + accounts.insert(new BankAccount("Test 123")); - std::for_each(accounts.begin(), accounts.end(), [] (BankAccount* bankAccount){ - int rnd = distribution(generator); - Bill bill = billPrinter(rnd, CurrencyValue::USD); - std::cout << bill << std::endl; - //Money* res = bankAccount + &bill; - //std::cout << *res << std::endl; + 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); + //std::cout << *bill << std::endl; + BankAccount* res = *bankAccount + bill; + } + std::cout << *bankAccount << std::endl; }); } \ No newline at end of file