08_PTRN: Anfang Grundgerüst

This commit is contained in:
Johannes Theiner 2018-11-30 14:35:28 +01:00
parent c229d4855b
commit dd69b74ea2
10 changed files with 150 additions and 0 deletions

View File

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

View File

@ -0,0 +1,5 @@
#include <utility>
#include "BankAccount.h"
BankAccount::BankAccount(std::string owner, Money money) : owner(std::move(owner)), money(std::move(money)){}

View File

@ -0,0 +1,19 @@
#ifndef C_C_BANKACCOUNT_H
#define C_C_BANKACCOUNT_H
#include <string>
#include "Money.h"
class BankAccount {
private:
std::string owner;
Money money;
public:
BankAccount(std::string owner, Money money);
};
#endif

View File

@ -0,0 +1,9 @@
#include <utility>
#include "Currency.h"
Currency::Currency(std::string name, float rate) : name(std::move(name)), rate(rate) {}
bool Currency::operator==(Currency &currency) {
return name == currency.name;
}

16
src/08_PTRN/MP/Currency.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef C_C_CURRENCY_H
#define C_C_CURRENCY_H
#include <string>
class Currency {
public:
std::string name;
float rate;
Currency(std::string name, float rate);
bool operator==(Currency &currency);
};
#endif

35
src/08_PTRN/MP/Money.cpp Normal file
View File

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

28
src/08_PTRN/MP/Money.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef C_C_MONEY_H
#define C_C_MONEY_H
#include "Currency.h"
#include "Storage.h"
/**
*
*
* @author Johannes Theiner <kontakt@joethei.xyz>
* @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

View File

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

17
src/08_PTRN/MP/Storage.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef C_C_STORAGE_H
#define C_C_STORAGE_H
#include <unordered_set>
#include <unordered_map>
#include "Currency.h"
#include "BankAccount.h"
class Storage {
public:
std::unordered_set<Currency> currencies;
std::unordered_map<std::string, BankAccount> accounts;
};
#endif

19
src/08_PTRN/MP/maín.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <unordered_set>
#include <unordered_map>
#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"])));
}