|
| 1 | +#include "AForm.hpp" |
| 2 | +#include "Bureaucrat.hpp" |
| 3 | + |
| 4 | +AForm::AForm(std::string name, int gradeToSign, int gradeToExecute, std::string target) |
| 5 | + : _name(name), _signed(false), _gradeToSign(gradeToSign), _gradeToExecute(gradeToExecute), _target(target) { |
| 6 | + if (gradeToSign < 1 || gradeToExecute < 1) { |
| 7 | + throw GradeTooHighException(); |
| 8 | + } |
| 9 | + if (gradeToSign > 150 || gradeToExecute > 150) { |
| 10 | + throw GradeTooLowException(); |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +AForm::AForm(const AForm& other) : _name(other._name), _signed(other._signed), _gradeToSign(other._gradeToSign), _gradeToExecute(other._gradeToExecute), _target(other._target) {} |
| 15 | + |
| 16 | +AForm& AForm::operator=(const AForm& other) { |
| 17 | + if (this != &other) { |
| 18 | + _signed = other.isSigned(); |
| 19 | + } |
| 20 | + return *this; |
| 21 | +} |
| 22 | + |
| 23 | +AForm::~AForm() {} |
| 24 | + |
| 25 | +std::string AForm::getName() const { |
| 26 | + return _name; |
| 27 | +} |
| 28 | + |
| 29 | +bool AForm::isSigned() const { |
| 30 | + return _signed; |
| 31 | +} |
| 32 | + |
| 33 | +int AForm::getGradeToSign() const { |
| 34 | + return _gradeToSign; |
| 35 | +} |
| 36 | + |
| 37 | +int AForm::getGradeToExecute() const { |
| 38 | + return _gradeToExecute; |
| 39 | +} |
| 40 | + |
| 41 | +std::string AForm::getTarget() const { |
| 42 | + return _target; |
| 43 | +} |
| 44 | + |
| 45 | +void AForm::beSigned(const Bureaucrat& bureaucrat) { |
| 46 | + if (bureaucrat.getGrade() <= _gradeToSign) { |
| 47 | + _signed = true; |
| 48 | + } else { |
| 49 | + throw GradeTooLowException(); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +void AForm::checkExecutability(const Bureaucrat& executor) const { |
| 54 | + if (!_signed) { |
| 55 | + throw FormNotSignedException(); |
| 56 | + } |
| 57 | + if (executor.getGrade() > _gradeToExecute) { |
| 58 | + throw GradeTooLowException(); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +const char* AForm::GradeTooHighException::what() const throw() { |
| 63 | + return "Grade is too high!"; |
| 64 | +} |
| 65 | + |
| 66 | +const char* AForm::GradeTooLowException::what() const throw() { |
| 67 | + return "Grade is too low!"; |
| 68 | +} |
| 69 | + |
| 70 | +const char* AForm::FormNotSignedException::what() const throw() { |
| 71 | + return "Form is not signed!"; |
| 72 | +} |
| 73 | + |
| 74 | +std::ostream& operator<<(std::ostream& os, const AForm& form) { |
| 75 | + os << "Form: " << form.getName() |
| 76 | + << ", Signed: " << (form.isSigned() ? "Yes" : "No") |
| 77 | + << ", Grade to Sign: " << form.getGradeToSign() |
| 78 | + << ", Grade to Execute: " << form.getGradeToExecute(); |
| 79 | + return os; |
| 80 | +} |
0 commit comments