ex01
This commit is contained in:
132
ex01/Bureaucrat/Bureaucrat.cpp
Normal file
132
ex01/Bureaucrat/Bureaucrat.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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 "../Form/Form.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++;
|
||||
}
|
||||
|
||||
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
|
||||
<< "couldn't sign "
|
||||
<< 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!");
|
||||
}
|
||||
56
ex01/Bureaucrat/Bureaucrat.hpp
Normal file
56
ex01/Bureaucrat/Bureaucrat.hpp
Normal file
@@ -0,0 +1,56 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Bureaucrat.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef BUREAUCRAT_HPP
|
||||
|
||||
# define BUREAUCRAT_HPP
|
||||
|
||||
# include <iostream>
|
||||
|
||||
class Form;
|
||||
|
||||
class Bureaucrat
|
||||
{
|
||||
private:
|
||||
const std::string name;
|
||||
int grade;
|
||||
|
||||
public:
|
||||
Bureaucrat();
|
||||
Bureaucrat(const Bureaucrat &other);
|
||||
Bureaucrat &operator=(const Bureaucrat &other);
|
||||
~Bureaucrat();
|
||||
|
||||
Bureaucrat(std::string name, int grade);
|
||||
|
||||
std::string getName() const;
|
||||
int getGrade() const;
|
||||
|
||||
void incrementGrade();
|
||||
void decrementGrade();
|
||||
|
||||
void signForm(Form &form);
|
||||
|
||||
class GradeTooHighException : public std::exception {
|
||||
public:
|
||||
virtual const char* what() const throw();
|
||||
};
|
||||
|
||||
class GradeTooLowException : public std::exception {
|
||||
public:
|
||||
virtual const char* what() const throw();
|
||||
};
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream &os, const Bureaucrat &bureaucrat);
|
||||
|
||||
#endif
|
||||
BIN
ex01/Bureaucrat/Bureaucrat.o
Normal file
BIN
ex01/Bureaucrat/Bureaucrat.o
Normal file
Binary file not shown.
Reference in New Issue
Block a user