From: Lukas Jiriste Date: Sun, 19 Jan 2025 14:57:16 +0000 (+0100) Subject: Implement everything except for execute functions X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=87ac2693e36a144f334c737605b80d35c0c20d89;p=42%2FCPP_Module_05 Implement everything except for execute functions --- diff --git a/ex02/AForm.cpp b/ex02/AForm.cpp index 56f714b..5d77859 100644 --- a/ex02/AForm.cpp +++ b/ex02/AForm.cpp @@ -9,11 +9,12 @@ AForm::AForm() { } -AForm::AForm(const std::string &name, int signatory_grade, int executor_grade) +AForm::AForm(const std::string &name, int signatory_grade, int executor_grade, std::string target) : m_name(name) , m_is_signed(0) , m_signatory_grade(signatory_grade) , m_executor_grade(executor_grade) + , m_target(target) { if (signatory_grade < Bureaucrat::HIGHEST_GRADE || executor_grade < Bureaucrat::HIGHEST_GRADE) throw (Bureaucrat::GradeTooHighException()); @@ -69,6 +70,8 @@ void AForm::beSigned(const Bureaucrat &bureaucrat) void AForm::checkExecutorGrade(const Bureaucrat &bureaucrat) const { + if (!m_is_signed) + throw (AForm::FormNotSignedException()); if (bureaucrat.getGrade() > m_executor_grade) throw (Bureaucrat::GradeTooLowException()); } @@ -85,3 +88,23 @@ std::ostream &operator<<(std::ostream &ostream, const AForm &form) ostream << "\t\t\t\tSignature\n"; return (ostream); } + +AForm::FormNotSignedException::FormNotSignedException() + : std::runtime_error::runtime_error("The form is not signed") +{ +} + +AForm::FormNotSignedException::FormNotSignedException(const AForm::FormNotSignedException &other) + : std::runtime_error::runtime_error(other.what()) +{ +} + +AForm::FormNotSignedException::~FormNotSignedException() throw() +{ +} + +AForm::FormNotSignedException &AForm::FormNotSignedException::operator=(const AForm::FormNotSignedException &other) +{ + std::runtime_error::operator = (other); + return (*this); +} diff --git a/ex02/AForm.h b/ex02/AForm.h index 052f11d..81c657f 100644 --- a/ex02/AForm.h +++ b/ex02/AForm.h @@ -12,13 +12,17 @@ class AForm bool m_is_signed; const int m_signatory_grade; const int m_executor_grade; + const std::string m_target; + // These are protected only so that inheriting classes can use them + // in their "non implementations" of there functions + protected: AForm(); AForm &operator=(const AForm &other); public: - AForm(const std::string &name, int signatory_grade, int executor_grade); + AForm(const std::string &name, int signatory_grade, int executor_grade, std::string target); AForm(const AForm &other); virtual ~AForm(); @@ -29,8 +33,20 @@ class AForm void beSigned(const Bureaucrat &bureaucrat); void checkExecutorGrade(const Bureaucrat &bureaucrat) const; void virtual execute(const Bureaucrat &bureaucrat) const = 0; + + class FormNotSignedException; }; std::ostream &operator<<(std::ostream &ostream, const AForm &form); +class AForm::FormNotSignedException : public std::runtime_error +{ + public: + FormNotSignedException(); + FormNotSignedException(const FormNotSignedException &other); + ~FormNotSignedException() throw(); + + FormNotSignedException &operator=(const FormNotSignedException &other); +}; + #endif // AFORM_H diff --git a/ex02/Bureaucrat.cpp b/ex02/Bureaucrat.cpp index ae6a380..7d34b8c 100644 --- a/ex02/Bureaucrat.cpp +++ b/ex02/Bureaucrat.cpp @@ -88,6 +88,11 @@ void Bureaucrat::executeForm(const AForm &form) const << " because his grade " << m_grade << " is lower than the necessary " << form.getSignatoryGrade() << '\n'; } + catch (const AForm::FormNotSignedException &exception) + { + std::cout << m_name << " couldn't execute the form " << form.getName() + << " because the norm has not yet been signed.\n"; + } } std::ostream &operator<<(std::ostream &ostream, const Bureaucrat &bureaucrat) diff --git a/ex02/Makefile b/ex02/Makefile index 9261211..d1fd937 100644 --- a/ex02/Makefile +++ b/ex02/Makefile @@ -15,9 +15,12 @@ endif SRCDIR := . -SOURCES := main.cpp \ - Bureaucrat.cpp \ - AForm.cpp \ +SOURCES := main.cpp \ + Bureaucrat.cpp \ + AForm.cpp \ + ShrubberyCreationForm.cpp \ + RobotomyRequestForm.cpp \ + PresidentialPardonForm.cpp \ SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES)) diff --git a/ex02/PresidentialPardonForm.cpp b/ex02/PresidentialPardonForm.cpp new file mode 100644 index 0000000..322873f --- /dev/null +++ b/ex02/PresidentialPardonForm.cpp @@ -0,0 +1,25 @@ +#include "PresidentialPardonForm.h" + +PresidentialPardonForm::PresidentialPardonForm() + : AForm() +{ +} + +PresidentialPardonForm::PresidentialPardonForm(const std::string &target) + : AForm("Presidential Pardon Form", 25, 5, target) +{ +} + +PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm &other) + : AForm(other) +{ +} + +PresidentialPardonForm::~PresidentialPardonForm() +{ +} + +void PresidentialPardonForm::execute(Bureaucrat const &executor) const +{ + checkExecutorGrade(executor); +} diff --git a/ex02/PresidentialPardonForm.h b/ex02/PresidentialPardonForm.h new file mode 100644 index 0000000..5f5c166 --- /dev/null +++ b/ex02/PresidentialPardonForm.h @@ -0,0 +1,24 @@ +#ifndef PRESIDENTIALPARDONFORM_H +# define PRESIDENTIALPARDONFORM_H + +#include "AForm.h" +#include + +class PresidentialPardonForm : public AForm +{ + // These are protected only so that inheriting classes can use them + // in their "non implementations" of there functions + protected: + PresidentialPardonForm(); + + using AForm::operator=; + + public: + PresidentialPardonForm(const std::string &target); + PresidentialPardonForm(const PresidentialPardonForm &other); + ~PresidentialPardonForm(); + + void execute(Bureaucrat const &executor) const; +}; + +#endif // PRESIDENTIALPARDONFORM_H diff --git a/ex02/RobotomyRequestForm.cpp b/ex02/RobotomyRequestForm.cpp new file mode 100644 index 0000000..a814a90 --- /dev/null +++ b/ex02/RobotomyRequestForm.cpp @@ -0,0 +1,25 @@ +#include "RobotomyRequestForm.h" + +RobotomyRequestForm::RobotomyRequestForm() + : AForm() +{ +} + +RobotomyRequestForm::RobotomyRequestForm(const std::string &target) + : AForm("Robotomy Request Form", 72, 45, target) +{ +} + +RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm &other) + : AForm(other) +{ +} + +RobotomyRequestForm::~RobotomyRequestForm() +{ +} + +void RobotomyRequestForm::execute(Bureaucrat const &executor) const +{ + checkExecutorGrade(executor); +} diff --git a/ex02/RobotomyRequestForm.h b/ex02/RobotomyRequestForm.h new file mode 100644 index 0000000..b81ccfa --- /dev/null +++ b/ex02/RobotomyRequestForm.h @@ -0,0 +1,24 @@ +#ifndef ROBOTOMYREQUESTFORM_H +# define ROBOTOMYREQUESTFORM_H + +#include "AForm.h" +#include + +class RobotomyRequestForm : public AForm +{ + // These are protected only so that inheriting classes can use them + // in their "non implementations" of there functions + protected: + RobotomyRequestForm(); + + using AForm::operator=; + + public: + RobotomyRequestForm(const std::string &target); + RobotomyRequestForm(const RobotomyRequestForm &other); + ~RobotomyRequestForm(); + + void execute(Bureaucrat const &executor) const; +}; + +#endif // ROBOTOMYREQUESTFORM_H diff --git a/ex02/ShrubberyCreationForm.cpp b/ex02/ShrubberyCreationForm.cpp new file mode 100644 index 0000000..d3ec877 --- /dev/null +++ b/ex02/ShrubberyCreationForm.cpp @@ -0,0 +1,25 @@ +#include "ShrubberyCreationForm.h" + +ShrubberyCreationForm::ShrubberyCreationForm() + : AForm() +{ +} + +ShrubberyCreationForm::ShrubberyCreationForm(const std::string &target) + : AForm("Shrubbery Creation Form", 145, 137, target) +{ +} + +ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm &other) + : AForm(other) +{ +} + +ShrubberyCreationForm::~ShrubberyCreationForm() +{ +} + +void ShrubberyCreationForm::execute(Bureaucrat const &executor) const +{ + checkExecutorGrade(executor); +} diff --git a/ex02/ShrubberyCreationForm.h b/ex02/ShrubberyCreationForm.h new file mode 100644 index 0000000..9ee7e15 --- /dev/null +++ b/ex02/ShrubberyCreationForm.h @@ -0,0 +1,24 @@ +#ifndef SHRUBBERYCREATIONFORM_H +# define SHRUBBERYCREATIONFORM_H + +#include "AForm.h" +#include + +class ShrubberyCreationForm : public AForm +{ + // These are protected only so that inheriting classes can use them + // in their "non implementations" of there functions + protected: + ShrubberyCreationForm(); + + using AForm::operator=; + + public: + ShrubberyCreationForm(const std::string &target); + ShrubberyCreationForm(const ShrubberyCreationForm &other); + ~ShrubberyCreationForm(); + + void execute(Bureaucrat const &executor) const; +}; + +#endif // SHRUBBERYCREATIONFORM_H diff --git a/ex02/ShruberryCreationForm.h b/ex02/ShruberryCreationForm.h deleted file mode 100644 index 0c47e1c..0000000 --- a/ex02/ShruberryCreationForm.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef SHRUBERRYCREATIONFORM_H -# define SHRUBERRYCREATIONFORM_H - -class ShruberryCreationForm -{ - private: - - public: - using l - ShruberryCreationForm(const ShruberryCreationForm &other); - ~ShruberryCreationForm(); - - ShruberryCreationForm &operator=(const ShruberryCreationForm &other); -}; - -ShruberryCreationForm::ShruberryCreationForm(const std::string &target) - : AForm("Shruberry Creation Form", 145, 137, target) -{ -} - -ShruberryCreationForm::~ShruberryCreationForm() -{ -} - -#endif // SHRUBERRYCREATIONFORM_H diff --git a/ex02/main.cpp b/ex02/main.cpp index fda8686..95f808d 100644 --- a/ex02/main.cpp +++ b/ex02/main.cpp @@ -1,11 +1,43 @@ #include "Bureaucrat.h" #include "AForm.h" +#include "ShrubberyCreationForm.h" +#include "RobotomyRequestForm.h" +#include "PresidentialPardonForm.h" #include int main(void) { - Bureaucrat boss("Boss", 1); - Bureaucrat employee("Employee", 110); + Bureaucrat employee0("Employee0", 1); + Bureaucrat employee1("Employee1", 20); + Bureaucrat employee2("Employee2", 50); + Bureaucrat employee3("Employee3", 140); + Bureaucrat employee4("Employee4", 150); + ShrubberyCreationForm shrub("test_sh"); + RobotomyRequestForm robot("test_rob"); + PresidentialPardonForm pres("test_pres"); + employee4.signForm(shrub); + employee3.signForm(robot); + employee2.signForm(pres); + std::cout << '\n'; + employee3.executeForm(shrub); + employee2.executeForm(robot); + employee1.executeForm(pres); + std::cout << '\n'; + employee2.executeForm(shrub); + employee1.executeForm(robot); + employee0.executeForm(pres); + std::cout << '\n'; + employee3.signForm(shrub); + employee2.signForm(robot); + employee1.signForm(pres); + std::cout << '\n'; + employee3.executeForm(shrub); + employee2.executeForm(robot); + employee1.executeForm(pres); + std::cout << '\n'; + employee2.executeForm(shrub); + employee1.executeForm(robot); + employee0.executeForm(pres); return (0); }