cpp/CPP05/ex01/Bureaucrat.cpp
2024-12-04 17:29:31 +01:00

75 lines
1.7 KiB
C++

#include "Bureaucrat.hpp"
Bureaucrat::Bureaucrat() : name("") {
this->grade = 150;
}
Bureaucrat::Bureaucrat(std::string name, int grade) :name(name){
if (grade > 150)
throw Bureaucrat::GradeTooLowException();
else if (grade < 1)
throw Bureaucrat::GradeTooHighException();
this->grade = grade;
}
Bureaucrat::~Bureaucrat() {
}
Bureaucrat::Bureaucrat(const Bureaucrat &other) : name(other.name) {
*this = other;
}
Bureaucrat &Bureaucrat::operator=(const Bureaucrat &other) {
if (this != &other) {
this->grade = other.grade;
}
return (*this);
}
std::string Bureaucrat::getName() const {
return this->name;
}
int Bureaucrat::getGrade() const {
return this->grade;
}
void Bureaucrat::increaseGrade() {
if (this->grade <= 1)
throw Bureaucrat::GradeTooHighException();
this->grade--;
}
void Bureaucrat::decreaseGrade() {
if (this->grade >= 150)
throw Bureaucrat::GradeTooLowException();
this->grade++;
}
void Bureaucrat::signForm(Form &form) {
if (form.isSigned()) {
std::cout << name << " couldn't sign " << form.getName() << " because it's already signed" << std::endl;
return;
}
try {
form.beSigned(*this);
std::cout << name << " signed " << form.getName() << std::endl;
}
catch (std::exception & e) {
std::cout << name << " couldn't sign " << form.getName() << " because grade too low" << std::endl;
}
}
std::ostream& operator<<( std::ostream& os, Bureaucrat const& crat ) {
return os << crat.getName() << ", bureaucrat grade " << crat.getGrade() << "." << std::endl;
}
char const* Bureaucrat::GradeTooHighException::what() const throw() {
return "Grade is too high";
}
char const* Bureaucrat::GradeTooLowException::what() const throw() {
return "Grade is too low";
}