08_PTRN: Anfang Grundgerüst
This commit is contained in:
parent
c229d4855b
commit
dd69b74ea2
|
@ -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)
|
|
@ -0,0 +1,5 @@
|
|||
#include <utility>
|
||||
|
||||
#include "BankAccount.h"
|
||||
|
||||
BankAccount::BankAccount(std::string owner, Money money) : owner(std::move(owner)), money(std::move(money)){}
|
|
@ -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
|
|
@ -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 ¤cy) {
|
||||
return name == currency.name;
|
||||
}
|
|
@ -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 ¤cy);
|
||||
};
|
||||
|
||||
|
||||
#endif
|
|
@ -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;
|
||||
}
|
|
@ -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
|
|
@ -0,0 +1 @@
|
|||
#include "Storage.h"
|
|
@ -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
|
|
@ -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"])));
|
||||
}
|
Loading…
Reference in New Issue