Implement everything except for execute functions
authorLukas Jiriste <ljiriste@student.42prague.com>
Sun, 19 Jan 2025 14:57:16 +0000 (15:57 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sun, 19 Jan 2025 14:57:16 +0000 (15:57 +0100)
12 files changed:
ex02/AForm.cpp
ex02/AForm.h
ex02/Bureaucrat.cpp
ex02/Makefile
ex02/PresidentialPardonForm.cpp [new file with mode: 0644]
ex02/PresidentialPardonForm.h [new file with mode: 0644]
ex02/RobotomyRequestForm.cpp [new file with mode: 0644]
ex02/RobotomyRequestForm.h [new file with mode: 0644]
ex02/ShrubberyCreationForm.cpp [new file with mode: 0644]
ex02/ShrubberyCreationForm.h [new file with mode: 0644]
ex02/ShruberryCreationForm.h [deleted file]
ex02/main.cpp

index 56f714bf7043567ba7633d80b4fe4ae674a70d75..5d77859d14bc0d8b1d853e421c61430bd0f0a7e8 100644 (file)
@@ -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);
+}
index 052f11d1e74136934f0cb501fda0e7b5537911fc..81c657fb69527a1bba8fcca315e24f00d466afb6 100644 (file)
@@ -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
index ae6a3804bd673d97c054b1a1d2afd21f8713e452..7d34b8cdc2af96fbd35c80f1e1682f3a5ecdc217 100644 (file)
@@ -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)
index 926121166f48ea6b289c4d1b7a9246a204407b7e..d1fd937956883a50c2eb7ee9adba9b7bc45f78d1 100644 (file)
@@ -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 (file)
index 0000000..322873f
--- /dev/null
@@ -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 (file)
index 0000000..5f5c166
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef PRESIDENTIALPARDONFORM_H
+# define PRESIDENTIALPARDONFORM_H
+
+#include "AForm.h"
+#include <string>
+
+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 (file)
index 0000000..a814a90
--- /dev/null
@@ -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 (file)
index 0000000..b81ccfa
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef ROBOTOMYREQUESTFORM_H
+# define ROBOTOMYREQUESTFORM_H
+
+#include "AForm.h"
+#include <string>
+
+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 (file)
index 0000000..d3ec877
--- /dev/null
@@ -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 (file)
index 0000000..9ee7e15
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef SHRUBBERYCREATIONFORM_H
+# define SHRUBBERYCREATIONFORM_H
+
+#include "AForm.h"
+#include <string>
+
+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 (file)
index 0c47e1c..0000000
+++ /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
index fda8686602be678efbd662170a087bfdf46d2f2b..95f808dd12e3c6641b1702a9369d5e4d15314a96 100644 (file)
@@ -1,11 +1,43 @@
 #include "Bureaucrat.h"
 #include "AForm.h"
+#include "ShrubberyCreationForm.h"
+#include "RobotomyRequestForm.h"
+#include "PresidentialPardonForm.h"
 #include <iostream>
 
 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);
 }