08_PTNR: Neuanfang Grundgerüst

This commit is contained in:
Johannes Theiner 2018-12-01 16:48:49 +01:00
parent b37de1bd06
commit d0cd6d6a10
19 changed files with 240 additions and 218 deletions

View File

@ -1 +1,2 @@
add_executable(08_PTRN_MP MP/Money.cpp MP/Money.h MP/Currency.cpp MP/Currency.h MP/main.cpp MP/BankAccount.cpp MP/BankAccount.h MP/Storage.cpp MP/Storage.h) add_executable(08_PTRN_MP MP/VehicleFactory.cpp MP/VehicleFactory.hpp MP/Vehicle.cpp MP/Vehicle.hpp MP/Car.cpp MP/Car.hpp MP/Truck.cpp MP/Truck.hpp MP/Logger.cpp MP/Logger.hpp MP/main.cpp)
add_executable(08_PTRN_Testat Testat/main.cpp Testat/Money.cpp Testat/Money.h Testat/Logger.cpp Testat/Logger.hpp Testat/Currency.h Testat/BankAccount.cpp Testat/BankAccount.h)

View File

@ -1,7 +0,0 @@
#include <utility>
#include <utility>
#include "BankAccount.h"
BankAccount::BankAccount(std::string owner, Money money) : owner(std::move(owner)), money(money){}

View File

@ -1,19 +0,0 @@
#ifndef C_C_BANKACCOUNT_H
#define C_C_BANKACCOUNT_H
#include <string>
#include "Money.h"
class BankAccount {
private:
std::string owner;
Money money;
public:
BankAccount(std::string owner = "none", Money money = Money(0, Storage().USD));
};
#endif

View File

@ -1,9 +0,0 @@
#include <utility>
#include "Currency.h"
Currency::Currency(std::string name, float rate) : name(std::move(name)), rate(rate) {}
bool Currency::operator==(Currency &currency) {
return name == currency.name;
}

View File

@ -1,17 +0,0 @@
#ifndef C_C_CURRENCY_H
#define C_C_CURRENCY_H
#include <string>
class Currency {
public:
std::string name;
float rate;
Currency(std::string name, float rate);
bool operator==(Currency &currency);
};
#endif

View File

@ -1,35 +0,0 @@
#include <utility>
#include "Money.h"
Money::Money(float value, Currency currency) : value(value), currency(std::move(currency)) {}
Money Money::operator+(Money money) {
return Money((this->value * this->currency.rate) + (money.value * money.currency.rate), this->currency);
}
Money Money::operator-(Money money) {
return Money(this->value - money.value, this->currency);
}
Money Money::operator*(Money money) {
return Money(this->value * money.value);
}
Money Money::operator/(Money money) {
return Money(this->value / money.value);
}
const Money Money::operator++(int) {
this->value++;
return *this;
}
const Money Money::operator--(int) {
this->value--;
return *this;
}
bool Money::operator==(Money &money) {
return this->value == money.value && this ->currency == money.currency;
}

View File

@ -1,28 +0,0 @@
#ifndef C_C_MONEY_H
#define C_C_MONEY_H
#include "Currency.h"
#include "Storage.h"
/**
*
*
* @author Johannes Theiner <kontakt@joethei.xyz>
* @since 0.0.1
*/
class Money {
private:
float value;
Currency currency;
public:
explicit Money(float value = 0, Currency currency = Storage().currencies["USD"]);
Money operator+(Money money);
Money operator-(Money money);
Money operator*(Money money);
Money operator/(Money money);
const Money operator++(int);
const Money operator--(int);
bool operator==(Money &money);
};
#endif

View File

@ -1 +0,0 @@
#include "Storage.h"

View File

@ -1,21 +0,0 @@
#ifndef C_C_STORAGE_H
#define C_C_STORAGE_H
#include <unordered_set>
#include <unordered_map>
#include "Currency.h"
#include "BankAccount.h"
class Storage {
public:
std::unordered_map<std::string, BankAccount> accounts;
static const Currency USD = Currency("US Dollar", 1);
static const Currency EUR = Currency("Euro", 0.879699);
static const Currency GBP = Currency("British Pound", 0.783667);
static const Currency AUD = Currency("Australian Dollar", 1.36962);
static const Currency CAD = Currency("Canadian Dollar", 1.33023);
};
#endif

View File

@ -1,13 +1,71 @@
#include <unordered_set> #include "Car.hpp"
#include <unordered_map> #include "Truck.hpp"
#include "Logger.hpp"
#include "VehicleFactory.hpp"
#include "Currency.h" #include <iostream>
#include "BankAccount.h" #include <vector>
#include "Storage.h"
Storage storage; void workOnCopy_naive(std::vector<Vehicle*> vehicles){
std::vector<Vehicle*> tempVec;
int main(int argc, char **argv) { for (int i=0; i<vehicles.size(); i++){
Vehicle * currentVehicle = vehicles[i];
storage.accounts.insert(BankAccount("Max Maier", Money(50, Storage().EUR))); Car* carToCopy = dynamic_cast<Car*>(currentVehicle); // RTTI type check
Truck* truckToCopy = dynamic_cast<Truck*>(currentVehicle);// may be nullptr
if(carToCopy){
tempVec.push_back(new Car(carToCopy->model(), carToCopy->maxWeight())); // type dependency to Car
}else if(truckToCopy){
tempVec.push_back(new Truck(truckToCopy->model(), truckToCopy->payload_kg())); // type dependecy
}else{
// TODO: what do we do here?
// Did someone add a new class to the code base?
return; // BUG: copies aren't free'd
}
}
// work on copies ...
// ...
for(auto vehi : tempVec){
std::cout << vehi->model() << " " << vehi->payload_kg() << " kg" << std::endl;
}
for (int i=0; i<tempVec.size(); i++){
delete tempVec[i];
}
tempVec.clear();
}
void workOnCopy_smart(std::vector<Vehicle*> vehicles){ // uses RAII, virtual ctor
class RAIIvector { // local RAII helper class
public:
std::vector<Vehicle*> tempVec;
~RAIIvector(){
for(auto vehi : tempVec){
delete vehi;
}
tempVec.clear();
}
};
RAIIvector rv;
for(auto vehi : vehicles){
rv.tempVec.push_back(vehi->clone());
}
// work on copies ...
// ...
for(auto vehi : rv.tempVec){
std::cout << vehi->model() << " " << vehi->payload_kg() << " kg" << std::endl;
}
// compiler WILL invoke RAIIvector::~RAIIvector() here
}
int main(int argc, const char * argv[]) {
std::vector<Vehicle*> vehicles;
vehicles.push_back(vehicleForPayload("Volkswagen Golf", 900)); // use factory for construction
vehicles.push_back(vehicleForPayload("Magirus Deutz", 18*1000));
Logger::getInstance()->log("work naively", __FILE__, __LINE__);
workOnCopy_naive(vehicles);
Logger::getInstance()->log("work smartly", __FILE__, __LINE__);
workOnCopy_smart(vehicles);
return 0;
} }

View File

@ -1,71 +0,0 @@
#include "Car.hpp"
#include "Truck.hpp"
#include "Logger.hpp"
#include "VehicleFactory.hpp"
#include <iostream>
#include <vector>
void workOnCopy_naive(std::vector<Vehicle*> vehicles){
std::vector<Vehicle*> tempVec;
for (int i=0; i<vehicles.size(); i++){
Vehicle * currentVehicle = vehicles[i];
Car* carToCopy = dynamic_cast<Car*>(currentVehicle); // RTTI type check
Truck* truckToCopy = dynamic_cast<Truck*>(currentVehicle);// may be nullptr
if(carToCopy){
tempVec.push_back(new Car(carToCopy->model(), carToCopy->maxWeight())); // type dependency to Car
}else if(truckToCopy){
tempVec.push_back(new Truck(truckToCopy->model(), truckToCopy->payload_kg())); // type dependecy
}else{
// TODO: what do we do here?
// Did someone add a new class to the code base?
return; // BUG: copies aren't free'd
}
}
// work on copies ...
// ...
for(auto vehi : tempVec){
std::cout << vehi->model() << " " << vehi->payload_kg() << " kg" << std::endl;
}
for (int i=0; i<tempVec.size(); i++){
delete tempVec[i];
}
tempVec.clear();
}
void workOnCopy_smart(std::vector<Vehicle*> vehicles){ // uses RAII, virtual ctor
class RAIIvector { // local RAII helper class
public:
std::vector<Vehicle*> tempVec;
~RAIIvector(){
for(auto vehi : tempVec){
delete vehi;
}
tempVec.clear();
}
};
RAIIvector rv;
for(auto vehi : vehicles){
rv.tempVec.push_back(vehi->clone());
}
// work on copies ...
// ...
for(auto vehi : rv.tempVec){
std::cout << vehi->model() << " " << vehi->payload_kg() << " kg" << std::endl;
}
// compiler WILL invoke RAIIvector::~RAIIvector() here
}
int main(int argc, const char * argv[]) {
std::vector<Vehicle*> vehicles;
vehicles.push_back(vehicleForPayload("Volkswagen Golf", 900)); // use factory for construction
vehicles.push_back(vehicleForPayload("Magirus Deutz", 18*1000));
Logger::getInstance()->log("work naively", __FILE__, __LINE__);
workOnCopy_naive(vehicles);
Logger::getInstance()->log("work smartly", __FILE__, __LINE__);
workOnCopy_smart(vehicles);
return 0;
}

View File

@ -0,0 +1,17 @@
//
// Created by JThei on 01.12.2018.
//
#include "BankAccount.h"
BankAccount::BankAccount(std::string name) : name(std::move(name)) {}
void BankAccount::add(Money _money) {
money = money + _money;
}
bool BankAccount::operator<(BankAccount a) {
return false;
}

View File

@ -0,0 +1,23 @@
//
// Created by JThei on 01.12.2018.
//
#ifndef C_C_BANKACCOUNTS_H
#define C_C_BANKACCOUNTS_H
#include <unordered_map>
#include "Money.h"
class BankAccount {
private:
std::string name;
Money money;
public:
explicit BankAccount(std::string name);
void add(Money money);
bool operator<(BankAccount a);
};
#endif //C_C_BANKACCOUNTS_H

View File

@ -0,0 +1,17 @@
//
// Created by JThei on 01.12.2018.
//
#ifndef C_C_CURRENCY_H
#define C_C_CURRENCY_H
enum class Currency {
USD=100,
EUR=80,
GPD=75
};
#endif //C_C_CURRENCY_H

View File

@ -0,0 +1,15 @@
// file Logger.cpp
#include "Logger.hpp"
Logger* Logger::_theInstance = nullptr;
Logger* Logger::getInstance(){
if (!_theInstance){
_theInstance = new StdoutLogger();
}
return _theInstance;
}
void StdoutLogger::log(std::string message, std::string file, int line){
std::cerr << message << " in " << file << " line " << line << std::endl;
}

View File

@ -0,0 +1,19 @@
// file Logger.hpp
#ifndef Logger_hpp
#define Logger_hpp
#include <iostream>
class Logger {
static Logger* _theInstance;
public:
virtual void log(std::string message, std::string file="", int line=0) = 0;
static Logger* getInstance();
};
class StdoutLogger: public Logger {
public:
void log(std::string message, std::string file="", int line=0);
};
#endif

View File

@ -0,0 +1,37 @@
//
// Created by JThei on 01.12.2018.
//
#include "Money.h"
Money::Money(int value, Currency currency) : value(value), currency(currency) {
}
Money Money::operator+(Money &a) {
return Money();
}
Money Money::operator-(Money &a) {
return Money();
}
const Money Money::operator++(int) {
return Money();
}
const Money Money::operator--(int) {
return Money();
}
bool Money::operator==(Money &a) {
return false;
}
bool Money::operator!=(Money &a) {
return false;
}
Currency Money::getCurrency() {
return currency;
}

View File

@ -0,0 +1,28 @@
//
// Created by JThei on 01.12.2018.
//
#ifndef C_C_MONEY_H
#define C_C_MONEY_H
#include "Currency.h"
class Money {
private:
int value;
Currency currency;
public:
explicit Money(int value = 0, Currency currency = Currency::USD);
Money operator+(Money &a);
Money operator-(Money &a);
const Money operator++(int);
const Money operator--(int);
bool operator==(Money &a);
bool operator!=(Money &a);
Currency getCurrency();
};
#endif //C_C_MONEY_H

View File

@ -0,0 +1,15 @@
#include <set>
#include "Currency.h"
#include "BankAccount.h"
int main(int argc, char **argv) {
// std::set<BankAccount> accounts;
//
// accounts.insert(BankAccount("Max"));
// accounts.insert(BankAccount("Max"));
// accounts.insert(BankAccount("Marius"));
// accounts.insert(BankAccount("Test"));
}