Implement ex03
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 21 Jan 2025 11:44:14 +0000 (12:44 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Tue, 21 Jan 2025 11:57:14 +0000 (12:57 +0100)
ex03/AForm.h
ex03/Intern.cpp [new file with mode: 0644]
ex03/Intern.h [new file with mode: 0644]
ex03/Makefile
ex03/PresidentialPardonForm.cpp
ex03/PresidentialPardonForm.h
ex03/RobotomyRequestForm.cpp
ex03/RobotomyRequestForm.h
ex03/ShrubberyCreationForm.cpp
ex03/ShrubberyCreationForm.h
ex03/main.cpp

index c6f867ccf7c3afc75380477adf35d86422fd7304..03cd41cca6f48bdd30701a415f89602b9e7a3e9b 100644 (file)
@@ -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 (file)
index 0000000..db208ac
--- /dev/null
@@ -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 (file)
index 0000000..6248637
--- /dev/null
@@ -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
index d1fd937956883a50c2eb7ee9adba9b7bc45f78d1..3349988f11ec8d35f595fc5a1affd8a49209708a 100644 (file)
@@ -18,6 +18,7 @@ SRCDIR := .
 SOURCES :=     main.cpp                                        \
                        Bureaucrat.cpp                          \
                        AForm.cpp                                       \
+                       Intern.cpp                                      \
                        ShrubberyCreationForm.cpp       \
                        RobotomyRequestForm.cpp         \
                        PresidentialPardonForm.cpp      \
index 292b7b34c2247fb75942c5818b909d929a6a7f71..8be280cb624496c0a311549e15f8ddeed7e5b52c 100644 (file)
@@ -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));
+}
index 5f5c1661ae4458b908e261fdd4d09add3eac0631..8bbf0455e944162328beee431ac1dad720a0b976 100644 (file)
@@ -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
index 964eabd05693a593211aa141a5647d089b91fad6..763156b23f15232ead9ca4e0c57162ca51b2fd06 100644 (file)
@@ -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));
+}
index 98f287b9e7b4df4cd21c9fc7c700c85d03116dd7..95e83975fef10d5594423fef424a9211afdb5482 100644 (file)
@@ -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
index 5a23890a74b5c26815b9806fb4fe219788099b77..88d469703dcf9adc1ebbff90ab608c478b573e00 100644 (file)
@@ -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));
+}
index 9ee7e157f6d779b7876a62dec7ce65bc79fe79c8..ef29aa6a1b84af73e4f961d52b7341669b917dd9 100644 (file)
@@ -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
index 0d5150c50e6ffdab2f6804cd3468db213f2b309a..89fe79d7f074ec279464bbb928c232bed56d6562 100644 (file)
@@ -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);
 }