Compare commits

..

17 Commits

Author SHA1 Message Date
aaa9688308 Intern fix, actions duplicated 2026-02-21 12:47:19 +01:00
a278e678c8 ex03 executable name -> intern 2026-02-21 12:15:47 +01:00
7823775797 Fixing header 2026-02-21 12:15:22 +01:00
aortigos
ffbe662286 Fixing header main to ex02 2026-02-18 21:29:38 +01:00
aortigos
57eefd3b37 ex03 finished 2026-02-18 21:28:38 +01:00
7539e90e6a Starting Intern Class 2026-02-18 10:39:09 +01:00
39ec747514 Starting ex03 2026-02-18 10:31:00 +01:00
f96fb516ff ex02 finished 2026-02-18 00:16:54 +01:00
c233d14af7 ShrubberyCreationForm create file 2026-02-17 18:01:24 +01:00
5d62dbd20d fixing and starting ShrubberyCreationForm 2026-02-17 17:49:39 +01:00
838b97e98a Removed isSigned param when initializing Form, because it should always start as FALSE 2026-02-17 16:30:19 +01:00
44c8e30732 Form always start in not signed 2026-02-17 14:07:58 +01:00
85ff75e7bc Cleaning .o 2026-02-17 13:54:10 +01:00
7f81562db3 ex02 created 2026-02-17 10:55:21 +01:00
bc4efdf7e5 Adding comments 2026-02-17 10:17:56 +01:00
90ed3ce3b2 Added one space on printing error at signing 2026-02-17 09:54:40 +01:00
d0a2146252 Adding comments 2026-02-17 09:50:40 +01:00
34 changed files with 2043 additions and 38 deletions

View File

@@ -1,17 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.cpp :+: :+: :+: */
/* Bureaucrat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/15 18:00:27 by aortigos #+# #+# */
/* Updated: 2026/02/06 02:28:09 by aortigos ### ########.fr */
/* Created: 2026/02/21 11:37:41 by aortigos #+# #+# */
/* Updated: 2026/02/21 11:53:41 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
//////////////////
//Constructores //
//////////////////
Bureaucrat::Bureaucrat() : name("NULL"), grade(150)
{
//std::cout << "Bureaucrat has been created" << std::endl;
@@ -45,6 +49,11 @@ Bureaucrat::Bureaucrat(std::string name, int grade) : name(name)
//std::cout << "Bureaucrat with params has been created" << std::endl;
}
//////////////////
// Getters //
//////////////////
std::string Bureaucrat::getName() const
{
return (this->name);
@@ -55,6 +64,11 @@ int Bureaucrat::getGrade() const
return (this->grade);
}
//////////////////
// Grade //
//////////////////
void Bureaucrat::incrementGrade()
{
if (this->grade <= 1)
@@ -69,6 +83,25 @@ void Bureaucrat::decrementGrade()
this->grade++;
}
//////////////////
// << //
//////////////////
std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
{
os << bureaucrat.getName()
<< ", bureaucrat grade "
<< bureaucrat.getGrade()
<< std::endl;
return (os);
}
//////////////////
// Excepciones //
//////////////////
const char* Bureaucrat::GradeTooHighException::what() const throw()
{
return ("Grade is too high!");
@@ -78,12 +111,3 @@ const char* Bureaucrat::GradeTooLowException::what() const throw()
{
return ("Grade is too low!");
}
std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
{
os << bureaucrat.getName()
<< ", bureaucrat grade "
<< bureaucrat.getGrade()
<< std::endl;
return (os);
}

View File

@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.hpp :+: :+: :+: */
/* Bureaucrat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/15 18:00:27 by aortigos #+# #+# */
/* Updated: 2026/02/06 02:28:09 by aortigos ### ########.fr */
/* Created: 2026/02/21 11:37:35 by aortigos #+# #+# */
/* Updated: 2026/02/21 11:37:36 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */

View File

@@ -3,13 +3,14 @@
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hadi <hadi@student.42.fr> +#+ +:+ +#+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/16 22:32:41 by hadi #+# #+# */
/* Updated: 2026/02/16 23:35:56 by hadi ### ########.fr */
/* Created: 2026/02/21 11:35:39 by aortigos #+# #+# */
/* Updated: 2026/02/21 11:35:40 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat/Bureaucrat.hpp"
int main()

View File

@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.cpp :+: :+: :+: */
/* Bureaucrat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/15 18:00:27 by aortigos #+# #+# */
/* Updated: 2026/02/06 02:28:09 by aortigos ### ########.fr */
/* Created: 2026/02/21 12:10:56 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:10:56 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
@@ -84,6 +84,10 @@ void Bureaucrat::decrementGrade()
this->grade++;
}
//////////////////
// Sign //
//////////////////
void Bureaucrat::signForm(Form &form)
{
try {
@@ -94,7 +98,7 @@ void Bureaucrat::signForm(Form &form)
<< std::endl;
} catch (std::exception &e) {
std::cout << this->name
<< "couldn't sign "
<< " couldn't sign "
<< form.getName()
<< " because "
<< e.what()

View File

@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.hpp :+: :+: :+: */
/* Bureaucrat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/15 18:00:27 by aortigos #+# #+# */
/* Updated: 2026/02/06 02:28:09 by aortigos ### ########.fr */
/* Created: 2026/02/21 12:11:02 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:11:14 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */

View File

@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Form.cpp :+: :+: :+: */
/* Form.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/15 18:00:27 by aortigos #+# #+# */
/* Updated: 2026/02/06 02:28:09 by aortigos ### ########.fr */
/* Created: 2026/02/21 12:11:18 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:11:18 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,8 +21,8 @@ Form::Form() : name("NULL"), isSigned(false), gradeToSign(1), gradeToExecute(1)
//std::cout << "Form default constructor called" << std::endl;
}
Form::Form(std::string name, bool isSigned, int gradeToSign, int gradeToExecute)
: name(name), isSigned(isSigned),
Form::Form(std::string name, int gradeToSign, int gradeToExecute)
: name(name), isSigned(false),
gradeToSign(gradeToSign), gradeToExecute(gradeToExecute)
{
if (gradeToSign > 150 || gradeToExecute > 150)

View File

@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Form.hpp :+: :+: :+: */
/* Form.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/15 18:00:27 by aortigos #+# #+# */
/* Updated: 2026/02/06 02:28:09 by aortigos ### ########.fr */
/* Created: 2026/02/21 12:11:23 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:11:30 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
@@ -31,7 +31,7 @@ class Form
Form& operator=(const Form &other);
~Form();
Form(std::string name, bool isSigned, int gradeToSign, int gradeToExecute);
Form(std::string name, int gradeToSign, int gradeToExecute);
std::string getName() const;
bool getIsSigned() const;

View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hadi <hadi@student.42.fr> +#+ +:+ +#+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/16 22:32:41 by hadi #+# #+# */
/* Updated: 2026/02/17 09:48:55 by hadi ### ########.fr */
/* Created: 2026/02/21 12:11:32 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:11:32 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,7 +18,7 @@ int main()
std::cout << "---- ex01 tests ----" << std::endl;
try {
Form form("AngelForm", false, 139, 5);
Form form("AngelForm", 139, 5);
Bureaucrat angel("Angel", 140);
std::cout << form << std::endl;

125
ex02/AForm/AForm.cpp Normal file
View File

@@ -0,0 +1,125 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* AForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:12:17 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:12:17 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "AForm.hpp"
#include "../Bureaucrat/Bureaucrat.hpp"
//////////////////
//Constructores //
//////////////////
AForm::AForm() : name("NULL"), isSigned(false), gradeToSign(1), gradeToExecute(1)
{
//std::cout << "AForm default constructor called" << std::endl;
}
AForm::AForm(std::string name, int gradeToSign, int gradeToExecute)
: name(name), isSigned(false),
gradeToSign(gradeToSign), gradeToExecute(gradeToExecute)
{
if (gradeToSign > 150 || gradeToExecute > 150)
throw GradeTooLowException();
else if (gradeToSign < 1 || gradeToExecute < 1)
throw GradeTooHighException();
//std::cout << "AForm constructor with params called" << std::endl;
}
AForm::AForm(const AForm &other) :
name (other.getName()), isSigned(other.getIsSigned()),
gradeToSign(other.getGradeToSign()),
gradeToExecute(other.getGradeToExecute())
{
//std::cout << "AForm copy constructor called" << std::endl;
}
AForm& AForm::operator=(const AForm &other)
{
if (this != &other)
this->isSigned = other.getIsSigned();
//std::cout << "AForm copy assigment operator called" << std::endl;
return (*this);
}
AForm::~AForm()
{
//std::cout << "Destructor called"
}
//////////////////
// Getters //
//////////////////
std::string AForm::getName() const
{
return (this->name);
}
bool AForm::getIsSigned() const
{
return (this->isSigned);
}
int AForm::getGradeToSign() const
{
return (this->gradeToSign);
}
int AForm::getGradeToExecute() const
{
return (this->gradeToExecute);
}
//////////////////
// SignAForm //
//////////////////
void AForm::beSigned(const Bureaucrat &br)
{
if (this->gradeToSign < br.getGrade())
throw GradeTooLowException();
this->isSigned = true;
}
//////////////////
// << //
//////////////////
std::ostream& operator<<(std::ostream &os, const AForm &form)
{
std::string isItSigned;
if (form.getIsSigned())
{
isItSigned = " is signed.";
} else {
isItSigned = " is not signed.";
}
os << form.getName() << isItSigned;
return (os);
}
//////////////////
// Excepciones //
//////////////////
const char* AForm::GradeTooLowException::what() const throw()
{
return ("grade too low");
}
const char* AForm::GradeTooHighException::what() const throw()
{
return ("grade too high");
}

60
ex02/AForm/AForm.hpp Normal file
View File

@@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* AForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:12:20 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:12:22 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef AForm_HPP
# define AForm_HPP
# include <iostream>
# include <string>
class Bureaucrat;
class AForm
{
private:
const std::string name;
bool isSigned;
const int gradeToSign;
const int gradeToExecute;
public:
AForm();
AForm(const AForm &other);
AForm& operator=(const AForm &other);
virtual ~AForm();
AForm(std::string name, int gradeToSign, int gradeToExecute);
std::string getName() const;
bool getIsSigned() const;
int getGradeToSign() const;
int getGradeToExecute() const;
void beSigned(const Bureaucrat &br);
virtual void execute(Bureaucrat const &executor) const = 0;
class GradeTooLowException : public std::exception {
public:
virtual const char *what() const throw();
};
class GradeTooHighException : public std::exception {
public:
virtual const char *what() const throw();
};
};
std::ostream& operator<<(std::ostream &os, const AForm &AForm);
#endif

View File

@@ -0,0 +1,154 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:12:30 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:12:31 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
#include "../AForm/AForm.hpp"
//////////////////
//Constructores //
//////////////////
Bureaucrat::Bureaucrat() : name("NULL"), grade(150)
{
//std::cout << "Bureaucrat has been created" << std::endl;
}
Bureaucrat::Bureaucrat(const Bureaucrat &other) : name(other.getName()), grade(other.getGrade())
{
//std::cout << "Copy assignment called" << std::endl;
}
Bureaucrat& Bureaucrat::operator=(const Bureaucrat &other)
{
if (this != &other)
this->grade = other.getGrade();
//std::cout << "Copy operator called" << std::endl;
return (*this);
}
Bureaucrat::~Bureaucrat()
{
//std::cout << "Bureaucrat destroyed" << std::endl;
}
Bureaucrat::Bureaucrat(std::string name, int grade) : name(name)
{
if (grade > 150)
throw GradeTooLowException();
else if (grade < 1)
throw GradeTooHighException();
this->grade = grade;
//std::cout << "Bureaucrat with params has been created" << std::endl;
}
//////////////////
// Getters //
//////////////////
std::string Bureaucrat::getName() const
{
return (this->name);
}
int Bureaucrat::getGrade() const
{
return (this->grade);
}
//////////////////
// Grade //
//////////////////
void Bureaucrat::incrementGrade()
{
if (this->grade <= 1)
throw GradeTooHighException();
this->grade--;
}
void Bureaucrat::decrementGrade()
{
if (this->grade >= 150)
throw GradeTooLowException();
this->grade++;
}
//////////////////
// Sign //
//////////////////
void Bureaucrat::signForm(AForm &form)
{
try {
form.beSigned(*this);
std::cout << this->name
<< " signed "
<< form.getName()
<< std::endl;
} catch (std::exception &e) {
std::cout << this->name
<< " couldn't sign "
<< form.getName()
<< " because "
<< e.what()
<< std::endl;
}
}
void Bureaucrat::executeForm(AForm &form)
{
try {
form.execute(*this);
std::cout << this->name
<< " executed "
<< form.getName()
<< std::endl;
} catch (std::exception &e) {
std::cout << this->name
<< " couldn't execute "
<< form.getName()
<< " because "
<< e.what()
<< std::endl;
}
}
//////////////////
// << //
//////////////////
std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
{
os << bureaucrat.getName()
<< ", bureaucrat grade "
<< bureaucrat.getGrade()
<< std::endl;
return (os);
}
//////////////////
// Excepciones //
//////////////////
const char* Bureaucrat::GradeTooHighException::what() const throw()
{
return ("Grade is too high!");
}
const char* Bureaucrat::GradeTooLowException::what() const throw()
{
return ("Grade is too low!");
}

View File

@@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:12:34 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:12:35 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BUREAUCRAT_HPP
# define BUREAUCRAT_HPP
# include <iostream>
class AForm;
class Bureaucrat
{
private:
const std::string name;
int grade;
public:
Bureaucrat();
Bureaucrat(const Bureaucrat &other);
Bureaucrat &operator=(const Bureaucrat &other);
~Bureaucrat();
Bureaucrat(std::string name, int grade);
std::string getName() const;
int getGrade() const;
void incrementGrade();
void decrementGrade();
void signForm(AForm &form);
void executeForm(AForm &form);
class GradeTooHighException : public std::exception {
public:
virtual const char* what() const throw();
};
class GradeTooLowException : public std::exception {
public:
virtual const char* what() const throw();
};
};
std::ostream& operator<<(std::ostream &os, const Bureaucrat &bureaucrat);
#endif

46
ex02/Makefile Normal file
View File

@@ -0,0 +1,46 @@
NAME = formExecution
SRC = main.cpp \
Bureaucrat/Bureaucrat.cpp AForm/AForm.cpp \
ShrubberyCreationForm/ShrubberyCreationForm.cpp \
RobotomyRequestForm/RobotomyRequestForm.cpp \
PresidentialPardonForm/PresidentialPardonForm.cpp
OBJ = $(SRC:.cpp=.o)
CC = c++
CFLAGS = -Wall -Wextra -Werror -std=c++98
GREEN = \033[0;32m
RED = \033[0;31m
RESET = \033[0m
TOTAL := $(words $(SRC))
COUNT = 0
all: $(NAME)
$(NAME): $(OBJ)
@rm -f .build_start
@$(CC) $(CFLAGS) $(OBJ) -o $(NAME)
@printf "\n$(GREEN)✔ Listo (100%%)\n$(RESET)"
%.o: %.cpp
@if [ ! -f .build_start ]; then printf "$(GREEN)Compilando archivos...\n$(RESET)"; touch .build_start; fi
@$(eval COUNT = $(shell echo $$(($(COUNT)+1))))
@PERCENT=$$(($(COUNT)*100/$(TOTAL))); \
BAR=$$(printf "%0.s#" $$(seq 1 $$((PERCENT/5)))); \
SPACE=$$(printf "%0.s " $$(seq 1 $$((20-PERCENT/5)))); \
printf "\r [$$BAR$$SPACE] %3d%% (%d/%d) $< " $$PERCENT $(COUNT) $(TOTAL)
@$(CC) $(CFLAGS) -c $< -o $@
clean:
@printf "$(RED)Eliminando...\n$(RESET)"
@rm -f $(OBJ)
fclean: clean
@rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re

View File

@@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PresidentialPardonForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:12:39 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:12:40 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "PresidentialPardonForm.hpp"
//////////////////
//Constructores //
//////////////////
PresidentialPardonForm::PresidentialPardonForm()
: AForm("PresidentialPardonForm", 25, 5),
target("NULL")
{
//std::cout << "PresidentialPardonForm default constructor called" << std::endl;
}
PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm &other)
: AForm(other),
target(other.getTarget())
{
//std::cout << "PresidentialPardonForm constructor with params called" << std::endl;
}
PresidentialPardonForm& PresidentialPardonForm::operator=(const PresidentialPardonForm &other)
{
if (this != &other)
{
AForm::operator=(other);
this->target = other.getTarget();
}
//std::cout << "AForm copy assigment operator called" << std::endl;
return (*this);
}
PresidentialPardonForm::~PresidentialPardonForm()
{
//std::cout << "Destructor called"
}
PresidentialPardonForm::PresidentialPardonForm(std::string target)
: AForm("PresidentialPardonForm", 25, 5),
target(target)
{
//std::cout << "AForm constructor with params called" << std::endl;
}
//////////////////
// Getters //
//////////////////
std::string PresidentialPardonForm::getTarget() const
{
return (this->target);
}
//////////////////
// Execute //
//////////////////
void PresidentialPardonForm::execute(Bureaucrat const &executor) const
{
if (!this->getIsSigned())
throw FormNotSignedException();
if (this->getGradeToExecute() < executor.getGrade())
throw AForm::GradeTooLowException();
// PresidentialPardonForm form action...
std::cout << this->target
<< " has been pardoned by Zaphod Beeblebrox."
<< std::endl;
}
//////////////////
// Exceptions //
//////////////////
const char* PresidentialPardonForm::FormNotSignedException::what() const throw()
{
return ("form is not signed.");
}

View File

@@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PresidentialPardonForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:12:43 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:12:44 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PRESIDENTIALPARDONFORM_HPP
# define PRESIDENTIALPARDONFORM_HPP
# include <iostream>
# include "../AForm/AForm.hpp"
# include "../Bureaucrat/Bureaucrat.hpp"
class PresidentialPardonForm : public AForm
{
private:
std::string target;
public:
PresidentialPardonForm();
PresidentialPardonForm(const PresidentialPardonForm &other);
PresidentialPardonForm &operator=(const PresidentialPardonForm &other);
~PresidentialPardonForm();
PresidentialPardonForm(std::string target);
std::string getTarget() const;
virtual void execute(Bureaucrat const &executor) const;
class FormNotSignedException : public std::exception {
public:
virtual const char* what() const throw();
};
};
#endif

View File

@@ -0,0 +1,97 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* RobotomyRequestForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:12:55 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:12:55 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "RobotomyRequestForm.hpp"
//////////////////
//Constructores //
//////////////////
RobotomyRequestForm::RobotomyRequestForm()
: AForm("RobotomyRequestForm", 72, 45),
target("NULL")
{
//std::cout << "RobotomyRequestForm default constructor called" << std::endl;
}
RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm &other)
: AForm(other),
target(other.getTarget())
{
//std::cout << "RobotomyRequestForm constructor with params called" << std::endl;
}
RobotomyRequestForm& RobotomyRequestForm::operator=(const RobotomyRequestForm &other)
{
if (this != &other)
{
AForm::operator=(other);
this->target = other.getTarget();
}
//std::cout << "AForm copy assigment operator called" << std::endl;
return (*this);
}
RobotomyRequestForm::~RobotomyRequestForm()
{
//std::cout << "Destructor called"
}
RobotomyRequestForm::RobotomyRequestForm(std::string target)
: AForm("RobotomyRequestForm", 72, 45),
target(target)
{
//std::cout << "AForm constructor with params called" << std::endl;
}
//////////////////
// Getters //
//////////////////
std::string RobotomyRequestForm::getTarget() const
{
return (this->target);
}
//////////////////
// Execute //
//////////////////
void RobotomyRequestForm::execute(Bureaucrat const &executor) const
{
if (!this->getIsSigned())
throw FormNotSignedException();
if (this->getGradeToExecute() < executor.getGrade())
throw AForm::GradeTooLowException();
// RobotomyRequestForm form action...
std::cout << "**bzzzzzzzzz --- **drilling noises*" << std::endl;
if (rand() % 2 == 0)
{
std::cout << this->target
<< " has been successfully robotomized" << std::endl;
} else {
std::cout << "Robotomization failed :(" << std::endl;
}
}
//////////////////
// Exceptions //
//////////////////
const char* RobotomyRequestForm::FormNotSignedException::what() const throw()
{
return ("form is not signed.");
}

View File

@@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* RobotomyRequestForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:13:02 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:13:02 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ROBOTOMYREQUESTFORM_HPP
# define ROBOTOMYREQUESTFORM_HPP
# include <iostream>
# include <cstdlib>
# include <ctime>
# include "../AForm/AForm.hpp"
# include "../Bureaucrat/Bureaucrat.hpp"
class RobotomyRequestForm : public AForm
{
private:
std::string target;
public:
RobotomyRequestForm();
RobotomyRequestForm(const RobotomyRequestForm &other);
RobotomyRequestForm &operator=(const RobotomyRequestForm &other);
~RobotomyRequestForm();
RobotomyRequestForm(std::string target);
std::string getTarget() const;
virtual void execute(Bureaucrat const &executor) const;
class FormNotSignedException : public std::exception {
public:
virtual const char* what() const throw();
};
};
#endif

View File

@@ -0,0 +1,109 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ShrubberyCreationForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:13:06 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:13:07 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "ShrubberyCreationForm.hpp"
//////////////////
//Constructores //
//////////////////
ShrubberyCreationForm::ShrubberyCreationForm()
: AForm("ShrubberyCreationForm", 145, 137),
target("NULL")
{
//std::cout << "ShrubberyCreationForm default constructor called" << std::endl;
}
ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm &other)
: AForm(other),
target(other.getTarget())
{
//std::cout << "ShrubberyCreationForm constructor with params called" << std::endl;
}
ShrubberyCreationForm& ShrubberyCreationForm::operator=(const ShrubberyCreationForm &other)
{
if (this != &other)
{
AForm::operator=(other);
this->target = other.getTarget();
}
//std::cout << "AForm copy assigment operator called" << std::endl;
return (*this);
}
ShrubberyCreationForm::~ShrubberyCreationForm()
{
//std::cout << "Destructor called"
}
ShrubberyCreationForm::ShrubberyCreationForm(std::string target)
: AForm("ShrubberyCreationForm", 145, 137),
target(target)
{
//std::cout << "AForm constructor with params called" << std::endl;
}
//////////////////
// Getters //
//////////////////
std::string ShrubberyCreationForm::getTarget() const
{
return (this->target);
}
//////////////////
// Execute //
//////////////////
void ShrubberyCreationForm::execute(Bureaucrat const &executor) const
{
if (!this->getIsSigned())
throw FormNotSignedException();
if (this->getGradeToExecute() < executor.getGrade())
throw AForm::GradeTooLowException();
// ShrubberyCreation form action...
std::string filename = this->target + "_shrubbery";
std::ofstream file(filename.c_str());
if (!file.is_open())
{
std::cerr << "Error: could not create file "
<< filename << std::endl;
return ;
}
file << " _-_\n";
file << " /~~ ~~\\\n";
file << " /~~ ~~\\\n";
file << "{ }\n";
file << " \\ _- -_ /\n";
file << " ~ \\ // ~\n";
file << "_- - | | _- _\n";
file << " _ - | | -_\n";
file << " // \\";
file.close();
}
//////////////////
// Exceptions //
//////////////////
const char* ShrubberyCreationForm::FormNotSignedException::what() const throw()
{
return ("form is not signed.");
}

View File

@@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ShrubberyCreationForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:13:12 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:13:13 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SHRUBBERYCREATIONFORM_HPP
# define SHRUBBERYCREATIONFORM_HPP
# include <iostream>
# include <fstream>
# include <string>
# include "../AForm/AForm.hpp"
# include "../Bureaucrat/Bureaucrat.hpp"
class ShrubberyCreationForm : public AForm
{
private:
std::string target;
public:
ShrubberyCreationForm();
ShrubberyCreationForm(const ShrubberyCreationForm &other);
ShrubberyCreationForm &operator=(const ShrubberyCreationForm &other);
~ShrubberyCreationForm();
ShrubberyCreationForm(std::string target);
std::string getTarget() const;
virtual void execute(Bureaucrat const &executor) const;
class FormNotSignedException : public std::exception {
public:
virtual const char* what() const throw();
};
};
#endif

41
ex02/main.cpp Normal file
View File

@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:13:17 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:13:18 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat/Bureaucrat.hpp"
#include "AForm/AForm.hpp"
#include "ShrubberyCreationForm/ShrubberyCreationForm.hpp"
#include "RobotomyRequestForm/RobotomyRequestForm.hpp"
#include "PresidentialPardonForm/PresidentialPardonForm.hpp"
int main()
{
// Inicilizamos semilla de rand, necesario para
// RobotomyRequestForm
srand(time(NULL));
std::cout << "---- ex02 tests ----" << std::endl;
RobotomyRequestForm form("home");
try {
Bureaucrat angel("Angel", 1);
angel.executeForm(form);
angel.signForm(form);
angel.executeForm(form);
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
std::cout << std::endl;
return (0);
}

125
ex03/AForm/AForm.cpp Normal file
View File

@@ -0,0 +1,125 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* AForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:13:40 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:13:40 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "AForm.hpp"
#include "../Bureaucrat/Bureaucrat.hpp"
//////////////////
//Constructores //
//////////////////
AForm::AForm() : name("NULL"), isSigned(false), gradeToSign(1), gradeToExecute(1)
{
//std::cout << "AForm default constructor called" << std::endl;
}
AForm::AForm(std::string name, int gradeToSign, int gradeToExecute)
: name(name), isSigned(false),
gradeToSign(gradeToSign), gradeToExecute(gradeToExecute)
{
if (gradeToSign > 150 || gradeToExecute > 150)
throw GradeTooLowException();
else if (gradeToSign < 1 || gradeToExecute < 1)
throw GradeTooHighException();
//std::cout << "AForm constructor with params called" << std::endl;
}
AForm::AForm(const AForm &other) :
name (other.getName()), isSigned(other.getIsSigned()),
gradeToSign(other.getGradeToSign()),
gradeToExecute(other.getGradeToExecute())
{
//std::cout << "AForm copy constructor called" << std::endl;
}
AForm& AForm::operator=(const AForm &other)
{
if (this != &other)
this->isSigned = other.getIsSigned();
//std::cout << "AForm copy assigment operator called" << std::endl;
return (*this);
}
AForm::~AForm()
{
//std::cout << "Destructor called"
}
//////////////////
// Getters //
//////////////////
std::string AForm::getName() const
{
return (this->name);
}
bool AForm::getIsSigned() const
{
return (this->isSigned);
}
int AForm::getGradeToSign() const
{
return (this->gradeToSign);
}
int AForm::getGradeToExecute() const
{
return (this->gradeToExecute);
}
//////////////////
// SignAForm //
//////////////////
void AForm::beSigned(const Bureaucrat &br)
{
if (this->gradeToSign < br.getGrade())
throw GradeTooLowException();
this->isSigned = true;
}
//////////////////
// << //
//////////////////
std::ostream& operator<<(std::ostream &os, const AForm &form)
{
std::string isItSigned;
if (form.getIsSigned())
{
isItSigned = " is signed.";
} else {
isItSigned = " is not signed.";
}
os << form.getName() << isItSigned;
return (os);
}
//////////////////
// Excepciones //
//////////////////
const char* AForm::GradeTooLowException::what() const throw()
{
return ("grade too low");
}
const char* AForm::GradeTooHighException::what() const throw()
{
return ("grade too high");
}

60
ex03/AForm/AForm.hpp Normal file
View File

@@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* AForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:13:37 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:13:37 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef AForm_HPP
# define AForm_HPP
# include <iostream>
# include <string>
class Bureaucrat;
class AForm
{
private:
const std::string name;
bool isSigned;
const int gradeToSign;
const int gradeToExecute;
public:
AForm();
AForm(const AForm &other);
AForm& operator=(const AForm &other);
virtual ~AForm();
AForm(std::string name, int gradeToSign, int gradeToExecute);
std::string getName() const;
bool getIsSigned() const;
int getGradeToSign() const;
int getGradeToExecute() const;
void beSigned(const Bureaucrat &br);
virtual void execute(Bureaucrat const &executor) const = 0;
class GradeTooLowException : public std::exception {
public:
virtual const char *what() const throw();
};
class GradeTooHighException : public std::exception {
public:
virtual const char *what() const throw();
};
};
std::ostream& operator<<(std::ostream &os, const AForm &AForm);
#endif

View File

@@ -0,0 +1,154 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:13:44 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:13:44 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
#include "../AForm/AForm.hpp"
//////////////////
//Constructores //
//////////////////
Bureaucrat::Bureaucrat() : name("NULL"), grade(150)
{
//std::cout << "Bureaucrat has been created" << std::endl;
}
Bureaucrat::Bureaucrat(const Bureaucrat &other) : name(other.getName()), grade(other.getGrade())
{
//std::cout << "Copy assignment called" << std::endl;
}
Bureaucrat& Bureaucrat::operator=(const Bureaucrat &other)
{
if (this != &other)
this->grade = other.getGrade();
//std::cout << "Copy operator called" << std::endl;
return (*this);
}
Bureaucrat::~Bureaucrat()
{
//std::cout << "Bureaucrat destroyed" << std::endl;
}
Bureaucrat::Bureaucrat(std::string name, int grade) : name(name)
{
if (grade > 150)
throw GradeTooLowException();
else if (grade < 1)
throw GradeTooHighException();
this->grade = grade;
//std::cout << "Bureaucrat with params has been created" << std::endl;
}
//////////////////
// Getters //
//////////////////
std::string Bureaucrat::getName() const
{
return (this->name);
}
int Bureaucrat::getGrade() const
{
return (this->grade);
}
//////////////////
// Grade //
//////////////////
void Bureaucrat::incrementGrade()
{
if (this->grade <= 1)
throw GradeTooHighException();
this->grade--;
}
void Bureaucrat::decrementGrade()
{
if (this->grade >= 150)
throw GradeTooLowException();
this->grade++;
}
//////////////////
// Sign //
//////////////////
void Bureaucrat::signForm(AForm &form)
{
try {
form.beSigned(*this);
std::cout << this->name
<< " signed "
<< form.getName()
<< std::endl;
} catch (std::exception &e) {
std::cout << this->name
<< " couldn't sign "
<< form.getName()
<< " because "
<< e.what()
<< std::endl;
}
}
void Bureaucrat::executeForm(AForm &form)
{
try {
form.execute(*this);
std::cout << this->name
<< " executed "
<< form.getName()
<< std::endl;
} catch (std::exception &e) {
std::cout << this->name
<< " couldn't execute "
<< form.getName()
<< " because "
<< e.what()
<< std::endl;
}
}
//////////////////
// << //
//////////////////
std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
{
os << bureaucrat.getName()
<< ", bureaucrat grade "
<< bureaucrat.getGrade()
<< std::endl;
return (os);
}
//////////////////
// Excepciones //
//////////////////
const char* Bureaucrat::GradeTooHighException::what() const throw()
{
return ("Grade is too high!");
}
const char* Bureaucrat::GradeTooLowException::what() const throw()
{
return ("Grade is too low!");
}

View File

@@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:13:47 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:13:48 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BUREAUCRAT_HPP
# define BUREAUCRAT_HPP
# include <iostream>
class AForm;
class Bureaucrat
{
private:
const std::string name;
int grade;
public:
Bureaucrat();
Bureaucrat(const Bureaucrat &other);
Bureaucrat &operator=(const Bureaucrat &other);
~Bureaucrat();
Bureaucrat(std::string name, int grade);
std::string getName() const;
int getGrade() const;
void incrementGrade();
void decrementGrade();
void signForm(AForm &form);
void executeForm(AForm &form);
class GradeTooHighException : public std::exception {
public:
virtual const char* what() const throw();
};
class GradeTooLowException : public std::exception {
public:
virtual const char* what() const throw();
};
};
std::ostream& operator<<(std::ostream &os, const Bureaucrat &bureaucrat);
#endif

91
ex03/Intern/Intern.cpp Normal file
View File

@@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Intern.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:14:13 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:45:04 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Intern.hpp"
//////////////////
// Constructors //
//////////////////
Intern::Intern()
{
// std::cout << "Intern default constructor called" << std::endl;
}
Intern::Intern(const Intern &other)
{
(void)other; // No atributes for copying
// std::cout << "Intern copy constructor called" << std::endl;
}
Intern& Intern::operator=(const Intern &other)
{
(void)other; // No atributes for copying
// std::cout << "Intern copy assignment operator called" << std::endl;
return (*this);
}
Intern::~Intern()
{
// std::cout << "Intern destructor called" << std::endl;
}
//////////////////
// Creators //
//////////////////
AForm* Intern::createShrubbery(const std::string &target)
{
return new ShrubberyCreationForm(target);
}
AForm* Intern::createRobotomy(const std::string &target)
{
return new RobotomyRequestForm(target);
}
AForm* Intern::createPardon(const std::string &target)
{
return new PresidentialPardonForm(target);
}
AForm* Intern::makeForm(const std::string &formName, const std::string &target)
{
std::string formNames[3] = {
"shrubbery creation",
"robotomy request",
"presidential pardon"
};
typedef AForm* (Intern::*FormCreator)(const std::string&);
FormCreator creators[3] = {
&Intern::createShrubbery,
&Intern::createRobotomy,
&Intern::createPardon
};
for (int i = 0; i < 3; i++)
{
if (formName == formNames[i])
{
std::cout << "Intern creates "
<< formName
<< std::endl;
return (this->*creators[i])(target);
}
}
std::cout << "Intern cannot create \"" << formName
<< "\" because it doesn't exist"
<< std::endl;
return (NULL);
}

40
ex03/Intern/Intern.hpp Normal file
View File

@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Intern.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:14:18 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:43:00 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef INTERN_HPP
# define INTERN_HPP
# include <iostream>
# include <string>
# include "../AForm/AForm.hpp"
# include "../ShrubberyCreationForm/ShrubberyCreationForm.hpp"
# include "../RobotomyRequestForm/RobotomyRequestForm.hpp"
# include "../PresidentialPardonForm/PresidentialPardonForm.hpp"
class Intern
{
private:
AForm* createShrubbery(const std::string &target);
AForm* createRobotomy(const std::string &target);
AForm* createPardon(const std::string &target);
public:
Intern();
Intern(const Intern &other);
Intern& operator=(const Intern &other);
~Intern();
AForm* makeForm(const std::string &formName, const std::string &target);
};
#endif

47
ex03/Makefile Normal file
View File

@@ -0,0 +1,47 @@
NAME = intern
SRC = main.cpp \
Bureaucrat/Bureaucrat.cpp AForm/AForm.cpp \
ShrubberyCreationForm/ShrubberyCreationForm.cpp \
RobotomyRequestForm/RobotomyRequestForm.cpp \
PresidentialPardonForm/PresidentialPardonForm.cpp \
Intern/Intern.cpp
OBJ = $(SRC:.cpp=.o)
CC = c++
CFLAGS = -Wall -Wextra -Werror -std=c++98
GREEN = \033[0;32m
RED = \033[0;31m
RESET = \033[0m
TOTAL := $(words $(SRC))
COUNT = 0
all: $(NAME)
$(NAME): $(OBJ)
@rm -f .build_start
@$(CC) $(CFLAGS) $(OBJ) -o $(NAME)
@printf "\n$(GREEN)✔ Listo (100%%)\n$(RESET)"
%.o: %.cpp
@if [ ! -f .build_start ]; then printf "$(GREEN)Compilando archivos...\n$(RESET)"; touch .build_start; fi
@$(eval COUNT = $(shell echo $$(($(COUNT)+1))))
@PERCENT=$$(($(COUNT)*100/$(TOTAL))); \
BAR=$$(printf "%0.s#" $$(seq 1 $$((PERCENT/5)))); \
SPACE=$$(printf "%0.s " $$(seq 1 $$((20-PERCENT/5)))); \
printf "\r [$$BAR$$SPACE] %3d%% (%d/%d) $< " $$PERCENT $(COUNT) $(TOTAL)
@$(CC) $(CFLAGS) -c $< -o $@
clean:
@printf "$(RED)Eliminando...\n$(RESET)"
@rm -f $(OBJ)
fclean: clean
@rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re

View File

@@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PresidentialPardonForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:14:23 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:14:23 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "PresidentialPardonForm.hpp"
//////////////////
//Constructores //
//////////////////
PresidentialPardonForm::PresidentialPardonForm()
: AForm("PresidentialPardonForm", 25, 5),
target("NULL")
{
//std::cout << "PresidentialPardonForm default constructor called" << std::endl;
}
PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm &other)
: AForm(other),
target(other.getTarget())
{
//std::cout << "PresidentialPardonForm constructor with params called" << std::endl;
}
PresidentialPardonForm& PresidentialPardonForm::operator=(const PresidentialPardonForm &other)
{
if (this != &other)
{
AForm::operator=(other);
this->target = other.getTarget();
}
//std::cout << "AForm copy assigment operator called" << std::endl;
return (*this);
}
PresidentialPardonForm::~PresidentialPardonForm()
{
//std::cout << "Destructor called"
}
PresidentialPardonForm::PresidentialPardonForm(std::string target)
: AForm("PresidentialPardonForm", 25, 5),
target(target)
{
//std::cout << "AForm constructor with params called" << std::endl;
}
//////////////////
// Getters //
//////////////////
std::string PresidentialPardonForm::getTarget() const
{
return (this->target);
}
//////////////////
// Execute //
//////////////////
void PresidentialPardonForm::execute(Bureaucrat const &executor) const
{
if (!this->getIsSigned())
throw FormNotSignedException();
if (this->getGradeToExecute() < executor.getGrade())
throw AForm::GradeTooLowException();
// PresidentialPardonForm form action...
std::cout << this->target
<< " has been pardoned by Zaphod Beeblebrox."
<< std::endl;
}
//////////////////
// Exceptions //
//////////////////
const char* PresidentialPardonForm::FormNotSignedException::what() const throw()
{
return ("form is not signed.");
}

View File

@@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PresidentialPardonForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:14:27 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:14:27 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PRESIDENTIALPARDONFORM_HPP
# define PRESIDENTIALPARDONFORM_HPP
# include <iostream>
# include "../AForm/AForm.hpp"
# include "../Bureaucrat/Bureaucrat.hpp"
class PresidentialPardonForm : public AForm
{
private:
std::string target;
public:
PresidentialPardonForm();
PresidentialPardonForm(const PresidentialPardonForm &other);
PresidentialPardonForm &operator=(const PresidentialPardonForm &other);
~PresidentialPardonForm();
PresidentialPardonForm(std::string target);
std::string getTarget() const;
virtual void execute(Bureaucrat const &executor) const;
class FormNotSignedException : public std::exception {
public:
virtual const char* what() const throw();
};
};
#endif

View File

@@ -0,0 +1,97 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* RobotomyRequestForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:14:32 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:14:32 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "RobotomyRequestForm.hpp"
//////////////////
//Constructores //
//////////////////
RobotomyRequestForm::RobotomyRequestForm()
: AForm("RobotomyRequestForm", 72, 45),
target("NULL")
{
//std::cout << "RobotomyRequestForm default constructor called" << std::endl;
}
RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm &other)
: AForm(other),
target(other.getTarget())
{
//std::cout << "RobotomyRequestForm constructor with params called" << std::endl;
}
RobotomyRequestForm& RobotomyRequestForm::operator=(const RobotomyRequestForm &other)
{
if (this != &other)
{
AForm::operator=(other);
this->target = other.getTarget();
}
//std::cout << "AForm copy assigment operator called" << std::endl;
return (*this);
}
RobotomyRequestForm::~RobotomyRequestForm()
{
//std::cout << "Destructor called"
}
RobotomyRequestForm::RobotomyRequestForm(std::string target)
: AForm("RobotomyRequestForm", 72, 45),
target(target)
{
//std::cout << "AForm constructor with params called" << std::endl;
}
//////////////////
// Getters //
//////////////////
std::string RobotomyRequestForm::getTarget() const
{
return (this->target);
}
//////////////////
// Execute //
//////////////////
void RobotomyRequestForm::execute(Bureaucrat const &executor) const
{
if (!this->getIsSigned())
throw FormNotSignedException();
if (this->getGradeToExecute() < executor.getGrade())
throw AForm::GradeTooLowException();
// RobotomyRequestForm form action...
std::cout << "**bzzzzzzzzz --- **drilling noises*" << std::endl;
if (rand() % 2 == 0)
{
std::cout << this->target
<< " has been successfully robotomized" << std::endl;
} else {
std::cout << "Robotomization failed :(" << std::endl;
}
}
//////////////////
// Exceptions //
//////////////////
const char* RobotomyRequestForm::FormNotSignedException::what() const throw()
{
return ("form is not signed.");
}

View File

@@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* RobotomyRequestForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:14:35 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:14:35 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ROBOTOMYREQUESTFORM_HPP
# define ROBOTOMYREQUESTFORM_HPP
# include <iostream>
# include <cstdlib>
# include <ctime>
# include "../AForm/AForm.hpp"
# include "../Bureaucrat/Bureaucrat.hpp"
class RobotomyRequestForm : public AForm
{
private:
std::string target;
public:
RobotomyRequestForm();
RobotomyRequestForm(const RobotomyRequestForm &other);
RobotomyRequestForm &operator=(const RobotomyRequestForm &other);
~RobotomyRequestForm();
RobotomyRequestForm(std::string target);
std::string getTarget() const;
virtual void execute(Bureaucrat const &executor) const;
class FormNotSignedException : public std::exception {
public:
virtual const char* what() const throw();
};
};
#endif

View File

@@ -0,0 +1,109 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ShrubberyCreationForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:14:43 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:14:43 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "ShrubberyCreationForm.hpp"
//////////////////
//Constructores //
//////////////////
ShrubberyCreationForm::ShrubberyCreationForm()
: AForm("ShrubberyCreationForm", 145, 137),
target("NULL")
{
//std::cout << "ShrubberyCreationForm default constructor called" << std::endl;
}
ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm &other)
: AForm(other),
target(other.getTarget())
{
//std::cout << "ShrubberyCreationForm constructor with params called" << std::endl;
}
ShrubberyCreationForm& ShrubberyCreationForm::operator=(const ShrubberyCreationForm &other)
{
if (this != &other)
{
AForm::operator=(other);
this->target = other.getTarget();
}
//std::cout << "AForm copy assigment operator called" << std::endl;
return (*this);
}
ShrubberyCreationForm::~ShrubberyCreationForm()
{
//std::cout << "Destructor called"
}
ShrubberyCreationForm::ShrubberyCreationForm(std::string target)
: AForm("ShrubberyCreationForm", 145, 137),
target(target)
{
//std::cout << "AForm constructor with params called" << std::endl;
}
//////////////////
// Getters //
//////////////////
std::string ShrubberyCreationForm::getTarget() const
{
return (this->target);
}
//////////////////
// Execute //
//////////////////
void ShrubberyCreationForm::execute(Bureaucrat const &executor) const
{
if (!this->getIsSigned())
throw FormNotSignedException();
if (this->getGradeToExecute() < executor.getGrade())
throw AForm::GradeTooLowException();
// ShrubberyCreation form action...
std::string filename = this->target + "_shrubbery";
std::ofstream file(filename.c_str());
if (!file.is_open())
{
std::cerr << "Error: could not create file "
<< filename << std::endl;
return ;
}
file << " _-_\n";
file << " /~~ ~~\\\n";
file << " /~~ ~~\\\n";
file << "{ }\n";
file << " \\ _- -_ /\n";
file << " ~ \\ // ~\n";
file << "_- - | | _- _\n";
file << " _ - | | -_\n";
file << " // \\";
file.close();
}
//////////////////
// Exceptions //
//////////////////
const char* ShrubberyCreationForm::FormNotSignedException::what() const throw()
{
return ("form is not signed.");
}

View File

@@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ShrubberyCreationForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:14:48 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:14:49 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SHRUBBERYCREATIONFORM_HPP
# define SHRUBBERYCREATIONFORM_HPP
# include <iostream>
# include <fstream>
# include <string>
# include "../AForm/AForm.hpp"
# include "../Bureaucrat/Bureaucrat.hpp"
class ShrubberyCreationForm : public AForm
{
private:
std::string target;
public:
ShrubberyCreationForm();
ShrubberyCreationForm(const ShrubberyCreationForm &other);
ShrubberyCreationForm &operator=(const ShrubberyCreationForm &other);
~ShrubberyCreationForm();
ShrubberyCreationForm(std::string target);
std::string getTarget() const;
virtual void execute(Bureaucrat const &executor) const;
class FormNotSignedException : public std::exception {
public:
virtual const char* what() const throw();
};
};
#endif

45
ex03/main.cpp Normal file
View File

@@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/21 12:14:52 by aortigos #+# #+# */
/* Updated: 2026/02/21 12:46:24 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat/Bureaucrat.hpp"
#include "AForm/AForm.hpp"
#include "Intern/Intern.hpp"
#include "ShrubberyCreationForm/ShrubberyCreationForm.hpp"
#include "RobotomyRequestForm/RobotomyRequestForm.hpp"
#include "PresidentialPardonForm/PresidentialPardonForm.hpp"
int main()
{
// Inicilizamos semilla de rand, necesario para
// RobotomyRequestForm
srand(time(NULL));
std::cout << "---- ex03 tests ----" << std::endl;
Bureaucrat angel("Angel", 1);
Intern intern;
try {
AForm* form = intern.makeForm("presidential pardon", "home");
angel.executeForm(*form);
angel.signForm(*form);
angel.executeForm(*form);
delete form;
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
std::cout << std::endl;
return (0);
}