42_CPP05/ex03/src/Bureaucrat.cpp
Camille Chauvet 05a614c92a add: ex03
2023-09-13 11:28:31 +00:00

104 lines
1.8 KiB
C++

#include "Bureaucrat.hpp"
#include <iostream>
#include <exception>
#include <ostream>
#include <string>
Bureaucrat::Bureaucrat()
{
std::cout << "Bureaucrat()" << std::endl;
}
Bureaucrat::Bureaucrat(const Bureaucrat& src)
{
*this = src;
std::cout << "Bureaucrat(Bureaucrat)" << std::endl;
}
Bureaucrat::Bureaucrat(const std::string& name, int grade):
_name(name)
{
setGrade(grade);
std::cout << "Bureaucrat(" << name << ", " << grade << ")" << std::endl;
}
Bureaucrat::~Bureaucrat()
{
std::cout << "~Bureaucrat()" << std::endl;
}
Bureaucrat& Bureaucrat::operator=(const Bureaucrat &src)
{
this->_grade = src._grade;
return *this;
}
void Bureaucrat::setGrade(int new_grade)
{
if (new_grade > 150)
throw Bureaucrat::GradeTooLowException();
if (new_grade < 1)
throw Bureaucrat::GradeTooHighException();
this->_grade = new_grade;
}
const std::string& Bureaucrat::getName(void) const
{
return this->_name;
}
int Bureaucrat::getGrade(void) const
{
return this->_grade;
}
void Bureaucrat::increment()
{
if (this->_grade == 1)
throw GradeTooHighException();
this->_grade--;
}
void Bureaucrat::decrement()
{
if (this->_grade == 150)
throw GradeTooLowException();
this->_grade++;
}
void Bureaucrat::signForm(AForm& form) const
{
form.beSigned(*this);
}
void Bureaucrat::executeForm(AForm const & form) const
{
try
{
form.execute(*this);
std::cout << this->_name << " executed " << form.getName() << std::endl;
}
catch (std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
const char* Bureaucrat::GradeTooLowException::what() const throw()
{
return "Too low grade";
}
const char* Bureaucrat::GradeTooHighException::what() const throw()
{
return "Too high grade";
}
std::ostream& operator<<(std::ostream &stream, Bureaucrat &src)
{
stream << src.getName() << ", bureaucrat grade " << src.getGrade() << "." << std::endl;
return stream;
}