cpp05/ex02/Bureaucrat.cpp

98 lines
1.7 KiB
C++

#include "AForm.hpp"
#include "Bureaucrat.hpp"
Bureaucrat::Bureaucrat() : _name("default"), _grade(150)
{
}
Bureaucrat::Bureaucrat(const Bureaucrat &copy) : _name(copy._name), _grade(copy._grade)
{
}
Bureaucrat::Bureaucrat(const std::string name, int grade) : _name(name)
{
if (grade < 1)
throw GradeTooHighException();
else if (grade > 150)
throw GradeTooLowException();
else
_grade = grade;
}
Bureaucrat &Bureaucrat::operator=(const Bureaucrat &other)
{
if (this != &other)
_grade = other._grade;
return (*this);
}
Bureaucrat::~Bureaucrat()
{
}
std::string Bureaucrat::getName() const
{
return (_name);
}
int Bureaucrat::getGrade() const
{
return (_grade);
}
void Bureaucrat::incrementGrade()
{
if (_grade == 1)
throw GradeTooHighException();
else
_grade--;
}
void Bureaucrat::decrementGrade()
{
if (_grade == 150)
throw GradeTooLowException();
else
_grade++;
}
std::ostream &operator<<(std::ostream &os, Bureaucrat const &other)
{
os << other.getName() << ", bureaucrat grade " << other.getGrade() << std::endl;
return (os);
}
const char *Bureaucrat::GradeTooHighException::what() const throw()
{
return ("Grade too high!");
}
const char *Bureaucrat::GradeTooLowException::what() const throw()
{
return ("Grade too low!");
}
void Bureaucrat::signForm(AForm &form)
{
try
{
form.beSigned(*this);
}
catch(const std::exception& e)
{
std::cout << _name << " couldn't sign " << form.getName() << " because " << e.what() << std::endl;
return;
}
std::cout << _name << " signed " << form.getName() << std::endl;
}
void Bureaucrat::executeForm(const AForm &form)
{
try {
form.execute(*this);
std::cout << _name << " executed " << form.getName() << std::endl;
} catch (const std::exception &e) {
std::cerr << e.what() << '\n';
}
}