feat: adding files made in fablab

This commit is contained in:
Raphael 2025-03-19 16:11:05 +01:00
parent 9067bf46fc
commit 5d81e1e176
17 changed files with 885 additions and 12 deletions

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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) {

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)");

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}
}

View file

@ -6,12 +6,12 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <string>
#include <iostream>
@ -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;
};

111
cpp05/ex02/AForm.cpp Normal file
View file

@ -0,0 +1,111 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* AForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 &copy) {
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;
}
}

68
cpp05/ex02/AForm.hpp Normal file
View file

@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* AForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <string>
#include <iostream>
#include <exception>
#include "AForm.hpp"
class Bureaucrat;
class AForm {
public:
AForm();
AForm(std::string const name, int execute, int sign);
AForm(AForm const &copy);
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

131
cpp05/ex02/Bureaucrat.cpp Normal file
View file

@ -0,0 +1,131 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/16 14:49:52 by rparodi #+# #+# */
/* Updated: 2025/03/18 15:20:46 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
#include <iostream>
#include <ostream>
/**
* @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;
}
}

55
cpp05/ex02/Bureaucrat.hpp Normal file
View file

@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <exception>
#include <string>
#include <iostream>
#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 &copy);
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

131
cpp05/ex02/Makefile Normal file
View file

@ -0,0 +1,131 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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}

View file

@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PresidentialPardonForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 &copy) : 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;
}
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PresidentialPardonForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 &copy);
PresidentialPardonForm& operator=(PresidentialPardonForm const &assign);
virtual void execute(Bureaucrat const &executor) const;
std::string getTarget() const;
private:
std::string _target;
};
#endif

View file

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* RobotomyRequestForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/19 15:51:14 by rparodi #+# #+# */
/* Updated: 2025/03/19 16:10:19 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "RobotomyRequestForm.hpp"
#include <cstdlib>
#include <ctime>
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 &copy) : 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;
}
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* RobotomyRequestForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <fstream>
#define ROBOT_SIGN_GRADE 72
#define ROBOT_EXEC_GRADE 45
class RobotomyRequestForm: public AForm {
public:
RobotomyRequestForm();
RobotomyRequestForm(std::string name);
RobotomyRequestForm(RobotomyRequestForm const &copy);
RobotomyRequestForm& operator=(RobotomyRequestForm const &assign);
virtual void execute(Bureaucrat const &executor) const;
std::string getTarget() const;
private:
std::string _target;
};
#endif

View file

@ -0,0 +1,20 @@
_-_
/~~ ~~\
/~~ ~~\
{ }
\ _- -_ /
~ \\ // ~
_- - | | _- _
_ - | | -_
// \\
_-_
/~~ ~~\
/~~ ~~\
{ }
\ _- -_ /
~ \\ // ~
_- - | | _- _
_ - | | -_
// \\

View file

@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ShrubberyCreationForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 &copy) : 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;
}
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ShrubberyCreationForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <fstream>
#define SHRUB_SIGN_GRADE 145
#define SHRUB_EXEC_GRADE 137
class ShrubberyCreationForm: public AForm {
public:
ShrubberyCreationForm();
ShrubberyCreationForm(std::string name);
ShrubberyCreationForm(ShrubberyCreationForm const &copy);
ShrubberyCreationForm& operator=(ShrubberyCreationForm const &assign);
virtual void execute(Bureaucrat const &executor) const;
std::string getTarget() const;
private:
std::string _target;
};
#endif

82
cpp05/ex02/main.cpp Normal file
View file

@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}