Skip to content

Commit

Permalink
hm
Browse files Browse the repository at this point in the history
  • Loading branch information
fcil committed Sep 12, 2022
1 parent 061ee93 commit 5efd01e
Show file tree
Hide file tree
Showing 7 changed files with 448 additions and 0 deletions.
98 changes: 98 additions & 0 deletions CPP/cpp-module-05/ex02/Bureaucrat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/12 12:00:32 by fcil #+# #+# */
/* Updated: 2022/09/12 16:06:48 by fcil ### ########.fr */
/* */
/* ************************************************************************** */

#include "Bureaucrat.hpp"

Bureaucrat::Bureaucrat()
{
this->_grade = LOWEST;
}

Bureaucrat::Bureaucrat(std::string name, int grade) : _name(name), _grade(grade)
{
if (grade < HIGHEST)
throw Bureaucrat::GradeTooHighException();
if (grade > LOWEST)
throw Bureaucrat::GradeTooLowException();
}

Bureaucrat::Bureaucrat(Bureaucrat const &b) : _name(b.getName()), _grade(b.getGrade())
{
*this = b;
}

Bureaucrat::~Bureaucrat()
{}

Bureaucrat& Bureaucrat::operator=(Bureaucrat const &b)
{
this->_grade = b.getGrade();

return (*this);
}

std::string Bureaucrat::getName() const
{
return (this->_name);
}

int Bureaucrat::getGrade() const
{
return (this->_grade);
}

void Bureaucrat::incrementGrade()
{
if (this->_grade - 1 < HIGHEST)
throw Bureaucrat::GradeTooHighException();
this->_grade--;
}

void Bureaucrat::decrementGrade()
{
if (this->_grade + 1 > LOWEST)
throw Bureaucrat::GradeTooLowException();
this->_grade++;

}

void Bureaucrat::signForm(Form &form)
{
if (form.getSignGrade() >= this->getGrade())
{
form.setSignedResult(true);
std::cout << "<" << this->getName() << "> signed <" <<
form.getName() << ">" << std::endl;
}
else
std::cout << "<" << this->getName() << "> cannot sign <" <<
form.getName() << "> because <the bureaucrat's grade is lower than "
"the form's grade to sign>."<< std::endl;
}

void Bureaucrat::executeForm(Form const &form)
{
try
{
form.execute(*this);
}
catch(const std::exception& e)
{
std::cout << this->getName() << "couldnt execute"<< form.getName() << " because " << e.what() << std::endl;
}
}

std::ostream& operator<<(std::ostream &o, Bureaucrat const &b)
{
o << b.getName() << ", bureaucrat grade " << b.getGrade() << ".";
return (o);
}
63 changes: 63 additions & 0 deletions CPP/cpp-module-05/ex02/Bureaucrat.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/11 21:39:02 by fcil #+# #+# */
/* Updated: 2022/09/12 15:20:57 by fcil ### ########.fr */
/* */
/* ************************************************************************** */

#pragma once

#include <iostream>
#include <string>
#include "Form.hpp"

#define LOWEST 150
#define HIGHEST 1

class Form;

class Bureaucrat
{
private:
std::string const _name;
int _grade;
public:
Bureaucrat();
Bureaucrat(std::string name, int grade);
Bureaucrat(Bureaucrat const &b);
~Bureaucrat();

Bureaucrat &operator=(Bureaucrat const &b);
std::string getName() const;
int getGrade() const;

void incrementGrade();
void decrementGrade();

void signForm(Form &form);
void executeForm(Form const &form);

class GradeTooHighException : public std::exception
{
public:
virtual const char* what() const throw()
{
return ("Grade is too High.");
}
};

class GradeTooLowException : public std::exception
{
public:
virtual const char* what() const throw()
{
return ("Grade is too Low.");
}
};
};
std::ostream& operator<<(std::ostream &o, Bureaucrat const &b);
104 changes: 104 additions & 0 deletions CPP/cpp-module-05/ex02/Form.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Form.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/12 14:11:13 by fcil #+# #+# */
/* Updated: 2022/09/12 16:03:35 by fcil ### ########.fr */
/* */
/* ************************************************************************** */

#include "Form.hpp"

Form::Form(void) : _name("null"), _signed(false), _gradeToSign(0), _gradeToExecute(0)
{}

Form::Form(const std::string name, int signGrade, int execGrade) : _name(name), _signed(false), _gradeToSign(signGrade), _gradeToExecute(execGrade)
{
if (this->_gradeToSign < HIGHEST || this->_gradeToExecute < HIGHEST)
throw GradeTooHighException();
if (this->_gradeToSign > LOWEST || this->_gradeToExecute > LOWEST)
throw GradeTooLowException();
}

Form::Form(Form const &f) : _name(f.getName()), _signed(f.getSignedResult()), _gradeToSign(f.getSignGrade()), _gradeToExecute(f.getExecGrade())
{
*this = f;
}

Form::~Form()
{}

Form& Form::operator=(Form const &f)
{
this->_signed = f.getSignedResult();

return (*this);
}

std::string Form::getName() const
{
return (this->_name);
}

bool Form::getSignedResult() const
{
return(this->_signed);
}

void Form::setSignedResult(bool signedResult)
{
this->_signed = signedResult;
}

int Form::getSignGrade(void) const
{
return (this->_gradeToSign);
}

int Form::getExecGrade(void) const
{
return (this->_gradeToExecute);
}

std::string Form::getFormTarget() const
{
return (this->target);
}

void Form::setFormTarget(std::string target)
{
this->target = target;
}

void Form::beSigned(Bureaucrat const &b)
{
if (this->getSignGrade() < b.getGrade())
throw GradeTooLowException();
else
this->_signed = true;
}

void Form::execute(Bureaucrat const &executor) const
{
if (executor.getGrade() > this->getExecGrade())
throw Form::GradeTooLowException();
if (!this->getSignedResult())
{
std::cout << "<" << this->getName() << "> cannot be executed by <" << executor.getName()
<< "> because the for is not signed" << std::endl;
return ;
}
executeForm();
}

std::ostream& operator<<(std::ostream &o, Form const &f)
{
o << f.getName() << "Form:" << std::endl;
o << "Status: " << f.getSignedResult() << std::endl;
o << "Grade To Sign: " << f.getSignGrade() << std::endl;
o << "Grade To Execute: " << f.getExecGrade() << std::endl;
return (o);
}
68 changes: 68 additions & 0 deletions CPP/cpp-module-05/ex02/Form.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Form.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/12 13:46:52 by fcil #+# #+# */
/* Updated: 2022/09/12 15:58:01 by fcil ### ########.fr */
/* */
/* ************************************************************************** */

#pragma once

#include "Bureaucrat.hpp"

class Bureaucrat;

class Form
{
private:
const std::string _name;
bool _signed;
const int _gradeToSign;
const int _gradeToExecute;
std::string target;

public:
Form(void);
Form(const std::string name, int signGrade, int execGrade);
Form(Form const &f);
virtual ~Form();

Form &operator=(Form const &f);

std::string getName() const;
bool getSignedResult() const;
void setSignedResult(bool signedResult);
int getSignGrade(void) const;
int getExecGrade(void) const;
std::string getFormTarget() const;
void setFormTarget(std::string target);

void beSigned(Bureaucrat const &b);

void execute(Bureaucrat const &executor) const;
virtual void executeForm() const = 0;

class GradeTooHighException : public std::exception
{
public:
virtual const char* what() const throw()
{
return ("<Form> Grade is too High.");
}
};

class GradeTooLowException : public std::exception
{
public:
virtual const char* what() const throw()
{
return ("<Form> Grade is too Low.");
}
};
};

std::ostream &operator<<(std::ostream &o, Form const &f);
Loading

0 comments on commit 5efd01e

Please sign in to comment.