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);
@ -36,3 +38,4 @@ std::string Bank::getName() {
return name; return name;
} }

View File

@ -4,6 +4,13 @@
#include "BankAccount.h" #include "BankAccount.h"
#include "HeapObject.h" #include "HeapObject.h"
namespace banking {
/**
* @author Johannes Theiner<kontakt@joethei.xyz>
* @version 1.0
*/
class Bank : public HeapObject { class Bank : public HeapObject {
private: private:
std::string name; std::string name;
@ -11,11 +18,18 @@ private:
int money; int money;
public: public:
explicit Bank(std::string name); explicit Bank(std::string name);
explicit Bank(Bank *bank); explicit Bank(Bank *bank);
void addAccount(BankAccount *account); 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,6 +7,12 @@
#include "Money.h" #include "Money.h"
#include "Person.h" #include "Person.h"
namespace banking {
/**
* @author Johannes Theiner<kontakt@joethei.xyz>
* @version 1.0
*/
class BankAccount { class BankAccount {
private: private:
std::string name; std::string name;
@ -14,12 +20,20 @@ private:
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); explicit BankAccount(BankAccount *bankAccount);
BankAccount* operator+(Money * money);
BankAccount *operator+(std::shared_ptr<Money> money);
Money *operator-(int value); 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,6 +4,12 @@
#include "Cash.h" #include "Cash.h"
namespace banking {
/**
* @author Johannes Theiner<kontakt@joethei.xyz>
* @version 1.0
*/
class Bill : public Cash{ class Bill : public Cash{
private: private:
std::string serial; std::string serial;
@ -11,8 +17,11 @@ 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"
namespace banking {
/**
* @author Johannes Theiner<kontakt@joethei.xyz>
* @version 1.0
*/
class Cash : public Money, public HeapObject{ class Cash : public Money, public HeapObject{
private: private:
public: 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,6 +7,12 @@
#include "Bill.h" #include "Bill.h"
#include "Coin.h" #include "Coin.h"
namespace banking {
/**
* @author Johannes Theiner<kontakt@joethei.xyz
* @version 1.0
*/
class CashFactory { class CashFactory {
private: private:
std::string randomString(size_t length); std::string randomString(size_t length);
@ -14,5 +20,7 @@ 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,6 +4,12 @@
#include "Cash.h" #include "Cash.h"
namespace banking {
/**
* @author Johannes Theiner<kontakt@joethei.xyz>
* @version 1.0
*/
class Coin : public Cash{ class Coin : public Cash{
private: private:
public: public:
@ -11,5 +17,8 @@ public:
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,12 +3,19 @@
#include <vector> #include <vector>
namespace banking {
enum class CurrencyValue { enum class CurrencyValue {
USD=100, USD=100,
EUR=80, EUR=80,
GPD=75, GPD=75,
}; };
/**
* @author Johannes Theiner<kontakt@joethei.xyz>
* @version 1.0
*/
class Currency { class Currency {
private: private:
CurrencyValue value; CurrencyValue value;
@ -19,5 +26,8 @@ public:
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"
namespace banking {
/**
* @author Johannes Theiner<kontakt@joethei.xyz>
* @version 1.0
*/
class Money { class Money {
private: 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"
namespace banking {
/**
* @author Johannes Theiner<kontakt@joethei.xyz>
* @version 1.0
*/
class Person { class Person {
private: 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;