Skip to content

Commit

Permalink
ex01
Browse files Browse the repository at this point in the history
  • Loading branch information
fcil committed Sep 12, 2022
1 parent e9a43f7 commit 8ba3672
Show file tree
Hide file tree
Showing 6 changed files with 350 additions and 0 deletions.
Binary file removed CPP/cpp-module-05/en.subject.pdf
Binary file not shown.
86 changes: 86 additions & 0 deletions CPP/cpp-module-05/ex01/Bureaucrat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <the bureaucrat's grade is lower than "
"the form's grade to sign>."<< std::endl;
}

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

#pragma once

#include <iostream>
#include <string>
#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);
82 changes: 82 additions & 0 deletions CPP/cpp-module-05/ex01/Form.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Form.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
59 changes: 59 additions & 0 deletions CPP/cpp-module-05/ex01/Form.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Form.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 ("<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);
63 changes: 63 additions & 0 deletions CPP/cpp-module-05/ex01/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}
}

0 comments on commit 8ba3672

Please sign in to comment.