From 43c84bca09c8956e0cfd66a64691973b54dd8aae Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Tue, 21 Jan 2025 12:44:14 +0100 Subject: [PATCH] Implement ex03 --- ex03/AForm.h | 3 +- ex03/Intern.cpp | 41 ++++++++++++++++++ ex03/Intern.h | 29 +++++++++++++ ex03/Makefile | 1 + ex03/PresidentialPardonForm.cpp | 5 +++ ex03/PresidentialPardonForm.h | 5 ++- ex03/RobotomyRequestForm.cpp | 5 +++ ex03/RobotomyRequestForm.h | 6 ++- ex03/ShrubberyCreationForm.cpp | 5 +++ ex03/ShrubberyCreationForm.h | 5 ++- ex03/main.cpp | 75 ++++++++++++++++++--------------- 11 files changed, 138 insertions(+), 42 deletions(-) create mode 100644 ex03/Intern.cpp create mode 100644 ex03/Intern.h diff --git a/ex03/AForm.h b/ex03/AForm.h index c6f867c..03cd41c 100644 --- a/ex03/AForm.h +++ b/ex03/AForm.h @@ -33,7 +33,8 @@ class AForm const std::string &getTarget() const; void beSigned(const Bureaucrat &bureaucrat); void checkExecutorGrade(const Bureaucrat &bureaucrat) const; - void virtual execute(const Bureaucrat &bureaucrat) const = 0; + virtual void execute(const Bureaucrat &bureaucrat) const = 0; + virtual AForm *createNew(const std::string &target) const = 0; class FormNotSignedException; }; diff --git a/ex03/Intern.cpp b/ex03/Intern.cpp new file mode 100644 index 0000000..db208ac --- /dev/null +++ b/ex03/Intern.cpp @@ -0,0 +1,41 @@ +#include "Intern.h" + +const ShrubberyCreationForm Intern::shrub_template(""); +const RobotomyRequestForm Intern::robo_template(""); +const PresidentialPardonForm Intern::pres_template(""); +const AForm *Intern::forms[FORM_COUNT] = { + &shrub_template, + &robo_template, + &pres_template, + }; + +Intern::Intern() +{ +} + +Intern::Intern(const Intern &other) +{ + *this = other; +} + +Intern::~Intern() +{ +} + +Intern &Intern::operator=(const Intern &other) +{ + if (this == &other) + return (*this); + return (*this); +} + +AForm *Intern::makeForm(const std::string &name, const std::string &target) +{ + for (size_t i(0); i < FORM_COUNT; ++i) + { + if (name == forms[i]->getName()) + return (forms[i]->createNew(target)); + } + std::cout << "Could not create " << name << " as it does not exist.\n"; + return (NULL); +} diff --git a/ex03/Intern.h b/ex03/Intern.h new file mode 100644 index 0000000..6248637 --- /dev/null +++ b/ex03/Intern.h @@ -0,0 +1,29 @@ +#ifndef INTERN_H +# define INTERN_H + +#include "AForm.h" +#include "ShrubberyCreationForm.h" +#include "RobotomyRequestForm.h" +#include "PresidentialPardonForm.h" + +class Intern +{ + private: + static const ShrubberyCreationForm shrub_template; + static const RobotomyRequestForm robo_template; + static const PresidentialPardonForm pres_template; + + static const size_t FORM_COUNT = 3; + static const AForm *forms[FORM_COUNT]; + + public: + Intern(); + Intern(const Intern &other); + ~Intern(); + + Intern &operator=(const Intern &other); + + AForm *makeForm(const std::string &name, const std::string &target); +}; + +#endif // INTERN_H diff --git a/ex03/Makefile b/ex03/Makefile index d1fd937..3349988 100644 --- a/ex03/Makefile +++ b/ex03/Makefile @@ -18,6 +18,7 @@ SRCDIR := . SOURCES := main.cpp \ Bureaucrat.cpp \ AForm.cpp \ + Intern.cpp \ ShrubberyCreationForm.cpp \ RobotomyRequestForm.cpp \ PresidentialPardonForm.cpp \ diff --git a/ex03/PresidentialPardonForm.cpp b/ex03/PresidentialPardonForm.cpp index 292b7b3..8be280c 100644 --- a/ex03/PresidentialPardonForm.cpp +++ b/ex03/PresidentialPardonForm.cpp @@ -24,3 +24,8 @@ void PresidentialPardonForm::execute(Bureaucrat const &executor) const checkExecutorGrade(executor); std::cout << getTarget() << " has been pardoned by Zaphod Beeblebrox.\n"; } + +PresidentialPardonForm *PresidentialPardonForm::createNew(const std::string &target) const +{ + return (new PresidentialPardonForm(target)); +} diff --git a/ex03/PresidentialPardonForm.h b/ex03/PresidentialPardonForm.h index 5f5c166..8bbf045 100644 --- a/ex03/PresidentialPardonForm.h +++ b/ex03/PresidentialPardonForm.h @@ -6,9 +6,9 @@ class PresidentialPardonForm : public AForm { + protected: // These are protected only so that inheriting classes can use them // in their "non implementations" of there functions - protected: PresidentialPardonForm(); using AForm::operator=; @@ -18,7 +18,8 @@ class PresidentialPardonForm : public AForm PresidentialPardonForm(const PresidentialPardonForm &other); ~PresidentialPardonForm(); - void execute(Bureaucrat const &executor) const; + void execute(Bureaucrat const &executor) const; + PresidentialPardonForm *createNew(const std::string &target) const; }; #endif // PRESIDENTIALPARDONFORM_H diff --git a/ex03/RobotomyRequestForm.cpp b/ex03/RobotomyRequestForm.cpp index 964eabd..763156b 100644 --- a/ex03/RobotomyRequestForm.cpp +++ b/ex03/RobotomyRequestForm.cpp @@ -42,3 +42,8 @@ bool RobotomyRequestForm::getRandomBit() } return (std::rand() > (RAND_MAX / 2)); } + +RobotomyRequestForm *RobotomyRequestForm::createNew(const std::string &target) const +{ + return (new RobotomyRequestForm(target)); +} diff --git a/ex03/RobotomyRequestForm.h b/ex03/RobotomyRequestForm.h index 98f287b..95e8397 100644 --- a/ex03/RobotomyRequestForm.h +++ b/ex03/RobotomyRequestForm.h @@ -8,9 +8,10 @@ class RobotomyRequestForm : public AForm { private: static bool getRandomBit(); + + protected: // These are protected only so that inheriting classes can use them // in their "non implementations" of there functions - protected: RobotomyRequestForm(); using AForm::operator=; @@ -20,7 +21,8 @@ class RobotomyRequestForm : public AForm RobotomyRequestForm(const RobotomyRequestForm &other); ~RobotomyRequestForm(); - void execute(Bureaucrat const &executor) const; + void execute(Bureaucrat const &executor) const; + RobotomyRequestForm *createNew(const std::string &target) const; }; #endif // ROBOTOMYREQUESTFORM_H diff --git a/ex03/ShrubberyCreationForm.cpp b/ex03/ShrubberyCreationForm.cpp index 5a23890..88d4697 100644 --- a/ex03/ShrubberyCreationForm.cpp +++ b/ex03/ShrubberyCreationForm.cpp @@ -43,3 +43,8 @@ void ShrubberyCreationForm::execute(Bureaucrat const &executor) const << " ,#####. ,#####. \n"; target_file.close(); } + +ShrubberyCreationForm *ShrubberyCreationForm::createNew(const std::string &target) const +{ + return (new ShrubberyCreationForm(target)); +} diff --git a/ex03/ShrubberyCreationForm.h b/ex03/ShrubberyCreationForm.h index 9ee7e15..ef29aa6 100644 --- a/ex03/ShrubberyCreationForm.h +++ b/ex03/ShrubberyCreationForm.h @@ -6,9 +6,9 @@ class ShrubberyCreationForm : public AForm { + protected: // These are protected only so that inheriting classes can use them // in their "non implementations" of there functions - protected: ShrubberyCreationForm(); using AForm::operator=; @@ -18,7 +18,8 @@ class ShrubberyCreationForm : public AForm ShrubberyCreationForm(const ShrubberyCreationForm &other); ~ShrubberyCreationForm(); - void execute(Bureaucrat const &executor) const; + void execute(Bureaucrat const &executor) const; + ShrubberyCreationForm *createNew(const std::string &target) const; }; #endif // SHRUBBERYCREATIONFORM_H diff --git a/ex03/main.cpp b/ex03/main.cpp index 0d5150c..89fe79d 100644 --- a/ex03/main.cpp +++ b/ex03/main.cpp @@ -1,5 +1,6 @@ #include "Bureaucrat.h" #include "AForm.h" +#include "Intern.h" #include "ShrubberyCreationForm.h" #include "RobotomyRequestForm.h" #include "PresidentialPardonForm.h" @@ -7,48 +8,52 @@ int main(void) { - 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"); + Bureaucrat employee0("Employee0", 1); + Bureaucrat employee1("Employee1", 20); + Bureaucrat employee2("Employee2", 50); + Bureaucrat employee3("Employee3", 140); + Bureaucrat employee4("Employee4", 150); + Intern intern; + AForm *shrub(intern.makeForm("Shrubbery Creation Form", "test_sh")); + AForm *robot(intern.makeForm("Robotomy Request Form", "test_rob")); + AForm *pres(intern.makeForm("Presidential Pardon Form", "test_pres")); - employee4.signForm(shrub); - employee3.signForm(robot); - employee2.signForm(pres); + employee4.signForm(*shrub); + employee3.signForm(*robot); + employee2.signForm(*pres); std::cout << '\n'; - employee3.executeForm(shrub); - employee2.executeForm(robot); - employee1.executeForm(pres); + employee3.executeForm(*shrub); + employee2.executeForm(*robot); + employee1.executeForm(*pres); std::cout << '\n'; - employee2.executeForm(shrub); - employee1.executeForm(robot); - employee0.executeForm(pres); + employee2.executeForm(*shrub); + employee1.executeForm(*robot); + employee0.executeForm(*pres); std::cout << '\n'; - employee3.signForm(shrub); - employee2.signForm(robot); - employee1.signForm(pres); + employee3.signForm(*shrub); + employee2.signForm(*robot); + employee1.signForm(*pres); std::cout << '\n'; - employee3.executeForm(shrub); - employee2.executeForm(robot); - employee1.executeForm(pres); + employee3.executeForm(*shrub); + employee2.executeForm(*robot); + employee1.executeForm(*pres); std::cout << '\n'; - employee2.executeForm(shrub); + employee2.executeForm(*shrub); std::cout << '\n'; - employee1.executeForm(robot); - employee1.executeForm(robot); - employee1.executeForm(robot); - employee1.executeForm(robot); - employee1.executeForm(robot); - employee1.executeForm(robot); - employee1.executeForm(robot); - employee1.executeForm(robot); - employee1.executeForm(robot); - employee1.executeForm(robot); + employee1.executeForm(*robot); + employee1.executeForm(*robot); + employee1.executeForm(*robot); + employee1.executeForm(*robot); + employee1.executeForm(*robot); + employee1.executeForm(*robot); + employee1.executeForm(*robot); + employee1.executeForm(*robot); + employee1.executeForm(*robot); + employee1.executeForm(*robot); std::cout << '\n'; - employee0.executeForm(pres); + employee0.executeForm(*pres); + delete shrub; + delete robot; + delete pres; return (0); } -- 2.30.2