08_PTRN: weitere Arbeit am Grundgerüst

This commit is contained in:
Johannes Theiner 2018-12-04 10:50:54 +01:00
parent 534006088d
commit 7e43e93d8c
18 changed files with 99 additions and 41 deletions

1
.gitignore vendored
View File

@ -14,4 +14,5 @@ doc/
googletest-build googletest-build
googletest-download googletest-download
googletest-src googletest-src
lib

View File

@ -19,6 +19,7 @@ if(DOXYGEN_FOUND)
) )
endif(DOXYGEN_FOUND) endif(DOXYGEN_FOUND)
#[[
# Download and unpack googletest at configure time # Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt) configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
@ -55,6 +56,7 @@ endif()
#add_executable(example example.cpp) #add_executable(example example.cpp)
#target_link_libraries(example gtest_main) #target_link_libraries(example gtest_main)
#add_test(NAME example_test COMMAND example) #add_test(NAME example_test COMMAND example)
]]
add_subdirectory(src) add_subdirectory(src)

View File

@ -3,7 +3,7 @@
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
PROJECT_NAME = C/C++ PROJECT_NAME = C/C++
PROJECT_NUMBER = PROJECT_NUMBER =
OUTPUT_DIRECTORY = @CMAKE_CURRENT_SOURCE_DIR@/doc OUTPUT_DIRECTORY = /home/joethei/workspaces/C_CPP/doc
CREATE_SUBDIRS = NO CREATE_SUBDIRS = NO
OUTPUT_LANGUAGE = Englisch OUTPUT_LANGUAGE = Englisch
... ...
@ -17,7 +17,7 @@ EXTRACT_ALL = NO
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
# configuration options related to the input files # configuration options related to the input files
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
INPUT = @CMAKE_CURRENT_SOURCE_DIR@/src/ INPUT = /home/joethei/workspaces/C_CPP/src/
FILE_PATTERNS = FILE_PATTERNS =
RECURSIVE = YES RECURSIVE = YES
EXCLUDE = EXCLUDE =

View File

@ -1,4 +1,4 @@
add_executable(07_STD_MP MP/grundgeruest.cpp MP/BinaryOctet.cpp MP/BinaryOctet.h) 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) add_executable(07_STD_Testat Testat/grundgeruest.cpp Testat/BinaryOctet.h Testat/BinaryOctet.cpp)
target_link_libraries(07_STD_Testat gtest_main) #target_link_libraries(07_STD_Testat gtest_main)
add_test(NAME 07_STD_Testat COMMAND InsertBinaryOctet) #add_test(NAME 07_STD_Testat COMMAND InsertBinaryOctet)

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_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) 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)

View File

@ -2,8 +2,9 @@
BankAccount::BankAccount(std::string name) : name(std::move(name)) {} BankAccount::BankAccount(std::string name) : name(std::move(name)) {}
void BankAccount::add(Cash cash) { Money* BankAccount::operator+(Money* money) {
money = Money(money.getValue() + cash.getValue(), cash.getCurrency()); Money *result = new Money(money->getValue() + getMoney().getValue());
return result;
} }
bool BankAccount::operator<(BankAccount bankAccount) { bool BankAccount::operator<(BankAccount bankAccount) {

View File

@ -12,7 +12,7 @@ private:
Money money; Money money;
public: public:
explicit BankAccount(std::string name); explicit BankAccount(std::string name);
void add(Cash cash); Money* operator+(Money* money);
bool operator<(BankAccount bankAccount); bool operator<(BankAccount bankAccount);
std::string getName(); std::string getName();
Money getMoney(); Money getMoney();

View File

@ -0,0 +1,11 @@
#include <utility>
#include "Bill.h"
Bill::Bill(int value, Currency currency, std::string serial) : Cash(value, currency), serial(std::move(serial)){
}
std::string Bill::getSerial() {
return serial;
}

16
src/08_PTRN/Testat/Bill.h Normal file
View File

@ -0,0 +1,16 @@
#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();
};
#endif

View File

@ -4,10 +4,6 @@
#include "Cash.h" #include "Cash.h"
Cash::Cash(double value, Currency currency, std::string serial) : Money(value, currency), serial(std::move(serial)) { Cash::Cash(int value, Currency currency) : Money(value, currency) {
} }
std::string Cash::getSerial() {
return serial;
}

View File

@ -1,21 +1,13 @@
//
// Created by JThei on 02.12.2018.
//
#ifndef C_C_CASH_H #ifndef C_C_CASH_H
#define C_C_CASH_H #define C_C_CASH_H
#include <string> #include <string>
#include "Money.h" #include "Money.h"
class Cash : public Money{ class Cash : public Money{
private: private:
std::string serial;
public: public:
Cash(double value, Currency currency, std::string serial); Cash(int value, Currency currency);
std::string getSerial();
}; };
#endif
#endif //C_C_CASH_H

View File

@ -0,0 +1 @@
#include "CashFactory.h"

View File

@ -0,0 +1,9 @@
#ifndef C_C_CASHFACTORY_H
#define C_C_CASHFACTORY_H
class CashFactory {
};
#endif

View File

@ -0,0 +1,5 @@
#include "Coin.h"
Coin::Coin(int value, Currency currency) : Cash(value, currency) {
}

14
src/08_PTRN/Testat/Coin.h Normal file
View File

@ -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);
};
#endif

View File

@ -1,6 +1,6 @@
#include "Money.h" #include "Money.h"
Money::Money(double value, Currency currency) : value(value), currency(currency) { Money::Money(int value, Currency currency) : value(value), currency(currency) {
} }
@ -10,6 +10,12 @@ Money& Money::operator=(Money money) {
return *this; return *this;
} }
Money &Money::operator=(Money *money) {
value = money->getValue();
currency = money->getCurrency();
return *this;
}
Money Money::operator+(Money &a) { Money Money::operator+(Money &a) {
if(currency == a.currency) { if(currency == a.currency) {
return Money(); return Money();
@ -40,6 +46,6 @@ Currency Money::getCurrency() {
return currency; return currency;
} }
double Money::getValue() { int Money::getValue() {
return value; return value;
} }

View File

@ -1,16 +1,16 @@
#ifndef C_C_MONEY_H #ifndef C_C_MONEY_H
#define C_C_MONEY_H #define C_C_MONEY_H
#include "Currency.h" #include "Currency.h"
class Money { class Money {
private: private:
double value; int value;
Currency currency; Currency currency;
public: public:
explicit Money(double value = 0, Currency currency = CurrencyValue::USD); explicit Money(int value = 0, Currency currency = CurrencyValue::USD);
Money& operator=(Money money); Money& operator=(Money money);
Money& operator=(Money * money);
Money operator+(Money &a); Money operator+(Money &a);
Money operator-(Money &a); Money operator-(Money &a);
const Money operator++(int); const Money operator++(int);
@ -18,9 +18,8 @@ public:
bool operator==(Money &a); bool operator==(Money &a);
bool operator!=(Money &a); bool operator!=(Money &a);
double getValue(); int getValue();
Currency getCurrency(); Currency getCurrency();
}; };
#endif #endif

View File

@ -5,9 +5,10 @@
#include <random> #include <random>
#include "Currency.h" #include "Currency.h"
#include "BankAccount.h" #include "BankAccount.h"
#include "Bill.h"
std::default_random_engine generator; std::default_random_engine generator;
std::uniform_real_distribution<double> distribution(-100, 500); std::uniform_int_distribution<int> distribution(-100, 500);
std::string random_string(size_t length) { std::string random_string(size_t length) {
auto randchar = []() -> char { auto randchar = []() -> char {
@ -35,8 +36,13 @@ std::ostream &operator<<(std::ostream &os, Currency currency) {
return os; 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) { std::ostream &operator<<(std::ostream &os, Cash cash) {
os << cash.getValue() << " " << cash.getCurrency() << ": " << cash.getSerial(); os << cash.getValue() << " " << cash.getCurrency();
return os; return os;
} }
@ -50,8 +56,8 @@ std::ostream &operator<<(std::ostream &os, BankAccount bankAccount) {
return os; return os;
} }
Cash cashPrinter(double value, Currency currency) { Bill billPrinter(int value, Currency currency) {
return Cash(value, currency, random_string(5)); return Bill(value, currency, random_string(5));
} }
@ -63,12 +69,11 @@ int main(int argc, char **argv) {
accounts.insert(new BankAccount("Marius")); accounts.insert(new BankAccount("Marius"));
accounts.insert(new BankAccount("Test")); accounts.insert(new BankAccount("Test"));
std::for_each(accounts.begin(), accounts.end(), [] (BankAccount* bankAccount){ std::for_each(accounts.begin(), accounts.end(), [] (BankAccount* bankAccount){
double rnd = distribution(generator); int rnd = distribution(generator);
Cash cash = cashPrinter(rnd, CurrencyValue::USD); Bill bill = billPrinter(rnd, CurrencyValue::USD);
std::cout << cash << std::endl; std::cout << bill << std::endl;
bankAccount->add(cash); //Money* res = bankAccount + &bill;
std::cout << *bankAccount << std::endl; //std::cout << *res << std::endl;
}); });
} }