diff --git a/src/08_PTRN/CMakeLists.txt b/src/08_PTRN/CMakeLists.txt index 20d7803..91e5740 100644 --- a/src/08_PTRN/CMakeLists.txt +++ b/src/08_PTRN/CMakeLists.txt @@ -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) \ No newline at end of file +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) \ No newline at end of file diff --git a/src/08_PTRN/Testat/BankAccount.cpp b/src/08_PTRN/Testat/BankAccount.cpp index af0767b..48d4705 100644 --- a/src/08_PTRN/Testat/BankAccount.cpp +++ b/src/08_PTRN/Testat/BankAccount.cpp @@ -1,17 +1,22 @@ -// -// Created by JThei on 01.12.2018. -// - #include "BankAccount.h" BankAccount::BankAccount(std::string name) : name(std::move(name)) {} -void BankAccount::add(Money _money) { - money = money + _money; +void BankAccount::add(Cash cash) { + money = Money(money.getValue() + cash.getValue(), cash.getCurrency()); } -bool BankAccount::operator<(BankAccount a) { - return false; +bool BankAccount::operator<(BankAccount bankAccount) { + return money.getValue() < bankAccount.money.getValue(); +} + +std::string BankAccount::getName() { + return name; +} + +Money BankAccount::getMoney() { + return money; } + diff --git a/src/08_PTRN/Testat/BankAccount.h b/src/08_PTRN/Testat/BankAccount.h index 33c97ba..e1f288e 100644 --- a/src/08_PTRN/Testat/BankAccount.h +++ b/src/08_PTRN/Testat/BankAccount.h @@ -1,13 +1,10 @@ -// -// Created by JThei on 01.12.2018. -// - #ifndef C_C_BANKACCOUNTS_H #define C_C_BANKACCOUNTS_H #include #include "Money.h" +#include "Cash.h" class BankAccount { private: @@ -15,9 +12,11 @@ private: Money money; public: explicit BankAccount(std::string name); - void add(Money money); - bool operator<(BankAccount a); + void add(Cash cash); + bool operator<(BankAccount bankAccount); + std::string getName(); + Money getMoney(); }; -#endif //C_C_BANKACCOUNTS_H +#endif diff --git a/src/08_PTRN/Testat/Cash.cpp b/src/08_PTRN/Testat/Cash.cpp new file mode 100644 index 0000000..22ebf06 --- /dev/null +++ b/src/08_PTRN/Testat/Cash.cpp @@ -0,0 +1,13 @@ +#include +#include + +#include "Cash.h" + + +Cash::Cash(double value, Currency currency, std::string serial) : Money(value, currency), serial(std::move(serial)) { + +} + +std::string Cash::getSerial() { + return serial; +} diff --git a/src/08_PTRN/Testat/Cash.h b/src/08_PTRN/Testat/Cash.h new file mode 100644 index 0000000..f1bdaf4 --- /dev/null +++ b/src/08_PTRN/Testat/Cash.h @@ -0,0 +1,21 @@ +// +// Created by JThei on 02.12.2018. +// + +#ifndef C_C_CASH_H +#define C_C_CASH_H + + +#include +#include "Money.h" + +class Cash : public Money{ +private: + std::string serial; +public: + Cash(double value, Currency currency, std::string serial); + std::string getSerial(); +}; + + +#endif //C_C_CASH_H diff --git a/src/08_PTRN/Testat/Currency.cpp b/src/08_PTRN/Testat/Currency.cpp new file mode 100644 index 0000000..2c28571 --- /dev/null +++ b/src/08_PTRN/Testat/Currency.cpp @@ -0,0 +1,19 @@ +#include +#include "Currency.h" + +Currency::Currency(CurrencyValue value) : value(value) { + +} + +bool Currency::operator==(Currency ¤cy) { + return value == currency.value; +} + +CurrencyValue Currency::getValue() { + return value; +} + +Currency &Currency::operator=(Currency currency) { + value = currency.value; + return *this; +} diff --git a/src/08_PTRN/Testat/Currency.h b/src/08_PTRN/Testat/Currency.h index 344ab84..69f46ca 100644 --- a/src/08_PTRN/Testat/Currency.h +++ b/src/08_PTRN/Testat/Currency.h @@ -1,17 +1,23 @@ -// -// Created by JThei on 01.12.2018. -// - #ifndef C_C_CURRENCY_H #define C_C_CURRENCY_H -enum class Currency { +enum class CurrencyValue { USD=100, EUR=80, - GPD=75 + GPD=75, +}; + + +class Currency { +private: + CurrencyValue value; +public: + Currency(CurrencyValue value); + bool operator==(Currency ¤cy); + Currency& operator=(Currency currency); + CurrencyValue getValue(); }; - -#endif //C_C_CURRENCY_H +#endif \ No newline at end of file diff --git a/src/08_PTRN/Testat/Money.cpp b/src/08_PTRN/Testat/Money.cpp index 893c3de..b4c3fd4 100644 --- a/src/08_PTRN/Testat/Money.cpp +++ b/src/08_PTRN/Testat/Money.cpp @@ -1,15 +1,19 @@ -// -// Created by JThei on 01.12.2018. -// - #include "Money.h" -Money::Money(int value, Currency currency) : value(value), currency(currency) { +Money::Money(double value, Currency currency) : value(value), currency(currency) { } +Money& Money::operator=(Money money) { + value = money.getValue(); + currency = money.getCurrency(); + return *this; +} + Money Money::operator+(Money &a) { - return Money(); + if(currency == a.currency) { + return Money(); + }else return Money(-1, Currency(CurrencyValue::USD)); } Money Money::operator-(Money &a) { @@ -35,3 +39,7 @@ bool Money::operator!=(Money &a) { Currency Money::getCurrency() { return currency; } + +double Money::getValue() { + return value; +} diff --git a/src/08_PTRN/Testat/Money.h b/src/08_PTRN/Testat/Money.h index 1b9a1de..b0facb3 100644 --- a/src/08_PTRN/Testat/Money.h +++ b/src/08_PTRN/Testat/Money.h @@ -1,7 +1,3 @@ -// -// Created by JThei on 01.12.2018. -// - #ifndef C_C_MONEY_H #define C_C_MONEY_H @@ -10,10 +6,11 @@ class Money { private: - int value; + double value; Currency currency; public: - explicit Money(int value = 0, Currency currency = Currency::USD); + explicit Money(double value = 0, Currency currency = CurrencyValue::USD); + Money& operator=(Money money); Money operator+(Money &a); Money operator-(Money &a); const Money operator++(int); @@ -21,8 +18,9 @@ public: bool operator==(Money &a); bool operator!=(Money &a); + double getValue(); Currency getCurrency(); }; -#endif //C_C_MONEY_H +#endif diff --git a/src/08_PTRN/Testat/main.cpp b/src/08_PTRN/Testat/main.cpp index 3adb79b..05e3afa 100644 --- a/src/08_PTRN/Testat/main.cpp +++ b/src/08_PTRN/Testat/main.cpp @@ -1,15 +1,74 @@ - - #include +#include +#include +#include +#include #include "Currency.h" #include "BankAccount.h" +std::default_random_engine generator; +std::uniform_real_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"; + if(value == CurrencyValue::GPD) return "British Pound"; + return "Say what ?"; +} + +std::ostream &operator<<(std::ostream &os, Currency currency) { + os << currency_to_string(currency.getValue()); + return os; +} + +std::ostream &operator<<(std::ostream &os, Cash cash) { + os << cash.getValue() << " " << cash.getCurrency() << ": " << cash.getSerial(); + return os; +} + +std::ostream &operator<<(std::ostream &os, Money money) { + os << money.getValue() << " " << money.getCurrency(); + return os; +} + +std::ostream &operator<<(std::ostream &os, BankAccount bankAccount) { + os << bankAccount.getName() << ": " << bankAccount.getMoney(); + return os; +} + +Cash cashPrinter(double value, Currency currency) { + return Cash(value, currency, random_string(5)); +} + int main(int argc, char **argv) { -// std::set accounts; -// -// accounts.insert(BankAccount("Max")); -// accounts.insert(BankAccount("Max")); -// accounts.insert(BankAccount("Marius")); -// accounts.insert(BankAccount("Test")); + std::set accounts; + + accounts.insert(new BankAccount("Max")); + accounts.insert(new BankAccount("Max")); + accounts.insert(new BankAccount("Marius")); + accounts.insert(new BankAccount("Test")); + + + std::for_each(accounts.begin(), accounts.end(), [] (BankAccount* bankAccount){ + double rnd = distribution(generator); + Cash cash = cashPrinter(rnd, CurrencyValue::USD); + std::cout << cash << std::endl; + bankAccount->add(cash); + std::cout << *bankAccount << std::endl; + }); } \ No newline at end of file