08_PTRN: Münzen können gedruckt werden

+ Bank Konten haben jetzt Besitzer
This commit is contained in:
Johannes Theiner 2018-12-16 15:05:46 +01:00
parent ca86c2e3ca
commit 35406afa44
20 changed files with 84 additions and 182 deletions

View File

@ -8,55 +8,6 @@ SET(CMAKE_BUILD_TYPE Debug)
set (EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
set (LIBRARY_OUTPUT_PATH ${EXECUTABLE_OUTPUT_PATH})
# add a target to generate API documentation with Doxygen
find_package(Doxygen)
if(DOXYGEN_FOUND)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
add_custom_target(doc
${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen" VERBATIM
)
endif(DOXYGEN_FOUND)
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
endif()
# Now simply link against gtest or gtest_main as needed. Eg
#add_executable(example example.cpp)
#target_link_libraries(example gtest_main)
#add_test(NAME example_test COMMAND example)
add_subdirectory(src)
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "bin" "doc" "CMakeFiles" "C" "googletest-build" "googletest-download" "googletest-src" "lib")
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "bin" "doc" "CMakeFiles" "C" "lib")

View File

@ -1,15 +0,0 @@
cmake_minimum_required(VERSION 2.8.2)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)

View File

@ -1,42 +0,0 @@
#---------------------------------------------------------------------------
# Project related configuration options
#---------------------------------------------------------------------------
PROJECT_NAME = C/C++
PROJECT_NUMBER =
OUTPUT_DIRECTORY = @CMAKE_CURRENT_SOURCE_DIR@/doc
CREATE_SUBDIRS = NO
OUTPUT_LANGUAGE = English
...
JAVADOC_AUTOBRIEF = YES
...
#---------------------------------------------------------------------------
# Build related configuration options
#---------------------------------------------------------------------------
EXTRACT_ALL = NO
...
#---------------------------------------------------------------------------
# configuration options related to the input files
#---------------------------------------------------------------------------
INPUT = @CMAKE_CURRENT_SOURCE_DIR@/src/
FILE_PATTERNS =
RECURSIVE = YES
EXCLUDE =
...
#---------------------------------------------------------------------------
# configuration options related to the HTML output
#---------------------------------------------------------------------------
GENERATE_HTML = YES
HTML_OUTPUT = html
HTML_FILE_EXTENSION = .html
HTML_HEADER =
HTML_FOOTER =
...
...
GENERATE_LATEX = NO
#---------------------------------------------------------------------------
# Configuration::additions related to the search engine
#---------------------------------------------------------------------------
SEARCHENGINE = YES

View File

@ -1,4 +1,2 @@
add_executable(07_STD_MP MP/grundgeruest.cpp MP/BinaryOctet.cpp MP/BinaryOctet.h)
add_executable(07_STD_Testat Testat/grundgeruest.cpp Testat/BinaryOctet.h Testat/BinaryOctet.cpp)
target_link_libraries(07_STD_Testat gtest_main)
add_test(NAME 07_STD_Testat COMMAND InsertBinaryOctet)
add_executable(07_STD_Testat Testat/grundgeruest.cpp Testat/BinaryOctet.h Testat/BinaryOctet.cpp)

View File

@ -5,7 +5,6 @@
#include <random>
#include <vector>
#include <algorithm>
#include <gtest/gtest.h>
#include "BinaryOctet.h"
// for println();
@ -61,7 +60,3 @@ int main() {
std::cout << binaryOctet << std::endl;
}
}
TEST(STD_Testat, InsertBinaryOctet) {
ASSERT_EQ(42, 42);
}

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/Logger.cpp Testat/Logger.hpp 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.cpp Testat/CashFactory.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.cpp Testat/CashFactory.h Testat/Person.cpp Testat/Person.h)

View File

@ -1,14 +1,16 @@
#include "BankAccount.h"
BankAccount::BankAccount(std::string name) : name(std::move(name)) {}
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;
}
bool BankAccount::operator<(BankAccount bankAccount) {
return money.getValue() < bankAccount.money.getValue();
bool BankAccount::operator<(BankAccount * bankAccount) {
return money.getValue() < bankAccount->money.getValue();
}
std::string BankAccount::getName() {
@ -19,5 +21,9 @@ Money BankAccount::getMoney() {
return money;
}
Person *BankAccount::getOwner() {
return owner;
}

View File

@ -5,16 +5,19 @@
#include "Money.h"
#include "Cash.h"
#include "Person.h"
class BankAccount {
private:
std::string name;
Person * owner;
Money money;
public:
explicit BankAccount(std::string name);
BankAccount* operator+(Money* money);
bool operator<(BankAccount bankAccount);
explicit BankAccount(Person * owner, std::string name);
BankAccount* operator+(Money * money);
bool operator<(BankAccount * bankAccount);
std::string getName();
Person * getOwner();
Money getMoney();
};

View File

@ -13,4 +13,4 @@ public:
};
#endif
#endif

View File

@ -8,7 +8,7 @@ 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;
return nullptr;
}else {
std::string serial = randomString(15);
while(std::find(usedSerials.begin(), usedSerials.end(), serial) != usedSerials.end()) {
@ -19,6 +19,13 @@ Bill * CashFactory::printBill(int value, Currency currency) {
}
}
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[] =

View File

@ -4,13 +4,15 @@
#include <string>
#include <vector>
#include "Bill.h"
#include "Coin.h"
class CashFactory {
private:
std::vector<std::string> usedSerials;
std::string randomString(size_t length);
public:
Bill* printBill(int value, Currency currency);
Bill * printBill(int value, Currency currency);
Coin * printCoin(int value, Currency currency);
};

View File

@ -2,4 +2,4 @@
Coin::Coin(int value, Currency currency) : Cash(value, currency) {
}
}

View File

@ -10,5 +10,4 @@ public:
Coin(int value, Currency currency);
};
#endif

View File

@ -1,15 +0,0 @@
// file Logger.cpp
#include "Logger.hpp"
Logger* Logger::_theInstance = nullptr;
Logger* Logger::getInstance(){
if (!_theInstance){
_theInstance = new StdoutLogger();
}
return _theInstance;
}
void StdoutLogger::log(std::string message, std::string file, int line){
std::cerr << message << " in " << file << " line " << line << std::endl;
}

View File

@ -1,19 +0,0 @@
// file Logger.hpp
#ifndef Logger_hpp
#define Logger_hpp
#include <iostream>
class Logger {
static Logger* _theInstance;
public:
virtual void log(std::string message, std::string file="", int line=0) = 0;
static Logger* getInstance();
};
class StdoutLogger: public Logger {
public:
void log(std::string message, std::string file="", int line=0);
};
#endif

View File

@ -18,28 +18,22 @@ Money &Money::operator=(Money *money) {
Money Money::operator+(Money &a) {
if(currency == a.currency) {
return Money();
return Money(value + a.value);
}else return Money(-1, Currency(CurrencyValue::USD));
}
Money Money::operator-(Money &a) {
return Money();
if(currency == a.currency) {
return Money(value - a.value);
}else return Money(-1, Currency(CurrencyValue::USD));
}
const Money Money::operator++(int) {
return Money();
return Money(value++);
}
const Money Money::operator--(int) {
return Money();
}
bool Money::operator==(Money &a) {
return false;
}
bool Money::operator!=(Money &a) {
return false;
return Money(value--);
}
Currency Money::getCurrency() {

View File

@ -15,8 +15,6 @@ public:
Money operator-(Money &a);
const Money operator++(int);
const Money operator--(int);
bool operator==(Money &a);
bool operator!=(Money &a);
int getValue();
Currency getCurrency();

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,16 @@
#ifndef C_C_PERSON_H
#define C_C_PERSON_H
#include <string>
class Person {
private:
std::string name;
public:
Person(std::string name);
std::string getName();
};
#endif

View File

@ -38,8 +38,13 @@ std::ostream &operator<<(std::ostream &os, Money money) {
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.getName() << ": " << bankAccount.getMoney();
os << bankAccount.getOwner() << " " << bankAccount.getName() << ": " << bankAccount.getMoney();
return os;
}
@ -48,10 +53,13 @@ std::ostream &operator<<(std::ostream &os, BankAccount bankAccount) {
int main(int argc, char **argv) {
std::set<BankAccount*> accounts;
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"));
Person * max = new Person("Max Müller");
Person * marius = new Person("Marius Müller");
accounts.insert(new BankAccount(max, "Konto 1"));
accounts.insert(new BankAccount(max, "Konto 2"));
accounts.insert(new BankAccount(marius, "Konto 1"));
accounts.insert(new BankAccount(marius, "Konto 2"));
auto * cashFactory = new CashFactory();
@ -60,8 +68,13 @@ int main(int argc, char **argv) {
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;
Coin * coin = cashFactory->printCoin(rnd, CurrencyValue::USD);
if(bill != nullptr) {
*bankAccount + bill;
}
if(coin != nullptr) {
*bankAccount + coin;
}
}
std::cout << *bankAccount << std::endl;
});