Compare commits
2 Commits
838b97e98a
...
c233d14af7
| Author | SHA1 | Date | |
|---|---|---|---|
| c233d14af7 | |||
| 5d62dbd20d |
@@ -106,6 +106,24 @@ void Bureaucrat::signForm(AForm &form)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//////////////////
|
//////////////////
|
||||||
// << //
|
// << //
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ class Bureaucrat
|
|||||||
void decrementGrade();
|
void decrementGrade();
|
||||||
|
|
||||||
void signForm(AForm &form);
|
void signForm(AForm &form);
|
||||||
|
void executeForm(AForm &form);
|
||||||
|
|
||||||
class GradeTooHighException : public std::exception {
|
class GradeTooHighException : public std::exception {
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
NAME = form
|
NAME = form
|
||||||
|
|
||||||
SRC = main.cpp \
|
SRC = main.cpp \
|
||||||
Bureaucrat/Bureaucrat.cpp AForm/AForm.cpp
|
Bureaucrat/Bureaucrat.cpp AForm/AForm.cpp \
|
||||||
|
ShrubberyCreationForm/ShrubberyCreationForm.cpp
|
||||||
|
|
||||||
OBJ = $(SRC:.cpp=.o)
|
OBJ = $(SRC:.cpp=.o)
|
||||||
|
|
||||||
|
|||||||
100
ex02/ShrubberyCreationForm/ShrubberyCreationForm.cpp
Normal file
100
ex02/ShrubberyCreationForm/ShrubberyCreationForm.cpp
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ShrubberyCreationForm.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 "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)
|
||||||
|
{
|
||||||
|
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());
|
||||||
|
|
||||||
|
file << " _-_\n";
|
||||||
|
file << " /~~ ~~\\\n";
|
||||||
|
file << " /~~ ~~\\\n";
|
||||||
|
file << "{ }\n";
|
||||||
|
file << " \\ _- -_ /\n";
|
||||||
|
file << " ~ \\ // ~\n";
|
||||||
|
file << "_- - | | _- _\n";
|
||||||
|
file << " _ - | | -_\n";
|
||||||
|
file << " // \\";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//////////////////
|
||||||
|
// Exceptions //
|
||||||
|
//////////////////
|
||||||
|
|
||||||
|
const char* ShrubberyCreationForm::FormNotSignedException::what() const throw()
|
||||||
|
{
|
||||||
|
return ("form is not signed.");
|
||||||
|
}
|
||||||
45
ex02/ShrubberyCreationForm/ShrubberyCreationForm.hpp
Normal file
45
ex02/ShrubberyCreationForm/ShrubberyCreationForm.hpp
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ShrubberyCreationForm.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 SHRUBBERYCREATIONFORM_HPP
|
||||||
|
|
||||||
|
# define SHRUBBERYCREATIONFORM_HPP
|
||||||
|
|
||||||
|
# include <iostream>
|
||||||
|
# include <fstream>
|
||||||
|
# include "../AForm/AForm.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;
|
||||||
|
|
||||||
|
void execute(Bureaucrat const &executor);
|
||||||
|
|
||||||
|
class FormNotSignedException : public std::exception {
|
||||||
|
public:
|
||||||
|
virtual const char* what() const throw();
|
||||||
|
};
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -6,20 +6,24 @@
|
|||||||
/* 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 16:24:37 by hadi ### ########.fr */
|
/* Updated: 2026/02/17 17:53:10 by hadi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "Bureaucrat/Bureaucrat.hpp"
|
#include "Bureaucrat/Bureaucrat.hpp"
|
||||||
#include "AForm/AForm.hpp"
|
#include "ShrubberyCreationForm/ShrubberyCreationForm.hpp"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::cout << "---- ex01 tests ----" << std::endl;
|
std::cout << "---- ex01 tests ----" << std::endl;
|
||||||
|
ShrubberyCreationForm form("home");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Bureaucrat angel("Angel", 140);
|
Bureaucrat angel("Angel", 137);
|
||||||
|
|
||||||
|
angel.executeForm(form);
|
||||||
|
angel.signForm(form);
|
||||||
|
angel.executeForm(form);
|
||||||
} catch (std::exception &e) {
|
} catch (std::exception &e) {
|
||||||
std::cout << e.what() << std::endl;
|
std::cout << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user