d05 ex01 form ok

This commit is contained in:
Hugo LAMY
2022-02-28 17:56:20 +01:00
parent ce1bde6d2c
commit 4946eb3b8c
11 changed files with 452 additions and 11 deletions

View File

@@ -0,0 +1,98 @@
#include "Bureaucrat.hpp"
#define COPLIEN_COLOR B_CYAN
/*********************************************
* CONSTRUCTORS
*********************************************/
Bureaucrat::Bureaucrat( std::string name, int grade )
: _name(name)
, _grade(grade) {
if (grade > 150)
throw GradeTooLowException();
if (grade < 1)
throw GradeTooHighException();
std::cout << COPLIEN_COLOR "Bureaucrat constructor" RESET "\n";
return;
}
Bureaucrat::Bureaucrat( Bureaucrat const & src ) {
std::cout << COPLIEN_COLOR "Bureaucrat copy constructor" RESET "\n";
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
Bureaucrat::~Bureaucrat() {
std::cout << COPLIEN_COLOR "Bureaucrat destructor" RESET "\n";
return;
}
/*********************************************
* OPERATORS
*********************************************/
Bureaucrat & Bureaucrat::operator=( Bureaucrat const & rhs ) {
if ( this != &rhs )
_grade = rhs.getGrade();
return *this;
}
std::ostream & operator<<(std::ostream & o, Bureaucrat const & rhs)
{
o
<< rhs.getName() << ", bureaucrat grade "
<< rhs.getGrade();
return (o);
}
/*********************************************
* ACCESSORS
*********************************************/
std::string Bureaucrat::getName() const {return _name;}
int Bureaucrat::getGrade() const {return _grade;}
/*********************************************
* PUBLIC MEMBER FUNCTIONS
*********************************************/
void Bureaucrat::gradeUp() {
if (_grade > 1)
_grade--;
else
throw GradeTooHighException();
}
void Bureaucrat::gradeDown() {
if (_grade < 150)
_grade++;
else
throw GradeTooLowException();
}
void Bureaucrat::signForm( Form & f) {
try {
f.beSigned( *this );
std::cout << _name << " signed " << f.getName() << "\n";
}
catch (std::exception & e) {
std::cout << _name << " couldn't sign " << f.getName()
<< " because [" << e.what() << "]\n";
}
}
/*********************************************
* NESTED CLASS
*********************************************/
const char * Bureaucrat::GradeTooHighException::what() const throw() {
return (B_RED "grade higher than 1" RESET);
}
const char * Bureaucrat::GradeTooLowException::what() const throw() {
return (B_RED "grade lower than 150" RESET);
}

90
d05/ex01/srcs/Form.cpp Normal file
View File

@@ -0,0 +1,90 @@
#include "Form.hpp"
#define COPLIEN_COLOR B_CYAN
/*********************************************
* CONSTRUCTORS
*********************************************/
Form::Form( std::string name, int signedGrade, int executeGrade )
: _name(name)
, _signed(false)
, _signedGrade(signedGrade)
, _executeGrade(executeGrade) {
if (signedGrade > 150 || executeGrade > 150)
throw Form::GradeTooLowException();
if (signedGrade < 1 || executeGrade < 1)
throw Form::GradeTooHighException();
std::cout << COPLIEN_COLOR "Form constructor" RESET "\n";
return;
}
Form::Form( Form const & src )
: _name(src.getName())
, _signedGrade(src.getSignedGrade())
, _executeGrade(src.getExecuteGrade()) {
std::cout << COPLIEN_COLOR "Form copy constructor" RESET "\n";
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
Form::~Form() {
std::cout << COPLIEN_COLOR "Form destructor" RESET "\n";
return;
}
/*********************************************
* OPERATORS
*********************************************/
Form & Form::operator=( Form const & rhs ) {
if ( this != &rhs ) {
_signed = rhs.getSigned();
}
return *this;
}
std::ostream & operator<<(std::ostream & o, Form const & rhs)
{
o << "form: "
<< rhs.getName() << ", is signed: "
<< rhs.getSigned() << ", need at least ["
<< rhs.getSignedGrade() << "] to be signed, need at least ["
<< rhs.getExecuteGrade() << "] to be executed";
return (o);
}
/*********************************************
* ACCESSORS
*********************************************/
std::string Form::getName() const {return _name;}
bool Form::getSigned() const {return _signed;}
int Form::getSignedGrade() const {return _signedGrade;}
int Form::getExecuteGrade() const {return _executeGrade;}
/*********************************************
* PUBLIC MEMBER FUNCTIONS
*********************************************/
void Form::beSigned( Bureaucrat const & b) {
if (b.getGrade() < _signedGrade)
_signed = true;
else
throw Form::GradeTooLowException();
}
/*********************************************
* NESTED CLASS
*********************************************/
const char * Form::GradeTooHighException::what() const throw() {
return (B_RED "grade too high" RESET);
}
const char * Form::GradeTooLowException::what() const throw() {
return (B_RED "grade too low" RESET);
}