From 525d4e2beb66f8ff2ec6492c9ac3251c84b22ac3 Mon Sep 17 00:00:00 2001 From: whaffman Date: Thu, 26 Jun 2025 13:52:00 +0200 Subject: [PATCH] Refactor AForm and derived classes to introduce isExecutable method for better code reuse and clarity --- ex02/inc/AForm.hpp | 22 ++++++++++++---------- ex02/src/AForm.cpp | 9 +++++++++ ex02/src/PresidentialPardonForm.cpp | 5 +---- ex02/src/RobotomyRequestForm.cpp | 11 ++++------- ex02/src/ShrubberyCreationForm.cpp | 6 +----- 5 files changed, 27 insertions(+), 26 deletions(-) diff --git a/ex02/inc/AForm.hpp b/ex02/inc/AForm.hpp index de0af13..142bc8b 100644 --- a/ex02/inc/AForm.hpp +++ b/ex02/inc/AForm.hpp @@ -10,22 +10,24 @@ class AForm { private: const std::string _name; - bool _isSigned; - const int _signGrade; - const int _executeGrade; + bool _isSigned; + const int _signGrade; + const int _executeGrade; public: - AForm() = delete; + AForm() = delete; AForm(std::string name, int signGrade, int executeGrade); AForm(const AForm &other); ~AForm(); - AForm &operator=(const AForm &other) = delete; + AForm &operator=(const AForm &other) = delete; const std::string &getName() const; - bool getIsSigned() const; - int getSignGrade() const; - int getExecuteGrade() const; - void beSigned(const Bureaucrat &bureaucrat); - virtual void execute(const Bureaucrat &executor) const = 0; // Pure virtual function, making AForm an abstract class + bool getIsSigned() const; + int getSignGrade() const; + int getExecuteGrade() const; + void beSigned(const Bureaucrat &bureaucrat); + bool isExecutable(const Bureaucrat &executor) const; + virtual void execute(const Bureaucrat &executor) + const = 0; // Pure virtual function, making AForm an abstract class class GradeTooHighException : public std::exception { diff --git a/ex02/src/AForm.cpp b/ex02/src/AForm.cpp index 2838378..9ee11da 100644 --- a/ex02/src/AForm.cpp +++ b/ex02/src/AForm.cpp @@ -56,6 +56,15 @@ void AForm::beSigned(const Bureaucrat &bureaucrat) _isSigned = true; } +bool AForm::isExecutable(const Bureaucrat &executor) const +{ + if (!_isSigned) + throw FormNotSignedException(); + if (executor.getGrade() > _executeGrade) + throw GradeTooLowException(); + return true; +} + const char *AForm::GradeTooHighException::what() const throw() { return "Form grade is too high!"; diff --git a/ex02/src/PresidentialPardonForm.cpp b/ex02/src/PresidentialPardonForm.cpp index f5662a7..3408467 100644 --- a/ex02/src/PresidentialPardonForm.cpp +++ b/ex02/src/PresidentialPardonForm.cpp @@ -40,10 +40,7 @@ std::ostream &operator<<(std::ostream &os, const PresidentialPardonForm &form) void PresidentialPardonForm::execute(const Bureaucrat &executor) const { - if (!getIsSigned()) - throw AForm::FormNotSignedException(); - if (executor.getGrade() > getExecuteGrade()) - throw AForm::GradeTooLowException(); + isExecutable(executor); std::cout << PRESIDENTIAL_PARDON_FORM<< executor.getName() << " made shure Zaphod Beeblebrox pardoned " << _target << "." << std::endl; diff --git a/ex02/src/RobotomyRequestForm.cpp b/ex02/src/RobotomyRequestForm.cpp index 5595ad1..1340eca 100644 --- a/ex02/src/RobotomyRequestForm.cpp +++ b/ex02/src/RobotomyRequestForm.cpp @@ -26,19 +26,16 @@ RobotomyRequestForm::~RobotomyRequestForm() void RobotomyRequestForm::execute(const Bureaucrat &executor) const { - if (!getIsSigned()) - throw AForm::FormNotSignedException(); - if (executor.getGrade() > getExecuteGrade()) - throw AForm::GradeTooLowException(); + isExecutable(executor); - std::cout << ROBOTOMY_REQUEST_FORM DESTRUCTOR + std::cout << ROBOTOMY_REQUEST_FORM << "Drrr, Drrr. Drilling noises... " << std::endl; std::srand(static_cast(std::time(0))); if (std::rand() % 2) - std::cout << ROBOTOMY_REQUEST_FORM DESTRUCTOR << _target + std::cout << ROBOTOMY_REQUEST_FORM << _target << " has been robotomized!" << std::endl; else - std::cout << ROBOTOMY_REQUEST_FORM DESTRUCTOR << _target + std::cout << ROBOTOMY_REQUEST_FORM << _target << "'s robotomy failed. " << _target << " is not the same anymore..." << std::endl; } diff --git a/ex02/src/ShrubberyCreationForm.cpp b/ex02/src/ShrubberyCreationForm.cpp index 2e06f9a..1c1f037 100644 --- a/ex02/src/ShrubberyCreationForm.cpp +++ b/ex02/src/ShrubberyCreationForm.cpp @@ -29,11 +29,7 @@ ShrubberyCreationForm::~ShrubberyCreationForm() void ShrubberyCreationForm::execute(const Bureaucrat &executor) const { - if (!getIsSigned()) - throw AForm::FormNotSignedException(); - if (executor.getGrade() > getExecuteGrade()) - throw AForm::GradeTooLowException(); - + isExecutable(executor); std::ofstream ofs(_target + "_shrubbery"); if (!ofs) {