diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b1daee0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +ex0*/obj/*.o +ex0*/ex0* diff --git a/ex00/src/Bureaucrat.cpp b/ex00/src/Bureaucrat.cpp index b04c29f..1d072e6 100644 --- a/ex00/src/Bureaucrat.cpp +++ b/ex00/src/Bureaucrat.cpp @@ -8,7 +8,8 @@ Bureaucrat::Bureaucrat() std::cout << "Bureaucrat()" << std::endl; } -Bureaucrat::Bureaucrat(const Bureaucrat& src) +Bureaucrat::Bureaucrat(const Bureaucrat& src): + _name(src._name) { *this = src; std::cout << "Bureaucrat(Bureaucrat)" << std::endl; diff --git a/ex01/src/Bureaucrat.cpp b/ex01/src/Bureaucrat.cpp index f2a0e98..c725a59 100644 --- a/ex01/src/Bureaucrat.cpp +++ b/ex01/src/Bureaucrat.cpp @@ -8,7 +8,8 @@ Bureaucrat::Bureaucrat() std::cout << "Bureaucrat()" << std::endl; } -Bureaucrat::Bureaucrat(const Bureaucrat& src) +Bureaucrat::Bureaucrat(const Bureaucrat& src): + _name(src._name) { *this = src; std::cout << "Bureaucrat(Bureaucrat)" << std::endl; diff --git a/ex01/src/Form.cpp b/ex01/src/Form.cpp index 9449555..d414707 100644 --- a/ex01/src/Form.cpp +++ b/ex01/src/Form.cpp @@ -9,7 +9,8 @@ Form::Form() std::cout << "Form()" << std::endl; } -Form::Form(const Form& src) +Form::Form(const Form& src): + _name(src._name) { std::cout << "Form(Form)" << std::endl; *this = src; diff --git a/ex02/src/AForm.cpp b/ex02/src/AForm.cpp index 9449555..20f6c40 100644 --- a/ex02/src/AForm.cpp +++ b/ex02/src/AForm.cpp @@ -1,39 +1,42 @@ -#include "Form.hpp" +#include "AForm.hpp" #include #include #include -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; } -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), _signed(0), _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) throw GradeTooLowException(); if (to_execute < 1 || to_sign < 1) 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->_to_sign = src._to_sign; @@ -42,11 +45,11 @@ Form& Form::operator=(const Form& src) return *this; } -void Form::beSigned(const Bureaucrat &bureaucrat) +void AForm::beSigned(const Bureaucrat &bureaucrat) { 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; } 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; } -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; } -bool Form::getSigned() const +bool AForm::getSigned() const { return this->_signed; } -int Form::getGradeToBeExecute() const +int AForm::getGradeToBeExecute() const { return this->_to_execute; } -int Form::getGradeToBeSign() const +int AForm::getGradeToBeSign() const { 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; } -const char* Form::GradeTooLowException::what() const throw() +const char* AForm::GradeTooLowException::what() const throw() { 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"; } diff --git a/ex02/src/AForm.hpp b/ex02/src/AForm.hpp index b7098f6..f6b87d0 100644 --- a/ex02/src/AForm.hpp +++ b/ex02/src/AForm.hpp @@ -14,10 +14,13 @@ class AForm bool _signed; int _to_sign; int _to_execute; + + protected: + const std::string _target; public: 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); virtual ~AForm(); @@ -29,6 +32,7 @@ class AForm int getGradeToBeSign() const; void beSigned(const Bureaucrat& bureaucrat); + virtual void execute(const Bureaucrat& exec) const = 0; class GradeTooHighException: public std::exception { @@ -42,6 +46,12 @@ class AForm 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); diff --git a/ex02/src/Bureaucrat.cpp b/ex02/src/Bureaucrat.cpp index f2a0e98..c26042e 100644 --- a/ex02/src/Bureaucrat.cpp +++ b/ex02/src/Bureaucrat.cpp @@ -66,11 +66,29 @@ void Bureaucrat::decrement() this->_grade++; } -void Bureaucrat::signForm(Form& form) const +void Bureaucrat::signForm(AForm& form) const { 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() { return "Too low grade"; diff --git a/ex02/src/Bureaucrat.hpp b/ex02/src/Bureaucrat.hpp index 2b4d8b4..a32bb06 100644 --- a/ex02/src/Bureaucrat.hpp +++ b/ex02/src/Bureaucrat.hpp @@ -27,6 +27,7 @@ class Bureaucrat void increment(); void decrement(); void signForm(AForm& form) const; + void executeForm(AForm const & form) const; class GradeTooHighException: public std::exception { diff --git a/ex02/src/PresidentialPardonForm.cpp b/ex02/src/PresidentialPardonForm.cpp new file mode 100644 index 0000000..706c018 --- /dev/null +++ b/ex02/src/PresidentialPardonForm.cpp @@ -0,0 +1,42 @@ +#include "PresidentialPardonForm.hpp" +#include "AForm.hpp" + +#include +#include + +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); +} diff --git a/ex02/src/PresidentialPardonForm.hpp b/ex02/src/PresidentialPardonForm.hpp new file mode 100644 index 0000000..87c78c9 --- /dev/null +++ b/ex02/src/PresidentialPardonForm.hpp @@ -0,0 +1,15 @@ +#include "AForm.hpp" +#include "Bureaucrat.hpp" +#include + +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; +}; diff --git a/ex02/src/RobotomyRequestForm.cpp b/ex02/src/RobotomyRequestForm.cpp new file mode 100644 index 0000000..e55897f --- /dev/null +++ b/ex02/src/RobotomyRequestForm.cpp @@ -0,0 +1,42 @@ +#include "RobotomyRequestForm.hpp" +#include "AForm.hpp" + +#include +#include + +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); +} diff --git a/ex02/src/RobotomyRequestForm.hpp b/ex02/src/RobotomyRequestForm.hpp new file mode 100644 index 0000000..ea09d7e --- /dev/null +++ b/ex02/src/RobotomyRequestForm.hpp @@ -0,0 +1,15 @@ +#include "AForm.hpp" +#include "Bureaucrat.hpp" +#include + +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; +}; diff --git a/ex02/src/ShrubberyCreationForm.cpp b/ex02/src/ShrubberyCreationForm.cpp new file mode 100644 index 0000000..f17648f --- /dev/null +++ b/ex02/src/ShrubberyCreationForm.cpp @@ -0,0 +1,71 @@ +#include "ShrubberyCreationForm.hpp" +#include "AForm.hpp" + +#include +#include +#include + +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); +} diff --git a/ex02/src/ShrubberyCreationForm.hpp b/ex02/src/ShrubberyCreationForm.hpp new file mode 100644 index 0000000..ded5f64 --- /dev/null +++ b/ex02/src/ShrubberyCreationForm.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "AForm.hpp" +#include "Bureaucrat.hpp" +#include + +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; +};