08_PTRN: weitere arbeit an der CashFactory
This commit is contained in:
parent
59c400a3cb
commit
68ca01ba1b
|
@ -7,7 +7,6 @@ cmake-build-debug
|
||||||
bin/
|
bin/
|
||||||
**/CMakeFiles
|
**/CMakeFiles
|
||||||
CMakeCache.txt
|
CMakeCache.txt
|
||||||
cmake_install.cmake
|
|
||||||
Makefile
|
Makefile
|
||||||
/C
|
/C
|
||||||
doc/
|
doc/
|
||||||
|
@ -15,4 +14,5 @@ googletest-build
|
||||||
googletest-download
|
googletest-download
|
||||||
googletest-src
|
googletest-src
|
||||||
lib
|
lib
|
||||||
|
*.cmake
|
||||||
|
CMakeDoxyfile.in
|
||||||
|
|
2
Doxyfile
2
Doxyfile
|
@ -5,7 +5,7 @@ PROJECT_NAME = C/C++
|
||||||
PROJECT_NUMBER =
|
PROJECT_NUMBER =
|
||||||
OUTPUT_DIRECTORY = @CMAKE_CURRENT_SOURCE_DIR@/doc
|
OUTPUT_DIRECTORY = @CMAKE_CURRENT_SOURCE_DIR@/doc
|
||||||
CREATE_SUBDIRS = NO
|
CREATE_SUBDIRS = NO
|
||||||
OUTPUT_LANGUAGE = Englisch
|
OUTPUT_LANGUAGE = English
|
||||||
...
|
...
|
||||||
JAVADOC_AUTOBRIEF = YES
|
JAVADOC_AUTOBRIEF = YES
|
||||||
...
|
...
|
||||||
|
|
|
@ -2,9 +2,9 @@
|
||||||
|
|
||||||
BankAccount::BankAccount(std::string name) : name(std::move(name)) {}
|
BankAccount::BankAccount(std::string name) : name(std::move(name)) {}
|
||||||
|
|
||||||
Money* BankAccount::operator+(Money* money) {
|
BankAccount * BankAccount::operator+(Money* money) {
|
||||||
Money *result = new Money(money->getValue() + getMoney().getValue());
|
BankAccount::money = new Money(money->getValue() + getMoney().getValue());
|
||||||
return result;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BankAccount::operator<(BankAccount bankAccount) {
|
bool BankAccount::operator<(BankAccount bankAccount) {
|
||||||
|
|
|
@ -12,7 +12,7 @@ private:
|
||||||
Money money;
|
Money money;
|
||||||
public:
|
public:
|
||||||
explicit BankAccount(std::string name);
|
explicit BankAccount(std::string name);
|
||||||
Money* operator+(Money* money);
|
BankAccount* operator+(Money* money);
|
||||||
bool operator<(BankAccount bankAccount);
|
bool operator<(BankAccount bankAccount);
|
||||||
std::string getName();
|
std::string getName();
|
||||||
Money getMoney();
|
Money getMoney();
|
||||||
|
|
|
@ -1 +1,35 @@
|
||||||
|
#include <algorithm>
|
||||||
|
#include <iostream>
|
||||||
#include "CashFactory.h"
|
#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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -1,8 +1,16 @@
|
||||||
#ifndef C_C_CASHFACTORY_H
|
#ifndef C_C_CASHFACTORY_H
|
||||||
#define C_C_CASHFACTORY_H
|
#define C_C_CASHFACTORY_H
|
||||||
|
|
||||||
class CashFactory {
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include "Bill.h"
|
||||||
|
|
||||||
|
class CashFactory {
|
||||||
|
private:
|
||||||
|
std::vector<std::string> usedSerials;
|
||||||
|
std::string randomString(size_t length);
|
||||||
|
public:
|
||||||
|
Bill* printBill(int value, Currency currency);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
#include "Currency.h"
|
#include "Currency.h"
|
||||||
|
|
||||||
Currency::Currency(CurrencyValue value) : value(value) {
|
Currency::Currency(CurrencyValue value) : value(value) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Currency::operator==(Currency ¤cy) {
|
bool Currency::operator==(Currency ¤cy) {
|
||||||
|
@ -16,4 +15,4 @@ CurrencyValue Currency::getValue() {
|
||||||
Currency &Currency::operator=(Currency currency) {
|
Currency &Currency::operator=(Currency currency) {
|
||||||
value = currency.value;
|
value = currency.value;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
|
@ -1,13 +1,14 @@
|
||||||
#ifndef C_C_CURRENCY_H
|
#ifndef C_C_CURRENCY_H
|
||||||
#define C_C_CURRENCY_H
|
#define C_C_CURRENCY_H
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
enum class CurrencyValue {
|
enum class CurrencyValue {
|
||||||
USD=100,
|
USD=100,
|
||||||
EUR=80,
|
EUR=80,
|
||||||
GPD=75,
|
GPD=75,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class Currency {
|
class Currency {
|
||||||
private:
|
private:
|
||||||
CurrencyValue value;
|
CurrencyValue value;
|
||||||
|
@ -16,8 +17,7 @@ public:
|
||||||
bool operator==(Currency ¤cy);
|
bool operator==(Currency ¤cy);
|
||||||
Currency& operator=(Currency currency);
|
Currency& operator=(Currency currency);
|
||||||
CurrencyValue getValue();
|
CurrencyValue getValue();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -6,24 +6,11 @@
|
||||||
#include "Currency.h"
|
#include "Currency.h"
|
||||||
#include "BankAccount.h"
|
#include "BankAccount.h"
|
||||||
#include "Bill.h"
|
#include "Bill.h"
|
||||||
|
#include "CashFactory.h"
|
||||||
|
|
||||||
std::default_random_engine generator;
|
std::default_random_engine generator;
|
||||||
std::uniform_int_distribution<int> distribution(-100, 500);
|
std::uniform_int_distribution<int> 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) {
|
std::string currency_to_string(CurrencyValue value) {
|
||||||
if(value == CurrencyValue::USD) return "US Dollar";
|
if(value == CurrencyValue::USD) return "US Dollar";
|
||||||
if(value == CurrencyValue::EUR) return "Euro";
|
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) {
|
std::ostream &operator<<(std::ostream &os, Bill bill) {
|
||||||
os << bill.getValue() << " " << bill.getCurrency() << bill.getSerial();
|
os << bill.getValue() << " " << bill.getCurrency() << " " << bill.getSerial();
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,24 +43,26 @@ std::ostream &operator<<(std::ostream &os, BankAccount bankAccount) {
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
Bill billPrinter(int value, Currency currency) {
|
|
||||||
return Bill(value, currency, random_string(5));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
std::set<BankAccount*> accounts;
|
std::set<BankAccount*> accounts;
|
||||||
|
|
||||||
accounts.insert(new BankAccount("Max"));
|
accounts.insert(new BankAccount("Max Müller"));
|
||||||
accounts.insert(new BankAccount("Max"));
|
accounts.insert(new BankAccount("Max Maier"));
|
||||||
accounts.insert(new BankAccount("Marius"));
|
accounts.insert(new BankAccount("Marius Muller"));
|
||||||
accounts.insert(new BankAccount("Test"));
|
accounts.insert(new BankAccount("Test 123"));
|
||||||
|
|
||||||
std::for_each(accounts.begin(), accounts.end(), [] (BankAccount* bankAccount){
|
auto * cashFactory = new CashFactory();
|
||||||
int rnd = distribution(generator);
|
|
||||||
Bill bill = billPrinter(rnd, CurrencyValue::USD);
|
std::for_each(accounts.begin(), accounts.end(), [cashFactory] (BankAccount* bankAccount) {
|
||||||
std::cout << bill << std::endl;
|
std::cout << *bankAccount << std::endl;
|
||||||
//Money* res = bankAccount + &bill;
|
for (int i = 0; i < 1000; ++i) {
|
||||||
//std::cout << *res << std::endl;
|
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;
|
||||||
});
|
});
|
||||||
}
|
}
|
Loading…
Reference in New Issue