10_PITF: Namespaces hinzugefügt

This commit is contained in:
Johannes Theiner 2019-01-06 11:29:33 +01:00
parent a2c74528cd
commit 964a00f596
22 changed files with 229 additions and 107 deletions

View File

@ -1 +1,3 @@
<img src="https://teamcity.joethei.xyz/app/rest/builds/aggregated/strob:(branch:(buildType:(id:Studium_Programmierung_CC),policy:active_history_and_active_vcs_branches),locator:(buildType:(id:Studium_Programmierung_CC)))/statusIcon.svg"> <img src="https://teamcity.joethei.xyz/app/rest/builds/aggregated/strob:(branch:(buildType:(id:Studium_Programmierung_CC),policy:active_history_and_active_vcs_branches),locator:(buildType:(id:Studium_Programmierung_CC)))/statusIcon.svg">
[Dokumentation](https://files.joethei.space/documentation/Studium/C_CPP/)

View File

@ -6,6 +6,8 @@
#include "Bank.h" #include "Bank.h"
using namespace banking;
std::default_random_engine gen(static_cast<unsigned long>(time(nullptr))); std::default_random_engine gen(static_cast<unsigned long>(time(nullptr)));
std::uniform_int_distribution<int> dist(0, 9000); std::uniform_int_distribution<int> dist(0, 9000);
@ -13,7 +15,7 @@ Bank::Bank(std::string name) : name(std::move(name)) {}
Bank::Bank(Bank *bank) : name(std::move(bank->getName())) {} Bank::Bank(Bank *bank) : name(std::move(bank->getName())) {}
void Bank::addAccount(BankAccount * account) { void Bank::addAccount(BankAccount *account) {
accounts.insert(std::move(std::make_unique<BankAccount>(account))); accounts.insert(std::move(std::make_unique<BankAccount>(account)));
} }
@ -21,8 +23,8 @@ void Bank::moveMoney(std::shared_ptr<Bank> bank, int amount) {
//ab und zu etwas Geld verlieren //ab und zu etwas Geld verlieren
int chance = dist(gen); int chance = dist(gen);
int value = dist(gen); int value = dist(gen);
if(chance == 42 && amount >= value) { if (chance == 42 && amount >= value) {
amount -=value; amount -= value;
} }
money -= amount; money -= amount;
bank->money -= amount; bank->money -= amount;
@ -36,3 +38,4 @@ std::string Bank::getName() {
return name; return name;
} }

View File

@ -4,18 +4,32 @@
#include "BankAccount.h" #include "BankAccount.h"
#include "HeapObject.h" #include "HeapObject.h"
class Bank : public HeapObject{
private: namespace banking {
/**
* @author Johannes Theiner<kontakt@joethei.xyz>
* @version 1.0
*/
class Bank : public HeapObject {
private:
std::string name; std::string name;
std::set<std::shared_ptr<BankAccount>> accounts; std::set<std::shared_ptr<BankAccount>> accounts;
int money; int money;
public: public:
explicit Bank(std::string name); explicit Bank(std::string name);
explicit Bank(Bank* bank);
void addAccount(BankAccount * account); explicit Bank(Bank *bank);
void addAccount(BankAccount *account);
void moveMoney(std::shared_ptr<Bank> bank, int amount); void moveMoney(std::shared_ptr<Bank> bank, int amount);
std::set<std::shared_ptr<BankAccount>> getAccounts(); std::set<std::shared_ptr<BankAccount>> getAccounts();
std::string getName(); std::string getName();
}; };
}
#endif #endif

View File

@ -8,6 +8,8 @@
#include "BankAccount.h" #include "BankAccount.h"
#include "CashFactory.h" #include "CashFactory.h"
using namespace banking;
std::default_random_engine gen2(static_cast<unsigned long>(time(nullptr))); std::default_random_engine gen2(static_cast<unsigned long>(time(nullptr)));
std::uniform_int_distribution<int> dist2(0, 9000); std::uniform_int_distribution<int> dist2(0, 9000);
@ -22,7 +24,7 @@ BankAccount::BankAccount(BankAccount *bankAccount) : name(std::move(bankAccount-
} }
BankAccount *BankAccount::operator+(Money * money) { BankAccount *BankAccount::operator+(std::shared_ptr<Money> money) {
//ab und zu Geld verlieren //ab und zu Geld verlieren
int rnd = dist2(gen2); int rnd = dist2(gen2);

View File

@ -7,19 +7,33 @@
#include "Money.h" #include "Money.h"
#include "Person.h" #include "Person.h"
class BankAccount { namespace banking {
private:
/**
* @author Johannes Theiner<kontakt@joethei.xyz>
* @version 1.0
*/
class BankAccount {
private:
std::string name; std::string name;
std::shared_ptr<Person> owner; std::shared_ptr<Person> owner;
std::shared_ptr<Money> money; std::shared_ptr<Money> money;
public: public:
explicit BankAccount(std::shared_ptr<Person> owner, std::string name); explicit BankAccount(std::shared_ptr<Person> owner, std::string name);
explicit BankAccount(BankAccount* bankAccount);
BankAccount* operator+(Money * money); explicit BankAccount(BankAccount *bankAccount);
Money* operator-(int value);
BankAccount *operator+(std::shared_ptr<Money> money);
Money *operator-(int value);
std::string getName(); std::string getName();
std::shared_ptr<Person> getOwner(); std::shared_ptr<Person> getOwner();
std::shared_ptr<Money> getMoney(); std::shared_ptr<Money> getMoney();
}; };
}
#endif #endif

View File

@ -2,6 +2,8 @@
#include "Bill.h" #include "Bill.h"
using namespace banking;
Bill::Bill(int value, Currency currency, std::string serial) : Cash(value, currency), serial(std::move(serial)){ Bill::Bill(int value, Currency currency, std::string serial) : Cash(value, currency), serial(std::move(serial)){
} }

View File

@ -4,15 +4,24 @@
#include "Cash.h" #include "Cash.h"
class Bill : public Cash{ namespace banking {
private:
/**
* @author Johannes Theiner<kontakt@joethei.xyz>
* @version 1.0
*/
class Bill : public Cash{
private:
std::string serial; std::string serial;
public: public:
explicit Bill(int value, Currency currency, std::string serial); explicit Bill(int value, Currency currency, std::string serial);
explicit Bill(Bill *bill); explicit Bill(Bill *bill);
std::string getSerial(); std::string getSerial();
int getValue(); int getValue() override;
}; };
}
#endif #endif

View File

@ -3,4 +3,6 @@
#include "Cash.h" #include "Cash.h"
using namespace banking;
Cash::Cash(int value, Currency currency) : Money(value, currency) {} Cash::Cash(int value, Currency currency) : Money(value, currency) {}

View File

@ -5,10 +5,19 @@
#include "Money.h" #include "Money.h"
#include "HeapObject.h" #include "HeapObject.h"
class Cash : public Money, public HeapObject{ namespace banking {
private:
public: /**
* @author Johannes Theiner<kontakt@joethei.xyz>
* @version 1.0
*/
class Cash : public Money, public HeapObject{
private:
public:
Cash(int value, Currency currency); Cash(int value, Currency currency);
}; };
}
#endif #endif

View File

@ -1,6 +1,8 @@
#include <algorithm> #include <algorithm>
#include "CashFactory.h" #include "CashFactory.h"
using namespace banking;
int allowedBills[5] = {100, 50, 20, 10, 5}; int allowedBills[5] = {100, 50, 20, 10, 5};
int allowedCoins[8] = {1, 2, 5, 10, 20, 50, 100, 200}; int allowedCoins[8] = {1, 2, 5, 10, 20, 50, 100, 200};

View File

@ -7,12 +7,20 @@
#include "Bill.h" #include "Bill.h"
#include "Coin.h" #include "Coin.h"
class CashFactory { namespace banking {
private:
/**
* @author Johannes Theiner<kontakt@joethei.xyz
* @version 1.0
*/
class CashFactory {
private:
std::string randomString(size_t length); std::string randomString(size_t length);
public: public:
std::shared_ptr<Bill> printBill(int value, Currency currency); std::shared_ptr<Bill> printBill(int value, Currency currency);
std::shared_ptr<Coin> printCoin(int value, Currency currency); std::shared_ptr<Coin> printCoin(int value, Currency currency);
}; };
}
#endif #endif

View File

@ -1,5 +1,7 @@
#include "Coin.h" #include "Coin.h"
using namespace banking;
Coin::Coin(int value, Currency currency) : Cash(value, currency) { Coin::Coin(int value, Currency currency) : Cash(value, currency) {
} }

View File

@ -4,12 +4,21 @@
#include "Cash.h" #include "Cash.h"
class Coin : public Cash{ namespace banking {
private:
public: /**
* @author Johannes Theiner<kontakt@joethei.xyz>
* @version 1.0
*/
class Coin : public Cash{
private:
public:
explicit Coin(int value, Currency currency); explicit Coin(int value, Currency currency);
explicit Coin(Coin *coin); explicit Coin(Coin *coin);
int getValue() override; int getValue() override;
}; };
}
#endif #endif

View File

@ -1,6 +1,8 @@
#include <string> #include <string>
#include "Currency.h" #include "Currency.h"
using namespace banking;
Currency::Currency(CurrencyValue value) : value(value) { Currency::Currency(CurrencyValue value) : value(value) {
} }

View File

@ -3,21 +3,31 @@
#include <vector> #include <vector>
enum class CurrencyValue { namespace banking {
enum class CurrencyValue {
USD=100, USD=100,
EUR=80, EUR=80,
GPD=75, GPD=75,
}; };
class Currency {
private: /**
* @author Johannes Theiner<kontakt@joethei.xyz>
* @version 1.0
*/
class Currency {
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();
}; };
}
#endif #endif

View File

@ -12,9 +12,6 @@ void* HeapObject::operator new (size_t size){
return new char[size]; return new char[size];
} }
HeapObject::HeapObject(){ HeapObject::HeapObject(){
ctorCount++; ctorCount++;
} }
@ -24,8 +21,7 @@ HeapObject::~HeapObject(){
} }
bool HeapObject::assertionsHold(){ bool HeapObject::assertionsHold(){
//assert(ctorCount == newCount); // all objects have been allocated on heap assert(ctorCount == newCount); // all objects have been allocated on heap
std::cout << ctorCount << " : " << dtorCount << std::endl;
assert(ctorCount == dtorCount); // all objects have been deleted assert(ctorCount == dtorCount); // all objects have been deleted
return true; return true;
} }

View File

@ -3,6 +3,10 @@
#include <cstdlib> #include <cstdlib>
/**
* @author Carsten Link
* @version 1.0
*/
class HeapObject { class HeapObject {
public: public:
void *operator new(size_t size); void *operator new(size_t size);

View File

@ -1,6 +1,8 @@
#include <iostream> #include <iostream>
#include "Money.h" #include "Money.h"
using namespace banking;
Money::Money(int value, Currency currency) : value(value), currency(currency) {} Money::Money(int value, Currency currency) : value(value), currency(currency) {}
Money::Money(Money *money) : value(money->getValue()), currency(money->getCurrency()) {} Money::Money(Money *money) : value(money->getValue()), currency(money->getCurrency()) {}

View File

@ -3,23 +3,40 @@
#include "Currency.h" #include "Currency.h"
class Money { namespace banking {
private:
/**
* @author Johannes Theiner<kontakt@joethei.xyz>
* @version 1.0
*/
class Money {
private:
int value; int value;
Currency currency; Currency currency;
public: public:
explicit Money(int value = 0, Currency currency = CurrencyValue::USD); explicit Money(int value = 0, Currency currency = CurrencyValue::USD);
explicit Money(Money *money); explicit Money(Money *money);
Money& operator=(Money money);
Money& operator=(Money * money); Money &operator=(Money money);
Money &operator=(Money *money);
Money operator+(Money &a); Money operator+(Money &a);
Money operator-(Money &a); Money operator-(Money &a);
const Money operator++(int); const Money operator++(int);
const Money operator--(int); const Money operator--(int);
bool operator<(Money &a); bool operator<(Money &a);
virtual int getValue(); virtual int getValue();
Currency getCurrency(); Currency getCurrency();
}; };
}
#endif #endif

View File

@ -2,6 +2,8 @@
#include "Person.h" #include "Person.h"
using namespace banking;
Person::Person(std::string name) : name(std::move(name)){ Person::Person(std::string name) : name(std::move(name)){
} }

View File

@ -6,15 +6,24 @@
#include <memory> #include <memory>
#include "Money.h" #include "Money.h"
class Person { namespace banking {
private:
/**
* @author Johannes Theiner<kontakt@joethei.xyz>
* @version 1.0
*/
class Person {
private:
std::string name; std::string name;
std::vector<std::shared_ptr<Money>> money; std::vector<std::shared_ptr<Money>> money;
public: public:
explicit Person(std::string name); explicit Person(std::string name);
explicit Person(Person* person);
explicit Person(Person *person);
std::string getName(); std::string getName();
}; };
}
#endif #endif

View File

@ -8,6 +8,8 @@
#include "CashFactory.h" #include "CashFactory.h"
#include "Bank.h" #include "Bank.h"
using namespace banking;
std::default_random_engine generator(static_cast<unsigned long>(time(nullptr))); std::default_random_engine generator(static_cast<unsigned long>(time(nullptr)));
std::uniform_int_distribution<int> distribution(-100, 500); std::uniform_int_distribution<int> distribution(-100, 500);
@ -93,11 +95,11 @@ void simulate() {
std::shared_ptr<Bill> bill = cashFactory->printBill(rnd, CurrencyValue::USD); std::shared_ptr<Bill> bill = cashFactory->printBill(rnd, CurrencyValue::USD);
std::shared_ptr<Coin> coin = cashFactory->printCoin(rnd, CurrencyValue::USD); std::shared_ptr<Coin> coin = cashFactory->printCoin(rnd, CurrencyValue::USD);
if (bill != nullptr) { if (bill != nullptr) {
*bankAccount + bill.get(); *bankAccount + bill;
moneyVector.push_back(std::static_pointer_cast<Money, Bill>(bill)); moneyVector.push_back(std::static_pointer_cast<Money, Bill>(bill));
} }
if (coin != nullptr) { if (coin != nullptr) {
*bankAccount + coin.get(); *bankAccount + coin;
moneyVector.push_back(std::static_pointer_cast<Money, Coin>(coin)); moneyVector.push_back(std::static_pointer_cast<Money, Coin>(coin));
} }
Money *fee = *bankAccount - 10; Money *fee = *bankAccount - 10;