From 2b5b3b29cb4be4e8761ebf667fddc52c2b588dd8 Mon Sep 17 00:00:00 2001 From: Raphael Date: Mon, 17 Mar 2025 22:09:20 +0100 Subject: [PATCH] feat: adding the great way to use beSigned --- cpp05/ex01/Bureaucrat.cpp | 15 ++++++--------- cpp05/ex01/Bureaucrat.hpp | 4 ++-- cpp05/ex01/Form.cpp | 14 ++++++++------ cpp05/ex01/Form.hpp | 14 +++++++++++++- cpp05/ex01/main.cpp | 16 +++++++++++++++- 5 files changed, 44 insertions(+), 19 deletions(-) diff --git a/cpp05/ex01/Bureaucrat.cpp b/cpp05/ex01/Bureaucrat.cpp index 76b59ad..e19a8ef 100644 --- a/cpp05/ex01/Bureaucrat.cpp +++ b/cpp05/ex01/Bureaucrat.cpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/16 14:49:52 by rparodi #+# #+# */ -/* Updated: 2025/03/17 16:59:03 by rparodi ### ########.fr */ +/* Updated: 2025/03/17 21:45:02 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -114,13 +114,10 @@ std::ostream& operator<<(std::ostream& output, Bureaucrat const &toPrint) { return (output); } -void Bureaucrat::signForm(Form const &form) { - if (form.isSigned()) { - std::cerr << this->getName() << " couldn’t sign " << form.getName() << " because form is already signed !" << std::endl; - } else { - if (form.getSign() < this->_grade) { - throw GradeTooLowException(); - return; - } +void Bureaucrat::signForm(Form &form) { + try { + form.beSigned(*this); + } catch (std::exception &e) { + std::cout << this->getName() << " cannot sign " << form.getName() << " because " << e.what() << std::endl; } } diff --git a/cpp05/ex01/Bureaucrat.hpp b/cpp05/ex01/Bureaucrat.hpp index 5e1b045..ac8832a 100644 --- a/cpp05/ex01/Bureaucrat.hpp +++ b/cpp05/ex01/Bureaucrat.hpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/16 14:48:33 by rparodi #+# #+# */ -/* Updated: 2025/03/17 17:06:04 by rparodi ### ########.fr */ +/* Updated: 2025/03/17 22:00:20 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,7 +33,7 @@ class Bureaucrat { int getGrade(void) const; void promote(void); void demote(void); - void signForm(Form const &form); + void signForm(Form &form); class GradeTooHighException : public std::exception { virtual const char *what() const throw() { return ("Error:\n> Grade is too high (have to be lower than 1)"); diff --git a/cpp05/ex01/Form.cpp b/cpp05/ex01/Form.cpp index c577fa6..5bca48d 100644 --- a/cpp05/ex01/Form.cpp +++ b/cpp05/ex01/Form.cpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/17 14:19:41 by rparodi #+# #+# */ -/* Updated: 2025/03/17 20:17:10 by rparodi ### ########.fr */ +/* Updated: 2025/03/17 22:07:57 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -80,10 +80,12 @@ std::ostream& operator<<(std::ostream& output, Form const &toPrint) { } void Form::beSigned(Bureaucrat &bureaucrat) { - try { - bureaucrat.signForm(*this); - } catch (std::exception & err) { - std::cerr << err.what() << std::endl; + if (bureaucrat.getGrade() >= this->_to_sign) { + throw FormForSupperior(); + } else if (_signed) { + throw FormAlreadySigned(); + } else { + std::cout << "Bureaucrat " << bureaucrat.getName() << " signs the form " << this->_name << std::endl; + this->_signed = true; } - _signed = true; } diff --git a/cpp05/ex01/Form.hpp b/cpp05/ex01/Form.hpp index 0005ffe..0e2eba1 100644 --- a/cpp05/ex01/Form.hpp +++ b/cpp05/ex01/Form.hpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/17 14:20:06 by rparodi #+# #+# */ -/* Updated: 2025/03/17 20:17:37 by rparodi ### ########.fr */ +/* Updated: 2025/03/17 22:00:37 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ #include #include +#include class Bureaucrat; @@ -35,6 +36,17 @@ class Form { return ("Error:\n> Grade is too low (have to be higher than 150)"); } }; + class FormForSupperior: public std::exception { + virtual const char *what() const throw() { + return ("the bureaucrat grade is too low !"); + } + }; + class FormAlreadySigned: public std::exception { + virtual const char *what() const throw() { + return ("form is already signed !"); + } + }; + std::string getName() const; void beSigned(Bureaucrat &bureaucrat); int getExecute() const; diff --git a/cpp05/ex01/main.cpp b/cpp05/ex01/main.cpp index 1f15f4f..4e93f01 100644 --- a/cpp05/ex01/main.cpp +++ b/cpp05/ex01/main.cpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/16 14:47:26 by rparodi #+# #+# */ -/* Updated: 2025/03/17 16:37:04 by rparodi ### ########.fr */ +/* Updated: 2025/03/17 20:43:54 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,7 +62,21 @@ int main(void) { } std::cout << std::endl << std::endl << b2 << std::endl << std::endl; + try { + b2.promote(); + } catch (std::exception & err) { + std::cerr << RED << err.what() << RESET << std::endl; + } + std::cout << std::endl << YELLOW << "\t\t[ Testing the overload of the '<<' operator ]" << RESET << std::endl; Form f1("Norme to cpp", 1, 1); std::cout << f1 << std::endl; + + std::cout << std::endl << YELLOW << "\t\t[ Testing beSigned() methode with a new form]" << RESET << std::endl; + f1.beSigned(b1); + std::cout << f1 << std::endl; + + std::cout << std::endl << YELLOW << "\t\t[ Testing beSigned() methode with a signed form ]" << RESET << std::endl; + f1.beSigned(b1); + std::cout << f1 << std::endl; }