ex05 presque fini

This commit is contained in:
kinou-p 2022-08-05 15:39:46 +02:00
parent 04eaf3c332
commit 26aae431ac
33 changed files with 1232 additions and 33 deletions

View File

@ -6,21 +6,26 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/18 21:34:42 by apommier #+# #+# */ /* Created: 2022/07/18 21:34:42 by apommier #+# #+# */
/* Updated: 2022/07/19 11:59:12 by apommier ### ########.fr */ /* Updated: 2022/08/05 13:12:53 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "Bureaucrat.hpp" #include "Bureaucrat.hpp"
Bureaucrat::Bureaucrat() : _name("unnamed bureaucrat")
{
this->_grade = 150;
}
Bureaucrat::Bureaucrat(int grade, std::string name) : _name(name) Bureaucrat::Bureaucrat(int grade, std::string name) : _name(name)
{ {
this->_grade = grade; this->_grade = grade;
this->checkGrade(); this->checkGrade();
} }
Bureaucrat::Bureaucrat(const Bureaucrat& copy) Bureaucrat::Bureaucrat(const Bureaucrat& copy) : _name(copy.getName())
{ {
*this = copy; this->_grade = copy.getGrade();
} }
Bureaucrat::~Bureaucrat() Bureaucrat::~Bureaucrat()

View File

@ -20,6 +20,7 @@
class Bureaucrat{ class Bureaucrat{
public: public:
Bureaucrat();
Bureaucrat(int grade, std::string name); Bureaucrat(int grade, std::string name);
Bureaucrat(const Bureaucrat& copy); Bureaucrat(const Bureaucrat& copy);
~Bureaucrat(); ~Bureaucrat();

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/18 21:34:45 by apommier #+# #+# */ /* Created: 2022/07/18 21:34:45 by apommier #+# #+# */
/* Updated: 2022/07/19 11:32:05 by apommier ### ########.fr */ /* Updated: 2022/08/05 13:24:05 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,14 +14,80 @@
int main() int main()
{ {
std::cout << "================Bureaucrat exeption and assignation test=================\n\n";
std::cout << "---Bureaucrat grade of 151 :\n";
try try
{ {
Bureaucrat First(151, "john"); Bureaucrat john1(151, "john");
std::cout << "Bureaucrat become bureaucrat!\n";
} }
catch(std::exception &e) catch(std::exception &e)
{ {
std::cout << e.what(); std::cout << e.what();
return (-1);
} }
std::cout << "\n---Bureaucrat grade of -1 :\n";
try
{
Bureaucrat john2(-1, "john");
std::cout << "Bureaucrat become bureaucrat!\n";
}
catch(std::exception &e)
{
std::cout << e.what();
}
std::cout << "\n---Bureaucrat grade of 50 :\n";
try
{
Bureaucrat john3(50, "john");
std::cout << "Bureaucrat become bureaucrat!\n";
std::cout << john3 << std::endl;
std::cout << "\n---Copy test :\n";
Bureaucrat john4(john3);
std::cout << john4 << std::endl;
std::cout << "---Assignement test :\n";
Bureaucrat john5;
john5 = john4;
std::cout << john5 << std::endl;
}
catch(std::exception &e)
{
std::cout << e.what();
}
std::cout << "\n================Bureaucrat member function test=================\n\n";
try
{
Bureaucrat john2(50, "john");
std::cout << "Bureaucrat become bureaucrat!\n";\
std::cout << john2 << std::endl;
john2.upGrade();
std::cout << john2 << std::endl;
john2.upGrade();
std::cout << john2 << std::endl;
john2.downGrade();
std::cout << john2 << std::endl;
}
catch(std::exception &e)
{
std::cout << e.what();
}
std::cout << "---same but with exeption\n";
try
{
Bureaucrat john2(1, "john");
std::cout << "Bureaucrat become bureaucrat!\n";
std::cout << john2 << std::endl;
john2.upGrade();
std::cout << john2 << std::endl;
john2.downGrade();
std::cout << john2 << std::endl;
}
catch(std::exception &e)
{
std::cout << e.what();
}
return (0); return (0);
} }

View File

@ -6,21 +6,26 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/18 21:34:42 by apommier #+# #+# */ /* Created: 2022/07/18 21:34:42 by apommier #+# #+# */
/* Updated: 2022/07/19 12:55:48 by apommier ### ########.fr */ /* Updated: 2022/08/05 13:13:08 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "Bureaucrat.hpp" #include "Bureaucrat.hpp"
Bureaucrat::Bureaucrat() : _name("unnamed_bureaucrat")
{
this->_grade = 150;
}
Bureaucrat::Bureaucrat(int grade, std::string name) : _name(name) Bureaucrat::Bureaucrat(int grade, std::string name) : _name(name)
{ {
this->_grade = grade; this->_grade = grade;
this->checkGrade(); this->checkGrade();
} }
Bureaucrat::Bureaucrat(const Bureaucrat& copy) Bureaucrat::Bureaucrat(const Bureaucrat& copy) : _name(copy.getName())
{ {
*this = copy; this->_grade = copy.getGrade();
} }
Bureaucrat::~Bureaucrat() Bureaucrat::~Bureaucrat()

View File

@ -23,6 +23,7 @@ class Form;
class Bureaucrat{ class Bureaucrat{
public: public:
Bureaucrat();
Bureaucrat(int grade, std::string name); Bureaucrat(int grade, std::string name);
Bureaucrat(const Bureaucrat& copy); Bureaucrat(const Bureaucrat& copy);
~Bureaucrat(); ~Bureaucrat();

View File

@ -12,6 +12,13 @@
#include "Form.hpp" #include "Form.hpp"
Form::Form() : _name("unnamed form")
{
this->_signedGrade = 1;
this->_executionGrade = 1;
this->_isSigned = 0;
}
Form::Form(int signedGrade, int executionGrade, std::string name) : _name(name) Form::Form(int signedGrade, int executionGrade, std::string name) : _name(name)
{ {
this->_signedGrade = signedGrade; this->_signedGrade = signedGrade;

View File

@ -23,6 +23,7 @@ class Bureaucrat;
class Form{ class Form{
public : public :
Form();
Form(int signedGrade, int executionGrade, std::string name); Form(int signedGrade, int executionGrade, std::string name);
Form(const Form& copy); Form(const Form& copy);
~Form(); ~Form();

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/18 21:34:45 by apommier #+# #+# */ /* Created: 2022/07/18 21:34:45 by apommier #+# #+# */
/* Updated: 2022/07/19 12:42:13 by apommier ### ########.fr */ /* Updated: 2022/08/05 13:30:03 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,14 +15,97 @@
int main() int main()
{ {
std::cout << "================Bureaucrat exeption test=================\n\n";
std::cout << "---Bureaucrat grade of 151 :\n";
try try
{ {
Bureaucrat First(151, "john"); Bureaucrat john1(151, "john");
std::cout << "Bureaucrat become bureaucrat!\n";
}
catch(std::exception &e)
{
std::cout << e.what();
}
std::cout << "\n---Bureaucrat grade of -1 :\n";
try
{
Bureaucrat john2(-1, "john");
std::cout << "Bureaucrat become bureaucrat!\n";
}
catch(std::exception &e)
{
std::cout << e.what();
}
std::cout << "\n---Bureaucrat grade of 50 :\n";
try
{
Bureaucrat john3(50, "john");
std::cout << "Bureaucrat become bureaucrat!\n";
std::cout << john3 << std::endl;
std::cout << "\n---Copy test :\n";
Bureaucrat john4(john3);
std::cout << john4 << std::endl;
std::cout << "---Assignement test :\n";
Bureaucrat john5;
john5 = john4;
std::cout << john5 << std::endl;
}
catch(std::exception &e)
{
std::cout << e.what();
}
std::cout << "\n=======================Form test=======================\n\n";
std::cout << "---Form execution grade of 151 :\n";
try
{
Form form1(10, 151, "form_one");
std::cout << "Form created!\n";
}
catch(std::exception &e)
{
std::cout << e.what();
}
std::cout << "\n---Form signed grade of -1 :\n";
try
{
Form form2(-1, 10, "form_two");
std::cout << "Form created!\n";
}
catch(std::exception &e)
{
std::cout << e.what();
}
std::cout << "\n---Form signed grade of 50 :\n";
try
{
Form form3(50, 10, "form_three");
std::cout << "Form created!\n";
std::cout << "trying to sign form by a 50 rank!\n";
Bureaucrat john(50, "john");
form3.beSigned(john);
std::cout << form3;
}
catch(std::exception &e)
{
std::cout << e.what();
}
std::cout << "\n---Form signed grade of 50 :\n";
try
{
Bureaucrat john(51, "john");
std::cout << john;
Form form3(50, 10, "form_three");
std::cout << form3;
std::cout << "---trying to sign form with john---\n";
form3.beSigned(john);
} }
catch(std::exception &e) catch(std::exception &e)
{ {
std::cout << e.what(); std::cout << e.what();
return (-1);
} }
return (0);
} }

View File

@ -12,6 +12,13 @@
#include "AForm.hpp" #include "AForm.hpp"
AForm::AForm() : _name("unnamed form")
{
this->_signedGrade = 1;
this->_executionGrade = 1;
this->_isSigned = 0;
}
AForm::AForm(int signedGrade, int executionGrade, std::string name) : _name(name) AForm::AForm(int signedGrade, int executionGrade, std::string name) : _name(name)
{ {
this->_signedGrade = signedGrade; this->_signedGrade = signedGrade;
@ -91,7 +98,7 @@ void AForm::checkGrade() const
throw AForm::GradeTooHighException(); throw AForm::GradeTooHighException();
} }
void AForm::execute(Bureaucrat const & executor) const void AForm::checkExecution(Bureaucrat const & executor) const
{ {
if (!this->_isSigned) if (!this->_isSigned)
throw AForm::formIsNotSignedException(); throw AForm::formIsNotSignedException();

View File

@ -23,9 +23,10 @@ class Bureaucrat;
class AForm{ class AForm{
public : public :
AForm();
AForm(int signedGrade, int executionGrade, std::string name); AForm(int signedGrade, int executionGrade, std::string name);
AForm(const AForm& copy); AForm(const AForm& copy);
virtual ~AForm() = 0; virtual ~AForm();
AForm &operator=(const AForm& rhs); AForm &operator=(const AForm& rhs);
const std::string getName() const; const std::string getName() const;
@ -36,7 +37,8 @@ class AForm{
void checkGrade() const; void checkGrade() const;
void beSigned(Bureaucrat &bureaucrat); void beSigned(Bureaucrat &bureaucrat);
void execute(Bureaucrat const & executor) const; virtual void execute(Bureaucrat const & executor) const = 0;
void checkExecution(Bureaucrat const & executor) const;
class GradeTooLowException : public std::exception class GradeTooLowException : public std::exception
{ {

View File

@ -6,21 +6,26 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/18 21:34:42 by apommier #+# #+# */ /* Created: 2022/07/18 21:34:42 by apommier #+# #+# */
/* Updated: 2022/08/04 15:28:47 by apommier ### ########.fr */ /* Updated: 2022/08/05 13:13:29 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "Bureaucrat.hpp" #include "Bureaucrat.hpp"
Bureaucrat::Bureaucrat() : _name("unnamed bureaucrat")
{
this->_grade = 150;
}
Bureaucrat::Bureaucrat(int grade, std::string name) : _name(name) Bureaucrat::Bureaucrat(int grade, std::string name) : _name(name)
{ {
this->_grade = grade; this->_grade = grade;
this->checkGrade(); this->checkGrade();
} }
Bureaucrat::Bureaucrat(const Bureaucrat& copy) Bureaucrat::Bureaucrat(const Bureaucrat& copy) : _name(copy.getName())
{ {
*this = copy; this->_grade = copy.getGrade();
} }
Bureaucrat::~Bureaucrat() Bureaucrat::~Bureaucrat()
@ -74,7 +79,7 @@ void Bureaucrat::checkGrade() const
throw Bureaucrat::GradeTooHighException(); throw Bureaucrat::GradeTooHighException();
} }
void Bureaucrat::signForm(AForm form) void Bureaucrat::signForm(AForm &form)
{ {
try try
{ {
@ -89,4 +94,5 @@ void Bureaucrat::signForm(AForm form)
void Bureaucrat::executeForm(AForm const & form) const void Bureaucrat::executeForm(AForm const & form) const
{ {
form.execute(*this); form.execute(*this);
std::cout << "<bureaucrat> executed <form>" << std::endl;
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/18 21:34:43 by apommier #+# #+# */ /* Created: 2022/07/18 21:34:43 by apommier #+# #+# */
/* Updated: 2022/08/04 15:26:47 by apommier ### ########.fr */ /* Updated: 2022/08/05 12:57:35 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,6 +23,7 @@ class AForm;
class Bureaucrat{ class Bureaucrat{
public: public:
Bureaucrat();
Bureaucrat(int grade, std::string name); Bureaucrat(int grade, std::string name);
Bureaucrat(const Bureaucrat& copy); Bureaucrat(const Bureaucrat& copy);
~Bureaucrat(); ~Bureaucrat();
@ -34,7 +35,7 @@ class Bureaucrat{
void downGrade(); void downGrade();
void checkGrade() const; void checkGrade() const;
void signForm(AForm form); void signForm(AForm &form);
void executeForm(AForm const & form) const; void executeForm(AForm const & form) const;
class GradeTooLowException : public std::exception class GradeTooLowException : public std::exception

View File

@ -14,7 +14,7 @@
PresidentialPardonForm::PresidentialPardonForm() : AForm(25, 5, "PresidentialPardonForm") PresidentialPardonForm::PresidentialPardonForm() : AForm(25, 5, "PresidentialPardonForm")
{ {
this->_target = "anonymousTarget";
} }
PresidentialPardonForm::PresidentialPardonForm(std::string target) : AForm(25, 5, "PresidentialPardonForm") PresidentialPardonForm::PresidentialPardonForm(std::string target) : AForm(25, 5, "PresidentialPardonForm")
@ -41,5 +41,6 @@ PresidentialPardonForm &PresidentialPardonForm::operator=(const PresidentialPard
void PresidentialPardonForm::execute(Bureaucrat const & executor) const void PresidentialPardonForm::execute(Bureaucrat const & executor) const
{ {
AForm::execute(executor); AForm::checkExecution(executor);
std::cout << this->_target << " has been forgiven by Zaphod Beeblebrox\n";
} }

View File

@ -11,10 +11,11 @@
/* ************************************************************************** */ /* ************************************************************************** */
#include "RobotomyRequestForm.hpp" #include "RobotomyRequestForm.hpp"
#include <cstdlib>
RobotomyRequestForm::RobotomyRequestForm() : AForm(72, 45, "RobotomyRequestForm") RobotomyRequestForm::RobotomyRequestForm() : AForm(72, 45, "RobotomyRequestForm")
{ {
this->_target = "Unknow Target";
} }
RobotomyRequestForm::RobotomyRequestForm(std::string target) : AForm(72, 45, "RobotomyRequestForm") RobotomyRequestForm::RobotomyRequestForm(std::string target) : AForm(72, 45, "RobotomyRequestForm")
@ -41,5 +42,10 @@ RobotomyRequestForm &RobotomyRequestForm::operator=(const RobotomyRequestForm& r
void RobotomyRequestForm::execute(Bureaucrat const & executor) const void RobotomyRequestForm::execute(Bureaucrat const & executor) const
{ {
AForm::execute(executor); AForm::checkExecution(executor);
std::cout << "Brrrzrzrzrzzrzrzzrrzz...\n";
if (std::rand() % 2)
std::cout << this->_target << " has been robotomized\n";
else
std::cout << this->_target << " hasn't been robtomized\n";
} }

View File

@ -17,7 +17,7 @@
ShrubberyCreationForm::ShrubberyCreationForm() : AForm(145, 137, "RobotomyRequestForm") ShrubberyCreationForm::ShrubberyCreationForm() : AForm(145, 137, "RobotomyRequestForm")
{ {
this->_target = "Unknow Target";
} }
ShrubberyCreationForm::ShrubberyCreationForm(std::string target) : AForm(145, 137, "RobotomyRequestForm") ShrubberyCreationForm::ShrubberyCreationForm(std::string target) : AForm(145, 137, "RobotomyRequestForm")
@ -44,9 +44,10 @@ ShrubberyCreationForm &ShrubberyCreationForm::operator=(const ShrubberyCreationF
void ShrubberyCreationForm::execute(Bureaucrat const & executor) const void ShrubberyCreationForm::execute(Bureaucrat const & executor) const
{ {
std::string name = this->_target; std::string name = this->_target + "_shrubbery";
std::cout << name << std::endl; std::cout << name << std::endl;
AForm::execute(executor); (void)executor;
std::ofstream outfile((std::string)name + "test"); //AForm::checkExecution(executor);
std::ofstream outfile(name.c_str());
outfile << " .\n . : \n . . :: :: \n . . ::: :: \n : : ::::. .. \n .. :: :: : ::. .:\n : ::: ::: . :: ::: .:.\n :: :::: . : :: ::: .::. \n ::: :: ::: : ::: .::.\n `::. ::: ::. `::::.::.\n `:::. :::. ::: :: :::::.\n `:::. ::bd:: :::::.\n `:::. :::. ::::. \n `::. `:::. :::: \n `:::. `::: :::: \n :::. :::: :::: \n ::bd:::bd:::: \n ::::::::::\n ::::::::\n :::(o): . . \n ::o:::(... \n `.. ::o:::: \n `):o:::: \n ::(o)::: \n .:::::: \n :::::::. \n :::::::::. \n ...::::::::::..."; outfile << " .\n . : \n . . :: :: \n . . ::: :: \n : : ::::. .. \n .. :: :: : ::. .:\n : ::: ::: . :: ::: .:.\n :: :::: . : :: ::: .::. \n ::: :: ::: : ::: .::.\n `::. ::: ::. `::::.::.\n `:::. :::. ::: :: :::::.\n `:::. ::bd:: :::::.\n `:::. :::. ::::. \n `::. `:::. :::: \n `:::. `::: :::: \n :::. :::: :::: \n ::bd:::bd:::: \n ::::::::::\n ::::::::\n :::(o): . . \n ::o:::(... \n `.. ::o:::: \n `):o:::: \n ::(o)::: \n .:::::: \n :::::::. \n :::::::::. \n ...::::::::::...";
} }

View File

@ -12,6 +12,9 @@
#include "AForm.hpp" #include "AForm.hpp"
#include "Bureaucrat.hpp" #include "Bureaucrat.hpp"
#include "PresidentialPardonForm.hpp"
#include "RobotomyRequestForm.hpp"
#include "ShrubberyCreationForm.hpp"
int main() int main()
{ {
@ -22,9 +25,17 @@ int main()
catch(std::exception &e) catch(std::exception &e)
{ {
std::cout << e.what(); std::cout << e.what();
return (-1);
} }
//ShrubberyCreationForm test; Bureaucrat First(1, "john");
//test.execute() ShrubberyCreationForm test("niceTree");
try
{
test.execute(First);
}
catch(std::exception &e)
{
std::cout << e.what();
}
return (0); return (0);
} }

133
cpp05/ex03/AForm.cpp Normal file
View File

@ -0,0 +1,133 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* AForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 11:54:01 by apommier #+# #+# */
/* Updated: 2022/07/19 12:54:48 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "AForm.hpp"
AForm::AForm() : _name("unnamed form")
{
this->_signedGrade = 1;
this->_executionGrade = 1;
this->_isSigned = 0;
}
AForm::AForm(int signedGrade, int executionGrade, std::string name) : _name(name)
{
this->_signedGrade = signedGrade;
this->_executionGrade = executionGrade;
this->_isSigned = 0;
this->checkGrade();
}
AForm::AForm(const AForm& copy)
{
*this = copy;
}
AForm::~AForm()
{
}
AForm &AForm::operator=(const AForm& rhs)
{
if (this != &rhs)
{
std::cout << "Only grade can be copied, name is const\n";
this->_signedGrade = rhs.getSignedGrade();
this->_executionGrade = rhs.getExecutionGrade();
}
return (*this);
}
std::ostream &operator<<(std::ostream &out, const AForm &Aform)
{
out << Aform.getName() << "Aform need at least " << Aform.getSignedGrade() << " grade to be signed and ";
out << Aform.getExecutionGrade() << " grade to be executed, ";
if (Aform.getIsSigned())
out << Aform.getName() << " Aform is signed\n";
else
out << Aform.getName() << " Aform isn't signed\n";
return (out);
}
int AForm::getSignedGrade() const
{
return (this->_signedGrade);
}
int AForm::getExecutionGrade() const
{
return (this->_executionGrade);
}
int AForm::getIsSigned() const
{
return (this->_isSigned);
}
const std::string AForm::getName() const
{
return (this->_name);
}
void AForm::beSigned(Bureaucrat &bureaucrat)
{
if (bureaucrat.getGrade() > this->_signedGrade)
throw AForm::signedGradeTooLowException();
else
{
this->_isSigned = 1;
std::cout << bureaucrat.getName() << " signed " << this->_name << " form\n";
}
}
void AForm::checkGrade() const
{
if (this->_signedGrade > 150 || this->_executionGrade > 150)
throw AForm::GradeTooLowException();
else if (this->_signedGrade < 1 || this->_executionGrade < 1)
throw AForm::GradeTooHighException();
}
void AForm::checkExecution(Bureaucrat const & executor) const
{
if (!this->_isSigned)
throw AForm::formIsNotSignedException();
else if (executor.getGrade() > this->_executionGrade)
throw AForm::executionGradeTooLowException();
}
const char* AForm::GradeTooLowException::what() const throw()
{
return ("Signed or Execution grade is too low\n");
}
const char* AForm::GradeTooHighException::what() const throw()
{
return ("Signed or Execution grade is too high\n");
}
const char* AForm::signedGradeTooLowException::what() const throw()
{
return ("Signed grade is too low\n");
}
const char* AForm::executionGradeTooLowException::what() const throw()
{
return ("Can't execute because Execution grade is too low\n");
}
const char* AForm::formIsNotSignedException::what() const throw()
{
return ("Can't execute because Form isn't signed\n");
}

83
cpp05/ex03/AForm.hpp Normal file
View File

@ -0,0 +1,83 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* AForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 11:54:09 by apommier #+# #+# */
/* Updated: 2022/07/19 12:52:43 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef AFORM_HPP
# define AFORM_HPP
# include <ostream>
# include <string>
# include <iostream>
# include "Bureaucrat.hpp"
class Bureaucrat;
class AForm{
public :
AForm();
AForm(int signedGrade, int executionGrade, std::string name);
AForm(const AForm& copy);
virtual ~AForm();
AForm &operator=(const AForm& rhs);
const std::string getName() const;
int getSignedGrade() const;
int getExecutionGrade() const;
int getIsSigned() const;
void checkGrade() const;
void beSigned(Bureaucrat &bureaucrat);
virtual void execute(Bureaucrat const & executor) const = 0;
void checkExecution(Bureaucrat const & executor) const;
class GradeTooLowException : public std::exception
{
public :
virtual const char* what() const throw();
};
class GradeTooHighException : public std::exception
{
public :
virtual const char* what() const throw();
};
class signedGradeTooLowException : public std::exception
{
public :
virtual const char* what() const throw();
};
class executionGradeTooLowException : public std::exception
{
public :
virtual const char* what() const throw();
};
class formIsNotSignedException : public std::exception
{
public :
virtual const char* what() const throw();
};
private :
std::string const _name;
bool _isSigned;
int _signedGrade;
int _executionGrade;
};
std::ostream &operator<<(std::ostream &out, const AForm &Aform);
#endif

View File

@ -0,0 +1,28 @@
.
. :
. . :: ::
. . ::: ::
: : ::::. ..
.. :: :: : ::. .:
: ::: ::: . :: ::: .:.
:: :::: . : :: ::: .::.
::: :: ::: : ::: .::.
`::. ::: ::. `::::.::.
`:::. :::. ::: :: :::::.
`:::. ::bd:: :::::.
`:::. :::. ::::.
`::. `:::. ::::
`:::. `::: ::::
:::. :::: ::::
::bd:::bd::::
::::::::::
::::::::
:::(o): . .
::o:::(...
`.. ::o::::
`):o::::
::(o):::
.::::::
:::::::.
:::::::::.
...::::::::::...

110
cpp05/ex03/Bureaucrat.cpp Normal file
View File

@ -0,0 +1,110 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/18 21:34:42 by apommier #+# #+# */
/* Updated: 2022/08/05 14:14:42 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
Bureaucrat::Bureaucrat() : _name("unnamed_bureaucrat")
{
this->_grade = 150;
}
Bureaucrat::Bureaucrat(int grade, std::string name) : _name(name)
{
this->_grade = grade;
this->checkGrade();
}
Bureaucrat::Bureaucrat(const Bureaucrat& copy) : _name(copy.getName())
{
this->_grade = copy.getGrade();
}
Bureaucrat::~Bureaucrat()
{
}
Bureaucrat &Bureaucrat::operator=(const Bureaucrat& rhs)
{
if (this != &rhs)
{
std::cout << "Only grade can be copied, name is const\n";
this->_grade = rhs.getGrade();
}
return (*this);
}
std::ostream &operator<<(std::ostream &out, const Bureaucrat &bureaucrat)
{
out << bureaucrat.getName() << ", bureaucrat grade " << bureaucrat.getGrade();
return (out);
}
const std::string Bureaucrat::getName() const
{
return (this->_name);
}
int Bureaucrat::getGrade() const
{
return (this->_grade);
}
void Bureaucrat::upGrade()
{
this->_grade--;
this->checkGrade();
}
void Bureaucrat::downGrade()
{
this->_grade++;
this->checkGrade();
}
void Bureaucrat::checkGrade() const
{
if (this->_grade > 150)
throw Bureaucrat::GradeTooLowException();
else if (this->_grade < 1)
throw Bureaucrat::GradeTooHighException();
}
void Bureaucrat::signForm(AForm &form)
{
try
{
form.beSigned(*this);
}
catch (std::exception &e)
{
std::cout << this->_name << " couldnt sign " << form.getName() << " form because "<< e.what();
}
}
void Bureaucrat::executeForm(AForm const & form) const
{
form.execute(*this);
std::cout << this->_name << " executed " << form.getName() << std::endl;
}
const char* Bureaucrat::GradeTooLowException::what() const throw()
{
return ("Bureaucrat grade is too low\n");
}
const char* Bureaucrat::GradeTooHighException::what() const throw()
{
return ("Bureaucrat grade is too high\n");
}

61
cpp05/ex03/Bureaucrat.hpp Normal file
View File

@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/18 21:34:43 by apommier #+# #+# */
/* Updated: 2022/08/05 12:47:03 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BUREAUCRAT_HPP
# define BUREAUCRAT_HPP
# include <ostream>
# include <string>
# include <iostream>
# include "AForm.hpp"
class AForm;
class Bureaucrat{
public:
Bureaucrat();
Bureaucrat(int grade, std::string name);
Bureaucrat(const Bureaucrat& copy);
~Bureaucrat();
Bureaucrat &operator=(const Bureaucrat& rhs);
const std::string getName() const;
int getGrade() const;
void upGrade();
void downGrade();
void checkGrade() const;
void signForm(AForm &form);
void executeForm(AForm const & form) const;
class GradeTooLowException : public std::exception
{
public :
virtual const char* what() const throw();
};
class GradeTooHighException : public std::exception
{
public :
virtual const char* what() const throw();
};
private:
std::string const _name;
int _grade;
};
std::ostream &operator<<(std::ostream &out, const Bureaucrat &bureaucrat);
#endif

65
cpp05/ex03/Intern.cpp Normal file
View File

@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Intern.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/05 12:02:52 by apommier #+# #+# */
/* Updated: 2022/08/05 12:02:54 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "Intern.hpp"
Intern::Intern()
{
}
Intern::Intern(const Intern& copy)
{
*this = copy;
}
Intern::~Intern()
{
}
Intern &Intern::operator=(const Intern& rhs)
{
(void)rhs;
return (*this);
}
AForm *Intern::makeForm(std::string formName, std::string target)
{
std::string formNames[3] = {"shrubbery creation", "robotomy request", "presidential pardon"};
int i = 0;
while (i < 3 && formNames[i] != formName)
i++;
AForm* ret = NULL;
switch (i)
{
case 0:
ret = new ShrubberyCreationForm(target);
break;
case 1:
ret = new RobotomyRequestForm(target);
break;
case 2:
ret = new PresidentialPardonForm(target);
break;
default:
break;
}
if (ret)
std::cout << "Intern creates " << formName << std::endl;
else
std::cout << "Intern say that " << formName << " form doesn't exist\n";
return (ret);
}

27
cpp05/ex03/Intern.hpp Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Intern.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/05 02:25:32 by apommier #+# #+# */
/* Updated: 2022/08/05 11:12:21 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "AForm.hpp"
#include "PresidentialPardonForm.hpp"
#include "RobotomyRequestForm.hpp"
#include "ShrubberyCreationForm.hpp"
class Intern{
public:
Intern();
Intern(const Intern& copy);
~Intern();
Intern &operator=(const Intern& rhs);
AForm *makeForm(std::string formName, std::string target);
};

44
cpp05/ex03/Makefile Normal file
View File

@ -0,0 +1,44 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/07/18 21:34:54 by apommier #+# #+# #
# Updated: 2022/08/05 11:08:29 by apommier ### ########.fr #
# #
# **************************************************************************** #
NAME = a.out
SRCS = main.cpp\
Bureaucrat.cpp\
AForm.cpp\
PresidentialPardonForm.cpp\
RobotomyRequestForm.cpp\
ShrubberyCreationForm.cpp\
Intern.cpp
OBJS = ${SRCS:.cpp=.o}
CC = c++
CFLAGS = -Wall -Wextra -Werror -std=c++98
RM = rm -rf
.cpp.o:
$(CC) ${CFLAGS} -c $< -o $(<:.cpp=.o)
${NAME}: ${OBJS}
${CC} ${LIB} ${OBJS} -o ${NAME}
all: ${NAME}
clean:
@${RM} ${OBJS}
fclean: clean
@${RM} ${NAME}
re: fclean all
.PHONY: all clean fclean re

View File

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PresidentialPardonForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/23 12:16:26 by apommier #+# #+# */
/* Updated: 2022/08/04 18:52:20 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "PresidentialPardonForm.hpp"
PresidentialPardonForm::PresidentialPardonForm() : AForm(25, 5, "PresidentialPardonForm")
{
this->_target = "anonymousTarget";
}
PresidentialPardonForm::PresidentialPardonForm(std::string target) : AForm(25, 5, "PresidentialPardonForm")
{
this->_target = target;
}
PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm& copy) : AForm(copy)
{
*this = copy;
}
PresidentialPardonForm::~PresidentialPardonForm()
{
}
PresidentialPardonForm &PresidentialPardonForm::operator=(const PresidentialPardonForm& rhs)
{
if (&rhs != this)
this->_target = rhs._target;
return (*this);
}
void PresidentialPardonForm::execute(Bureaucrat const & executor) const
{
AForm::checkExecution(executor);
std::cout << this->_target << " has been forgiven by Zaphod Beeblebrox\n";
}

View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PresidentialPardonForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/02 18:21:41 by apommier #+# #+# */
/* Updated: 2022/08/04 15:07:38 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PRESIDENTIALPARDONFORM_HPP
# define PRESIDENTIALPARDONFORM_HPP
# include "AForm.hpp"
class PresidentialPardonForm : public AForm{
public :
PresidentialPardonForm();
PresidentialPardonForm(std::string target);
PresidentialPardonForm(const PresidentialPardonForm& copy);
~PresidentialPardonForm();
PresidentialPardonForm &operator=(const PresidentialPardonForm& rhs);
void execute(Bureaucrat const & executor) const;
private :
std::string _target;
};
#endif

View File

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* RobotomyRequestForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/04 15:14:32 by apommier #+# #+# */
/* Updated: 2022/08/05 14:30:26 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "RobotomyRequestForm.hpp"
#include <cstdlib>
RobotomyRequestForm::RobotomyRequestForm() : AForm(72, 45, "RobotomyRequestForm")
{
this->_target = "Unknow Target";
}
RobotomyRequestForm::RobotomyRequestForm(std::string target) : AForm(72, 45, "RobotomyRequestForm")
{
this->_target = target;
}
RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm& copy) : AForm(copy)
{
*this = copy;
}
RobotomyRequestForm::~RobotomyRequestForm()
{
}
RobotomyRequestForm &RobotomyRequestForm::operator=(const RobotomyRequestForm& rhs)
{
if (&rhs != this)
this->_target = rhs._target;
return (*this);
}
void RobotomyRequestForm::execute(Bureaucrat const & executor) const
{
AForm::checkExecution(executor);
std::cout << "Brrrzrzrzrzzrzrzzrrzz...\n";
if (std::rand() % 2)
std::cout << this->_target << " has been robotomized\n";
else
std::cout << this->_target << " hasn't been robtomized\n";
}

View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* RobotomyRequestForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 13:00:11 by apommier #+# #+# */
/* Updated: 2022/07/23 12:30:09 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ROBOTOMYREQUESTFORM_HPP
# define ROBOTOMYREQUESTFORM_HPP
# include "AForm.hpp"
class RobotomyRequestForm : public AForm{
public :
RobotomyRequestForm();
RobotomyRequestForm(std::string target);
RobotomyRequestForm(const RobotomyRequestForm& copy);
~RobotomyRequestForm();
RobotomyRequestForm &operator=(const RobotomyRequestForm& rhs);
void execute(Bureaucrat const & executor) const;
private :
std::string _target;
};
#endif

View File

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ShrubberyCreationForm.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 13:00:14 by apommier #+# #+# */
/* Updated: 2022/08/05 14:33:22 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "ShrubberyCreationForm.hpp"
# include <iostream>
# include <fstream>
# include <sstream>
ShrubberyCreationForm::ShrubberyCreationForm() : AForm(145, 137, "RobotomyRequestForm")
{
this->_target = "Unknow Target";
}
ShrubberyCreationForm::ShrubberyCreationForm(std::string target) : AForm(145, 137, "RobotomyRequestForm")
{
this->_target = target;
}
ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm& copy) : AForm(copy)
{
*this = copy;
}
ShrubberyCreationForm::~ShrubberyCreationForm()
{
}
ShrubberyCreationForm &ShrubberyCreationForm::operator=(const ShrubberyCreationForm& rhs)
{
if (&rhs != this)
this->_target = rhs._target;
return (*this);
}
void ShrubberyCreationForm::execute(Bureaucrat const & executor) const
{
AForm::checkExecution(executor);
std::string name = this->_target + "_shrubbery";
(void)executor;
std::ofstream outfile(name.c_str());
outfile << " .\n . : \n . . :: :: \n . . ::: :: \n : : ::::. .. \n .. :: :: : ::. .:\n : ::: ::: . :: ::: .:.\n :: :::: . : :: ::: .::. \n ::: :: ::: : ::: .::.\n `::. ::: ::. `::::.::.\n `:::. :::. ::: :: :::::.\n `:::. ::bd:: :::::.\n `:::. :::. ::::. \n `::. `:::. :::: \n `:::. `::: :::: \n :::. :::: :::: \n ::bd:::bd:::: \n ::::::::::\n ::::::::\n :::(o): . . \n ::o:::(... \n `.. ::o:::: \n `):o:::: \n ::(o)::: \n .:::::: \n :::::::. \n :::::::::. \n ...::::::::::...";
}

View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ShrubberyCreationForm.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 13:00:12 by apommier #+# #+# */
/* Updated: 2022/08/04 18:54:10 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SHRUBBERYCREATIONFORM_HPP
# define SHRUBBERYCREATIONFORM_HPP
# include "AForm.hpp"
class ShrubberyCreationForm : public AForm{
public :
ShrubberyCreationForm();
ShrubberyCreationForm(std::string target);
ShrubberyCreationForm(const ShrubberyCreationForm& copy);
~ShrubberyCreationForm();
ShrubberyCreationForm &operator=(const ShrubberyCreationForm& rhs);
void execute(Bureaucrat const & executor) const;
private :
std::string _target;
};
#endif

View File

@ -0,0 +1,28 @@
.
. :
. . :: ::
. . ::: ::
: : ::::. ..
.. :: :: : ::. .:
: ::: ::: . :: ::: .:.
:: :::: . : :: ::: .::.
::: :: ::: : ::: .::.
`::. ::: ::. `::::.::.
`:::. :::. ::: :: :::::.
`:::. ::bd:: :::::.
`:::. :::. ::::.
`::. `:::. ::::
`:::. `::: ::::
:::. :::: ::::
::bd:::bd::::
::::::::::
::::::::
:::(o): . .
::o:::(...
`.. ::o::::
`):o::::
::(o):::
.::::::
:::::::.
:::::::::.
...::::::::::...

137
cpp05/ex03/main.cpp Normal file
View File

@ -0,0 +1,137 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/18 21:34:45 by apommier #+# #+# */
/* Updated: 2022/08/05 14:46:57 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "AForm.hpp"
#include "Bureaucrat.hpp"
#include "PresidentialPardonForm.hpp"
#include "RobotomyRequestForm.hpp"
#include "ShrubberyCreationForm.hpp"
int main()
{
std::cout << "===============================Presidential pardon=============================\n\n";
std::cout << "Trying to execute without sign :\n";
try
{
Bureaucrat john(150, "john");
PresidentialPardonForm form("Assassin");
john.executeForm(form);
}
catch(std::exception &e)
{
std::cout << e.what();
}
std::cout << "\nTrying to execute without the right grade :\n";
try
{
Bureaucrat john(25, "john");
PresidentialPardonForm form("Assassin");
form.beSigned(john);
john.executeForm(form);
}
catch(std::exception &e)
{
std::cout << e.what();
}
std::cout << "\nAll good :\n";
try
{
Bureaucrat john(5, "john");
PresidentialPardonForm form("Assassin");
form.beSigned(john);
john.executeForm(form);
}
catch(std::exception &e)
{
std::cout << e.what();
}
std::cout << "\n===============================Robotomy request=============================\n\n";
std::cout << "Trying to execute without sign :\n";
try
{
Bureaucrat john(150, "john");
RobotomyRequestForm form("Assassin");
john.executeForm(form);
}
catch(std::exception &e)
{
std::cout << e.what();
}
std::cout << "\nTrying to execute without the right grade :\n";
try
{
Bureaucrat john(46, "john");
RobotomyRequestForm form("Assassin");
form.beSigned(john);
john.executeForm(form);
}
catch(std::exception &e)
{
std::cout << e.what();
}
std::cout << "\nAll good :\n";
try
{
Bureaucrat john(5, "john");
RobotomyRequestForm form("Assassin");
form.beSigned(john);
john.executeForm(form);
}
catch(std::exception &e)
{
std::cout << e.what();
}
std::cout << "\n===============================Shrubbery Creation=============================\n\n";
std::cout << "Trying to execute without sign :\n";
try
{
Bureaucrat john(150, "john");
ShrubberyCreationForm form("Assassin");
john.executeForm(form);
}
catch(std::exception &e)
{
std::cout << e.what();
}
std::cout << "\nTrying to execute without the right grade :\n";
try
{
Bureaucrat john(137, "john");
ShrubberyCreationForm form("Assassin");
form.beSigned(john);
john.executeForm(form);
}
catch(std::exception &e)
{
std::cout << e.what();
}
std::cout << "\nAll good :\n";
try
{
Bureaucrat john(5, "john");
ShrubberyCreationForm form("Assassin");
form.beSigned(john);
john.executeForm(form);
}
catch(std::exception &e)
{
std::cout << e.what();
}
}

View File

@ -0,0 +1,28 @@
.
. :
. . :: ::
. . ::: ::
: : ::::. ..
.. :: :: : ::. .:
: ::: ::: . :: ::: .:.
:: :::: . : :: ::: .::.
::: :: ::: : ::: .::.
`::. ::: ::. `::::.::.
`:::. :::. ::: :: :::::.
`:::. ::bd:: :::::.
`:::. :::. ::::.
`::. `:::. ::::
`:::. `::: ::::
:::. :::: ::::
::bd:::bd::::
::::::::::
::::::::
:::(o): . .
::o:::(...
`.. ::o::::
`):o::::
::(o):::
.::::::
:::::::.
:::::::::.
...::::::::::...