From 5d81e1e1769e16f08c2d823763c30c89502d3473 Mon Sep 17 00:00:00 2001 From: Raphael Date: Wed, 19 Mar 2025 16:11:05 +0100 Subject: [PATCH] feat: adding files made in fablab --- cpp05/ex01/Bureaucrat.cpp | 4 +- cpp05/ex01/Bureaucrat.hpp | 4 +- cpp05/ex01/Form.cpp | 6 +- cpp05/ex01/Form.hpp | 10 +- cpp05/ex02/AForm.cpp | 111 ++++++++++++++++++++++ cpp05/ex02/AForm.hpp | 68 +++++++++++++ cpp05/ex02/Bureaucrat.cpp | 131 ++++++++++++++++++++++++++ cpp05/ex02/Bureaucrat.hpp | 55 +++++++++++ cpp05/ex02/Makefile | 131 ++++++++++++++++++++++++++ cpp05/ex02/PresidentialPardonForm.cpp | 48 ++++++++++ cpp05/ex02/PresidentialPardonForm.hpp | 33 +++++++ cpp05/ex02/RobotomyRequestForm.cpp | 56 +++++++++++ cpp05/ex02/RobotomyRequestForm.hpp | 35 +++++++ cpp05/ex02/Roger_shrubbery | 20 ++++ cpp05/ex02/ShrubberyCreationForm.cpp | 68 +++++++++++++ cpp05/ex02/ShrubberyCreationForm.hpp | 35 +++++++ cpp05/ex02/main.cpp | 82 ++++++++++++++++ 17 files changed, 885 insertions(+), 12 deletions(-) create mode 100644 cpp05/ex02/AForm.cpp create mode 100644 cpp05/ex02/AForm.hpp create mode 100644 cpp05/ex02/Bureaucrat.cpp create mode 100644 cpp05/ex02/Bureaucrat.hpp create mode 100644 cpp05/ex02/Makefile create mode 100644 cpp05/ex02/PresidentialPardonForm.cpp create mode 100644 cpp05/ex02/PresidentialPardonForm.hpp create mode 100644 cpp05/ex02/RobotomyRequestForm.cpp create mode 100644 cpp05/ex02/RobotomyRequestForm.hpp create mode 100644 cpp05/ex02/Roger_shrubbery create mode 100644 cpp05/ex02/ShrubberyCreationForm.cpp create mode 100644 cpp05/ex02/ShrubberyCreationForm.hpp create mode 100644 cpp05/ex02/main.cpp diff --git a/cpp05/ex01/Bureaucrat.cpp b/cpp05/ex01/Bureaucrat.cpp index e19a8ef..46def2e 100644 --- a/cpp05/ex01/Bureaucrat.cpp +++ b/cpp05/ex01/Bureaucrat.cpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/16 14:49:52 by rparodi #+# #+# */ -/* Updated: 2025/03/17 21:45:02 by rparodi ### ########.fr */ +/* Updated: 2025/03/18 11:43:49 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -114,7 +114,7 @@ std::ostream& operator<<(std::ostream& output, Bureaucrat const &toPrint) { return (output); } -void Bureaucrat::signForm(Form &form) { +void Bureaucrat::signForm(Form const &form) { try { form.beSigned(*this); } catch (std::exception &e) { diff --git a/cpp05/ex01/Bureaucrat.hpp b/cpp05/ex01/Bureaucrat.hpp index ac8832a..4b758e9 100644 --- a/cpp05/ex01/Bureaucrat.hpp +++ b/cpp05/ex01/Bureaucrat.hpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/16 14:48:33 by rparodi #+# #+# */ -/* Updated: 2025/03/17 22:00:20 by rparodi ### ########.fr */ +/* Updated: 2025/03/18 11:44:02 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,7 +33,7 @@ class Bureaucrat { int getGrade(void) const; void promote(void); void demote(void); - void signForm(Form &form); + void signForm(Form const &form); class GradeTooHighException : public std::exception { virtual const char *what() const throw() { return ("Error:\n> Grade is too high (have to be lower than 1)"); diff --git a/cpp05/ex01/Form.cpp b/cpp05/ex01/Form.cpp index 8c17d11..35ec6aa 100644 --- a/cpp05/ex01/Form.cpp +++ b/cpp05/ex01/Form.cpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/17 14:19:41 by rparodi #+# #+# */ -/* Updated: 2025/03/18 09:42:55 by rparodi ### ########.fr */ +/* Updated: 2025/03/18 11:44:31 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -79,14 +79,14 @@ std::ostream& operator<<(std::ostream& output, Form const &toPrint) { return (output); } -void Form::beSigned(Bureaucrat &bureaucrat) { +void Form::beSigned(Bureaucrat const &bureaucrat) const { if (bureaucrat.getGrade() > this->_to_sign) { throw FormForSupperior(); } else if (_signed == true) { throw FormAlreadySigned(); } else { - std::cout << "Bureaucrat " << bureaucrat.getName() << " signs the form " << this->_name << std::endl; + std::cout << "Bureaucrat " << bureaucrat.getName() << " signs the Form " << this->_name << std::endl; this->_signed = true; } } diff --git a/cpp05/ex01/Form.hpp b/cpp05/ex01/Form.hpp index 0e2eba1..3a73b7a 100644 --- a/cpp05/ex01/Form.hpp +++ b/cpp05/ex01/Form.hpp @@ -6,12 +6,12 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/17 14:20:06 by rparodi #+# #+# */ -/* Updated: 2025/03/17 22:00:37 by rparodi ### ########.fr */ +/* Updated: 2025/03/18 11:49:04 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef FORM_HPP -#define FORM_HPP +#ifndef Form_HPP +#define Form_HPP #include #include @@ -48,13 +48,13 @@ class Form { }; std::string getName() const; - void beSigned(Bureaucrat &bureaucrat); + void beSigned(Bureaucrat const &bureaucrat) const; int getExecute() const; int getSign() const; bool isSigned() const; private: std::string _name; - bool _signed; + mutable bool _signed; int _to_execute; int _to_sign; }; diff --git a/cpp05/ex02/AForm.cpp b/cpp05/ex02/AForm.cpp new file mode 100644 index 0000000..5f9c5bf --- /dev/null +++ b/cpp05/ex02/AForm.cpp @@ -0,0 +1,111 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* AForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/17 14:19:41 by rparodi #+# #+# */ +/* Updated: 2025/03/18 15:09:05 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "AForm.hpp" +#include "Bureaucrat.hpp" + +/** + * @brief Constructor by default of AForm + */ +AForm::AForm(void) : _name("Default"), _signed(false), _to_execute(150), _to_sign(150) { + std::cout << "[AForm] Default constructor called" << std::endl; +} +AForm::AForm(std::string const name, int execute, int sign) : _name(name), _signed(false) { + if (execute < 1) { + throw GradeTooHighException(); + } else if (execute > 150) { + throw GradeTooLowException(); + } else { + this->_to_execute = execute; + } + if (sign < 1) { + throw GradeTooHighException(); + } else if (sign > 150) { + throw GradeTooLowException(); + } else { + this->_to_sign = sign; + } + std::cout << "[AForm] Smart constructor called" << std::endl; +} + +AForm::AForm(AForm const ©) { + std::cout << "[AForm] Copy constructor called" << std::endl; + this->_name = copy._name; + this->_to_sign = copy._to_sign; + this->_to_execute = copy._to_execute; + _signed = copy._signed; +} + +AForm& AForm::operator=(AForm const &assign) { + std::cout << "[AForm] Assign constructor called" << std::endl; + this->_name = assign._name; + this->_to_sign = assign._to_sign; + this->_to_execute = assign._to_execute; + this->_signed = assign._signed; + return *this; +} + +AForm::~AForm() { + std::cout << "[AForm] Destructor called" << std::endl; +} + +std::string AForm::getName() const { + return this->_name; +} + +int AForm::getExecute() const { + return this->_to_execute; +} + +int AForm::getSign() const { + return this->_to_sign; +} + +bool AForm::isSigned() const { + return this->_signed; +} + +void AForm::setSign(int new_sign) { + this->_to_sign = new_sign; +} + +void AForm::setExecute(int new_exec) { + this->_to_execute = new_exec; +} + +std::ostream& operator<<(std::ostream& output, AForm const &toPrint) { + output << toPrint.getName() << ", Form\n\tTo execute: " << toPrint.getExecute() << "\n\tTo sign: " << toPrint.getSign() << "\n\tAlready signed: " << (toPrint.isSigned() ? "✅ | Yes" : "❌ | No"); + return (output); +} + +void AForm::execute(Bureaucrat const &executor) const { + if (executor.getGrade() > this->_to_execute) { + throw AFormForSupperior(); + } else if (_signed == false) { + throw AFormAlreadySigned(); + } + else { + std::cout << "Bureaucrat " << executor.getName() << " executes the Form " << this->_name << std::endl; + } +} + +void AForm::beSigned(Bureaucrat const &bureaucrat) const { + if (bureaucrat.getGrade() > this->_to_sign) { + throw AFormForSupperior(); + } else if (_signed == true) { + throw AFormAlreadySigned(); + } + else { + std::cout << "Bureaucrat " << bureaucrat.getName() << " signs the Form " << this->_name << std::endl; + this->_signed = true; + } +} diff --git a/cpp05/ex02/AForm.hpp b/cpp05/ex02/AForm.hpp new file mode 100644 index 0000000..58cc899 --- /dev/null +++ b/cpp05/ex02/AForm.hpp @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* AForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/17 14:20:06 by rparodi #+# #+# */ +/* Updated: 2025/03/18 17:49:34 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef AFORM_HPP +#define AFORM_HPP + +#include +#include +#include +#include "AForm.hpp" + +class Bureaucrat; + +class AForm { + public: + AForm(); + AForm(std::string const name, int execute, int sign); + AForm(AForm const ©); + AForm& operator=(AForm const &assign); + ~AForm(); + class GradeTooHighException : public std::exception { + virtual const char *what() const throw() { + return ("Error:\n> Grade is too high (have to be lower than 1)"); + } + }; + class GradeTooLowException : public std::exception { + virtual const char *what() const throw() { + return ("Error:\n> Grade is too low (have to be higher than 150)"); + } + }; + class AFormForSupperior: public std::exception { + virtual const char *what() const throw() { + return ("the bureaucrat grade is too low !"); + } + }; + class AFormAlreadySigned: public std::exception { + virtual const char *what() const throw() { + return ("form is already signed !"); + } + }; + + void beSigned(Bureaucrat const &bureaucrat) const; + virtual void execute(Bureaucrat const &executor) const = 0; + int getExecute() const; + int getSign() const; + std::string getName() const; + bool isSigned() const; + void setSign(int new_sign); + void setExecute(int new_exec); + private: + std::string _name; + mutable bool _signed; + int _to_execute; + int _to_sign; +}; + +std::ostream& operator<<(std::ostream& output, AForm const &Form); + +#endif diff --git a/cpp05/ex02/Bureaucrat.cpp b/cpp05/ex02/Bureaucrat.cpp new file mode 100644 index 0000000..c819066 --- /dev/null +++ b/cpp05/ex02/Bureaucrat.cpp @@ -0,0 +1,131 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/16 14:49:52 by rparodi #+# #+# */ +/* Updated: 2025/03/18 15:20:46 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" +#include +#include + +/** + * @brief Constructor by default of Bureaucrat + */ +Bureaucrat::Bureaucrat(void) : _name("Roger"), _grade(150) { + std::cout << "[Bureaucrat] Default constructor called" << std::endl; +} + +/** + * @brief Better constructor easier to test + * + * @param name the name of the Bureaucrat + * @param grade grade to start the carreer + */ +Bureaucrat::Bureaucrat(std::string name, int grade) : _name(name) { + std::cout << "[Bureaucrat] Smart constructor called" << std::endl; + if (grade < 1) { + throw GradeTooHighException(); + } else if (grade > 150) { + throw GradeTooLowException(); + } else { + this->_grade = grade; + } +} + +Bureaucrat::Bureaucrat(Bureaucrat const & copy) : _name(copy._name), _grade(copy._grade) { + std::cout << "[Bureaucrat] Copy constructor called" << std::endl; +} + +/** + * @brief Constructor with the operator '=' (Overload) + * + * @param assign Bureaucrat to copie + * @return It's a constructor + */ +Bureaucrat& Bureaucrat::operator=(Bureaucrat const & assign) { + std::cout << "[Bureaucrat] Assign constructor called" << std::endl; + this->_grade = assign._grade; + this->_name = assign._name; + return *this; +} + +/** + * @brief Destructor for the Bureaucrat + */ +Bureaucrat::~Bureaucrat() { + std::cout << "[Bureaucrat] Desctuctor called" << std::endl; + +} + +/** + * @brief Getter for the grade + * + * @return grade (unsigned int 8bits) + */ +int Bureaucrat::getGrade() const { + return (this->_grade); +} + +/** + * @brief Getter for the name + * + * @return name (string from standard) + */ +std::string Bureaucrat::getName() const { + return (this->_name); +} + +/** + * @brief Get a promotion for the Bureaucrat + */ +void Bureaucrat::promote() { + if (this->_grade == 1) { + throw GradeTooHighException(); + } else { + --this->_grade; + } +} + +/** + * @brief Get a demotion for the Bureaucrat + */ +void Bureaucrat::demote() { + if (this->_grade == 150) { + throw GradeTooLowException(); + } else { + ++this->_grade; + } +} +/** + * @brief Overload of the '<<' operator to print + * + * @param output previous output + * @param toPrint Bureaucrat to print + * @return the output with the Bureaucrat + */ +std::ostream& operator<<(std::ostream& output, Bureaucrat const &toPrint) { + output << toPrint.getName() << ", bureaucrat grade " << toPrint.getGrade() << "."; + return (output); +} + +void Bureaucrat::executeForm(AForm const &form) const { + try { + form.execute(*this); + } catch (std::exception &e) { + std::cout << this->getName() << " cannot execute " << form.getName() << " because " << e.what() << std::endl; + } +} + +void Bureaucrat::signForm(AForm &form) { + try { + form.beSigned(*this); + } catch (std::exception &e) { + std::cout << this->getName() << " cannot sign " << form.getName() << " because " << e.what() << std::endl; + } +} diff --git a/cpp05/ex02/Bureaucrat.hpp b/cpp05/ex02/Bureaucrat.hpp new file mode 100644 index 0000000..f88ae6b --- /dev/null +++ b/cpp05/ex02/Bureaucrat.hpp @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/16 14:48:33 by rparodi #+# #+# */ +/* Updated: 2025/03/18 15:13:08 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BUREAUCRAT_HPP +#define BUREAUCRAT_HPP + +#include +#include +#include +#include "AForm.hpp" + +#define RESET "\033[0m" +#define YELLOW "\033[0;33m" +#define RED "\033[0;31m" + +class Bureaucrat { + public: + Bureaucrat(void); + Bureaucrat(std::string name, int grade); + Bureaucrat(Bureaucrat const ©); + Bureaucrat& operator=(Bureaucrat const &assign); + ~Bureaucrat(void); + std::string getName(void) const; + int getGrade(void) const; + void promote(void); + void demote(void); + void signForm(AForm &form); + void executeForm(AForm const &form) const; + class GradeTooHighException : public std::exception { + virtual const char *what() const throw() { + return ("Error:\n> Grade is too high (have to be lower than 1)"); + } + }; + class GradeTooLowException : public std::exception { + virtual const char *what() const throw() { + return ("Error:\n> Grade is too low (have to be higher than 150)"); + } + }; + private: + std::string _name; + int _grade; +}; + +std::ostream& operator<<(std::ostream& output, Bureaucrat const &bureaucrat); + +#endif diff --git a/cpp05/ex02/Makefile b/cpp05/ex02/Makefile new file mode 100644 index 0000000..9eeed39 --- /dev/null +++ b/cpp05/ex02/Makefile @@ -0,0 +1,131 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: rparodi +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/11/12 11:05:05 by rparodi #+# #+# # +# Updated: 2025/03/18 22:57:32 by rparodi ### ########.fr # +# # +# **************************************************************************** # + +# Variables + +# Name +NAME = form + +# Commands +CXX = c++ +RM = rm -rf + +# Flags +# Mandatory flags for 42 +CXXFLAGS = -Werror -Wextra -Wall -std=c++98 -I./includes/ +# Flags to have the dependences (can be removed for correction) +CXXFLAGS += -MMD +# Flags to have the debug (can be removed for correction) +DEBUG = -g3 +DEBUG += -fsanitize=address +CXXFLAGS += $(DEBUG) + +# Sources +SRC = Bureaucrat.cpp \ + AForm.cpp \ + RobotomyRequestForm.cpp \ + ShrubberyCreationForm.cpp \ + PresidentialPardonForm.cpp \ + main.cpp + +# Objects +OBJDIRNAME = ./build +OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.cpp=.o)) + +# Colors +GREEN = \033[32m +GREY = \033[0;90m +RED = \033[0;31m +GOLD = \033[38;5;220m +END = \033[0m + +# Rules + +# All (make all) +all: header $(NAME) footer + +# Bonus (make bonus) +bonus: header $(OBJ) footer + @mkdir -p $(OBJDIRNAME) + @printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n' + @$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ) + +# Clean (make clean) +clean: + @printf '$(GREY) Removing $(END)$(RED)Objects$(END)\n' + @printf '$(GREY) Removing $(END)$(RED)Objects Folder$(END)\n' + @$(RM) $(OBJDIRNAME) + +# Clean (make fclean) +fclean: clean + @printf '$(GREY) Removing $(END)$(RED)Program$(END)\n' + @$(RM) $(NAME) + @echo "" + +# Restart (make re) +re: header fclean all + +# Dependences for all +$(NAME): $(OBJ) + @mkdir -p $(OBJDIRNAME) + @printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n' + @$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ) + +# Creating the objects +$(OBJDIRNAME)/%.o: %.cpp + @mkdir -p $(dir $@) + @printf '$(GREY) Compiling $(END)$(GREEN)$<$(END)\n' + @$(CXX) $(CXXFLAGS) -o $@ -c $< + +# Header +header: + @clear + @printf '\n\n' + @printf '$(GOLD) ******* ****** ******* $(END)\n' + @printf '$(GOLD) ****** *** ******* $(END)\n' + @printf '$(GOLD) ******* * ******* $(END)\n' + @printf '$(GOLD) ****** ******* $(END)\n' + @printf '$(GOLD) ******* ******* $(END)\n' + @printf '$(GOLD) ******************* ******* * $(END)\n' + @printf '$(GOLD) ******************* ******* *** $(END)\n' + @printf '$(GOLD) ****** ******* ****** $(END)\n' + @printf '$(GOLD) ****** $(END)\n' + @printf '$(GOLD) ****** $(END)\n' + @printf '$(GREY) Made by rparodi$(END)\n\n' + +# Footer +footer: + @printf "\n" + @printf "$(GOLD) ,_ _,$(END)\n" + @printf "$(GOLD) | \\___//|$(END)\n" + @printf "$(GOLD) |=6 6=|$(END)\n" + @printf "$(GOLD) \\=._Y_.=/$(END)\n" + @printf "$(GOLD) ) \` ( ,$(END)\n" + @printf "$(GOLD) / \\ (('$(END)\n" + @printf "$(GOLD) | | ))$(END)\n" + @printf "$(GOLD) /| | | |\\_//$(END)\n" + @printf "$(GOLD) \\| |._.| |/-\`$(END)\n" + @printf "$(GOLD) '\"' '\"'$(END)\n" + @printf ' $(GREY)The compilation is$(END) $(GOLD)finish$(END)\n $(GREY)Have a good $(END)$(GOLD)correction !$(END)\n' + +clangd: + @echo \ + "CompileFlags:\n" \ + " Add:\n" \ + " - \"-std=c++98 -Wall -Wextra -Werror\"\n" \ + " - \"-I"$(shell pwd)"/includes\"\n" \ + " - \"-xc++\"\n" \ + > .clangd + +# Phony +.PHONY: all bonus clean fclean re clangd +-include ${OBJ:.o=.d} diff --git a/cpp05/ex02/PresidentialPardonForm.cpp b/cpp05/ex02/PresidentialPardonForm.cpp new file mode 100644 index 0000000..0c2d1aa --- /dev/null +++ b/cpp05/ex02/PresidentialPardonForm.cpp @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/18 12:17:47 by rparodi #+# #+# */ +/* Updated: 2025/03/19 15:37:19 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "PresidentialPardonForm.hpp" + +PresidentialPardonForm::PresidentialPardonForm() : AForm("PresidentialPardonForm", PRES_SIGN_GRADE, PRES_EXEC_GRADE) { + std::cout << "PresidentialPardonForm default constructor" << std::endl; +} + +PresidentialPardonForm::PresidentialPardonForm(std::string target) : AForm("PresidentialPardonForm", PRES_SIGN_GRADE, PRES_EXEC_GRADE) { + _target = target; + std::cout << "PresidentialPardonForm smart constructor" << std::endl; +} + +PresidentialPardonForm::PresidentialPardonForm(PresidentialPardonForm const ©) : AForm("PresidentialPardonForm", PRES_SIGN_GRADE, PRES_EXEC_GRADE) { + _target = copy.getTarget(); + *this = copy; + std::cout << "PresidentialPardonForm copy constructor" << std::endl; +} + +PresidentialPardonForm& PresidentialPardonForm::operator=( PresidentialPardonForm const & assign ) +{ + std::cout << "Copy assignement operator called" << std::endl; + setSign(assign.getSign()); + setExecute(assign.getExecute()); + return (*this); +} + +std::string PresidentialPardonForm::getTarget() const { + return _target; +} + +void PresidentialPardonForm::execute(Bureaucrat const &executor) const { + if (executor.getGrade() > this->getExecute()) { + throw AForm::GradeTooLowException(); + } else { + std::cout << getTarget() << " has been pardoned by Zaphod Beeblebrox." << std::endl; + } +} diff --git a/cpp05/ex02/PresidentialPardonForm.hpp b/cpp05/ex02/PresidentialPardonForm.hpp new file mode 100644 index 0000000..ebb21b6 --- /dev/null +++ b/cpp05/ex02/PresidentialPardonForm.hpp @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/18 11:47:54 by rparodi #+# #+# */ +/* Updated: 2025/03/18 22:53:39 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PRESIDENTIALPARDONFORM_HPP +#define PRESIDENTIALPARDONFORM_HPP + +#include "AForm.hpp" +#include "Bureaucrat.hpp" + +#define PRES_SIGN_GRADE 25 +#define PRES_EXEC_GRADE 5 + +class PresidentialPardonForm: public AForm { + public: + PresidentialPardonForm(); + PresidentialPardonForm(std::string name); + PresidentialPardonForm(PresidentialPardonForm const ©); + PresidentialPardonForm& operator=(PresidentialPardonForm const &assign); + virtual void execute(Bureaucrat const &executor) const; + std::string getTarget() const; + private: + std::string _target; +}; +#endif diff --git a/cpp05/ex02/RobotomyRequestForm.cpp b/cpp05/ex02/RobotomyRequestForm.cpp new file mode 100644 index 0000000..24ffdb4 --- /dev/null +++ b/cpp05/ex02/RobotomyRequestForm.cpp @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RobotomyRequestForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/19 15:51:14 by rparodi #+# #+# */ +/* Updated: 2025/03/19 16:10:19 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "RobotomyRequestForm.hpp" +#include +#include + +RobotomyRequestForm::RobotomyRequestForm() : AForm("RobotomyRequestForm", ROBOT_SIGN_GRADE, ROBOT_EXEC_GRADE) { + std::cout << "RobotomyRequestForm default constructor" << std::endl; +} + +RobotomyRequestForm::RobotomyRequestForm(std::string target) : AForm("RobotomyRequestForm", ROBOT_SIGN_GRADE, ROBOT_EXEC_GRADE) { + _target = target; + std::cout << "RobotomyRequestForm smart constructor" << std::endl; +} + +RobotomyRequestForm::RobotomyRequestForm(RobotomyRequestForm const ©) : AForm("RobotomyRequestForm", ROBOT_SIGN_GRADE, ROBOT_EXEC_GRADE) { + _target = copy.getTarget(); + *this = copy; + std::cout << "RobotomyRequestForm copy constructor" << std::endl; +} + +RobotomyRequestForm& RobotomyRequestForm::operator=( RobotomyRequestForm const & assign ) +{ + std::cout << "Copy assignement operator called" << std::endl; + setSign(assign.getSign()); + setExecute(assign.getExecute()); + return (*this); +} + +std::string RobotomyRequestForm::getTarget() const { + return _target; +} + +void RobotomyRequestForm::execute(Bureaucrat const &executor) const { + static bool isEven = true; + if (executor.getGrade() > this->getExecute()) { + throw AForm::GradeTooLowException(); + } else { + if (isEven) { + std::cout << this->getTarget() << " success his robotomization" << std::endl; + } else { + std::cout << this->getTarget() << " failed his robotomization" << std::endl; + } + isEven = !isEven; + } +} diff --git a/cpp05/ex02/RobotomyRequestForm.hpp b/cpp05/ex02/RobotomyRequestForm.hpp new file mode 100644 index 0000000..70b602f --- /dev/null +++ b/cpp05/ex02/RobotomyRequestForm.hpp @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RobotomyRequestForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/19 15:52:06 by rparodi #+# #+# */ +/* Updated: 2025/03/19 15:54:51 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ROBOTOMYREQUESTFORM_HPP +#define ROBOTOMYREQUESTFORM_HPP + +#include "AForm.hpp" +#include "Bureaucrat.hpp" +#include + +#define ROBOT_SIGN_GRADE 72 +#define ROBOT_EXEC_GRADE 45 + +class RobotomyRequestForm: public AForm { + public: + RobotomyRequestForm(); + RobotomyRequestForm(std::string name); + RobotomyRequestForm(RobotomyRequestForm const ©); + RobotomyRequestForm& operator=(RobotomyRequestForm const &assign); + virtual void execute(Bureaucrat const &executor) const; + std::string getTarget() const; + private: + std::string _target; +}; + +#endif diff --git a/cpp05/ex02/Roger_shrubbery b/cpp05/ex02/Roger_shrubbery new file mode 100644 index 0000000..3b48fdd --- /dev/null +++ b/cpp05/ex02/Roger_shrubbery @@ -0,0 +1,20 @@ + _-_ + /~~ ~~\ + /~~ ~~\ +{ } + \ _- -_ / + ~ \\ // ~ +_- - | | _- _ + _ - | | -_ + // \\ + + _-_ + /~~ ~~\ + /~~ ~~\ +{ } + \ _- -_ / + ~ \\ // ~ +_- - | | _- _ + _ - | | -_ + // \\ + diff --git a/cpp05/ex02/ShrubberyCreationForm.cpp b/cpp05/ex02/ShrubberyCreationForm.cpp new file mode 100644 index 0000000..3119621 --- /dev/null +++ b/cpp05/ex02/ShrubberyCreationForm.cpp @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ShrubberyCreationForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/18 18:05:02 by rparodi #+# #+# */ +/* Updated: 2025/03/19 15:52:44 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ShrubberyCreationForm.hpp" + +ShrubberyCreationForm::ShrubberyCreationForm() : AForm("ShrubberyCreationForm", SHRUB_SIGN_GRADE, SHRUB_EXEC_GRADE) { + std::cout << "ShrubberyCreationForm default constructor" << std::endl; +} + +ShrubberyCreationForm::ShrubberyCreationForm(std::string target) : AForm("ShrubberyCreationForm", SHRUB_SIGN_GRADE, SHRUB_EXEC_GRADE) { + _target = target; + std::cout << "ShrubberyCreationForm smart constructor" << std::endl; +} + +ShrubberyCreationForm::ShrubberyCreationForm(ShrubberyCreationForm const ©) : AForm("ShrubberyCreationForm", SHRUB_SIGN_GRADE, SHRUB_EXEC_GRADE) { + _target = copy.getTarget(); + *this = copy; + std::cout << "ShrubberyCreationForm copy constructor" << std::endl; +} + +ShrubberyCreationForm& ShrubberyCreationForm::operator=( ShrubberyCreationForm const & assign ) +{ + std::cout << "Copy assignement operator called" << std::endl; + setSign(assign.getSign()); + setExecute(assign.getExecute()); + return (*this); +} + +std::string ShrubberyCreationForm::getTarget() const { + return _target; +} + +void ShrubberyCreationForm::execute(Bureaucrat const &executor) const { + if (executor.getGrade() > this->getExecute()) { + throw AForm::GradeTooLowException(); + } else { + std::string filename = this->getTarget() + "_shrubbery"; + std::fstream file(filename.c_str(), std::ios::out); + file << \ + " _-_" << std::endl << \ + " /~~ ~~\\" << std::endl << \ + " /~~ ~~\\" << std::endl << \ + "{ }" << std::endl << \ + " \\ _- -_ /" << std::endl << \ + " ~ \\\\ // ~" << std::endl << \ + "_- - | | _- _" << std::endl << \ + " _ - | | -_" << std::endl << \ + " // \\\\" << std::endl << std::endl << \ + " _-_" << std::endl << \ + " /~~ ~~\\" << std::endl << \ + " /~~ ~~\\" << std::endl << \ + "{ }" << std::endl << \ + " \\ _- -_ /" << std::endl << \ + " ~ \\\\ // ~" << std::endl << \ + "_- - | | _- _" << std::endl << \ + " _ - | | -_" << std::endl << \ + " // \\\\" << std::endl << std::endl; + } +} diff --git a/cpp05/ex02/ShrubberyCreationForm.hpp b/cpp05/ex02/ShrubberyCreationForm.hpp new file mode 100644 index 0000000..0d2ee0c --- /dev/null +++ b/cpp05/ex02/ShrubberyCreationForm.hpp @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ShrubberyCreationForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/18 12:15:09 by rparodi #+# #+# */ +/* Updated: 2025/03/19 15:45:37 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SHRUBBERYCREATIONFORM_HPP +#define SHRUBBERYCREATIONFORM_HPP + +#include "AForm.hpp" +#include "Bureaucrat.hpp" +#include + +#define SHRUB_SIGN_GRADE 145 +#define SHRUB_EXEC_GRADE 137 + +class ShrubberyCreationForm: public AForm { + public: + ShrubberyCreationForm(); + ShrubberyCreationForm(std::string name); + ShrubberyCreationForm(ShrubberyCreationForm const ©); + ShrubberyCreationForm& operator=(ShrubberyCreationForm const &assign); + virtual void execute(Bureaucrat const &executor) const; + std::string getTarget() const; + private: + std::string _target; +}; + +#endif diff --git a/cpp05/ex02/main.cpp b/cpp05/ex02/main.cpp new file mode 100644 index 0000000..94790f9 --- /dev/null +++ b/cpp05/ex02/main.cpp @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/16 14:47:26 by rparodi #+# #+# */ +/* Updated: 2025/03/19 16:10:42 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" +#include "AForm.hpp" +#include "PresidentialPardonForm.hpp" +#include "ShrubberyCreationForm.hpp" +#include "RobotomyRequestForm.hpp" + +int main(void) { + std::cout << YELLOW << "\t\t[ Testing with the grade 0 ]" << RESET << std::endl; + try { + Bureaucrat b1("Roger", 0); + } catch (std::exception & err) { + std::cerr << RED << err.what() << RESET << std::endl; + } + + std::cout << std::endl << YELLOW << "\t\t[ Testing with the grade 151 ]" << RESET << std::endl; + try { + Bureaucrat b1("Robert", 151); + } catch (std::exception & err) { + std::cerr << RED << err.what() << RESET << std::endl; + } + + Bureaucrat b1("Bob", 150); + std::cout << std::endl << YELLOW << "\t\t[ Testing with the demote a Bureaucrate level 150 ]" << RESET << std::endl; + try { + b1.demote(); + } catch (std::exception & err) { + std::cerr << RED << err.what() << RESET << std::endl; + } + std::cout << std::endl << std::endl << b1 << std::endl << std::endl; + std::cout << std::endl << YELLOW << "\t\t[ Testing with the promote a Bureaucrate level 150 ]" << RESET << std::endl; + try { + b1.promote(); + } catch (std::exception & err) { + std::cerr << RED << err.what() << RESET << std::endl; + } + std::cout << std::endl << std::endl << b1 << std::endl << std::endl; + + Bureaucrat b2("Norminet", 1); + std::cout << std::endl << YELLOW << "\t\t[ Testing with the promote a Bureaucrate level 1 ]" << RESET << std::endl; + try { + b2.promote(); + } catch (std::exception & err) { + std::cerr << RED << err.what() << RESET << std::endl; + } + std::cout << std::endl << std::endl << b2 << std::endl << std::endl; + std::cout << std::endl << YELLOW << "\t\t[ Testing with the demote a Bureaucrate level 1 ]" << RESET << std::endl; + try { + b2.demote(); + } catch (std::exception & err) { + std::cerr << RED << err.what() << RESET << std::endl; + } + std::cout << std::endl << std::endl << b2 << std::endl << std::endl; + + std::cout << b2 << std::endl; + try { + b2.promote(); + } catch (std::exception & err) { + std::cerr << RED << err.what() << RESET << std::endl; + } + std::cout << b2 << std::endl; + std::cout << std::endl << YELLOW << "\t\t[ Testing PresidentialPardonForm ]" << RESET << std::endl; + PresidentialPardonForm ppf("Roger"); + b2.executeForm(ppf); + std::cout << std::endl << YELLOW << "\t\t[ Testing ShrubberyCreationForm ]" << RESET << std::endl; + ShrubberyCreationForm scf("Roger"); + b2.executeForm(scf); + std::cout << std::endl << YELLOW << "\t\t[ Testing ShrubberyCreationForm ]" << RESET << std::endl; + RobotomyRequestForm rrf("Roger"); + b2.executeForm(rrf); +}