10_PITF: einen Fehler behoben, neuen erhalten

This commit is contained in:
Johannes Theiner 2019-01-02 12:25:29 +01:00
parent 3dd56c45a9
commit dd71612348
10 changed files with 43 additions and 12 deletions

View File

@ -21,10 +21,8 @@ BankAccount::BankAccount(BankAccount &bankAccount) : name(bankAccount.name) {
} }
std::shared_ptr<BankAccount> BankAccount::operator+(std::shared_ptr<Bill> money) { std::shared_ptr<BankAccount> BankAccount::operator+(std::shared_ptr<Bill> money) {
std::shared_ptr<Money> ptr = std::make_shared<Money>(new Money(money->getValue() + this->money->getValue(), money->getCurrency()));
std::cout << "Test" << std::endl;
BankAccount::money = ptr; this->money->setValue(this->money->getValue() + money->getValue());
return std::make_shared<BankAccount>(*this); return std::make_shared<BankAccount>(*this);
} }
@ -37,12 +35,13 @@ std::shared_ptr<BankAccount> BankAccount::operator+(std::shared_ptr<Coin> money)
return std::make_shared<BankAccount>(*this); return std::make_shared<BankAccount>(*this);
} }
std::shared_ptr<Money> BankAccount::operator-(int value) { std::shared_ptr<Coin> BankAccount::operator-(int value) {
auto * cashFactory = new CashFactory(); auto * cashFactory = new CashFactory();
return cashFactory->printCoin(value, CurrencyValue::USD); return cashFactory->printCoin(value, CurrencyValue::USD);
} }
bool BankAccount::operator<(std::shared_ptr<BankAccount> bankAccount) { bool BankAccount::operator<(std::shared_ptr<BankAccount> bankAccount) {
return this->money->getValue() < bankAccount->money->getValue(); return this->money->getValue() < bankAccount->money->getValue();
} }

View File

@ -17,7 +17,7 @@ public:
BankAccount(BankAccount& bankAccount); BankAccount(BankAccount& bankAccount);
std::shared_ptr<BankAccount> operator+(std::shared_ptr<Coin> money); std::shared_ptr<BankAccount> operator+(std::shared_ptr<Coin> money);
std::shared_ptr<BankAccount> operator+(std::shared_ptr<Bill> money); std::shared_ptr<BankAccount> operator+(std::shared_ptr<Bill> money);
std::shared_ptr<Money> operator-(int value); std::shared_ptr<Coin> operator-(int value);
bool operator<(std::shared_ptr<BankAccount> bankAccount); bool operator<(std::shared_ptr<BankAccount> bankAccount);
std::string getName(); std::string getName();
std::shared_ptr<Person> getOwner(); std::shared_ptr<Person> getOwner();

View File

@ -10,6 +10,10 @@ Bill::Bill(Bill *bill) : Cash(bill->getValue(), bill->getCurrency()), serial(bil
} }
Currency Bill::getCurrency() {
return Money::getCurrency();
}
int Bill::getValue() { int Bill::getValue() {
return Money::getValue() * 10; return Money::getValue() * 10;
} }

View File

@ -11,7 +11,8 @@ public:
Bill(int value, Currency currency, std::string serial); Bill(int value, Currency currency, std::string serial);
explicit Bill(Bill *bill); explicit Bill(Bill *bill);
std::string getSerial(); std::string getSerial();
int getValue() override; Currency getCurrency();
int getValue();
}; };

View File

@ -1,7 +1,6 @@
#ifndef C_C_CASH_H #ifndef C_C_CASH_H
#define C_C_CASH_H #define C_C_CASH_H
#include <string>
#include "Money.h" #include "Money.h"
#include "HeapObject.h" #include "HeapObject.h"

View File

@ -8,6 +8,10 @@ Coin::Coin(Coin *coin) : Cash(coin->getValue(), coin->getCurrency()) {
} }
Currency Coin::getCurrency() {
return Money::getCurrency();
}
int Coin::getValue() { int Coin::getValue() {
return Money::getValue(); return Money::getValue();
} }

View File

@ -9,7 +9,8 @@ private:
public: public:
Coin(int value, Currency currency); Coin(int value, Currency currency);
explicit Coin(Coin *coin); explicit Coin(Coin *coin);
int getValue() override; Currency getCurrency();
int getValue();
}; };
#endif #endif

View File

@ -20,6 +20,18 @@ std::shared_ptr<Money> Money::operator+(Money &a) {
}else return std::make_shared<Money>(Money(-1, Currency(CurrencyValue::USD))); }else return std::make_shared<Money>(Money(-1, Currency(CurrencyValue::USD)));
} }
std::shared_ptr<Money> Money::operator+(Bill &a) {
if(currency == a.getCurrency()) {
return std::make_shared<Money>(Money(value + a.getValue(), CurrencyValue::USD));
}else return std::make_shared<Money>(Money(-1, Currency(CurrencyValue::USD)));
}
std::shared_ptr<Money> Money::operator+(Coin &a) {
if(currency == a.getCurrency()) {
return std::make_shared<Money>(Money(value + a.getValue(), CurrencyValue::USD));
}else return std::make_shared<Money>(Money(-1, Currency(CurrencyValue::USD)));
}
std::shared_ptr<Money> Money::operator-(Money &a) { std::shared_ptr<Money> Money::operator-(Money &a) {
if(currency == a.currency) { if(currency == a.currency) {
return std::make_unique<Money>(Money(value - a.value, CurrencyValue::USD)); return std::make_unique<Money>(Money(value - a.value, CurrencyValue::USD));
@ -40,6 +52,10 @@ int Money::getValue() {
return value; return value;
} }
void Money::setValue(int value) {
this->value = value;
}
Currency Money::getCurrency() { Currency Money::getCurrency() {
return currency; return currency;
} }

View File

@ -4,6 +4,8 @@
#include <memory> #include <memory>
#include "Currency.h" #include "Currency.h"
#include "Bill.h"
#include "Coin.h"
class Money { class Money {
private: private:
@ -14,12 +16,15 @@ public:
explicit Money(Money* money); explicit Money(Money* money);
std::shared_ptr<Money> operator=(Money money); std::shared_ptr<Money> operator=(Money money);
std::shared_ptr<Money> operator+(Money &a); std::shared_ptr<Money> operator+(Money &a);
std::shared_ptr<Money> operator+(Bill &a);
std::shared_ptr<Money> operator+(Coin &a);
std::shared_ptr<Money> operator-(Money &a); std::shared_ptr<Money> operator-(Money &a);
const std::shared_ptr<Money> operator++(int); const std::shared_ptr<Money> operator++(int);
const std::shared_ptr<Money> operator--(int); const std::shared_ptr<Money> operator--(int);
virtual int getValue(); virtual int getValue();
Currency getCurrency(); virtual void setValue(int value);
virtual Currency getCurrency();
}; };
#endif #endif

View File

@ -29,7 +29,7 @@ std::ostream &operator<<(std::ostream &os, std::shared_ptr<Bill> const& bill) {
return os; return os;
} }
std::ostream &operator<<(std::ostream &os, std::shared_ptr<Cash> const& cash) { std::ostream &operator<<(std::ostream &os, std::shared_ptr<Coin> const& cash) {
os << cash->getValue() << " " << cash->getCurrency(); os << cash->getValue() << " " << cash->getCurrency();
return os; return os;
} }
@ -56,6 +56,8 @@ CashFactory *cashFactory;
void setup() { void setup() {
//FIXME: das hier könnte das Problem sein //FIXME: das hier könnte das Problem sein
// http://www.cplusplus.com/forum/beginner/74320/ // http://www.cplusplus.com/forum/beginner/74320/
// Fehler könnte so gefunden werden:
// https://stackoverflow.com/a/6685693
std::shared_ptr<Bank> bank1 = std::make_shared<Bank>(new Bank("Bank 1")); std::shared_ptr<Bank> bank1 = std::make_shared<Bank>(new Bank("Bank 1"));
std::shared_ptr<Bank> bank2 = std::make_shared<Bank>(new Bank("Bank 2")); std::shared_ptr<Bank> bank2 = std::make_shared<Bank>(new Bank("Bank 2"));
@ -102,9 +104,9 @@ void simulate() {
bankAccount->operator+(bill); bankAccount->operator+(bill);
} }
if (coin != nullptr) { if (coin != nullptr) {
bankAccount->operator+(coin); //bankAccount->operator+(coin);
} }
std::shared_ptr<Money> fee = bankAccount->operator-(10); //std::shared_ptr<Money> fee = bankAccount->operator-(10);
} }
std::cout << "Test2" << std::endl; std::cout << "Test2" << std::endl;
std::cout << bankAccount.get() << std::endl; std::cout << bankAccount.get() << std::endl;