From 36a2871a3d916e0a8265f09aa7a21240062c896f Mon Sep 17 00:00:00 2001 From: starnakin Date: Tue, 12 Sep 2023 17:25:39 +0200 Subject: [PATCH] finish ex02 --- ex02/src/AForm.cpp | 4 +- ex02/src/Bureaucrat.cpp | 9 +- ex02/src/PresidentialPardonForm.cpp | 19 +-- ex02/src/RobotomyRequestForm.cpp | 11 +- ex02/src/ShrubberyCreationForm.cpp | 6 +- ex02/src/main.cpp | 253 ++++++++++++++++++++++------ 6 files changed, 227 insertions(+), 75 deletions(-) diff --git a/ex02/src/AForm.cpp b/ex02/src/AForm.cpp index 20f6c40..a0dbf6d 100644 --- a/ex02/src/AForm.cpp +++ b/ex02/src/AForm.cpp @@ -64,8 +64,6 @@ void AForm::execute(const Bureaucrat &exec) const 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 @@ -107,5 +105,5 @@ const char* AForm::GradeTooHighException::what() const throw() const char* AForm::NotSignedException::what() const throw() { - return "grade is to high"; + return "form not signed"; } diff --git a/ex02/src/Bureaucrat.cpp b/ex02/src/Bureaucrat.cpp index c26042e..3ae419a 100644 --- a/ex02/src/Bureaucrat.cpp +++ b/ex02/src/Bureaucrat.cpp @@ -1,5 +1,6 @@ #include "Bureaucrat.hpp" #include +#include #include #include @@ -78,13 +79,9 @@ void Bureaucrat::executeForm(AForm const & form) const form.execute(*this); std::cout << this->_name << " executed " << form.getName() << std::endl; } - catch Form::GradeTooLowException + catch (std::exception& e) { - - } - catch - { - + std::cout << e.what() << std::endl; } } diff --git a/ex02/src/PresidentialPardonForm.cpp b/ex02/src/PresidentialPardonForm.cpp index 706c018..998bbc3 100644 --- a/ex02/src/PresidentialPardonForm.cpp +++ b/ex02/src/PresidentialPardonForm.cpp @@ -5,13 +5,13 @@ #include PresidentialPardonForm::PresidentialPardonForm(): - AForm("PresidentialPardonForm", 45, 72, "") + AForm("PresidentialPardonForm", 5, 25, "") { - + std::cout << "PresidentialPardonForm()" << std::endl; } PresidentialPardonForm::PresidentialPardonForm(const std::string& target): - AForm("PresidentialPardonForm", 45, 72, target) + AForm("PresidentialPardonForm", 5, 25, target) { std::cout << "PresidentialPardonForm(" << target << ")" << std::endl; } @@ -22,6 +22,11 @@ PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm& src std::cout << "PresidentialPardonForm(PresidentialPardonForm)" << std::endl; } +PresidentialPardonForm::~PresidentialPardonForm() +{ + std::cout << "PresidentialPardonForm()" << std::endl; +} + PresidentialPardonForm& PresidentialPardonForm::operator=(const PresidentialPardonForm& src) { if (this != & src) @@ -31,12 +36,6 @@ PresidentialPardonForm& PresidentialPardonForm::operator=(const PresidentialPard 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); + std::cout << this->_target << " has been pardoned by Zaphod Beeblebrox" << std::endl; } diff --git a/ex02/src/RobotomyRequestForm.cpp b/ex02/src/RobotomyRequestForm.cpp index e55897f..8b382e0 100644 --- a/ex02/src/RobotomyRequestForm.cpp +++ b/ex02/src/RobotomyRequestForm.cpp @@ -7,7 +7,7 @@ RobotomyRequestForm::RobotomyRequestForm(): AForm("RobotomyRequestForm", 45, 72, "") { - + std::cout << "RobotomyRequestForm()" << std::endl; } RobotomyRequestForm::RobotomyRequestForm(const std::string& target): @@ -22,6 +22,11 @@ RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm& src): std::cout << "RobotomyRequestForm(RobotomyRequestForm)" << std::endl; } +RobotomyRequestForm::~RobotomyRequestForm() +{ + std::cout << "~RobotomyRequestForm()" << std::endl; +} + RobotomyRequestForm& RobotomyRequestForm::operator=(const RobotomyRequestForm& src) { if (this != & src) @@ -32,11 +37,13 @@ RobotomyRequestForm& RobotomyRequestForm::operator=(const RobotomyRequestForm& s void RobotomyRequestForm::execute(const Bureaucrat &exec) const { static bool ok(false); + + AForm::execute(exec); + 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/ShrubberyCreationForm.cpp b/ex02/src/ShrubberyCreationForm.cpp index f17648f..3001a31 100644 --- a/ex02/src/ShrubberyCreationForm.cpp +++ b/ex02/src/ShrubberyCreationForm.cpp @@ -12,9 +12,9 @@ ShrubberyCreationForm::ShrubberyCreationForm(): } ShrubberyCreationForm::ShrubberyCreationForm(const std::string& target): - AForm("", 137, 145, target) + AForm("ShrubberyCreationForm", 137, 145, target) { - std::cout << "ShrubberyCreationForm(" << ", " << target << ")" << std::endl; + std::cout << "ShrubberyCreationForm(" << target << ")" << std::endl; } ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm& src): @@ -57,6 +57,7 @@ void ShrubberyCreationForm::execute(const Bureaucrat &exec) const " _ - | | -_\n" " // \\\\\n" ; + AForm::execute(exec); std::ofstream file((this->_target + "_shruberry").c_str()); @@ -67,5 +68,4 @@ void ShrubberyCreationForm::execute(const Bureaucrat &exec) const } file << asciiTrees; - AForm::execute(exec); } diff --git a/ex02/src/main.cpp b/ex02/src/main.cpp index b7ec015..500a14c 100644 --- a/ex02/src/main.cpp +++ b/ex02/src/main.cpp @@ -1,4 +1,7 @@ #include "Bureaucrat.hpp" +#include "RobotomyRequestForm.hpp" +#include "ShrubberyCreationForm.hpp" +#include "PresidentialPardonForm.hpp" #include int main() { @@ -95,68 +98,28 @@ int main() { std::cout << "------------- Form TEST-----------" << std::endl; - std::cout << "Constructor too high sign grade" << std::endl; - { - try { - Form("form", 42, 0); - } catch (std::exception &e) { - std::cout << e.what() << std::endl; - } - } - std::cout << std::endl; - - std::cout << "Constructor too low sign grade" << std::endl; - { - try { - Form("form", 42, 151); - } catch (std::exception &e) { - std::cout << e.what() << std::endl; - } - } - std::cout << std::endl; - - std::cout << "Constructor too high exec grade" << std::endl; - { - try { - Form("form", 0, 42); - } catch (std::exception &e) { - std::cout << e.what() << std::endl; - } - } - std::cout << std::endl; - - std::cout << "Constructor too low exec grade" << std::endl; - { - try { - Form("form", 151, 42); - } catch (std::exception &e) { - std::cout << e.what() << std::endl; - } - } - std::cout << std::endl; - std::cout << "print" << std::endl; { - Form form("form", 42, 42); + RobotomyRequestForm form("moi"); std::cout << form; } std::cout << std::endl; - std::cout << "------------- Form Sign TEST-----------" << std::endl; + std::cout << "------------- RobotomyRequestForm Sign TEST-----------" << std::endl; - std::cout << "Bureaucrat sign a Form" << std::endl; + std::cout << "Bureaucrat sign a RobotomyRequestForm" << std::endl; { - Form form("form", 42, 42); + RobotomyRequestForm form("form"); Bureaucrat crat("crat", 42); crat.signForm(form); } std::cout << std::endl; - std::cout << "Too low Bureaucrat sign a Form" << std::endl; + std::cout << "Too low Bureaucrat sign a RobotomyRequestForm" << std::endl; { - Form form("form", 42, 1); - Bureaucrat crat("crat", 42); + RobotomyRequestForm form("form"); + Bureaucrat crat("crat", 150); try { crat.signForm(form); } catch (std::exception &e) { @@ -165,22 +128,210 @@ int main() { } std::cout << std::endl; - std::cout << "High Bureaucrat sign a low Form" << std::endl; + std::cout << "High Bureaucrat sign a low RobotomyRequestForm" << std::endl; { - Form form("form", 42, 150); + RobotomyRequestForm form("form"); Bureaucrat crat("crat", 1); crat.signForm(form); } std::cout << std::endl; - std::cout << "Form already sign" << std::endl; + std::cout << "RobotomyRequestForm already sign" << std::endl; { - Form form("form", 42, 150); + RobotomyRequestForm form("form"); Bureaucrat crat("crat", 1); crat.signForm(form); crat.signForm(form); } std::cout << std::endl; + + std::cout << "------------- RobotomyRequestForm TEST-----------" << std::endl; + + std::cout << "Bureaucrat sign a RobotomyRequestForm" << std::endl; + { + RobotomyRequestForm form("form"); + Bureaucrat crat("crat", 42); + + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "Too low Bureaucrat sign a RobotomyRequestForm" << std::endl; + { + RobotomyRequestForm form("form"); + Bureaucrat crat("crat", 150); + try { + crat.signForm(form); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "High Bureaucrat sign a low RobotomyRequestForm" << std::endl; + { + RobotomyRequestForm form("form"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "RobotomyRequestForm already sign" << std::endl; + { + RobotomyRequestForm form("form"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "RobotomyRequestForm execute not signed form" << std::endl; + { + RobotomyRequestForm form("form"); + Bureaucrat crat("crat", 1); + + crat.executeForm(form); + } + std::cout << std::endl; + std::cout << "RobotomyRequestForm execute" << std::endl; + { + RobotomyRequestForm form("bozo"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + + crat.executeForm(form); + crat.executeForm(form); + crat.executeForm(form); + } + std::cout << std::endl; + + std::cout << "------------- PresidentialPardonForm TEST-----------" << std::endl; + + std::cout << "Bureaucrat sign a PresidentialPardonForm" << std::endl; + { + PresidentialPardonForm form("form"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "Too low Bureaucrat sign a PresidentialPardonForm" << std::endl; + { + PresidentialPardonForm form("form"); + Bureaucrat crat("crat", 150); + try { + crat.signForm(form); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "High Bureaucrat sign a low PresidentialPardonForm" << std::endl; + { + PresidentialPardonForm form("form"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "PresidentialPardonForm already sign" << std::endl; + { + PresidentialPardonForm form("form"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "PresidentialPardonForm not signed execute" << std::endl; + { + PresidentialPardonForm form("form"); + Bureaucrat crat("crat", 1); + + crat.executeForm(form); + } + std::cout << std::endl; + + std::cout << "PresidentialPardonForm execute" << std::endl; + { + PresidentialPardonForm form("form"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + + crat.executeForm(form); + } + std::cout << std::endl; + + + std::cout << "------------- ShrubberyCreationForm TEST-----------" << std::endl; + + std::cout << "Bureaucrat sign a ShrubberyCreationForm" << std::endl; + { + ShrubberyCreationForm form("form"); + Bureaucrat crat("crat", 42); + + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "Too low Bureaucrat sign a ShrubberyCreationForm" << std::endl; + { + ShrubberyCreationForm form("form"); + Bureaucrat crat("crat", 150); + try { + crat.signForm(form); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "High Bureaucrat sign a low ShrubberyCreationForm" << std::endl; + { + ShrubberyCreationForm form("form"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "ShrubberyCreationForm already sign" << std::endl; + { + ShrubberyCreationForm form("form"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "ShrubberyCreationForm not sign execute" << std::endl; + { + ShrubberyCreationForm form("form"); + Bureaucrat crat("crat", 1); + + crat.executeForm(form); + } + std::cout << std::endl; + + std::cout << "ShrubberyCreationForm execute" << std::endl; + { + ShrubberyCreationForm form("bozo"); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + + crat.executeForm(form); + } + std::cout << std::endl; }