Skip to content

Commit

Permalink
finished
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatih Cil committed Sep 12, 2022
1 parent 5efd01e commit a97828b
Show file tree
Hide file tree
Showing 21 changed files with 951 additions and 8 deletions.
4 changes: 2 additions & 2 deletions CPP/cpp-module-05/ex01/Form.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/12 14:11:13 by fcil #+# #+# */
/* Updated: 2022/09/12 14:56:43 by fcil ### ########.fr */
/* Updated: 2022/09/12 17:14:42 by fcil ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -74,7 +74,7 @@ void Form::beSigned(Bureaucrat const &b)

std::ostream& operator<<(std::ostream &o, Form const &f)
{
o << f.getName() << "Form:" << std::endl;
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;
Expand Down
7 changes: 4 additions & 3 deletions CPP/cpp-module-05/ex02/Form.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/12 14:11:13 by fcil #+# #+# */
/* Updated: 2022/09/12 16:03:35 by fcil ### ########.fr */
/* Updated: 2022/09/12 17:14:06 by fcil ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -76,7 +76,7 @@ void Form::setFormTarget(std::string target)
void Form::beSigned(Bureaucrat const &b)
{
if (this->getSignGrade() < b.getGrade())
throw GradeTooLowException();
throw Form::GradeTooLowException();
else
this->_signed = true;
}
Expand All @@ -92,11 +92,12 @@ void Form::execute(Bureaucrat const &executor) const
return ;
}
executeForm();
std::cout << executor.getName() << " executed " << this->getName() << std::endl;
}

std::ostream& operator<<(std::ostream &o, Form const &f)
{
o << f.getName() << "Form:" << std::endl;
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;
Expand Down
45 changes: 45 additions & 0 deletions CPP/cpp-module-05/ex02/PresidentialPardonForm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PresidentialPardonForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/12 16:10:36 by fcil #+# #+# */
/* Updated: 2022/09/12 16:41:53 by fcil ### ########.fr */
/* */
/* ************************************************************************** */

#include "PresidentialPardonForm.hpp"

PresidentialPardonForm::PresidentialPardonForm() :
Form("PresidentialPardonForm", 25, 5)
{}

PresidentialPardonForm::~PresidentialPardonForm()
{}

PresidentialPardonForm::PresidentialPardonForm(std::string target) :
Form("PresidentialPardonForm", 25, 5)
{
this->setFormTarget(target);
}

PresidentialPardonForm::PresidentialPardonForm(PresidentialPardonForm const &src) :
Form(src.getName(), src.getSignGrade(), src.getExecGrade())
{
*this = src;
}

PresidentialPardonForm& PresidentialPardonForm::operator=(PresidentialPardonForm scf)
{
this->setSignedResult(scf.getSignedResult());
this->setFormTarget(scf.getFormTarget());

return (*this);
}

void PresidentialPardonForm::executeForm() const
{
std::cout << this->getFormTarget() << " has been pardoned by Zaphod Beeblebrox." << std::endl;
}
28 changes: 28 additions & 0 deletions CPP/cpp-module-05/ex02/PresidentialPardonForm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PresidentialPardonForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/12 16:08:24 by fcil #+# #+# */
/* Updated: 2022/09/12 16:29:59 by fcil ### ########.fr */
/* */
/* ************************************************************************** */

#pragma once

#include "Form.hpp"
#include <fstream>

class PresidentialPardonForm : public Form
{
public:
PresidentialPardonForm();
virtual ~PresidentialPardonForm();
PresidentialPardonForm(std::string target);
PresidentialPardonForm(PresidentialPardonForm const &src);
PresidentialPardonForm &operator=(PresidentialPardonForm scf);

virtual void executeForm() const;
};
52 changes: 52 additions & 0 deletions CPP/cpp-module-05/ex02/RobotomyRequestForm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* RobotomyRequestForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/12 16:10:36 by fcil #+# #+# */
/* Updated: 2022/09/12 16:39:14 by fcil ### ########.fr */
/* */
/* ************************************************************************** */

#include "RobotomyRequestForm.hpp"

RobotomyRequestForm::RobotomyRequestForm() :
Form("RobotomyRequestForm", 72, 45)
{}

RobotomyRequestForm::~RobotomyRequestForm()
{}

RobotomyRequestForm::RobotomyRequestForm(std::string target) :
Form("RobotomyRequestForm", 72, 45)
{
this->setFormTarget(target);
}

RobotomyRequestForm::RobotomyRequestForm(RobotomyRequestForm const &src) :
Form(src.getName(), src.getSignGrade(), src.getExecGrade())
{
*this = src;
}

RobotomyRequestForm& RobotomyRequestForm::operator=(RobotomyRequestForm scf)
{
this->setSignedResult(scf.getSignedResult());
this->setFormTarget(scf.getFormTarget());

return (*this);
}

void RobotomyRequestForm::executeForm() const
{
srand(time(NULL));
if (rand() % 2 == 0) {
std::cout << "Bzzzztttt...." << std::endl;
std::cout << this->getFormTarget() << " has been robotomized successfully." << std::endl;
}
else {
std::cout << this->getFormTarget() << " robotomy failed." << std::endl;
}
}
28 changes: 28 additions & 0 deletions CPP/cpp-module-05/ex02/RobotomyRequestForm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* RobotomyRequestForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/12 16:08:24 by fcil #+# #+# */
/* Updated: 2022/09/12 16:29:59 by fcil ### ########.fr */
/* */
/* ************************************************************************** */

#pragma once

#include "Form.hpp"
#include <fstream>

class RobotomyRequestForm : public Form
{
public:
RobotomyRequestForm();
virtual ~RobotomyRequestForm();
RobotomyRequestForm(std::string target);
RobotomyRequestForm(RobotomyRequestForm const &src);
RobotomyRequestForm &operator=(RobotomyRequestForm scf);

virtual void executeForm() const;
};
4 changes: 2 additions & 2 deletions CPP/cpp-module-05/ex02/ShrubberyCreationForm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/12 16:10:36 by fcil #+# #+# */
/* Updated: 2022/09/12 16:30:03 by fcil ### ########.fr */
/* Updated: 2022/09/12 16:45:12 by fcil ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -61,7 +61,7 @@ void ShrubberyCreationForm::executeForm() const
" \\ }{\n"
" }}{\n"
" }}{\n"
" canodis {{}\n"
" fcil {{}\n"
" , -=-~{ .-^- _\n"
" `}\n"
" {"
Expand Down
41 changes: 40 additions & 1 deletion CPP/cpp-module-05/ex02/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,52 @@
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/12 11:42:20 by fcil #+# #+# */
/* Updated: 2022/09/12 16:24:32 by fcil ### ########.fr */
/* Updated: 2022/09/12 16:54:24 by fcil ### ########.fr */
/* */
/* ************************************************************************** */

#include "iostream"
#include "Bureaucrat.hpp"
#include "PresidentialPardonForm.hpp"
#include "RobotomyRequestForm.hpp"
#include "ShrubberyCreationForm.hpp"

int main(void)
{
{
PresidentialPardonForm pp("PP");

Bureaucrat fatih("fatih", 2);
Bureaucrat abyerk("abyerk", 6);

fatih.signForm(pp);
abyerk.signForm(pp);

fatih.executeForm(pp);
abyerk.executeForm(pp);
std::cout << "--------------------------" << std::endl;
}
{
RobotomyRequestForm rqf("RQF");
Bureaucrat fatih("fatih", 3);
Bureaucrat abyerk("abyerk", 6);

fatih.signForm(rqf);
abyerk.signForm(rqf);

fatih.executeForm(rqf);
abyerk.executeForm(rqf);
std::cout << "--------------------------" << std::endl;
}
{
ShrubberyCreationForm musab("SCF");
Bureaucrat fatih("fatih", 3);
Bureaucrat abyerk("abyerk", 6);

fatih.signForm(musab);
abyerk.signForm(musab);

fatih.executeForm(musab);
abyerk.executeForm(musab);
}
}
98 changes: 98 additions & 0 deletions CPP/cpp-module-05/ex03/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);
}
Loading

0 comments on commit a97828b

Please sign in to comment.