cpp-partie-2/cpp05/ex02/Bureaucrat.cpp
2024-02-20 19:24:13 +01:00

125 lines
3.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 13:16:26 by fgras-ca #+# #+# */
/* Updated: 2024/02/20 17:02:49 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
// Constructeur par défaut
Bureaucrat::Bureaucrat() : name("Default"), grade(150)
{
std::cout << GREEN << "Bureaucrat default constructor called!" << RESET << std::endl;
}
// Constructeur avec paramètres
Bureaucrat::Bureaucrat(const std::string& name, int grade) :name(name), grade(grade)
{
if (grade < 1)
{
throw GradeTooHighException();
}
else if (grade > 150)
{
throw GradeTooLowException();
}
this->grade = grade;
std::cout << GREEN << "Bureaucrat " << this->name << " constructor called with grade " << this->grade << RESET << std::endl;
}
// Constructeur de copie
Bureaucrat::Bureaucrat(const Bureaucrat& other) : name(other.name), grade(other.grade)
{
std::cout << CYAN << "Copy constructor called for Bureaucrat " << this->name << RESET << std::endl;
}
// Opérateur d'affectation
Bureaucrat& Bureaucrat::operator=(const Bureaucrat& other)
{
if (this != &other)
{
// L'attribut name étant const, nous ne pouvons pas le modifier. Seul grade est copié.
this->grade = other.grade;
}
return (*this);
}
// Destructeur
Bureaucrat::~Bureaucrat()
{
std::cout << RED << "Bureaucrat " << this->name << " is destroyed!" << RESET << std::endl;
}
// Getter pour le nom
std::string Bureaucrat::getName() const
{
return (this->name);
}
// Getter pour le grade
int Bureaucrat::getGrade() const
{
return (this->grade);
}
// Incrémente le grade
void Bureaucrat::incrementGrade()
{
if (grade <= 1)
{
throw GradeTooHighException();
}
--grade;
}
// Décrémente le grade
void Bureaucrat::decrementGrade()
{
if (grade >= 150)
{
throw GradeTooLowException();
}
++grade;
}
void Bureaucrat::signForm(Form& form)
{
try
{
form.beSigned(*this);
std::cout << this->name << " signed " << form.getName() << std::endl;
}
catch (std::exception& e)
{
std::cout << this->name << " couldnt sign " << form.getName() << " because " << e.what() << std::endl;
}
}
void Bureaucrat::executeForm(AForm const &form)
{
try
{
form.execute(*this);
std::cout << name << " executed " << form.getName() << std::endl;
}
catch (std::exception& e)
{
std::cout << name << " couldnt execute " << form.getName() << " because: " << e.what() << std::endl;
}
}
// Méthodes pour les exceptions
const char* Bureaucrat::GradeTooHighException::what() const throw()
{
return ("Grade too hight\n");
}
const char* Bureaucrat::GradeTooLowException::what() const throw()
{
return ("Grade too low\n");
}
// Surcharge de l'opérateur <<
std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat)
{
os << bureaucrat.getName() << " bureaucrat grade " << bureaucrat.getGrade();
return (os);
}