From 5d62dbd20dd8c2840d8ab2e4c2e6c1566aa9e094 Mon Sep 17 00:00:00 2001 From: aortigos Date: Tue, 17 Feb 2026 17:49:39 +0100 Subject: [PATCH] fixing and starting ShrubberyCreationForm --- ex02/Bureaucrat/Bureaucrat.cpp | 18 ++++ ex02/Bureaucrat/Bureaucrat.hpp | 1 + ex02/Makefile | 3 +- .../ShrubberyCreationForm.cpp | 86 +++++++++++++++++++ .../ShrubberyCreationForm.hpp | 44 ++++++++++ ex02/main.cpp | 11 ++- 6 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 ex02/ShrubberyCreationForm/ShrubberyCreationForm.cpp create mode 100644 ex02/ShrubberyCreationForm/ShrubberyCreationForm.hpp diff --git a/ex02/Bureaucrat/Bureaucrat.cpp b/ex02/Bureaucrat/Bureaucrat.cpp index cf31d17..558f7c3 100644 --- a/ex02/Bureaucrat/Bureaucrat.cpp +++ b/ex02/Bureaucrat/Bureaucrat.cpp @@ -106,6 +106,24 @@ void Bureaucrat::signForm(AForm &form) } } +void Bureaucrat::executeForm(AForm &form) +{ + try { + form.execute(*this); + std::cout << this->name + << " executed " + << form.getName() + << std::endl; + } catch (std::exception &e) { + std::cout << this->name + << " couldn't execute " + << form.getName() + << " because " + << e.what() + << std::endl; + } +} + ////////////////// // << // diff --git a/ex02/Bureaucrat/Bureaucrat.hpp b/ex02/Bureaucrat/Bureaucrat.hpp index 1b9a493..b7e057e 100644 --- a/ex02/Bureaucrat/Bureaucrat.hpp +++ b/ex02/Bureaucrat/Bureaucrat.hpp @@ -39,6 +39,7 @@ class Bureaucrat void decrementGrade(); void signForm(AForm &form); + void executeForm(AForm &form); class GradeTooHighException : public std::exception { public: diff --git a/ex02/Makefile b/ex02/Makefile index e2077aa..6530fd6 100644 --- a/ex02/Makefile +++ b/ex02/Makefile @@ -1,7 +1,8 @@ NAME = form SRC = main.cpp \ - Bureaucrat/Bureaucrat.cpp AForm/AForm.cpp + Bureaucrat/Bureaucrat.cpp AForm/AForm.cpp \ + ShrubberyCreationForm/ShrubberyCreationForm.cpp OBJ = $(SRC:.cpp=.o) diff --git a/ex02/ShrubberyCreationForm/ShrubberyCreationForm.cpp b/ex02/ShrubberyCreationForm/ShrubberyCreationForm.cpp new file mode 100644 index 0000000..06d384d --- /dev/null +++ b/ex02/ShrubberyCreationForm/ShrubberyCreationForm.cpp @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ShrubberyCreationForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos target = other.getTarget(); + } + //std::cout << "AForm copy assigment operator called" << std::endl; + return (*this); +} + +ShrubberyCreationForm::~ShrubberyCreationForm() +{ + //std::cout << "Destructor called" +} + +ShrubberyCreationForm::ShrubberyCreationForm(std::string target) + : AForm("ShrubberyCreationForm", 145, 137), + target(target) +{ + //std::cout << "AForm constructor with params called" << std::endl; +} + +////////////////// +// Getters // +////////////////// + +std::string ShrubberyCreationForm::getTarget() const +{ + return (this->target); +} + + +////////////////// +// Execute // +////////////////// + +void ShrubberyCreationForm::execute(Bureaucrat const &executor) +{ + if (!this->getIsSigned()) + throw FormNotSignedException(); + if (this->getGradeToExecute() < executor.getGrade()) + throw AForm::GradeTooLowException(); + // Other things... +} + +////////////////// +// Exceptions // +////////////////// + +const char* ShrubberyCreationForm::FormNotSignedException::what() const throw() +{ + return ("form is not signed."); +} \ No newline at end of file diff --git a/ex02/ShrubberyCreationForm/ShrubberyCreationForm.hpp b/ex02/ShrubberyCreationForm/ShrubberyCreationForm.hpp new file mode 100644 index 0000000..b9303d2 --- /dev/null +++ b/ex02/ShrubberyCreationForm/ShrubberyCreationForm.hpp @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ShrubberyCreationForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +# include "../AForm/AForm.hpp" + +class ShrubberyCreationForm : public AForm +{ + private: + std::string target; + + public: + ShrubberyCreationForm(); + ShrubberyCreationForm(const ShrubberyCreationForm &other); + ShrubberyCreationForm &operator=(const ShrubberyCreationForm &other); + ~ShrubberyCreationForm(); + + ShrubberyCreationForm(std::string target); + + std::string getTarget() const; + + void execute(Bureaucrat const &executor); + + class FormNotSignedException : public std::exception { + public: + virtual const char* what() const throw(); + }; + +}; + +#endif diff --git a/ex02/main.cpp b/ex02/main.cpp index b44bfea..3abb033 100644 --- a/ex02/main.cpp +++ b/ex02/main.cpp @@ -6,20 +6,25 @@ /* By: hadi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/02/16 22:32:41 by hadi #+# #+# */ -/* Updated: 2026/02/17 16:24:37 by hadi ### ########.fr */ +/* Updated: 2026/02/17 17:49:09 by hadi ### ########.fr */ /* */ /* ************************************************************************** */ #include "Bureaucrat/Bureaucrat.hpp" -#include "AForm/AForm.hpp" +#include "ShrubberyCreationForm/ShrubberyCreationForm.hpp" int main() { std::cout << "---- ex01 tests ----" << std::endl; + ShrubberyCreationForm form("home"); try { - Bureaucrat angel("Angel", 140); + Bureaucrat angel("Angel", 137); + + angel.executeForm(form); + angel.signForm(form); + angel.executeForm(form); } catch (std::exception &e) { std::cout << e.what() << std::endl; }