155 lines
3.4 KiB
C++
155 lines
3.4 KiB
C++
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* Bureaucrat.cpp :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2026/02/21 12:12:30 by aortigos #+# #+# */
|
|
/* Updated: 2026/02/21 12:12:31 by aortigos ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "Bureaucrat.hpp"
|
|
#include "../AForm/AForm.hpp"
|
|
|
|
//////////////////
|
|
//Constructores //
|
|
//////////////////
|
|
|
|
Bureaucrat::Bureaucrat() : name("NULL"), grade(150)
|
|
{
|
|
//std::cout << "Bureaucrat has been created" << std::endl;
|
|
}
|
|
|
|
Bureaucrat::Bureaucrat(const Bureaucrat &other) : name(other.getName()), grade(other.getGrade())
|
|
{
|
|
//std::cout << "Copy assignment called" << std::endl;
|
|
}
|
|
|
|
Bureaucrat& Bureaucrat::operator=(const Bureaucrat &other)
|
|
{
|
|
if (this != &other)
|
|
this->grade = other.getGrade();
|
|
//std::cout << "Copy operator called" << std::endl;
|
|
return (*this);
|
|
}
|
|
|
|
Bureaucrat::~Bureaucrat()
|
|
{
|
|
//std::cout << "Bureaucrat destroyed" << std::endl;
|
|
}
|
|
|
|
Bureaucrat::Bureaucrat(std::string name, int grade) : name(name)
|
|
{
|
|
if (grade > 150)
|
|
throw GradeTooLowException();
|
|
else if (grade < 1)
|
|
throw GradeTooHighException();
|
|
this->grade = grade;
|
|
//std::cout << "Bureaucrat with params has been created" << std::endl;
|
|
}
|
|
|
|
|
|
//////////////////
|
|
// Getters //
|
|
//////////////////
|
|
|
|
std::string Bureaucrat::getName() const
|
|
{
|
|
return (this->name);
|
|
}
|
|
|
|
int Bureaucrat::getGrade() const
|
|
{
|
|
return (this->grade);
|
|
}
|
|
|
|
|
|
//////////////////
|
|
// Grade //
|
|
//////////////////
|
|
|
|
void Bureaucrat::incrementGrade()
|
|
{
|
|
if (this->grade <= 1)
|
|
throw GradeTooHighException();
|
|
this->grade--;
|
|
}
|
|
|
|
void Bureaucrat::decrementGrade()
|
|
{
|
|
if (this->grade >= 150)
|
|
throw GradeTooLowException();
|
|
this->grade++;
|
|
}
|
|
|
|
//////////////////
|
|
// Sign //
|
|
//////////////////
|
|
|
|
void Bureaucrat::signForm(AForm &form)
|
|
{
|
|
try {
|
|
form.beSigned(*this);
|
|
std::cout << this->name
|
|
<< " signed "
|
|
<< form.getName()
|
|
<< std::endl;
|
|
} catch (std::exception &e) {
|
|
std::cout << this->name
|
|
<< " couldn't sign "
|
|
<< form.getName()
|
|
<< " because "
|
|
<< e.what()
|
|
<< std::endl;
|
|
}
|
|
}
|
|
|
|
void Bureaucrat::executeForm(AForm &form)
|
|
{
|
|
try {
|
|
form.execute(*this);
|
|
std::cout << this->name
|
|
<< " executed "
|
|
<< form.getName()
|
|
<< std::endl;
|
|
} catch (std::exception &e) {
|
|
std::cout << this->name
|
|
<< " couldn't execute "
|
|
<< form.getName()
|
|
<< " because "
|
|
<< e.what()
|
|
<< std::endl;
|
|
}
|
|
}
|
|
|
|
|
|
//////////////////
|
|
// << //
|
|
//////////////////
|
|
|
|
std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
|
|
{
|
|
os << bureaucrat.getName()
|
|
<< ", bureaucrat grade "
|
|
<< bureaucrat.getGrade()
|
|
<< std::endl;
|
|
return (os);
|
|
}
|
|
|
|
|
|
//////////////////
|
|
// Excepciones //
|
|
//////////////////
|
|
|
|
const char* Bureaucrat::GradeTooHighException::what() const throw()
|
|
{
|
|
return ("Grade is too high!");
|
|
}
|
|
|
|
const char* Bureaucrat::GradeTooLowException::what() const throw()
|
|
{
|
|
return ("Grade is too low!");
|
|
}
|