ex02 finished

This commit is contained in:
2026-02-18 00:16:54 +01:00
parent c233d14af7
commit f96fb516ff
10 changed files with 314 additions and 10 deletions

View File

@@ -11,6 +11,7 @@
/* ************************************************************************** */ /* ************************************************************************** */
#include "AForm.hpp" #include "AForm.hpp"
#include "../Bureaucrat/Bureaucrat.hpp"
////////////////// //////////////////
//Constructores // //Constructores //

View File

@@ -15,7 +15,9 @@
# define AForm_HPP # define AForm_HPP
# include <iostream> # include <iostream>
# include "../Bureaucrat/Bureaucrat.hpp" # include <string>
class Bureaucrat;
class AForm class AForm
{ {
@@ -29,7 +31,7 @@ class AForm
AForm(); AForm();
AForm(const AForm &other); AForm(const AForm &other);
AForm& operator=(const AForm &other); AForm& operator=(const AForm &other);
~AForm(); virtual ~AForm();
AForm(std::string name, int gradeToSign, int gradeToExecute); AForm(std::string name, int gradeToSign, int gradeToExecute);
@@ -40,7 +42,7 @@ class AForm
void beSigned(const Bureaucrat &br); void beSigned(const Bureaucrat &br);
virtual void execute(Bureaucrat const &executor) = 0; virtual void execute(Bureaucrat const &executor) const = 0;
class GradeTooLowException : public std::exception { class GradeTooLowException : public std::exception {
public: public:

View File

@@ -1,8 +1,10 @@
NAME = form NAME = formExecution
SRC = main.cpp \ SRC = main.cpp \
Bureaucrat/Bureaucrat.cpp AForm/AForm.cpp \ Bureaucrat/Bureaucrat.cpp AForm/AForm.cpp \
ShrubberyCreationForm/ShrubberyCreationForm.cpp ShrubberyCreationForm/ShrubberyCreationForm.cpp \
RobotomyRequestForm/RobotomyRequestForm.cpp \
PresidentialPardonForm/PresidentialPardonForm.cpp
OBJ = $(SRC:.cpp=.o) OBJ = $(SRC:.cpp=.o)

View File

@@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PresidentialPardonForm.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 */
/* */
/* ************************************************************************** */
#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/15 18:00:27 by aortigos #+# #+# */
/* Updated: 2026/02/06 02:28:09 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/15 18:00:27 by aortigos #+# #+# */
/* Updated: 2026/02/06 02:28:09 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/15 18:00:27 by aortigos #+# #+# */
/* Updated: 2026/02/06 02:28:09 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

@@ -67,7 +67,7 @@ std::string ShrubberyCreationForm::getTarget() const
// Execute // // Execute //
////////////////// //////////////////
void ShrubberyCreationForm::execute(Bureaucrat const &executor) void ShrubberyCreationForm::execute(Bureaucrat const &executor) const
{ {
if (!this->getIsSigned()) if (!this->getIsSigned())
throw FormNotSignedException(); throw FormNotSignedException();
@@ -75,8 +75,16 @@ void ShrubberyCreationForm::execute(Bureaucrat const &executor)
throw AForm::GradeTooLowException(); throw AForm::GradeTooLowException();
// ShrubberyCreation form action... // ShrubberyCreation form action...
std::string filename = this->target + "_shrubbery"; std::string filename = this->target + "_shrubbery";
std::ofstream file(filename.c_str()); 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";
@@ -88,6 +96,7 @@ void ShrubberyCreationForm::execute(Bureaucrat const &executor)
file << " _ - | | -_\n"; file << " _ - | | -_\n";
file << " // \\"; file << " // \\";
file.close();
} }
////////////////// //////////////////

View File

@@ -16,7 +16,9 @@
# include <iostream> # include <iostream>
# include <fstream> # include <fstream>
# include <string>
# include "../AForm/AForm.hpp" # include "../AForm/AForm.hpp"
# include "../Bureaucrat/Bureaucrat.hpp"
class ShrubberyCreationForm : public AForm class ShrubberyCreationForm : public AForm
{ {
@@ -33,7 +35,7 @@ class ShrubberyCreationForm : public AForm
std::string getTarget() const; std::string getTarget() const;
void execute(Bureaucrat const &executor); virtual void execute(Bureaucrat const &executor) const;
class FormNotSignedException : public std::exception { class FormNotSignedException : public std::exception {
public: public:

View File

@@ -6,20 +6,27 @@
/* By: hadi <hadi@student.42.fr> +#+ +:+ +#+ */ /* By: hadi <hadi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/16 22:32:41 by hadi #+# #+# */ /* Created: 2026/02/16 22:32:41 by hadi #+# #+# */
/* Updated: 2026/02/17 17:53:10 by hadi ### ########.fr */ /* Updated: 2026/02/18 00:15:46 by hadi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "Bureaucrat/Bureaucrat.hpp" #include "Bureaucrat/Bureaucrat.hpp"
#include "AForm/AForm.hpp"
#include "ShrubberyCreationForm/ShrubberyCreationForm.hpp" #include "ShrubberyCreationForm/ShrubberyCreationForm.hpp"
#include "RobotomyRequestForm/RobotomyRequestForm.hpp"
#include "PresidentialPardonForm/PresidentialPardonForm.hpp"
int main() int main()
{ {
// Inicilizamos semilla de rand, necesario para
// RobotomyRequestForm
srand(time(NULL));
std::cout << "---- ex01 tests ----" << std::endl; std::cout << "---- ex01 tests ----" << std::endl;
ShrubberyCreationForm form("home"); RobotomyRequestForm form("home");
try { try {
Bureaucrat angel("Angel", 137); Bureaucrat angel("Angel", 1);
angel.executeForm(form); angel.executeForm(form);
angel.signForm(form); angel.signForm(form);