competency.cpp 2.06 KB
Newer Older
1 2
#include <iostream>

Alexis Lebis's avatar
Alexis Lebis committed
3 4
#include "competency.h"
#include "magnitude.h"
5

Alexis Lebis's avatar
Alexis Lebis committed
6
#include "exception/magnitudeException.h"
7 8 9
#include "exception/competencyEvolvingException.h"

int Competency::COMPETENCY_COUNTER = 0;
Alexis Lebis's avatar
Alexis Lebis committed
10 11 12

// === FACTORY

13
Competency Competency::build(Magnitude & m, std::string name)
Alexis Lebis's avatar
Alexis Lebis committed
14 15 16
{
    int id = Competency::assignID();
    if(name.empty())
17
        name = "Competency#"+std::to_string(id);
Alexis Lebis's avatar
Alexis Lebis committed
18 19 20 21
    
    return Competency(id, m, name);
}

22
Competency Competency::build(double d = 0, std::string name)
Alexis Lebis's avatar
Alexis Lebis committed
23 24 25
{
    int id = Competency::assignID();
    if(name.empty())
26
        name = "Competency#"+std::to_string(id);
Alexis Lebis's avatar
Alexis Lebis committed
27 28 29 30 31 32 33 34 35

    try
    {
        Magnitude m = Magnitude::build(d);
        return Competency(id, m, name); 
    }
    catch(MagnitudeException & e)
    {
        e.getMagnitude().rebase();
36
        throw CompetencyEvolvingException(new Competency(id, e.getMagnitude(), name)); 
Alexis Lebis's avatar
Alexis Lebis committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    }  
}

// === CONSTRUCTOR

Competency::Competency(int id, Magnitude m, std::string s)
    : _id(id), _m(m), _name(s)
{

}

// === STATIC

int Competency::assignID()
{
    return ++Competency::COMPETENCY_COUNTER;
}

// === FUNCTION

void Competency::evolveTowards(Magnitude & m)
{
59 60 61 62 63 64 65 66 67 68
    try
    {
        this->_m+= m;
    }
    catch(MagnitudeException & e)
    {
        e.getMagnitude().rebase();
        this->_m = e.getMagnitude();
        throw CompetencyEvolvingException(this);
    }
Alexis Lebis's avatar
Alexis Lebis committed
69 70 71 72
}

void Competency::evolveTowards(double d)
{
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    try
    {
        this->_m += d;
    }
    catch(MagnitudeException & e)
    {
        e.getMagnitude().rebase();
        this->_m = e.getMagnitude();
        throw CompetencyEvolvingException(this);
    }
}

const double Competency::competencyValue() const
{
    return this->_m.value();
}
Alexis Lebis's avatar
Alexis Lebis committed
89

90 91 92 93 94 95
// === OPERATOR
std::ostream& operator<<(std::ostream& Stream, const Competency & c) 
{ 
    std::string s = "Competency\n\tid:"+std::to_string(c.id())+"\n\tname:"+c.c_name()+"\n\tvalue:"+std::to_string(c.competencyValue());
    Stream << s ; 
    return Stream; 
96 97 98 99 100
}

bool Competency::operator==(const Competency & c) const
{
    return ( this->_id == c.id() || ( this->_name.compare(c.c_name()) == 0 ) );
Alexis Lebis's avatar
Alexis Lebis committed
101
}