85 lines
2 KiB
C++
85 lines
2 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(AForm &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;
|
|
}
|
|
}
|
|
|
|
void Bureaucrat::executeForm(const AForm &form) const{
|
|
try {
|
|
std::cout << name << " executed " << form.getName() << std::endl;
|
|
form.execute(*this);
|
|
}
|
|
catch (std::exception & e) {
|
|
std::cout << name << " couldn't execute " << 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";
|
|
}
|
|
|