08_PTNR: Weiter am Grundgerüst
This commit is contained in:
parent
d0cd6d6a10
commit
534006088d
|
@ -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)
|
||||
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)
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -1,13 +1,10 @@
|
|||
//
|
||||
// Created by JThei on 01.12.2018.
|
||||
//
|
||||
|
||||
#ifndef C_C_BANKACCOUNTS_H
|
||||
#define C_C_BANKACCOUNTS_H
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#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
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
#include <utility>
|
||||
#include <algorithm>
|
||||
|
||||
#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;
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
//
|
||||
// Created by JThei on 02.12.2018.
|
||||
//
|
||||
|
||||
#ifndef C_C_CASH_H
|
||||
#define C_C_CASH_H
|
||||
|
||||
|
||||
#include <string>
|
||||
#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
|
|
@ -0,0 +1,19 @@
|
|||
#include <string>
|
||||
#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;
|
||||
}
|
|
@ -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
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,15 +1,74 @@
|
|||
|
||||
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
#include <random>
|
||||
#include "Currency.h"
|
||||
#include "BankAccount.h"
|
||||
|
||||
std::default_random_engine generator;
|
||||
std::uniform_real_distribution<double> 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<BankAccount> accounts;
|
||||
//
|
||||
// accounts.insert(BankAccount("Max"));
|
||||
// accounts.insert(BankAccount("Max"));
|
||||
// accounts.insert(BankAccount("Marius"));
|
||||
// accounts.insert(BankAccount("Test"));
|
||||
std::set<BankAccount*> 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;
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue