Refactor AForm and derived classes to introduce isExecutable method for better code reuse and clarity
This commit is contained in:
parent
e631a84565
commit
525d4e2beb
@ -10,22 +10,24 @@ class AForm
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
const std::string _name;
|
const std::string _name;
|
||||||
bool _isSigned;
|
bool _isSigned;
|
||||||
const int _signGrade;
|
const int _signGrade;
|
||||||
const int _executeGrade;
|
const int _executeGrade;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
AForm() = delete;
|
AForm() = delete;
|
||||||
AForm(std::string name, int signGrade, int executeGrade);
|
AForm(std::string name, int signGrade, int executeGrade);
|
||||||
AForm(const AForm &other);
|
AForm(const AForm &other);
|
||||||
~AForm();
|
~AForm();
|
||||||
AForm &operator=(const AForm &other) = delete;
|
AForm &operator=(const AForm &other) = delete;
|
||||||
const std::string &getName() const;
|
const std::string &getName() const;
|
||||||
bool getIsSigned() const;
|
bool getIsSigned() const;
|
||||||
int getSignGrade() const;
|
int getSignGrade() const;
|
||||||
int getExecuteGrade() const;
|
int getExecuteGrade() const;
|
||||||
void beSigned(const Bureaucrat &bureaucrat);
|
void beSigned(const Bureaucrat &bureaucrat);
|
||||||
virtual void execute(const Bureaucrat &executor) const = 0; // Pure virtual function, making AForm an abstract class
|
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
|
class GradeTooHighException : public std::exception
|
||||||
{
|
{
|
||||||
|
|||||||
@ -56,6 +56,15 @@ void AForm::beSigned(const Bureaucrat &bureaucrat)
|
|||||||
_isSigned = true;
|
_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()
|
const char *AForm::GradeTooHighException::what() const throw()
|
||||||
{
|
{
|
||||||
return "Form grade is too high!";
|
return "Form grade is too high!";
|
||||||
|
|||||||
@ -40,10 +40,7 @@ std::ostream &operator<<(std::ostream &os, const PresidentialPardonForm &form)
|
|||||||
|
|
||||||
void PresidentialPardonForm::execute(const Bureaucrat &executor) const
|
void PresidentialPardonForm::execute(const Bureaucrat &executor) const
|
||||||
{
|
{
|
||||||
if (!getIsSigned())
|
isExecutable(executor);
|
||||||
throw AForm::FormNotSignedException();
|
|
||||||
if (executor.getGrade() > getExecuteGrade())
|
|
||||||
throw AForm::GradeTooLowException();
|
|
||||||
|
|
||||||
std::cout << PRESIDENTIAL_PARDON_FORM<< executor.getName() << " made shure Zaphod Beeblebrox pardoned "
|
std::cout << PRESIDENTIAL_PARDON_FORM<< executor.getName() << " made shure Zaphod Beeblebrox pardoned "
|
||||||
<< _target << "." << std::endl;
|
<< _target << "." << std::endl;
|
||||||
|
|||||||
@ -26,19 +26,16 @@ RobotomyRequestForm::~RobotomyRequestForm()
|
|||||||
|
|
||||||
void RobotomyRequestForm::execute(const Bureaucrat &executor) const
|
void RobotomyRequestForm::execute(const Bureaucrat &executor) const
|
||||||
{
|
{
|
||||||
if (!getIsSigned())
|
isExecutable(executor);
|
||||||
throw AForm::FormNotSignedException();
|
|
||||||
if (executor.getGrade() > getExecuteGrade())
|
|
||||||
throw AForm::GradeTooLowException();
|
|
||||||
|
|
||||||
std::cout << ROBOTOMY_REQUEST_FORM DESTRUCTOR
|
std::cout << ROBOTOMY_REQUEST_FORM
|
||||||
<< "Drrr, Drrr. Drilling noises... " << std::endl;
|
<< "Drrr, Drrr. Drilling noises... " << std::endl;
|
||||||
std::srand(static_cast<unsigned int>(std::time(0)));
|
std::srand(static_cast<unsigned int>(std::time(0)));
|
||||||
if (std::rand() % 2)
|
if (std::rand() % 2)
|
||||||
std::cout << ROBOTOMY_REQUEST_FORM DESTRUCTOR << _target
|
std::cout << ROBOTOMY_REQUEST_FORM << _target
|
||||||
<< " has been robotomized!" << std::endl;
|
<< " has been robotomized!" << std::endl;
|
||||||
else
|
else
|
||||||
std::cout << ROBOTOMY_REQUEST_FORM DESTRUCTOR << _target
|
std::cout << ROBOTOMY_REQUEST_FORM << _target
|
||||||
<< "'s robotomy failed. " << _target
|
<< "'s robotomy failed. " << _target
|
||||||
<< " is not the same anymore..." << std::endl;
|
<< " is not the same anymore..." << std::endl;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,11 +29,7 @@ ShrubberyCreationForm::~ShrubberyCreationForm()
|
|||||||
|
|
||||||
void ShrubberyCreationForm::execute(const Bureaucrat &executor) const
|
void ShrubberyCreationForm::execute(const Bureaucrat &executor) const
|
||||||
{
|
{
|
||||||
if (!getIsSigned())
|
isExecutable(executor);
|
||||||
throw AForm::FormNotSignedException();
|
|
||||||
if (executor.getGrade() > getExecuteGrade())
|
|
||||||
throw AForm::GradeTooLowException();
|
|
||||||
|
|
||||||
std::ofstream ofs(_target + "_shrubbery");
|
std::ofstream ofs(_target + "_shrubbery");
|
||||||
if (!ofs)
|
if (!ofs)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user