feat: adding the great way to use beSigned

This commit is contained in:
Raphael 2025-03-17 22:09:20 +01:00
parent 5829da2bcb
commit 2b5b3b29cb
5 changed files with 44 additions and 19 deletions

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/16 14:49:52 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); return (output);
} }
void Bureaucrat::signForm(Form const &form) { void Bureaucrat::signForm(Form &form) {
if (form.isSigned()) { try {
std::cerr << this->getName() << " couldnt sign " << form.getName() << " because form is already signed !" << std::endl; form.beSigned(*this);
} else { } catch (std::exception &e) {
if (form.getSign() < this->_grade) { std::cout << this->getName() << " cannot sign " << form.getName() << " because " << e.what() << std::endl;
throw GradeTooLowException();
return;
}
} }
} }

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/16 14:48:33 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; int getGrade(void) const;
void promote(void); void promote(void);
void demote(void); void demote(void);
void signForm(Form const &form); void signForm(Form &form);
class GradeTooHighException : public std::exception { class GradeTooHighException : public std::exception {
virtual const char *what() const throw() { virtual const char *what() const throw() {
return ("Error:\n> Grade is too high (have to be lower than 1)"); return ("Error:\n> Grade is too high (have to be lower than 1)");

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/17 14:19:41 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) { void Form::beSigned(Bureaucrat &bureaucrat) {
try { if (bureaucrat.getGrade() >= this->_to_sign) {
bureaucrat.signForm(*this); throw FormForSupperior();
} catch (std::exception & err) { } else if (_signed) {
std::cerr << err.what() << std::endl; throw FormAlreadySigned();
} else {
std::cout << "Bureaucrat " << bureaucrat.getName() << " signs the form " << this->_name << std::endl;
this->_signed = true;
} }
_signed = true;
} }

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/17 14:20:06 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 <string> #include <string>
#include <iostream> #include <iostream>
#include <exception>
class Bureaucrat; class Bureaucrat;
@ -35,6 +36,17 @@ class Form {
return ("Error:\n> Grade is too low (have to be higher than 150)"); 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; std::string getName() const;
void beSigned(Bureaucrat &bureaucrat); void beSigned(Bureaucrat &bureaucrat);
int getExecute() const; int getExecute() const;

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/16 14:47:26 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; 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; std::cout << std::endl << YELLOW << "\t\t[ Testing the overload of the '<<' operator ]" << RESET << std::endl;
Form f1("Norme to cpp", 1, 1); Form f1("Norme to cpp", 1, 1);
std::cout << f1 << std::endl; 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;
} }