Starting ex03
This commit is contained in:
154
ex03/Bureaucrat/Bureaucrat.cpp
Normal file
154
ex03/Bureaucrat/Bureaucrat.cpp
Normal file
@@ -0,0 +1,154 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Bureaucrat.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/15 18:00:27 by aortigos #+# #+# */
|
||||
/* Updated: 2026/02/06 02:28:09 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!");
|
||||
}
|
||||
Reference in New Issue
Block a user