add: ex02

This commit is contained in:
Camille Chauvet 2023-09-11 16:18:29 +00:00
parent 2fdad9f4de
commit 3990927c54
14 changed files with 281 additions and 26 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
ex0*/obj/*.o
ex0*/ex0*

View File

@ -8,7 +8,8 @@ Bureaucrat::Bureaucrat()
std::cout << "Bureaucrat()" << std::endl; std::cout << "Bureaucrat()" << std::endl;
} }
Bureaucrat::Bureaucrat(const Bureaucrat& src) Bureaucrat::Bureaucrat(const Bureaucrat& src):
_name(src._name)
{ {
*this = src; *this = src;
std::cout << "Bureaucrat(Bureaucrat)" << std::endl; std::cout << "Bureaucrat(Bureaucrat)" << std::endl;

View File

@ -8,7 +8,8 @@ Bureaucrat::Bureaucrat()
std::cout << "Bureaucrat()" << std::endl; std::cout << "Bureaucrat()" << std::endl;
} }
Bureaucrat::Bureaucrat(const Bureaucrat& src) Bureaucrat::Bureaucrat(const Bureaucrat& src):
_name(src._name)
{ {
*this = src; *this = src;
std::cout << "Bureaucrat(Bureaucrat)" << std::endl; std::cout << "Bureaucrat(Bureaucrat)" << std::endl;

View File

@ -9,7 +9,8 @@ Form::Form()
std::cout << "Form()" << std::endl; std::cout << "Form()" << std::endl;
} }
Form::Form(const Form& src) Form::Form(const Form& src):
_name(src._name)
{ {
std::cout << "Form(Form)" << std::endl; std::cout << "Form(Form)" << std::endl;
*this = src; *this = src;

View File

@ -1,39 +1,42 @@
#include "Form.hpp" #include "AForm.hpp"
#include <iostream> #include <iostream>
#include <ostream> #include <ostream>
#include <string> #include <string>
Form::Form() AForm::AForm()
{ {
std::cout << "Form()" << std::endl; std::cout << "AForm()" << std::endl;
} }
Form::Form(const Form& src) AForm::AForm(const AForm& src):
_name(src._name),
_target(src._target)
{ {
std::cout << "Form(Form)" << std::endl; std::cout << "AForm(AForm)" << std::endl;
*this = src; *this = src;
} }
Form::Form(const std::string& name, int to_execute, int to_sign): AForm::AForm(const std::string& name, int to_execute, int to_sign, const std::string& target):
_name(name), _name(name),
_signed(0), _signed(0),
_to_sign(to_sign), _to_sign(to_sign),
_to_execute(to_execute) _to_execute(to_execute),
_target(target)
{ {
std::cout << "Form(" << name << ", " << to_execute << ", " << to_sign << ")" << std::endl; std::cout << "AForm(" << name << ", " << to_execute << ", " << to_sign << ")" << std::endl;
if (to_execute > 150 || to_sign < 1) if (to_execute > 150 || to_sign < 1)
throw GradeTooLowException(); throw GradeTooLowException();
if (to_execute < 1 || to_sign < 1) if (to_execute < 1 || to_sign < 1)
throw GradeTooHighException(); throw GradeTooHighException();
} }
Form::~Form() AForm::~AForm()
{ {
std::cout << "~Form()" << std::endl; std::cout << "~AForm()" << std::endl;
} }
Form& Form::operator=(const Form& src) AForm& AForm::operator=(const AForm& src)
{ {
this->_signed = src._signed; this->_signed = src._signed;
this->_to_sign = src._to_sign; this->_to_sign = src._to_sign;
@ -42,11 +45,11 @@ Form& Form::operator=(const Form& src)
return *this; return *this;
} }
void Form::beSigned(const Bureaucrat &bureaucrat) void AForm::beSigned(const Bureaucrat &bureaucrat)
{ {
if (this->_signed == true) if (this->_signed == true)
{ {
std::cout << bureaucrat.getName() << " couldn't sign " << this->_name << " because " << "the form is already sign" << std::endl; std::cout << bureaucrat.getName() << " couldn't sign " << this->_name << " because " << "the AForm is already sign" << std::endl;
return; return;
} }
if (bureaucrat.getGrade() > this->_to_sign) if (bureaucrat.getGrade() > this->_to_sign)
@ -55,39 +58,54 @@ void Form::beSigned(const Bureaucrat &bureaucrat)
std::cout << bureaucrat.getName() << " signed " << this->_name << std::endl; std::cout << bureaucrat.getName() << " signed " << this->_name << std::endl;
} }
const std::string& Form::getName() const void AForm::execute(const Bureaucrat &exec) const
{
if (!this->_signed)
throw NotSignedException();
if (exec.getGrade() > this->_to_execute)
throw GradeTooLowException();
std::cout << exec.getName() << " executed " << this->_name << std::endl;
}
const std::string& AForm::getName() const
{ {
return this->_name; return this->_name;
} }
bool Form::getSigned() const bool AForm::getSigned() const
{ {
return this->_signed; return this->_signed;
} }
int Form::getGradeToBeExecute() const int AForm::getGradeToBeExecute() const
{ {
return this->_to_execute; return this->_to_execute;
} }
int Form::getGradeToBeSign() const int AForm::getGradeToBeSign() const
{ {
return this->_to_sign; return this->_to_sign;
} }
std::ostream& operator<<(std::ostream& os, const Form& src) std::ostream& operator<<(std::ostream& os, const AForm& src)
{ {
os << src.getName() << " form signed: " << src.getSigned() << ", grade to be execute: " << src.getGradeToBeExecute() << ", grade to be sign: " << src.getGradeToBeSign() << "." << std::endl; os << src.getName() << " AForm signed: " << src.getSigned() << ", grade to be execute: " << src.getGradeToBeExecute() << ", grade to be sign: " << src.getGradeToBeSign() << "." << std::endl;
return os; return os;
} }
const char* Form::GradeTooLowException::what() const throw() const char* AForm::GradeTooLowException::what() const throw()
{ {
return "grade is to low"; return "grade is to low";
} }
const char* Form::GradeTooHighException::what() const throw() const char* AForm::GradeTooHighException::what() const throw()
{
return "grade is to high";
}
const char* AForm::NotSignedException::what() const throw()
{ {
return "grade is to high"; return "grade is to high";
} }

View File

@ -14,10 +14,13 @@ class AForm
bool _signed; bool _signed;
int _to_sign; int _to_sign;
int _to_execute; int _to_execute;
protected:
const std::string _target;
public: public:
AForm(); AForm();
AForm(const std::string& name, int to_execute, int to_sign); AForm(const std::string& name, int to_execute, int to_sign, const std::string& target);
AForm(const AForm& src); AForm(const AForm& src);
virtual ~AForm(); virtual ~AForm();
@ -29,6 +32,7 @@ class AForm
int getGradeToBeSign() const; int getGradeToBeSign() const;
void beSigned(const Bureaucrat& bureaucrat); void beSigned(const Bureaucrat& bureaucrat);
virtual void execute(const Bureaucrat& exec) const = 0;
class GradeTooHighException: public std::exception class GradeTooHighException: public std::exception
{ {
@ -42,6 +46,12 @@ class AForm
const char * what() const throw(); const char * what() const throw();
}; };
class NotSignedException: public std::exception
{
public:
const char * what() const throw();
};
}; };
std::ostream& operator<<(std::ostream& os, const AForm& src); std::ostream& operator<<(std::ostream& os, const AForm& src);

View File

@ -66,11 +66,29 @@ void Bureaucrat::decrement()
this->_grade++; this->_grade++;
} }
void Bureaucrat::signForm(Form& form) const void Bureaucrat::signForm(AForm& form) const
{ {
form.beSigned(*this); form.beSigned(*this);
} }
void Bureaucrat::executeForm(AForm const & form) const
{
try
{
form.execute(*this);
std::cout << this->_name << " executed " << form.getName() << std::endl;
}
catch Form::GradeTooLowException
{
}
catch
{
}
}
const char* Bureaucrat::GradeTooLowException::what() const throw() const char* Bureaucrat::GradeTooLowException::what() const throw()
{ {
return "Too low grade"; return "Too low grade";

View File

@ -27,6 +27,7 @@ class Bureaucrat
void increment(); void increment();
void decrement(); void decrement();
void signForm(AForm& form) const; void signForm(AForm& form) const;
void executeForm(AForm const & form) const;
class GradeTooHighException: public std::exception class GradeTooHighException: public std::exception
{ {

View File

@ -0,0 +1,42 @@
#include "PresidentialPardonForm.hpp"
#include "AForm.hpp"
#include <string>
#include <iostream>
PresidentialPardonForm::PresidentialPardonForm():
AForm("PresidentialPardonForm", 45, 72, "")
{
}
PresidentialPardonForm::PresidentialPardonForm(const std::string& target):
AForm("PresidentialPardonForm", 45, 72, target)
{
std::cout << "PresidentialPardonForm(" << target << ")" << std::endl;
}
PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm& src):
AForm(src)
{
std::cout << "PresidentialPardonForm(PresidentialPardonForm)" << std::endl;
}
PresidentialPardonForm& PresidentialPardonForm::operator=(const PresidentialPardonForm& src)
{
if (this != & src)
*this = src;
return *this;
}
void PresidentialPardonForm::execute(const Bureaucrat &exec) const
{
static bool ok(false);
std::cout << "Bzit bzit" << std::endl;
if (ok)
std::cout << this->_target << " be robotomized" << std::endl;
else
std::cout << this->_target << " robotomized failed" << std::endl;
ok = !ok;
AForm::execute(exec);
}

View File

@ -0,0 +1,15 @@
#include "AForm.hpp"
#include "Bureaucrat.hpp"
#include <string>
class PresidentialPardonForm: public AForm
{
PresidentialPardonForm();
PresidentialPardonForm(const std::string& target);
PresidentialPardonForm(const PresidentialPardonForm& src);
~PresidentialPardonForm();
PresidentialPardonForm& operator=(const PresidentialPardonForm& src);
void execute(const Bureaucrat& exec) const;
};

View File

@ -0,0 +1,42 @@
#include "RobotomyRequestForm.hpp"
#include "AForm.hpp"
#include <string>
#include <iostream>
RobotomyRequestForm::RobotomyRequestForm():
AForm("RobotomyRequestForm", 45, 72, "")
{
}
RobotomyRequestForm::RobotomyRequestForm(const std::string& target):
AForm("RobotomyRequestForm", 45, 72, target)
{
std::cout << "RobotomyRequestForm(" << target << ")" << std::endl;
}
RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm& src):
AForm(src)
{
std::cout << "RobotomyRequestForm(RobotomyRequestForm)" << std::endl;
}
RobotomyRequestForm& RobotomyRequestForm::operator=(const RobotomyRequestForm& src)
{
if (this != & src)
*this = src;
return *this;
}
void RobotomyRequestForm::execute(const Bureaucrat &exec) const
{
static bool ok(false);
std::cout << "Bzit bzit" << std::endl;
if (ok)
std::cout << this->_target << " be robotomized" << std::endl;
else
std::cout << this->_target << " robotomized failed" << std::endl;
ok = !ok;
AForm::execute(exec);
}

View File

@ -0,0 +1,15 @@
#include "AForm.hpp"
#include "Bureaucrat.hpp"
#include <string>
class RobotomyRequestForm: public AForm
{
RobotomyRequestForm();
RobotomyRequestForm(const std::string& target);
RobotomyRequestForm(const RobotomyRequestForm& src);
~RobotomyRequestForm();
RobotomyRequestForm& operator=(const RobotomyRequestForm& src);
void execute(const Bureaucrat& exec) const;
};

View File

@ -0,0 +1,71 @@
#include "ShrubberyCreationForm.hpp"
#include "AForm.hpp"
#include <iostream>
#include <fstream>
#include <string>
ShrubberyCreationForm::ShrubberyCreationForm():
AForm("shrubbery creation", 137, 145, "")
{
std::cout << "ShrubberyCreationForm()" << std::endl;
}
ShrubberyCreationForm::ShrubberyCreationForm(const std::string& target):
AForm("", 137, 145, target)
{
std::cout << "ShrubberyCreationForm(" << ", " << target << ")" << std::endl;
}
ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm& src):
AForm(src)
{
std::cout << "ShrubberyCreationForm(ShrubberyCreationForm)" << std::endl;
}
ShrubberyCreationForm::~ShrubberyCreationForm()
{
std::cout << "~ShrubberyCreationForm()" << std::endl;
}
ShrubberyCreationForm& ShrubberyCreationForm::operator=(const ShrubberyCreationForm &src)
{
if (this != &src)
*this = src;
return *this;
}
void ShrubberyCreationForm::execute(const Bureaucrat &exec) const
{
static const std::string asciiTrees =
" _-_\n"
" /~~ ~~\\\n"
" /~~ ~~\\\n"
"{ }\n"
" \\ _- -_ /\n"
" ~ \\\\ // ~\n"
"_- - | | _- _\n"
" _ - | | -_\n"
" // \\\\\n"
" _-_\n"
" /~~ ~~\\\n"
" /~~ ~~\\\n"
"{ }\n"
" \\ _- -_ /\n"
" ~ \\\\ // ~\n"
"_- - | | _- _\n"
" _ - | | -_\n"
" // \\\\\n"
;
std::ofstream file((this->_target + "_shruberry").c_str());
if (!file.good())
{
std::cout << "error: " << this->_target << "_shruberry" << "file incorrect" << std::endl;
return;
}
file << asciiTrees;
AForm::execute(exec);
}

View File

@ -0,0 +1,18 @@
#pragma once
#include "AForm.hpp"
#include "Bureaucrat.hpp"
#include <string>
class ShrubberyCreationForm: public AForm
{
public:
ShrubberyCreationForm();
ShrubberyCreationForm(const ShrubberyCreationForm& src);
ShrubberyCreationForm(const std::string& target);
~ShrubberyCreationForm();
ShrubberyCreationForm& operator=(const ShrubberyCreationForm& src);
void execute(const Bureaucrat& exec) const;
};