From bb8d13b9ed077a794c34bce596db4e2292f78c31 Mon Sep 17 00:00:00 2001 From: adjoly Date: Thu, 3 Apr 2025 14:34:21 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat:=20finished?= =?UTF-8?q?=20ex02?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ ex02/AForm.cpp | 6 ++-- ex02/AForm.hpp | 4 +-- ex02/Bureaucrat.cpp | 10 +++++-- ex02/Bureaucrat.hpp | 8 +++-- ex02/Makefile | 4 +-- ex02/PresidentialPardonForm.cpp | 48 +++++++++++++++++++++++++++++ ex02/PresidentialPardonForm.hpp | 28 +++++++++++++++++ ex02/RobotomyRequestForm.cpp | 53 +++++++++++++++++++++++++++++++++ ex02/RobotomyRequestForm.hpp | 27 +++++++++++++++++ ex02/ShrubberyCreationForm.cpp | 46 ++++++++++++++++++++++++++-- ex02/ShrubberyCreationForm.hpp | 8 ++++- ex02/main.cpp | 16 ++++++---- 13 files changed, 238 insertions(+), 22 deletions(-) create mode 100644 ex02/PresidentialPardonForm.cpp create mode 100644 ex02/PresidentialPardonForm.hpp create mode 100644 ex02/RobotomyRequestForm.cpp create mode 100644 ex02/RobotomyRequestForm.hpp diff --git a/.gitignore b/.gitignore index d57b3be..3f90193 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,6 @@ compile_commands.json *.d +Form +AForm Bureaucrat diff --git a/ex02/AForm.cpp b/ex02/AForm.cpp index 372440f..224da3d 100644 --- a/ex02/AForm.cpp +++ b/ex02/AForm.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/09 17:38:19 by adjoly #+# #+# */ -/* Updated: 2025/04/01 11:06:48 by adjoly ### ########.fr */ +/* Updated: 2025/04/03 14:33:17 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ AForm::AForm(std::string name, uint8_t minForSign, uint8_t minForExec) AForm::AForm(const AForm &f) : _name(f.getName()), _signed(f.getSigned()), - _minForExec(f.getMinForExec()), _minForSign(f.getMinForSign()) { + _minForSign(f.getMinForSign()), _minForExec(f.getMinForExec()) { _log("➕", "AForm", "", "copy constructor called"); _gradeCheck(); } @@ -58,7 +58,7 @@ uint8_t AForm::getMinForSign(void) const { return _minForSign; } uint8_t AForm::getMinForExec(void) const { return _minForExec; } void AForm::beSigned(Bureaucrat b) { - if (_minForSign > b.getGrade()) { + if (_minForSign < b.getGrade()) { throw GradeTooLowException(); } _signed = true; diff --git a/ex02/AForm.hpp b/ex02/AForm.hpp index cb51c2e..44347d3 100644 --- a/ex02/AForm.hpp +++ b/ex02/AForm.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/08 20:10:59 by adjoly #+# #+# */ -/* Updated: 2025/04/01 11:03:13 by adjoly ### ########.fr */ +/* Updated: 2025/04/03 13:50:38 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,4 +61,4 @@ class AForm { void _gradeCheck(void) const; }; -std::ostream &operator<<(std::ostream &, Form &); +std::ostream &operator<<(std::ostream &, AForm &); diff --git a/ex02/Bureaucrat.cpp b/ex02/Bureaucrat.cpp index d618223..14219f1 100644 --- a/ex02/Bureaucrat.cpp +++ b/ex02/Bureaucrat.cpp @@ -6,12 +6,12 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/23 13:59:45 by adjoly #+# #+# */ -/* Updated: 2025/04/01 10:49:43 by adjoly ### ########.fr */ +/* Updated: 2025/04/03 14:21:09 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "Bureaucrat.hpp" -#include "Form.hpp" +#include "AForm.hpp" #include void _log(std::string emoji, std::string what, std::string who, @@ -56,6 +56,10 @@ Bureaucrat &Bureaucrat::operator=(Bureaucrat const &cpy) { return *this; } +void Bureaucrat::executeForm(const AForm &form) const { + form.execute(*this); +} + Bureaucrat &Bureaucrat::operator++(void) { _grade--; if (_grade < MAXGRADE) @@ -101,7 +105,7 @@ const char *Bureaucrat::GradeTooLowException::what() const throw() { return ("Grade is too low"); } -void Bureaucrat::signForm(Form &f) { +void Bureaucrat::signForm(AForm &f) { try { f.beSigned(*this); std::cout << _name << " signed " << f.getName() << std::endl; diff --git a/ex02/Bureaucrat.hpp b/ex02/Bureaucrat.hpp index 1e17fa5..ba8615e 100644 --- a/ex02/Bureaucrat.hpp +++ b/ex02/Bureaucrat.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/23 13:59:43 by adjoly #+# #+# */ -/* Updated: 2025/04/01 10:50:00 by adjoly ### ########.fr */ +/* Updated: 2025/04/03 13:50:01 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ void _log(std::string emoji, std::string what, std::string who, std::string str); -class Form; +class AForm; typedef unsigned char uint8_t; @@ -38,6 +38,8 @@ class Bureaucrat { const std::string &getName(void) const; uint8_t getGrade(void) const; + void executeForm(const AForm &) const; + class GradeTooHighException : public std::exception { public: virtual const char *what() const throw(); @@ -58,7 +60,7 @@ class Bureaucrat { Bureaucrat operator++(int); Bureaucrat operator--(int); - void signForm(Form &); + void signForm(AForm &); private: const std::string _name; uint8_t _grade; diff --git a/ex02/Makefile b/ex02/Makefile index 5636370..992b0f7 100644 --- a/ex02/Makefile +++ b/ex02/Makefile @@ -6,13 +6,13 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/25 16:09:27 by adjoly #+# #+# # -# Updated: 2025/03/30 15:14:32 by adjoly ### ########.fr # +# Updated: 2025/04/03 14:23:56 by adjoly ### ########.fr # # # # **************************************************************************** # SHELL = bash -NAME = Form +NAME = AForm CC = c++ diff --git a/ex02/PresidentialPardonForm.cpp b/ex02/PresidentialPardonForm.cpp new file mode 100644 index 0000000..2a81231 --- /dev/null +++ b/ex02/PresidentialPardonForm.cpp @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/03 09:45:26 by adjoly #+# #+# */ +/* Updated: 2025/04/03 14:13:21 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "PresidentialPardonForm.hpp" +#include "AForm.hpp" + +PresidentialPardonForm::PresidentialPardonForm(void) + : AForm("PresidentialPardonForm", 25, 5) { + _log("➕", "PresidentialPardonForm", "", "default constructor called"); +} + +PresidentialPardonForm::PresidentialPardonForm( + const PresidentialPardonForm &cpy) : AForm(cpy) { + _log("➕", "PresidentialPardonForm", "", "copy constructor called"); + if (this != &cpy) + *this = cpy; +} + +PresidentialPardonForm::PresidentialPardonForm(std::string &target) + : AForm(target, 25, 5) { + _log("➕", "PresidentialPardonForm", "", "target constructor called"); +} + +PresidentialPardonForm::~PresidentialPardonForm(void) { + _log("➖", "PresidentialPardonForm", "", "destructor called"); +} + +PresidentialPardonForm & +PresidentialPardonForm::operator=(const PresidentialPardonForm &cpy) { + _log("➕", "PresidentialPardonForm", "", + "copy assignement constructor called"); + (void)cpy; + return *this; +} + +void PresidentialPardonForm::_exec(const Bureaucrat &b) const { + std::cout << b.getName() << " has been pardoned by Zaphod Beeblebrox 🙀" + << std::endl; +} diff --git a/ex02/PresidentialPardonForm.hpp b/ex02/PresidentialPardonForm.hpp new file mode 100644 index 0000000..75c909b --- /dev/null +++ b/ex02/PresidentialPardonForm.hpp @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/03 09:44:16 by adjoly #+# #+# */ +/* Updated: 2025/04/03 14:04:03 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Bureaucrat.hpp" +#include "AForm.hpp" + +class PresidentialPardonForm : public AForm { + public: + PresidentialPardonForm(void); + PresidentialPardonForm(const PresidentialPardonForm &); + PresidentialPardonForm(std::string &); + ~PresidentialPardonForm(void); + + PresidentialPardonForm &operator=(const PresidentialPardonForm &); + private: + void _exec(const Bureaucrat &) const; +}; diff --git a/ex02/RobotomyRequestForm.cpp b/ex02/RobotomyRequestForm.cpp new file mode 100644 index 0000000..d3b02fe --- /dev/null +++ b/ex02/RobotomyRequestForm.cpp @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RobotomyRequestForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/03 09:19:30 by adjoly #+# #+# */ +/* Updated: 2025/04/03 14:17:12 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "RobotomyRequestForm.hpp" +#include "Bureaucrat.hpp" +#include + +RobotomyRequestForm::RobotomyRequestForm(void) + : AForm("RobotomyRequestForm", 72, 45) { + _log("➕", "RobotomyRequestForm", "", "default constructor called"); +} + +RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm &cpy) : AForm(cpy) { + _log("➕", "RobotomyRequestForm", "", "copy constructor called"); + if (this != &cpy) + *this = cpy; +} + +RobotomyRequestForm::RobotomyRequestForm(std::string &target) + : AForm(target, 72, 45) { + _log("➕", "RobotomyRequestForm", "", "target constructor called"); +} + +RobotomyRequestForm::~RobotomyRequestForm(void) { + _log("➖", "RobotomyRequestForm", "", "destructor called"); +} + +RobotomyRequestForm &RobotomyRequestForm::operator=(const RobotomyRequestForm &cpy) { + _log("➕", "RobotomyRequestForm", "", "copy assignement constructor called"); + (void)cpy; + return *this; +} + +void RobotomyRequestForm::_exec(const Bureaucrat &b) const { + std::cout << "Drilingg noisseeeeeeee !!!" << std::endl; + + std::srand(time(0)); + + if (std::rand() % 2) { + std::cout << b.getName() << " has been robotomized successfully ! :D" << std::endl; + } else { + std::cout << b.getName() << " robotomization failed 😿" << std::endl; + } +} diff --git a/ex02/RobotomyRequestForm.hpp b/ex02/RobotomyRequestForm.hpp new file mode 100644 index 0000000..a3bef36 --- /dev/null +++ b/ex02/RobotomyRequestForm.hpp @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RobotomyRequestForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/02 16:13:39 by adjoly #+# #+# */ +/* Updated: 2025/04/03 10:02:14 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "AForm.hpp" + +class RobotomyRequestForm : public AForm { + public: + RobotomyRequestForm(void); + RobotomyRequestForm(const RobotomyRequestForm &); + RobotomyRequestForm(std::string &); + ~RobotomyRequestForm(void); + + RobotomyRequestForm &operator=(const RobotomyRequestForm &); + private: + void _exec(const Bureaucrat &) const; +}; diff --git a/ex02/ShrubberyCreationForm.cpp b/ex02/ShrubberyCreationForm.cpp index 2368868..c7589c9 100644 --- a/ex02/ShrubberyCreationForm.cpp +++ b/ex02/ShrubberyCreationForm.cpp @@ -6,22 +6,62 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/01 09:25:06 by adjoly #+# #+# */ -/* Updated: 2025/04/01 10:53:21 by adjoly ### ########.fr */ +/* Updated: 2025/04/03 14:33:50 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "ShrubberyCreationForm.hpp" #include "AForm.hpp" +#include +#include +#include ShrubberyCreationForm::ShrubberyCreationForm(void) : AForm("ShrubberyCreationForm", 145, 137) { _log("➕", "ShrubberyCreationForm", "", "default constructor called"); } +ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm &cpy) : AForm(cpy) { + _log("➕", "ShrubberyCreationForm", "", "copy constructor called"); + if (this != &cpy) + *this = cpy; +} + +ShrubberyCreationForm::ShrubberyCreationForm(std::string &target) + : AForm(target, 72, 45) { + _log("➕", "ShrubberyCreationForm", "", "target constructor called"); +} + ShrubberyCreationForm::~ShrubberyCreationForm(void) { _log("➖", "ShrubberyCreationForm", "", "destructor called"); } -void ShrubberyCreationForm::_exec(const Bureaucrat &b) const { - +ShrubberyCreationForm &ShrubberyCreationForm::operator=(const ShrubberyCreationForm &cpy) { + _log("➕", "ShrubberyCreationForm", "", "copy assignement constructor called"); + (void)cpy; + return (*this); +} + +void ShrubberyCreationForm::_exec(const Bureaucrat &b) const { + std::ofstream file; + + file.open(std::string(b.getName() + "_shruberry").c_str()); + if (!file.is_open()) + throw std::runtime_error("Could not write to " + b.getName() + + "_shruberry"); + + file << " _-_\n" + " /~~ ~~\\\n" + " /~~ ~~\\\n" + "{ }\n" + " \\ _- -_ /\n" + " ~ \\ // ~\n" + "_- - | | _- _\n" + " _ - | | -_\n" + " // \\\n"; + + file.close(); + + std::cout << "ASCII tree created in : " + b.getName() + "_shruberry" + << std::endl; } diff --git a/ex02/ShrubberyCreationForm.hpp b/ex02/ShrubberyCreationForm.hpp index a627f6a..7d2bbd8 100644 --- a/ex02/ShrubberyCreationForm.hpp +++ b/ex02/ShrubberyCreationForm.hpp @@ -6,18 +6,24 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/01 09:19:24 by adjoly #+# #+# */ -/* Updated: 2025/04/01 10:18:36 by adjoly ### ########.fr */ +/* Updated: 2025/04/03 13:34:46 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once #include "AForm.hpp" +#include +#include class ShrubberyCreationForm : public AForm { public: ShrubberyCreationForm(void); + ShrubberyCreationForm(const ShrubberyCreationForm &); + ShrubberyCreationForm(std::string &); ~ShrubberyCreationForm(void); + + ShrubberyCreationForm &operator=(const ShrubberyCreationForm &); private: void _exec(const Bureaucrat &) const; }; diff --git a/ex02/main.cpp b/ex02/main.cpp index 16995a1..d410212 100644 --- a/ex02/main.cpp +++ b/ex02/main.cpp @@ -6,17 +6,17 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/23 14:01:11 by adjoly #+# #+# */ -/* Updated: 2025/03/30 15:13:07 by adjoly ### ########.fr */ +/* Updated: 2025/04/03 14:32:57 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "Bureaucrat.hpp" -#include "Form.hpp" +#include "AForm.hpp" +#include "ShrubberyCreationForm.hpp" #include int main(void) { Bureaucrat knl("Kanel", 10); - Bureaucrat su("Suki", 1); knl--; std::cout << knl << std::endl; @@ -43,9 +43,15 @@ int main(void) { } catch (const std::exception &e) { std::cerr << "Error : " << e.what() << std::endl; } - Form f("le contrattt", 5, 5); + + ShrubberyCreationForm s; - su.signForm(f); + try { + s.beSigned(knl); + knl.executeForm(s); + } catch (std::exception &e) { + std::cerr << "Error : " << e.what() << std::endl; + } std::cout << knl << std::endl; }