diff --git a/ex02/AForm/AForm.cpp b/ex02/AForm/AForm.cpp index 844a609..ecf5d12 100644 --- a/ex02/AForm/AForm.cpp +++ b/ex02/AForm/AForm.cpp @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include "AForm.hpp" +#include "../Bureaucrat/Bureaucrat.hpp" ////////////////// //Constructores // diff --git a/ex02/AForm/AForm.hpp b/ex02/AForm/AForm.hpp index eeb29dd..8b11d79 100644 --- a/ex02/AForm/AForm.hpp +++ b/ex02/AForm/AForm.hpp @@ -15,7 +15,9 @@ # define AForm_HPP # include -# include "../Bureaucrat/Bureaucrat.hpp" +# include + +class Bureaucrat; class AForm { @@ -29,7 +31,7 @@ class AForm AForm(); AForm(const AForm &other); AForm& operator=(const AForm &other); - ~AForm(); + virtual ~AForm(); AForm(std::string name, int gradeToSign, int gradeToExecute); @@ -40,7 +42,7 @@ class AForm void beSigned(const Bureaucrat &br); - virtual void execute(Bureaucrat const &executor) = 0; + virtual void execute(Bureaucrat const &executor) const = 0; class GradeTooLowException : public std::exception { public: diff --git a/ex02/Makefile b/ex02/Makefile index 6530fd6..5c8edfe 100644 --- a/ex02/Makefile +++ b/ex02/Makefile @@ -1,8 +1,10 @@ -NAME = form +NAME = formExecution SRC = main.cpp \ Bureaucrat/Bureaucrat.cpp AForm/AForm.cpp \ - ShrubberyCreationForm/ShrubberyCreationForm.cpp + ShrubberyCreationForm/ShrubberyCreationForm.cpp \ + RobotomyRequestForm/RobotomyRequestForm.cpp \ + PresidentialPardonForm/PresidentialPardonForm.cpp OBJ = $(SRC:.cpp=.o) diff --git a/ex02/PresidentialPardonForm/PresidentialPardonForm.cpp b/ex02/PresidentialPardonForm/PresidentialPardonForm.cpp new file mode 100644 index 0000000..2a50199 --- /dev/null +++ b/ex02/PresidentialPardonForm/PresidentialPardonForm.cpp @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos target = other.getTarget(); + } + //std::cout << "AForm copy assigment operator called" << std::endl; + return (*this); +} + +PresidentialPardonForm::~PresidentialPardonForm() +{ + //std::cout << "Destructor called" +} + +PresidentialPardonForm::PresidentialPardonForm(std::string target) + : AForm("PresidentialPardonForm", 25, 5), + target(target) +{ + //std::cout << "AForm constructor with params called" << std::endl; +} + +////////////////// +// Getters // +////////////////// + +std::string PresidentialPardonForm::getTarget() const +{ + return (this->target); +} + + +////////////////// +// Execute // +////////////////// + +void PresidentialPardonForm::execute(Bureaucrat const &executor) const +{ + if (!this->getIsSigned()) + throw FormNotSignedException(); + if (this->getGradeToExecute() < executor.getGrade()) + throw AForm::GradeTooLowException(); + + // PresidentialPardonForm form action... + std::cout << this->target + << " has been pardoned by Zaphod Beeblebrox." + << std::endl; + +} + +////////////////// +// Exceptions // +////////////////// + +const char* PresidentialPardonForm::FormNotSignedException::what() const throw() +{ + return ("form is not signed."); +} \ No newline at end of file diff --git a/ex02/PresidentialPardonForm/PresidentialPardonForm.hpp b/ex02/PresidentialPardonForm/PresidentialPardonForm.hpp new file mode 100644 index 0000000..247c657 --- /dev/null +++ b/ex02/PresidentialPardonForm/PresidentialPardonForm.hpp @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +# include "../AForm/AForm.hpp" +# include "../Bureaucrat/Bureaucrat.hpp" + +class PresidentialPardonForm : public AForm +{ + private: + std::string target; + + public: + PresidentialPardonForm(); + PresidentialPardonForm(const PresidentialPardonForm &other); + PresidentialPardonForm &operator=(const PresidentialPardonForm &other); + ~PresidentialPardonForm(); + + PresidentialPardonForm(std::string target); + + std::string getTarget() const; + + virtual void execute(Bureaucrat const &executor) const; + + class FormNotSignedException : public std::exception { + public: + virtual const char* what() const throw(); + }; + +}; + +#endif diff --git a/ex02/RobotomyRequestForm/RobotomyRequestForm.cpp b/ex02/RobotomyRequestForm/RobotomyRequestForm.cpp new file mode 100644 index 0000000..42b0902 --- /dev/null +++ b/ex02/RobotomyRequestForm/RobotomyRequestForm.cpp @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RobotomyRequestForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos target = other.getTarget(); + } + //std::cout << "AForm copy assigment operator called" << std::endl; + return (*this); +} + +RobotomyRequestForm::~RobotomyRequestForm() +{ + //std::cout << "Destructor called" +} + +RobotomyRequestForm::RobotomyRequestForm(std::string target) + : AForm("RobotomyRequestForm", 72, 45), + target(target) +{ + //std::cout << "AForm constructor with params called" << std::endl; +} + +////////////////// +// Getters // +////////////////// + +std::string RobotomyRequestForm::getTarget() const +{ + return (this->target); +} + + +////////////////// +// Execute // +////////////////// + +void RobotomyRequestForm::execute(Bureaucrat const &executor) const +{ + if (!this->getIsSigned()) + throw FormNotSignedException(); + if (this->getGradeToExecute() < executor.getGrade()) + throw AForm::GradeTooLowException(); + + // RobotomyRequestForm form action... + std::cout << "**bzzzzzzzzz --- **drilling noises*" << std::endl; + + if (rand() % 2 == 0) + { + std::cout << this->target + << " has been successfully robotomized" << std::endl; + } else { + std::cout << "Robotomization failed :(" << std::endl; + } + +} + +////////////////// +// Exceptions // +////////////////// + +const char* RobotomyRequestForm::FormNotSignedException::what() const throw() +{ + return ("form is not signed."); +} \ No newline at end of file diff --git a/ex02/RobotomyRequestForm/RobotomyRequestForm.hpp b/ex02/RobotomyRequestForm/RobotomyRequestForm.hpp new file mode 100644 index 0000000..5634142 --- /dev/null +++ b/ex02/RobotomyRequestForm/RobotomyRequestForm.hpp @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RobotomyRequestForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +# include +# include +# include "../AForm/AForm.hpp" +# include "../Bureaucrat/Bureaucrat.hpp" + + +class RobotomyRequestForm : public AForm +{ + private: + std::string target; + + public: + RobotomyRequestForm(); + RobotomyRequestForm(const RobotomyRequestForm &other); + RobotomyRequestForm &operator=(const RobotomyRequestForm &other); + ~RobotomyRequestForm(); + + RobotomyRequestForm(std::string target); + + std::string getTarget() const; + + virtual void execute(Bureaucrat const &executor) const; + + class FormNotSignedException : public std::exception { + public: + virtual const char* what() const throw(); + }; + +}; + +#endif diff --git a/ex02/ShrubberyCreationForm/ShrubberyCreationForm.cpp b/ex02/ShrubberyCreationForm/ShrubberyCreationForm.cpp index fb9f11a..b4b97d8 100644 --- a/ex02/ShrubberyCreationForm/ShrubberyCreationForm.cpp +++ b/ex02/ShrubberyCreationForm/ShrubberyCreationForm.cpp @@ -67,7 +67,7 @@ std::string ShrubberyCreationForm::getTarget() const // Execute // ////////////////// -void ShrubberyCreationForm::execute(Bureaucrat const &executor) +void ShrubberyCreationForm::execute(Bureaucrat const &executor) const { if (!this->getIsSigned()) throw FormNotSignedException(); @@ -75,8 +75,16 @@ void ShrubberyCreationForm::execute(Bureaucrat const &executor) throw AForm::GradeTooLowException(); // ShrubberyCreation form action... + std::string filename = this->target + "_shrubbery"; std::ofstream file(filename.c_str()); + + if (!file.is_open()) + { + std::cerr << "Error: could not create file " + << filename << std::endl; + return ; + } file << " _-_\n"; file << " /~~ ~~\\\n"; @@ -88,6 +96,7 @@ void ShrubberyCreationForm::execute(Bureaucrat const &executor) file << " _ - | | -_\n"; file << " // \\"; + file.close(); } ////////////////// diff --git a/ex02/ShrubberyCreationForm/ShrubberyCreationForm.hpp b/ex02/ShrubberyCreationForm/ShrubberyCreationForm.hpp index aef0fb9..2dc81d9 100644 --- a/ex02/ShrubberyCreationForm/ShrubberyCreationForm.hpp +++ b/ex02/ShrubberyCreationForm/ShrubberyCreationForm.hpp @@ -16,7 +16,9 @@ # include # include +# include # include "../AForm/AForm.hpp" +# include "../Bureaucrat/Bureaucrat.hpp" class ShrubberyCreationForm : public AForm { @@ -33,7 +35,7 @@ class ShrubberyCreationForm : public AForm std::string getTarget() const; - void execute(Bureaucrat const &executor); + virtual void execute(Bureaucrat const &executor) const; class FormNotSignedException : public std::exception { public: diff --git a/ex02/main.cpp b/ex02/main.cpp index 7825cc6..48979b2 100644 --- a/ex02/main.cpp +++ b/ex02/main.cpp @@ -6,20 +6,27 @@ /* By: hadi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/02/16 22:32:41 by hadi #+# #+# */ -/* Updated: 2026/02/17 17:53:10 by hadi ### ########.fr */ +/* Updated: 2026/02/18 00:15:46 by hadi ### ########.fr */ /* */ /* ************************************************************************** */ #include "Bureaucrat/Bureaucrat.hpp" +#include "AForm/AForm.hpp" #include "ShrubberyCreationForm/ShrubberyCreationForm.hpp" +#include "RobotomyRequestForm/RobotomyRequestForm.hpp" +#include "PresidentialPardonForm/PresidentialPardonForm.hpp" int main() { + // Inicilizamos semilla de rand, necesario para + // RobotomyRequestForm + srand(time(NULL)); + std::cout << "---- ex01 tests ----" << std::endl; - ShrubberyCreationForm form("home"); + RobotomyRequestForm form("home"); try { - Bureaucrat angel("Angel", 137); + Bureaucrat angel("Angel", 1); angel.executeForm(form); angel.signForm(form);