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.

94 lines
3.0 KiB

3 years ago
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/24 13:06:55 by narnaud #+# #+# */
3 years ago
/* Updated: 2022/06/27 09:13:57 by narnaud ### ########.fr */
3 years ago
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
2 years ago
/* Constructors */
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;
3 years ago
}
2 years ago
/* copy const. */
Bureaucrat::Bureaucrat(Bureaucrat const &src) { (void)src; }
3 years ago
2 years ago
/* assign const. */
Bureaucrat &Bureaucrat::operator=(Bureaucrat const &src) {
(void)src;
return (*this);
3 years ago
}
2 years ago
/* Destructor */
Bureaucrat::~Bureaucrat(void) {}
3 years ago
2 years ago
/* Getters */
3 years ago
2 years ago
const string Bureaucrat::getName(void) const { return (_name); }
3 years ago
2 years ago
int Bureaucrat::getGrade(void) const { return (_grade); }
3 years ago
2 years ago
/* Setters */
3 years ago
2 years ago
void Bureaucrat::incrGrade(int diff) throw(Bureaucrat::GradeTooHighException) {
int new_grade = _grade - diff;
if (new_grade < 1)
throw Bureaucrat::GradeTooHighException();
_grade = new_grade;
cout << _name << " grade increased." << endl;
3 years ago
}
2 years ago
void Bureaucrat::decrGrade(int diff) throw(Bureaucrat::GradeTooLowException) {
int new_grade = _grade + diff;
if (new_grade > 150)
throw Bureaucrat::GradeTooLowException();
_grade = new_grade;
cout << _name << " grade decreased." << endl;
3 years ago
}
2 years ago
/* Exceptions */
const char *Bureaucrat::GradeTooHighException::what(void) const throw() {
return ("error: grade is too high for a bureaucrat");
3 years ago
}
2 years ago
const char *Bureaucrat::GradeTooLowException::what(void) const throw() {
return ("error: grade is too low for a bureaucrat");
3 years ago
}
2 years ago
/* Externs */
void Bureaucrat::signForm(Form &form) const {
cout << *this << " sign " << form << ":";
try {
if (!form.beSigned(*this))
cout << " success."<< endl;
else
cout << " error.\n\tform is already signed." << endl;
} catch (std::exception &e) {
cout << " error.\n\tbureaucrat grade is too low." << endl;
}
3 years ago
}
2 years ago
/* Stream */
std::ostream &operator<<(std::ostream &out, Bureaucrat const &b) {
out << b.getName() << "(" << b.getGrade() << ")";
return (out);
3 years ago
}