From 5829da2bcb6ff754d0c714d22c72dfa14d4a97f2 Mon Sep 17 00:00:00 2001 From: Raphael Date: Mon, 17 Mar 2025 20:18:31 +0100 Subject: [PATCH] feat: finishing the form part --- .gitignore | 1 + cpp05/ex01/Bureaucrat.cpp | 126 ++++++++++++++++++++++++++++++++++++++ cpp05/ex01/Bureaucrat.hpp | 54 ++++++++++++++++ cpp05/ex01/Form.cpp | 89 +++++++++++++++++++++++++++ cpp05/ex01/Form.hpp | 52 ++++++++++++++++ cpp05/ex01/Makefile | 126 ++++++++++++++++++++++++++++++++++++++ cpp05/ex01/main.cpp | 68 ++++++++++++++++++++ 7 files changed, 516 insertions(+) create mode 100644 cpp05/ex01/Bureaucrat.cpp create mode 100644 cpp05/ex01/Bureaucrat.hpp create mode 100644 cpp05/ex01/Form.cpp create mode 100644 cpp05/ex01/Form.hpp create mode 100644 cpp05/ex01/Makefile create mode 100644 cpp05/ex01/main.cpp diff --git a/.gitignore b/.gitignore index 9b911a2..a3aa5da 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,5 @@ Fixed open polymorphism *.replace +form bureaucrat diff --git a/cpp05/ex01/Bureaucrat.cpp b/cpp05/ex01/Bureaucrat.cpp new file mode 100644 index 0000000..76b59ad --- /dev/null +++ b/cpp05/ex01/Bureaucrat.cpp @@ -0,0 +1,126 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/16 14:49:52 by rparodi #+# #+# */ +/* Updated: 2025/03/17 16:59:03 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::signForm(Form const &form) { + if (form.isSigned()) { + std::cerr << this->getName() << " couldn’t sign " << form.getName() << " because form is already signed !" << std::endl; + } else { + if (form.getSign() < this->_grade) { + throw GradeTooLowException(); + return; + } + } +} diff --git a/cpp05/ex01/Bureaucrat.hpp b/cpp05/ex01/Bureaucrat.hpp new file mode 100644 index 0000000..5e1b045 --- /dev/null +++ b/cpp05/ex01/Bureaucrat.hpp @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/16 14:48:33 by rparodi #+# #+# */ +/* Updated: 2025/03/17 17:06:04 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 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)"); + } + }; + 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/ex01/Form.cpp b/cpp05/ex01/Form.cpp new file mode 100644 index 0000000..c577fa6 --- /dev/null +++ b/cpp05/ex01/Form.cpp @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/17 14:19:41 by rparodi #+# #+# */ +/* Updated: 2025/03/17 20:17:10 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; +} + +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::beSigned(Bureaucrat &bureaucrat) { + try { + bureaucrat.signForm(*this); + } catch (std::exception & err) { + std::cerr << err.what() << std::endl; + } + _signed = true; +} diff --git a/cpp05/ex01/Form.hpp b/cpp05/ex01/Form.hpp new file mode 100644 index 0000000..0005ffe --- /dev/null +++ b/cpp05/ex01/Form.hpp @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/17 14:20:06 by rparodi #+# #+# */ +/* Updated: 2025/03/17 20:17:37 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FORM_HPP +#define FORM_HPP + +#include +#include + +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)"); + } + }; + std::string getName() const; + void beSigned(Bureaucrat &bureaucrat); + int getExecute() const; + int getSign() const; + bool isSigned() const; + private: + std::string _name; + bool _signed; + int _to_execute; + int _to_sign; +}; + +std::ostream& operator<<(std::ostream& output, Form const &Form); + +#endif diff --git a/cpp05/ex01/Makefile b/cpp05/ex01/Makefile new file mode 100644 index 0000000..9bf635b --- /dev/null +++ b/cpp05/ex01/Makefile @@ -0,0 +1,126 @@ +# **************************************************************************** # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: rparodi +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/11/12 11:05:05 by rparodi #+# #+# # +# Updated: 2024/12/21 19:50:58 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 -fsanitize=address +CXXFLAGS += $(DEBUG) + +# Sources +SRC = Bureaucrat.cpp \ + Form.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/ex01/main.cpp b/cpp05/ex01/main.cpp new file mode 100644 index 0000000..1f15f4f --- /dev/null +++ b/cpp05/ex01/main.cpp @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/16 14:47:26 by rparodi #+# #+# */ +/* Updated: 2025/03/17 16:37:04 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" +#include "Form.hpp" +#include + +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 << std::endl << YELLOW << "\t\t[ Testing the overload of the '<<' operator ]" << RESET << std::endl; + Form f1("Norme to cpp", 1, 1); + std::cout << f1 << std::endl; +}