Refactor AForm and derived classes to introduce isExecutable method for better code reuse and clarity

This commit is contained in:
whaffman 2025-06-26 13:52:00 +02:00
parent e631a84565
commit 525d4e2beb
5 changed files with 27 additions and 26 deletions

View File

@ -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
{

View File

@ -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!";

View File

@ -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;

View File

@ -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<unsigned int>(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;
}

View File

@ -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)
{