From 9771c8de4a8ed3921dff8d94d9a64967a7cbf334 Mon Sep 17 00:00:00 2001 From: Raphael Date: Wed, 19 Mar 2025 20:22:33 +0100 Subject: [PATCH] feat: adding the finished ex03 --- .gitignore | 1 + cpp05/ex03/Bureaucrat.cpp | 131 +++++++++++++++++++++++++ cpp05/ex03/Bureaucrat.hpp | 55 +++++++++++ cpp05/ex03/Form.cpp | 111 ++++++++++++++++++++++ cpp05/ex03/Form.hpp | 68 +++++++++++++ cpp05/ex03/Intern.cpp | 56 +++++++++++ cpp05/ex03/Intern.hpp | 31 ++++++ cpp05/ex03/Makefile | 132 ++++++++++++++++++++++++++ cpp05/ex03/PresidentialPardonForm.cpp | 48 ++++++++++ cpp05/ex03/PresidentialPardonForm.hpp | 33 +++++++ cpp05/ex03/RobotomyRequestForm.cpp | 56 +++++++++++ cpp05/ex03/RobotomyRequestForm.hpp | 35 +++++++ cpp05/ex03/ShrubberyCreationForm.cpp | 68 +++++++++++++ cpp05/ex03/ShrubberyCreationForm.hpp | 35 +++++++ cpp05/ex03/main.cpp | 105 ++++++++++++++++++++ 15 files changed, 965 insertions(+) create mode 100644 cpp05/ex03/Bureaucrat.cpp create mode 100644 cpp05/ex03/Bureaucrat.hpp create mode 100644 cpp05/ex03/Form.cpp create mode 100644 cpp05/ex03/Form.hpp create mode 100644 cpp05/ex03/Intern.cpp create mode 100644 cpp05/ex03/Intern.hpp create mode 100644 cpp05/ex03/Makefile create mode 100644 cpp05/ex03/PresidentialPardonForm.cpp create mode 100644 cpp05/ex03/PresidentialPardonForm.hpp create mode 100644 cpp05/ex03/RobotomyRequestForm.cpp create mode 100644 cpp05/ex03/RobotomyRequestForm.hpp create mode 100644 cpp05/ex03/ShrubberyCreationForm.cpp create mode 100644 cpp05/ex03/ShrubberyCreationForm.hpp create mode 100644 cpp05/ex03/main.cpp diff --git a/.gitignore b/.gitignore index e8a78b0..e1bdc88 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ polymorphism form bureaucrat *_shrubbery +intern diff --git a/cpp05/ex03/Bureaucrat.cpp b/cpp05/ex03/Bureaucrat.cpp new file mode 100644 index 0000000..24cd351 --- /dev/null +++ b/cpp05/ex03/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(Form 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(Form &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/ex03/Bureaucrat.hpp b/cpp05/ex03/Bureaucrat.hpp new file mode 100644 index 0000000..5f0e24b --- /dev/null +++ b/cpp05/ex03/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 "Form.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(Form &form); + void executeForm(Form 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/ex03/Form.cpp b/cpp05/ex03/Form.cpp new file mode 100644 index 0000000..f21fb0d --- /dev/null +++ b/cpp05/ex03/Form.cpp @@ -0,0 +1,111 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/17 14:19:41 by rparodi #+# #+# */ +/* Updated: 2025/03/18 15:09:05 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Form.hpp" +#include "Bureaucrat.hpp" + +/** + * @brief Constructor by default of Form + */ +Form::Form(void) : _name("Default"), _signed(false), _to_execute(150), _to_sign(150) { + std::cout << "[Form] Default constructor called" << std::endl; +} +Form::Form(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 << "[Form] Smart constructor called" << std::endl; +} + +Form::Form(Form const ©) { + std::cout << "[Form] Copy constructor called" << std::endl; + this->_name = copy._name; + this->_to_sign = copy._to_sign; + this->_to_execute = copy._to_execute; + _signed = copy._signed; +} + +Form& Form::operator=(Form const &assign) { + std::cout << "[Form] 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; +} + +Form::~Form() { + std::cout << "[Form] Destructor called" << std::endl; +} + +std::string Form::getName() const { + return this->_name; +} + +int Form::getExecute() const { + return this->_to_execute; +} + +int Form::getSign() const { + return this->_to_sign; +} + +bool Form::isSigned() const { + return this->_signed; +} + +void Form::setSign(int new_sign) { + this->_to_sign = new_sign; +} + +void Form::setExecute(int new_exec) { + this->_to_execute = new_exec; +} + +std::ostream& operator<<(std::ostream& output, Form 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 Form::execute(Bureaucrat const &executor) const { + if (executor.getGrade() > this->_to_execute) { + throw FormForSupperior(); + } else if (_signed == false) { + throw FormAlreadySigned(); + } + else { + std::cout << "Bureaucrat " << executor.getName() << " executes the Form " << this->_name << std::endl; + } +} + +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; + this->_signed = true; + } +} diff --git a/cpp05/ex03/Form.hpp b/cpp05/ex03/Form.hpp new file mode 100644 index 0000000..c86d973 --- /dev/null +++ b/cpp05/ex03/Form.hpp @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/17 14:20:06 by rparodi #+# #+# */ +/* Updated: 2025/03/19 19:57:45 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef AFORM_HPP +#define AFORM_HPP + +#include +#include +#include +#include "Form.hpp" + +class Bureaucrat; + +class Form { + public: + Form(); + Form(std::string const name, int execute, int sign); + Form(Form const ©); + Form& operator=(Form const &assign); + ~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)"); + } + }; + 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 FormForSupperior: public std::exception { + virtual const char *what() const throw() { + return ("the bureaucrat grade is too low !"); + } + }; + class FormAlreadySigned: 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; + 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, Form const &Form); + +#endif diff --git a/cpp05/ex03/Intern.cpp b/cpp05/ex03/Intern.cpp new file mode 100644 index 0000000..1187b3b --- /dev/null +++ b/cpp05/ex03/Intern.cpp @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Intern.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/19 19:43:56 by rparodi #+# #+# */ +/* Updated: 2025/03/19 20:18:23 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Intern.hpp" +#include + +Intern::Intern() { + std::cout << "[Intern] default constructor" << std::endl; +} + +Intern::Intern(Intern const & copy) { + std::cout << "[Intern] copy constructor" << std::endl; + *this = copy; +} + +Intern& Intern::operator= (Intern const & assign) { + std::cout << "[Intern] assignation operator" << std::endl; + if (this != &assign) { + (void)assign; + } + return *this; +} + +Intern::~Intern() { + std::cout << "[Intern] destructor" << std::endl; +} + +Form* Intern::makeForm(std::string type, std::string target) { + std::string formTypes[3] = {"shrubbery creation", "robotomy request", "presidential pardon"}; + for (int i = 0; i < 3; i++) { + if (type == formTypes[i]) { + std::cout << "Intern creates " << type << std::endl; + switch (i) { + case 0: + return new ShrubberyCreationForm(target); + case 1: + return new RobotomyRequestForm(target); + case 2: + return new PresidentialPardonForm(target); + default: + break; + } + } + } + std::cout << "Intern could not create " << type << std::endl; + return NULL; +} diff --git a/cpp05/ex03/Intern.hpp b/cpp05/ex03/Intern.hpp new file mode 100644 index 0000000..f716459 --- /dev/null +++ b/cpp05/ex03/Intern.hpp @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Intern.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/19 19:43:54 by rparodi #+# #+# */ +/* Updated: 2025/03/19 20:17:55 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef INTERN_HPP +#define INTERN_HPP + +#include +#include +#include "PresidentialPardonForm.hpp" +#include "RobotomyRequestForm.hpp" +#include "ShrubberyCreationForm.hpp" + +class Intern { + public: + Intern(); + Intern(Intern const & copy); + Intern& operator= (Intern const & assign); + ~Intern(); + Form* makeForm(std::string type, std::string target); +}; + +#endif diff --git a/cpp05/ex03/Makefile b/cpp05/ex03/Makefile new file mode 100644 index 0000000..6455820 --- /dev/null +++ b/cpp05/ex03/Makefile @@ -0,0 +1,132 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: rparodi +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/11/12 11:05:05 by rparodi #+# #+# # +# Updated: 2025/03/19 19:30:02 by rparodi ### ########.fr # +# # +# **************************************************************************** # + +# Variables + +# Name +NAME = intern + +# 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 \ + Form.cpp \ + RobotomyRequestForm.cpp \ + ShrubberyCreationForm.cpp \ + PresidentialPardonForm.cpp \ + Intern.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/ex03/PresidentialPardonForm.cpp b/cpp05/ex03/PresidentialPardonForm.cpp new file mode 100644 index 0000000..ea3ff63 --- /dev/null +++ b/cpp05/ex03/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() : Form("PresidentialPardonForm", PRES_SIGN_GRADE, PRES_EXEC_GRADE) { + std::cout << "PresidentialPardonForm default constructor" << std::endl; +} + +PresidentialPardonForm::PresidentialPardonForm(std::string target) : Form("PresidentialPardonForm", PRES_SIGN_GRADE, PRES_EXEC_GRADE) { + _target = target; + std::cout << "PresidentialPardonForm smart constructor" << std::endl; +} + +PresidentialPardonForm::PresidentialPardonForm(PresidentialPardonForm const ©) : Form("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 Form::GradeTooLowException(); + } else { + std::cout << getTarget() << " has been pardoned by Zaphod Beeblebrox." << std::endl; + } +} diff --git a/cpp05/ex03/PresidentialPardonForm.hpp b/cpp05/ex03/PresidentialPardonForm.hpp new file mode 100644 index 0000000..c8d05ab --- /dev/null +++ b/cpp05/ex03/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 "Form.hpp" +#include "Bureaucrat.hpp" + +#define PRES_SIGN_GRADE 25 +#define PRES_EXEC_GRADE 5 + +class PresidentialPardonForm: public Form { + 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/ex03/RobotomyRequestForm.cpp b/cpp05/ex03/RobotomyRequestForm.cpp new file mode 100644 index 0000000..d3324b6 --- /dev/null +++ b/cpp05/ex03/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() : Form("RobotomyRequestForm", ROBOT_SIGN_GRADE, ROBOT_EXEC_GRADE) { + std::cout << "RobotomyRequestForm default constructor" << std::endl; +} + +RobotomyRequestForm::RobotomyRequestForm(std::string target) : Form("RobotomyRequestForm", ROBOT_SIGN_GRADE, ROBOT_EXEC_GRADE) { + _target = target; + std::cout << "RobotomyRequestForm smart constructor" << std::endl; +} + +RobotomyRequestForm::RobotomyRequestForm(RobotomyRequestForm const ©) : Form("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 Form::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/ex03/RobotomyRequestForm.hpp b/cpp05/ex03/RobotomyRequestForm.hpp new file mode 100644 index 0000000..f655ccc --- /dev/null +++ b/cpp05/ex03/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 "Form.hpp" +#include "Bureaucrat.hpp" +#include + +#define ROBOT_SIGN_GRADE 72 +#define ROBOT_EXEC_GRADE 45 + +class RobotomyRequestForm: public Form { + 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/ex03/ShrubberyCreationForm.cpp b/cpp05/ex03/ShrubberyCreationForm.cpp new file mode 100644 index 0000000..db9b4d8 --- /dev/null +++ b/cpp05/ex03/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() : Form("ShrubberyCreationForm", SHRUB_SIGN_GRADE, SHRUB_EXEC_GRADE) { + std::cout << "ShrubberyCreationForm default constructor" << std::endl; +} + +ShrubberyCreationForm::ShrubberyCreationForm(std::string target) : Form("ShrubberyCreationForm", SHRUB_SIGN_GRADE, SHRUB_EXEC_GRADE) { + _target = target; + std::cout << "ShrubberyCreationForm smart constructor" << std::endl; +} + +ShrubberyCreationForm::ShrubberyCreationForm(ShrubberyCreationForm const ©) : Form("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 Form::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/ex03/ShrubberyCreationForm.hpp b/cpp05/ex03/ShrubberyCreationForm.hpp new file mode 100644 index 0000000..cf9cb8e --- /dev/null +++ b/cpp05/ex03/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 "Form.hpp" +#include "Bureaucrat.hpp" +#include + +#define SHRUB_SIGN_GRADE 145 +#define SHRUB_EXEC_GRADE 137 + +class ShrubberyCreationForm: public Form { + 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/ex03/main.cpp b/cpp05/ex03/main.cpp new file mode 100644 index 0000000..8207035 --- /dev/null +++ b/cpp05/ex03/main.cpp @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/16 14:47:26 by rparodi #+# #+# */ +/* Updated: 2025/03/19 20:21:34 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" +#include "PresidentialPardonForm.hpp" +#include "RobotomyRequestForm.hpp" +#include "ShrubberyCreationForm.hpp" +#include "Intern.hpp" + +#define SCF_LAUNCH 5 + +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 (With permissions) ]" << RESET << std::endl; + PresidentialPardonForm ppf("Roger"); + b2.executeForm(ppf); + + std::cout << std::endl << YELLOW << "\t\t[ Testing PresidentialPardonForm (Without permissions) ]" << RESET << std::endl; + b1.executeForm(ppf); + + std::cout << std::endl << YELLOW << "\t\t[ Testing ShrubberyCreationForm (With permissions) ]" << RESET << std::endl; + ShrubberyCreationForm scf("Roger"); + b2.executeForm(scf); + + std::cout << std::endl << YELLOW << "\t\t[ Testing ShrubberyCreationForm (Without permissions) ]" << RESET << std::endl; + b1.executeForm(scf); + + std::cout << std::endl << YELLOW << "\t\t[ Testing RobotomyRequestForm (With permissions) ]" << RESET << std::endl; + RobotomyRequestForm rrf("Roger"); + for (int i = 0; i < SCF_LAUNCH; i++) { + b2.executeForm(rrf); + } + + std::cout << std::endl << YELLOW << "\t\t[ Testing RobotomyRequestForm (Without permissions) ]" << RESET << std::endl; + b1.executeForm(rrf); + std::cout << std::endl << std::endl; + Intern stagiaire; + std::cout << std::endl << YELLOW << "\t\t[ Testing Intern with empty string ]" << RESET << std::endl; + stagiaire.makeForm("", "Roger"); + std::cout << std::endl << YELLOW << "\t\t[ Testing Intern with PresidentialPardonForm ]" << RESET << std::endl; + Form *pres_intern = stagiaire.makeForm("presidential pardon", "Roger"); + pres_intern->beSigned(b2); + b2.executeForm(*pres_intern); +}