10_PITF: Anfänge Modifikation

This commit is contained in:
Johannes Theiner 2018-12-26 23:26:23 +01:00
parent aa9ca3c694
commit 0d19570306
14 changed files with 94 additions and 68 deletions

View File

@ -7,11 +7,15 @@ Bank::Bank(std::string name) : name(std::move(name)) {
} }
void Bank::addAccount(BankAccount * account) { void Bank::addAccount(std::unique_ptr<BankAccount> account) {
accounts.insert(account); accounts.insert(account);
} }
std::set<BankAccount *> Bank::getAccounts() { void Bank::addMoney(std::unique_ptr<Money> money) {
this->money.insert(money);
}
std::set<std::unique_ptr<BankAccount>> Bank::getAccounts() {
return accounts; return accounts;
} }
@ -19,7 +23,4 @@ std::string Bank::getName() {
return name; return name;
} }
void Bank::addMoney(Money * money) {
this->money.insert(money);
}

View File

@ -7,13 +7,13 @@
class Bank { class Bank {
private: private:
std::string name; std::string name;
std::set<BankAccount *> accounts; std::set<std::unique_ptr<BankAccount>> accounts;
std::set<Money *> money; std::set<std::unique_ptr<Money>> money;
public: public:
explicit Bank(std::string name); explicit Bank(std::string name);
void addAccount(BankAccount * account); void addAccount(std::unique_ptr<BankAccount> account);
void addMoney(Money * money); void addMoney(std::unique_ptr<Money> money);
std::set<BankAccount *> getAccounts(); std::set<std::unique_ptr<BankAccount>> getAccounts();
std::string getName(); std::string getName();
}; };

View File

@ -1,36 +1,30 @@
#include <utility>
#include <memory>
#include "BankAccount.h" #include "BankAccount.h"
#include "CashFactory.h" #include "CashFactory.h"
BankAccount::BankAccount(Person * owner, std::string name) : name(std::move(name)) { BankAccount::BankAccount(std::shared_ptr<Person> owner, std::string name) : name(std::move(name)), owner(std::move(owner)) {
this->owner = owner;
} }
BankAccount * BankAccount::operator+(Money * money) { std::unique_ptr<BankAccount> BankAccount::operator+(std::unique_ptr<Money> money) {
std::unique_ptr<Money> ptr = std::make_unique<Money>(new Money(money->getValue() + getMoney()->getValue(), money->getCurrency()));
BankAccount::money = new Money(money->getValue() + getMoney().getValue());
return this; BankAccount::money.swap(ptr);
return std::make_unique<BankAccount>(this);
} }
Money * BankAccount::operator-(int value) { std::unique_ptr<Money> 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<(BankAccount * bankAccount) { bool BankAccount::operator<(std::unique_ptr<BankAccount> bankAccount) {
return money.getValue() < bankAccount->money.getValue(); return money->getValue() < bankAccount->getMoney()->getValue();
}
std::string BankAccount::getName() {
return name;
}
Money BankAccount::getMoney() {
return money;
}
Person *BankAccount::getOwner() {
return owner;
} }

View File

@ -2,6 +2,7 @@
#define C_C_BANKACCOUNTS_H #define C_C_BANKACCOUNTS_H
#include <unordered_map> #include <unordered_map>
#include <memory>
#include "Money.h" #include "Money.h"
#include "Cash.h" #include "Cash.h"
@ -10,16 +11,16 @@
class BankAccount { class BankAccount {
private: private:
std::string name; std::string name;
Person * owner; std::shared_ptr<Person> owner;
Money money; std::unique_ptr<Money> money;
public: public:
explicit BankAccount(Person * owner, std::string name); explicit BankAccount(std::shared_ptr<Person> owner, std::string name);
BankAccount* operator+(Money * money); std::unique_ptr<BankAccount> operator+(std::unique_ptr<Money> money);
Money* operator-(int value); std::unique_ptr<Money> operator-(int value);
bool operator<(BankAccount * bankAccount); bool operator<(std::unique_ptr<BankAccount> bankAccount);
std::string getName(); std::string getName();
Person * getOwner(); std::shared_ptr<Person> getOwner();
Money getMoney(); std::unique_ptr<Money> getMoney();
}; };

View File

@ -10,7 +10,7 @@ private:
public: public:
Bill(int value, Currency currency, std::string serial); Bill(int value, Currency currency, std::string serial);
std::string getSerial(); std::string getSerial();
int getValue(); int getValue() override;
}; };

View File

@ -6,7 +6,7 @@ int allowedCoins[8] = {1, 2, 5, 10, 20, 50, 100, 200};
std::vector<std::string> usedSerials; std::vector<std::string> usedSerials;
Bill * CashFactory::printBill(int value, Currency currency) { std::unique_ptr<Bill> CashFactory::printBill(int value, Currency currency) {
auto * iter = std::find(std::begin(allowedBills), std::end(allowedBills), value); auto * iter = std::find(std::begin(allowedBills), std::end(allowedBills), value);
if(iter == std::end(allowedBills)) { if(iter == std::end(allowedBills)) {
return nullptr; return nullptr;
@ -16,15 +16,16 @@ Bill * CashFactory::printBill(int value, Currency currency) {
serial = randomString(15); serial = randomString(15);
} }
usedSerials.push_back(serial); usedSerials.push_back(serial);
return new Bill(value, currency, serial);
return std::make_unique<Bill>(new Bill(value, currency, serial));
} }
} }
Coin * CashFactory::printCoin(int value, Currency currency) { std::unique_ptr<Coin> CashFactory::printCoin(int value, Currency currency) {
auto * iter = std::find(std::begin(allowedCoins), std::end(allowedCoins), value); auto * iter = std::find(std::begin(allowedCoins), std::end(allowedCoins), value);
if(iter == std::end(allowedCoins)) { if(iter == std::end(allowedCoins)) {
return nullptr; return nullptr;
}else return new Coin(value, currency); }else return std::make_unique<Coin>(new Coin(value, currency));
} }
std::string CashFactory::randomString(size_t length) { std::string CashFactory::randomString(size_t length) {

View File

@ -3,6 +3,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory>
#include "Bill.h" #include "Bill.h"
#include "Coin.h" #include "Coin.h"
@ -10,8 +12,8 @@ class CashFactory {
private: private:
std::string randomString(size_t length); std::string randomString(size_t length);
public: public:
Bill * printBill(int value, Currency currency); std::unique_ptr<Bill> printBill(int value, Currency currency);
Coin * printCoin(int value, Currency currency); std::unique_ptr<Coin> printCoin(int value, Currency currency);
}; };
#endif #endif

View File

@ -8,7 +8,7 @@ class Coin : public Cash{
private: private:
public: public:
Coin(int value, Currency currency); Coin(int value, Currency currency);
int getValue(); int getValue() override;
}; };
#endif #endif

View File

@ -4,7 +4,7 @@
Currency::Currency(CurrencyValue value) : value(value) { Currency::Currency(CurrencyValue value) : value(value) {
} }
bool Currency::operator==(Currency &currency) { bool Currency::operator==(Currency currency) {
return value == currency.value; return value == currency.value;
} }

View File

@ -14,7 +14,7 @@ private:
CurrencyValue value; CurrencyValue value;
public: public:
Currency(CurrencyValue value); Currency(CurrencyValue value);
bool operator==(Currency &currency); bool operator==(Currency currency);
Currency& operator=(Currency currency); Currency& operator=(Currency currency);
CurrencyValue getValue(); CurrencyValue getValue();

View File

@ -28,12 +28,14 @@ Money Money::operator-(Money &a) {
}else return Money(-1, Currency(CurrencyValue::USD)); }else return Money(-1, Currency(CurrencyValue::USD));
} }
const Money Money::operator++(int) { const std::unique_ptr<Money> Money::operator++(int) {
return Money(value++); value++;
return std::make_unique<Money>(this);
} }
const Money Money::operator--(int) { const std::unique_ptr<Money> Money::operator--(int) {
return Money(value--); value--;
return std::make_unique<Money>(this);
} }
int Money::getValue() { int Money::getValue() {
@ -43,3 +45,21 @@ int Money::getValue() {
Currency Money::getCurrency() { Currency Money::getCurrency() {
return currency; return currency;
} }
std::unique_ptr<Money> Money::operator+(std::unique_ptr<Money> &a) {
if(currency == a->getCurrency()) {
return std::make_unique<Money>(Money(value + a->getValue()));
}else return std::make_unique<Money>(Money(-1, Currency(CurrencyValue::USD)));
}
std::unique_ptr<Money> Money::operator-(std::unique_ptr<Money> &a) {
if(currency == a->getCurrency()) {
return std::make_unique<Money>(Money(value - a->getValue()));
}else return std::make_unique<Money>(Money(-1, Currency(CurrencyValue::USD)));
}
std::unique_ptr<Money> Money::operator=(std::unique_ptr<Money> money) {
value = money->getValue();
currency = money->getCurrency();
return std::make_unique<Money>(Money(value, currency));
}

View File

@ -1,6 +1,8 @@
#ifndef C_C_MONEY_H #ifndef C_C_MONEY_H
#define C_C_MONEY_H #define C_C_MONEY_H
#include <memory>
#include "Currency.h" #include "Currency.h"
class Money { class Money {
@ -9,12 +11,12 @@ private:
Currency currency; Currency currency;
public: public:
explicit Money(int value = 0, Currency currency = CurrencyValue::USD); explicit Money(int value = 0, Currency currency = CurrencyValue::USD);
Money& operator=(Money money); std::unique_ptr<Money> operator=(Money money);
Money& operator=(Money * money); std::unique_ptr<Money> operator=(std::unique_ptr<Money> money);
Money operator+(Money &a); std::unique_ptr<Money> operator+(std::unique_ptr<Money> &a);
Money operator-(Money &a); std::unique_ptr<Money> operator-(std::unique_ptr<Money> &a);
const Money operator++(int); const std::unique_ptr<Money> operator++(int);
const Money operator--(int); const std::unique_ptr<Money> operator--(int);
virtual int getValue(); virtual int getValue();
Currency getCurrency(); Currency getCurrency();

View File

@ -3,12 +3,14 @@
#include <string> #include <string>
#include <memory>
#include "Money.h" #include "Money.h"
class Person { class Person {
private: private:
std::string name; std::string name;
std::vector<Money *> money; std::vector<std::unique_ptr<Money>> money;
public: public:
explicit Person(std::string name); explicit Person(std::string name);
std::string getName(); std::string getName();

View File

@ -34,8 +34,8 @@ std::ostream &operator<<(std::ostream &os, Cash cash) {
return os; return os;
} }
std::ostream &operator<<(std::ostream &os, Money money) { std::ostream &operator<<(std::ostream &os, std::unique_ptr<Money> money) {
os << money.getValue() << " " << money.getCurrency(); os << money->getValue() << " " << money->getCurrency();
return os; return os;
} }
@ -50,7 +50,7 @@ std::ostream &operator<<(std::ostream &os, BankAccount bankAccount) {
} }
std::set<Bank *> banks; std::set<Bank *> banks;
std::set<Person *> owners; std::set<std::unique_ptr<Person>> owners;
CashFactory *cashFactory; CashFactory *cashFactory;
void setup() { void setup() {
@ -60,9 +60,9 @@ void setup() {
Bank *bank2 = new Bank("Bank 2"); Bank *bank2 = new Bank("Bank 2");
Bank *bank3 = new Bank("Bank 3"); Bank *bank3 = new Bank("Bank 3");
Person *max = new Person("Max Müller"); std::unique_ptr<Person> max = std::make_unique<Person>(new Person("Max Müller"));
Person *marius = new Person("Marius Müller"); std::unique_ptr<Person> marius = std::make_unique<Person>(new Person("Marius Müller"));
Person *alex = new Person("Alex Maier"); std::unique_ptr<Person> alex = std::make_unique<Person>(new Person("Alex Maier"));
owners.insert(max); owners.insert(max);
owners.insert(marius); owners.insert(marius);
@ -91,10 +91,10 @@ void simulate() {
std::cout << std::endl; std::cout << std::endl;
std::cout << "======================== " << bank->getName() << " ========================" << std::endl; std::cout << "======================== " << bank->getName() << " ========================" << std::endl;
std::cout << std::endl; std::cout << std::endl;
for (BankAccount *bankAccount : bank->getAccounts()) { for (std::unique_ptr<BankAccount> bankAccount : bank->getAccounts()) {
for (int i = 0; i < 10000; ++i) { for (int i = 0; i < 10000; ++i) {
int rnd = distribution(generator); int rnd = distribution(generator);
Bill *bill = cashFactory->printBill(rnd, CurrencyValue::USD); std::unique_ptr<Bill> bill = cashFactory->printBill(rnd, CurrencyValue::USD);
Coin *coin = cashFactory->printCoin(rnd, CurrencyValue::USD); Coin *coin = cashFactory->printCoin(rnd, CurrencyValue::USD);
if (bill != nullptr) { if (bill != nullptr) {
*bankAccount + bill; *bankAccount + bill;
@ -124,8 +124,11 @@ void tearDown() {
int main(int argc, char **argv) { int main(int argc, char **argv) {
std::cout << "+++++++++++++++++++++++++ " << "setup()" << " +++++++++++++++++++++++++" << std::endl;
setup(); setup();
std::cout << "+++++++++++++++++++++++++ " << "simulate()" << " +++++++++++++++++++++++++" << std::endl;
simulate(); simulate();
std::cout << "+++++++++++++++++++++++++ " << "tearDown()" << " +++++++++++++++++++++++++" << std::endl;
tearDown(); tearDown();
HeapObject::assertionsHold(); HeapObject::assertionsHold();