08_PTRN: weitere arbeit an der CashFactory

This commit is contained in:
Johannes Theiner 2018-12-11 11:28:24 +01:00
parent 59c400a3cb
commit 68ca01ba1b
9 changed files with 71 additions and 41 deletions

4
.gitignore vendored
View File

@ -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

View File

@ -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
...

View File

@ -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) {

View File

@ -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();

View File

@ -1 +1,35 @@
#include <algorithm>
#include <iostream>
#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;
}

View File

@ -1,8 +1,16 @@
#ifndef 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);
};

View File

@ -2,7 +2,6 @@
#include "Currency.h"
Currency::Currency(CurrencyValue value) : value(value) {
}
bool Currency::operator==(Currency &currency) {

View File

@ -1,13 +1,14 @@
#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;
@ -16,8 +17,7 @@ public:
bool operator==(Currency &currency);
Currency& operator=(Currency currency);
CurrencyValue getValue();
};
#endif

View File

@ -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<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) {
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<BankAccount*> 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){
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 = billPrinter(rnd, CurrencyValue::USD);
std::cout << bill << std::endl;
//Money* res = bankAccount + &bill;
//std::cout << *res << std::endl;
Bill * bill = cashFactory->printBill(rnd, CurrencyValue::USD);
//std::cout << *bill << std::endl;
BankAccount* res = *bankAccount + bill;
}
std::cout << *bankAccount << std::endl;
});
}