diff --git a/CPP/cpp-module-05/en.subject.pdf b/CPP/cpp-module-05/en.subject.pdf deleted file mode 100644 index def30ef..0000000 Binary files a/CPP/cpp-module-05/en.subject.pdf and /dev/null differ diff --git a/CPP/cpp-module-05/ex01/Bureaucrat.cpp b/CPP/cpp-module-05/ex01/Bureaucrat.cpp new file mode 100644 index 0000000..3b09bdd --- /dev/null +++ b/CPP/cpp-module-05/ex01/Bureaucrat.cpp @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fcil +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/12 12:00:32 by fcil #+# #+# */ +/* Updated: 2022/09/12 14:59:31 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() << "> signs <" << + form.getName() << ">" << std::endl; + } + else + std::cout << "<" << this->getName() << "> cannot sign <" << + form.getName() << "> because ."<< std::endl; +} + +std::ostream& operator<<(std::ostream &o, Bureaucrat const &b) +{ + o << b.getName() << ", bureaucrat grade " << b.getGrade() << "."; + return (o); +} diff --git a/CPP/cpp-module-05/ex01/Bureaucrat.hpp b/CPP/cpp-module-05/ex01/Bureaucrat.hpp new file mode 100644 index 0000000..3e2b009 --- /dev/null +++ b/CPP/cpp-module-05/ex01/Bureaucrat.hpp @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fcil +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/11 21:39:02 by fcil #+# #+# */ +/* Updated: 2022/09/12 14:55:03 by fcil ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include +#include "Form.hpp" + +#define LOWEST 150 +#define HIGHEST 1 + +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); + + 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); diff --git a/CPP/cpp-module-05/ex01/Form.cpp b/CPP/cpp-module-05/ex01/Form.cpp new file mode 100644 index 0000000..96d8d23 --- /dev/null +++ b/CPP/cpp-module-05/ex01/Form.cpp @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fcil +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/12 14:11:13 by fcil #+# #+# */ +/* Updated: 2022/09/12 14:56:43 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); +} + +void Form::beSigned(Bureaucrat const &b) +{ + if (this->getSignGrade() < b.getGrade()) + throw GradeTooLowException(); + else + this->_signed = true; +} + + +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); +} diff --git a/CPP/cpp-module-05/ex01/Form.hpp b/CPP/cpp-module-05/ex01/Form.hpp new file mode 100644 index 0000000..4640dd8 --- /dev/null +++ b/CPP/cpp-module-05/ex01/Form.hpp @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fcil +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/12 13:46:52 by fcil #+# #+# */ +/* Updated: 2022/09/12 14:56:35 by fcil ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include "Bureaucrat.hpp" + +class Form +{ +private: + const std::string _name; + bool _signed; + const int _gradeToSign; + const int _gradeToExecute; + +public: + Form(void); + Form(const std::string name, int signGrade, int execGrade); + Form(Form const &f); + ~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; + void beSigned(Bureaucrat const &b); + + 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, Form const &f); diff --git a/CPP/cpp-module-05/ex01/main.cpp b/CPP/cpp-module-05/ex01/main.cpp new file mode 100644 index 0000000..a835d53 --- /dev/null +++ b/CPP/cpp-module-05/ex01/main.cpp @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fcil +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/12 11:42:20 by fcil #+# #+# */ +/* Updated: 2022/09/12 13:06:26 by fcil ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" + +int main(void) +{ + try + { + Bureaucrat f("fatih", 4); + std::cout << f << std::endl; + } + catch(const Bureaucrat::GradeTooHighException &e) + { + std::cerr << e.what() << std::endl; + } + try + { + Bureaucrat a("abyerk", 200); + } + catch(const std::exception& e) + { + std::cerr << e.what() << std::endl; + } + try + { + Bureaucrat bureaucrat1("uwu", 1); + bureaucrat1.incrementGrade(); + std::cout << bureaucrat1 << std::endl; + } + catch (Bureaucrat::GradeTooHighException &e) + { + std::cerr << e.what() << std::endl; + } + catch (Bureaucrat::GradeTooLowException &e) + { + std::cerr << e.what() << std::endl; + } + + try + { + Bureaucrat bureaucrat2("blabla", 150); + bureaucrat2.decrementGrade(); + std::cout << bureaucrat2 << std::endl; + } + catch (Bureaucrat::GradeTooHighException &e) + { + std::cerr << e.what() << std::endl; + } + catch (Bureaucrat::GradeTooLowException &e) + { + std::cerr << e.what() << std::endl; + } +} \ No newline at end of file