10_PITF: Anfänge Modifikation
This commit is contained in:
parent
aa9ca3c694
commit
0d19570306
@ -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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -19,7 +23,4 @@ std::string Bank::getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
void Bank::addMoney(Money * money) {
|
||||
this->money.insert(money);
|
||||
}
|
||||
|
||||
|
@ -7,13 +7,13 @@
|
||||
class Bank {
|
||||
private:
|
||||
std::string name;
|
||||
std::set<BankAccount *> accounts;
|
||||
std::set<Money *> money;
|
||||
std::set<std::unique_ptr<BankAccount>> accounts;
|
||||
std::set<std::unique_ptr<Money>> money;
|
||||
public:
|
||||
explicit Bank(std::string name);
|
||||
void addAccount(BankAccount * account);
|
||||
void addMoney(Money * money);
|
||||
std::set<BankAccount *> getAccounts();
|
||||
void addAccount(std::unique_ptr<BankAccount> account);
|
||||
void addMoney(std::unique_ptr<Money> money);
|
||||
std::set<std::unique_ptr<BankAccount>> getAccounts();
|
||||
std::string getName();
|
||||
};
|
||||
|
||||
|
@ -1,36 +1,30 @@
|
||||
#include <utility>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "BankAccount.h"
|
||||
#include "CashFactory.h"
|
||||
|
||||
BankAccount::BankAccount(Person * owner, std::string name) : name(std::move(name)) {
|
||||
this->owner = owner;
|
||||
BankAccount::BankAccount(std::shared_ptr<Person> owner, std::string name) : name(std::move(name)), owner(std::move(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();
|
||||
|
||||
return cashFactory->printCoin(value, CurrencyValue::USD);
|
||||
}
|
||||
|
||||
bool BankAccount::operator<(BankAccount * bankAccount) {
|
||||
return money.getValue() < bankAccount->money.getValue();
|
||||
}
|
||||
|
||||
std::string BankAccount::getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
Money BankAccount::getMoney() {
|
||||
return money;
|
||||
}
|
||||
|
||||
Person *BankAccount::getOwner() {
|
||||
return owner;
|
||||
bool BankAccount::operator<(std::unique_ptr<BankAccount> bankAccount) {
|
||||
return money->getValue() < bankAccount->getMoney()->getValue();
|
||||
}
|
||||
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
#define C_C_BANKACCOUNTS_H
|
||||
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
|
||||
#include "Money.h"
|
||||
#include "Cash.h"
|
||||
@ -10,16 +11,16 @@
|
||||
class BankAccount {
|
||||
private:
|
||||
std::string name;
|
||||
Person * owner;
|
||||
Money money;
|
||||
std::shared_ptr<Person> owner;
|
||||
std::unique_ptr<Money> money;
|
||||
public:
|
||||
explicit BankAccount(Person * owner, std::string name);
|
||||
BankAccount* operator+(Money * money);
|
||||
Money* operator-(int value);
|
||||
bool operator<(BankAccount * bankAccount);
|
||||
explicit BankAccount(std::shared_ptr<Person> owner, std::string name);
|
||||
std::unique_ptr<BankAccount> operator+(std::unique_ptr<Money> money);
|
||||
std::unique_ptr<Money> operator-(int value);
|
||||
bool operator<(std::unique_ptr<BankAccount> bankAccount);
|
||||
std::string getName();
|
||||
Person * getOwner();
|
||||
Money getMoney();
|
||||
std::shared_ptr<Person> getOwner();
|
||||
std::unique_ptr<Money> getMoney();
|
||||
};
|
||||
|
||||
|
||||
|
@ -10,7 +10,7 @@ private:
|
||||
public:
|
||||
Bill(int value, Currency currency, std::string serial);
|
||||
std::string getSerial();
|
||||
int getValue();
|
||||
int getValue() override;
|
||||
};
|
||||
|
||||
|
||||
|
@ -6,7 +6,7 @@ int allowedCoins[8] = {1, 2, 5, 10, 20, 50, 100, 200};
|
||||
|
||||
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);
|
||||
if(iter == std::end(allowedBills)) {
|
||||
return nullptr;
|
||||
@ -16,15 +16,16 @@ Bill * CashFactory::printBill(int value, Currency currency) {
|
||||
serial = randomString(15);
|
||||
}
|
||||
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);
|
||||
if(iter == std::end(allowedCoins)) {
|
||||
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) {
|
||||
|
@ -3,6 +3,8 @@
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#include "Bill.h"
|
||||
#include "Coin.h"
|
||||
|
||||
@ -10,8 +12,8 @@ class CashFactory {
|
||||
private:
|
||||
std::string randomString(size_t length);
|
||||
public:
|
||||
Bill * printBill(int value, Currency currency);
|
||||
Coin * printCoin(int value, Currency currency);
|
||||
std::unique_ptr<Bill> printBill(int value, Currency currency);
|
||||
std::unique_ptr<Coin> printCoin(int value, Currency currency);
|
||||
};
|
||||
|
||||
#endif
|
@ -8,7 +8,7 @@ class Coin : public Cash{
|
||||
private:
|
||||
public:
|
||||
Coin(int value, Currency currency);
|
||||
int getValue();
|
||||
int getValue() override;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -4,7 +4,7 @@
|
||||
Currency::Currency(CurrencyValue value) : value(value) {
|
||||
}
|
||||
|
||||
bool Currency::operator==(Currency ¤cy) {
|
||||
bool Currency::operator==(Currency currency) {
|
||||
return value == currency.value;
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ private:
|
||||
CurrencyValue value;
|
||||
public:
|
||||
Currency(CurrencyValue value);
|
||||
bool operator==(Currency ¤cy);
|
||||
bool operator==(Currency currency);
|
||||
Currency& operator=(Currency currency);
|
||||
CurrencyValue getValue();
|
||||
|
||||
|
@ -28,12 +28,14 @@ Money Money::operator-(Money &a) {
|
||||
}else return Money(-1, Currency(CurrencyValue::USD));
|
||||
}
|
||||
|
||||
const Money Money::operator++(int) {
|
||||
return Money(value++);
|
||||
const std::unique_ptr<Money> Money::operator++(int) {
|
||||
value++;
|
||||
return std::make_unique<Money>(this);
|
||||
}
|
||||
|
||||
const Money Money::operator--(int) {
|
||||
return Money(value--);
|
||||
const std::unique_ptr<Money> Money::operator--(int) {
|
||||
value--;
|
||||
return std::make_unique<Money>(this);
|
||||
}
|
||||
|
||||
int Money::getValue() {
|
||||
@ -42,4 +44,22 @@ int Money::getValue() {
|
||||
|
||||
Currency Money::getCurrency() {
|
||||
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));
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
#ifndef C_C_MONEY_H
|
||||
#define C_C_MONEY_H
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "Currency.h"
|
||||
|
||||
class Money {
|
||||
@ -9,12 +11,12 @@ private:
|
||||
Currency currency;
|
||||
public:
|
||||
explicit Money(int value = 0, Currency currency = CurrencyValue::USD);
|
||||
Money& operator=(Money money);
|
||||
Money& operator=(Money * money);
|
||||
Money operator+(Money &a);
|
||||
Money operator-(Money &a);
|
||||
const Money operator++(int);
|
||||
const Money operator--(int);
|
||||
std::unique_ptr<Money> operator=(Money money);
|
||||
std::unique_ptr<Money> operator=(std::unique_ptr<Money> money);
|
||||
std::unique_ptr<Money> operator+(std::unique_ptr<Money> &a);
|
||||
std::unique_ptr<Money> operator-(std::unique_ptr<Money> &a);
|
||||
const std::unique_ptr<Money> operator++(int);
|
||||
const std::unique_ptr<Money> operator--(int);
|
||||
|
||||
virtual int getValue();
|
||||
Currency getCurrency();
|
||||
|
@ -3,12 +3,14 @@
|
||||
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
#include "Money.h"
|
||||
|
||||
class Person {
|
||||
private:
|
||||
std::string name;
|
||||
std::vector<Money *> money;
|
||||
std::vector<std::unique_ptr<Money>> money;
|
||||
public:
|
||||
explicit Person(std::string name);
|
||||
std::string getName();
|
||||
|
@ -34,8 +34,8 @@ std::ostream &operator<<(std::ostream &os, Cash cash) {
|
||||
return os;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, Money money) {
|
||||
os << money.getValue() << " " << money.getCurrency();
|
||||
std::ostream &operator<<(std::ostream &os, std::unique_ptr<Money> money) {
|
||||
os << money->getValue() << " " << money->getCurrency();
|
||||
return os;
|
||||
}
|
||||
|
||||
@ -50,7 +50,7 @@ std::ostream &operator<<(std::ostream &os, BankAccount bankAccount) {
|
||||
}
|
||||
|
||||
std::set<Bank *> banks;
|
||||
std::set<Person *> owners;
|
||||
std::set<std::unique_ptr<Person>> owners;
|
||||
CashFactory *cashFactory;
|
||||
|
||||
void setup() {
|
||||
@ -60,9 +60,9 @@ void setup() {
|
||||
Bank *bank2 = new Bank("Bank 2");
|
||||
Bank *bank3 = new Bank("Bank 3");
|
||||
|
||||
Person *max = new Person("Max Müller");
|
||||
Person *marius = new Person("Marius Müller");
|
||||
Person *alex = new Person("Alex Maier");
|
||||
std::unique_ptr<Person> max = std::make_unique<Person>(new Person("Max Müller"));
|
||||
std::unique_ptr<Person> marius = std::make_unique<Person>(new Person("Marius Müller"));
|
||||
std::unique_ptr<Person> alex = std::make_unique<Person>(new Person("Alex Maier"));
|
||||
|
||||
owners.insert(max);
|
||||
owners.insert(marius);
|
||||
@ -91,10 +91,10 @@ void simulate() {
|
||||
std::cout << std::endl;
|
||||
std::cout << "======================== " << bank->getName() << " ========================" << 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) {
|
||||
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);
|
||||
if (bill != nullptr) {
|
||||
*bankAccount + bill;
|
||||
@ -124,8 +124,11 @@ void tearDown() {
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
std::cout << "+++++++++++++++++++++++++ " << "setup()" << " +++++++++++++++++++++++++" << std::endl;
|
||||
setup();
|
||||
std::cout << "+++++++++++++++++++++++++ " << "simulate()" << " +++++++++++++++++++++++++" << std::endl;
|
||||
simulate();
|
||||
std::cout << "+++++++++++++++++++++++++ " << "tearDown()" << " +++++++++++++++++++++++++" << std::endl;
|
||||
tearDown();
|
||||
|
||||
HeapObject::assertionsHold();
|
||||
|
Loading…
Reference in New Issue
Block a user