You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
3.2 KiB
97 lines
3.2 KiB
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* Bureaucrat.cpp :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */
|
|
/* Updated: 2022/06/27 15:03:06 by narnaud ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "Bureaucrat.hpp"
|
|
|
|
Bureaucrat::Bureaucrat(void) {
|
|
}
|
|
|
|
Bureaucrat::Bureaucrat(const string name,
|
|
int grade) throw(Bureaucrat::GradeTooHighException,
|
|
Bureaucrat::GradeTooLowException)
|
|
: _name(name) {
|
|
|
|
if (grade < 1)
|
|
throw Bureaucrat::GradeTooHighException();
|
|
if (grade > 150)
|
|
throw Bureaucrat::GradeTooLowException();
|
|
_grade = grade;
|
|
}
|
|
|
|
Bureaucrat::Bureaucrat(Bureaucrat const &src) {
|
|
(void)src;
|
|
}
|
|
|
|
Bureaucrat &Bureaucrat::operator=(Bureaucrat const &src) {
|
|
(void)src;
|
|
return (*this);
|
|
}
|
|
|
|
Bureaucrat::~Bureaucrat(void) {
|
|
}
|
|
|
|
const string Bureaucrat::getName(void) const { return (_name); }
|
|
|
|
int Bureaucrat::getGrade(void) const { return (_grade); }
|
|
|
|
void Bureaucrat::incrGrade(int diff) throw(Bureaucrat::GradeTooHighException) {
|
|
int new_grade = _grade - diff;
|
|
if (new_grade < 1)
|
|
throw Bureaucrat::GradeTooHighException();
|
|
_grade = new_grade;
|
|
}
|
|
|
|
void Bureaucrat::decrGrade(int diff) throw(Bureaucrat::GradeTooLowException) {
|
|
int new_grade = _grade + diff;
|
|
if (new_grade > 150)
|
|
throw Bureaucrat::GradeTooLowException();
|
|
_grade = new_grade;
|
|
}
|
|
|
|
const char *Bureaucrat::GradeTooHighException::what(void) const throw() {
|
|
return ("Grade was too high for a bureaucrat");
|
|
}
|
|
|
|
const char *Bureaucrat::GradeTooLowException::what(void) const throw() {
|
|
return ("Grade was too low for a bureaucrat");
|
|
}
|
|
|
|
void Bureaucrat::signForm(Form &form) const {
|
|
try {
|
|
if (!form.beSigned(*this))
|
|
cout << _name << " succesfully signed " << form.getName() << endl;
|
|
else
|
|
cout << _name << " failed to sign " << form.getName()
|
|
<< ": form is already signed." << endl;
|
|
} catch (std::exception &e) {
|
|
cout << _name << " failed to sign " << form.getName() << ": " << e.what()
|
|
<< endl;
|
|
}
|
|
}
|
|
|
|
void Bureaucrat::executeForm(const Form &form) const {
|
|
try {
|
|
if (!form.execute(*this))
|
|
cout << _name << " succesfully executed " << form.getName() << endl;
|
|
else
|
|
cout << _name << " failed to execute " << form.getName()
|
|
<< ": form is not signed" << endl;
|
|
} catch (std::exception &e) {
|
|
cout << _name << " failed to execute " << form.getName() << ": " << e.what()
|
|
<< endl;
|
|
}
|
|
}
|
|
|
|
std::ostream &operator<<(std::ostream &out, Bureaucrat const &b) {
|
|
out << b.getName() << ", bureaucrat grade " << b.getGrade() << endl;
|
|
return (out);
|
|
}
|
|
|