From dd69b74ea2f0cf0023d80de8d35d2d7fe2d08b86 Mon Sep 17 00:00:00 2001 From: joethei Date: Fri, 30 Nov 2018 14:35:28 +0100 Subject: [PATCH] =?UTF-8?q?08=5FPTRN:=20Anfang=20Grundger=C3=BCst?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/08_PTRN/CMakeLists.txt | 1 + src/08_PTRN/MP/BankAccount.cpp | 5 +++++ src/08_PTRN/MP/BankAccount.h | 19 ++++++++++++++++++ src/08_PTRN/MP/Currency.cpp | 9 +++++++++ src/08_PTRN/MP/Currency.h | 16 ++++++++++++++++ src/08_PTRN/MP/Money.cpp | 35 ++++++++++++++++++++++++++++++++++ src/08_PTRN/MP/Money.h | 28 +++++++++++++++++++++++++++ src/08_PTRN/MP/Storage.cpp | 1 + src/08_PTRN/MP/Storage.h | 17 +++++++++++++++++ src/08_PTRN/MP/maín.cpp | 19 ++++++++++++++++++ 10 files changed, 150 insertions(+) create mode 100644 src/08_PTRN/MP/BankAccount.cpp create mode 100644 src/08_PTRN/MP/BankAccount.h create mode 100644 src/08_PTRN/MP/Currency.cpp create mode 100644 src/08_PTRN/MP/Currency.h create mode 100644 src/08_PTRN/MP/Money.cpp create mode 100644 src/08_PTRN/MP/Money.h create mode 100644 src/08_PTRN/MP/Storage.cpp create mode 100644 src/08_PTRN/MP/Storage.h create mode 100644 src/08_PTRN/MP/maín.cpp diff --git a/src/08_PTRN/CMakeLists.txt b/src/08_PTRN/CMakeLists.txt index e69de29..beb38f2 100644 --- a/src/08_PTRN/CMakeLists.txt +++ b/src/08_PTRN/CMakeLists.txt @@ -0,0 +1 @@ +add_executable(08_PTRN_MP MP/Money.cpp MP/Money.h MP/Currency.cpp MP/Currency.h MP/maín.cpp MP/BankAccount.cpp MP/BankAccount.h MP/Storage.cpp MP/Storage.h) \ No newline at end of file diff --git a/src/08_PTRN/MP/BankAccount.cpp b/src/08_PTRN/MP/BankAccount.cpp new file mode 100644 index 0000000..5280c37 --- /dev/null +++ b/src/08_PTRN/MP/BankAccount.cpp @@ -0,0 +1,5 @@ +#include + +#include "BankAccount.h" + +BankAccount::BankAccount(std::string owner, Money money) : owner(std::move(owner)), money(std::move(money)){} diff --git a/src/08_PTRN/MP/BankAccount.h b/src/08_PTRN/MP/BankAccount.h new file mode 100644 index 0000000..e2da0d4 --- /dev/null +++ b/src/08_PTRN/MP/BankAccount.h @@ -0,0 +1,19 @@ +#ifndef C_C_BANKACCOUNT_H +#define C_C_BANKACCOUNT_H + + +#include + +#include "Money.h" + +class BankAccount { +private: + std::string owner; + Money money; + +public: + BankAccount(std::string owner, Money money); +}; + + +#endif diff --git a/src/08_PTRN/MP/Currency.cpp b/src/08_PTRN/MP/Currency.cpp new file mode 100644 index 0000000..84851e9 --- /dev/null +++ b/src/08_PTRN/MP/Currency.cpp @@ -0,0 +1,9 @@ +#include + +#include "Currency.h" + +Currency::Currency(std::string name, float rate) : name(std::move(name)), rate(rate) {} + +bool Currency::operator==(Currency ¤cy) { + return name == currency.name; +} diff --git a/src/08_PTRN/MP/Currency.h b/src/08_PTRN/MP/Currency.h new file mode 100644 index 0000000..24340b8 --- /dev/null +++ b/src/08_PTRN/MP/Currency.h @@ -0,0 +1,16 @@ +#ifndef C_C_CURRENCY_H +#define C_C_CURRENCY_H + + +#include + +class Currency { +public: + std::string name; + float rate; + Currency(std::string name, float rate); + bool operator==(Currency ¤cy); +}; + + +#endif diff --git a/src/08_PTRN/MP/Money.cpp b/src/08_PTRN/MP/Money.cpp new file mode 100644 index 0000000..980cc3c --- /dev/null +++ b/src/08_PTRN/MP/Money.cpp @@ -0,0 +1,35 @@ +#include + +#include "Money.h" + +Money::Money(float value, Currency currency) : value(value), currency(std::move(currency)) {} + +Money Money::operator+(Money money) { + return Money((this->value * this->currency.rate) + (money.value * money.currency.rate), this->currency); +} + +Money Money::operator-(Money money) { + return Money(this->value - money.value, this->currency); +} + +Money Money::operator*(Money money) { + return Money(this->value * money.value); +} + +Money Money::operator/(Money money) { + return Money(this->value / money.value); +} + +const Money Money::operator++(int) { + this->value++; + return *this; +} + +const Money Money::operator--(int) { + this->value--; + return *this; +} + +bool Money::operator==(Money &money) { + return this->value == money.value && this ->currency == money.currency; +} diff --git a/src/08_PTRN/MP/Money.h b/src/08_PTRN/MP/Money.h new file mode 100644 index 0000000..96559a8 --- /dev/null +++ b/src/08_PTRN/MP/Money.h @@ -0,0 +1,28 @@ +#ifndef C_C_MONEY_H +#define C_C_MONEY_H + +#include "Currency.h" +#include "Storage.h" + +/** + * + * + * @author Johannes Theiner + * @since 0.0.1 + */ +class Money { +private: + float value; + Currency currency; +public: + explicit Money(float value = 0, Currency currency = Storage().currencies["USD"]); + Money operator+(Money money); + Money operator-(Money money); + Money operator*(Money money); + Money operator/(Money money); + const Money operator++(int); + const Money operator--(int); + bool operator==(Money &money); +}; + +#endif diff --git a/src/08_PTRN/MP/Storage.cpp b/src/08_PTRN/MP/Storage.cpp new file mode 100644 index 0000000..eb94cff --- /dev/null +++ b/src/08_PTRN/MP/Storage.cpp @@ -0,0 +1 @@ +#include "Storage.h" diff --git a/src/08_PTRN/MP/Storage.h b/src/08_PTRN/MP/Storage.h new file mode 100644 index 0000000..fb56c76 --- /dev/null +++ b/src/08_PTRN/MP/Storage.h @@ -0,0 +1,17 @@ +#ifndef C_C_STORAGE_H +#define C_C_STORAGE_H + + +#include +#include +#include "Currency.h" +#include "BankAccount.h" + +class Storage { +public: + std::unordered_set currencies; + std::unordered_map accounts; +}; + + +#endif diff --git a/src/08_PTRN/MP/maín.cpp b/src/08_PTRN/MP/maín.cpp new file mode 100644 index 0000000..3170c5f --- /dev/null +++ b/src/08_PTRN/MP/maín.cpp @@ -0,0 +1,19 @@ +#include +#include + +#include "Currency.h" +#include "BankAccount.h" +#include "Storage.h" + +Storage storage; + +int main(int argc, char **argv) { + storage.currencies.insert("USD", Currency("US Dollar", 1)); + storage.currencies.insert("EUR", Currency("Euro", 0.879699)); + storage.currencies.insert("GBP", Currency("British Pound", 0.783667)); + storage.currencies.insert("AUD", Currency("Australian Dollar", 1.36962)); + storage.currencies.insert("CAD", Currency("Canadian Dollar", 1.33023)); + + + storage.accounts.insert(BankAccount("Max Maier", Money(50, storage.currencies["EUR"]))); +} \ No newline at end of file