push for save
This commit is contained in:
parent
93a5094a0a
commit
2fef2f8dfc
|
|
@ -1,4 +1,4 @@
|
|||
#include "Form.hpp"
|
||||
#include "AForm.hpp"
|
||||
#include "Bureaucrat.hpp"
|
||||
|
||||
Bureaucrat::Bureaucrat() : _name("default"), _grade(150)
|
||||
|
|
@ -72,7 +72,7 @@ const char *Bureaucrat::GradeTooLowException::what() const throw()
|
|||
return ("Grade too low!");
|
||||
}
|
||||
|
||||
void Bureaucrat::signForm(Form &form)
|
||||
void Bureaucrat::signForm(AForm &form)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
@ -84,4 +84,14 @@ void Bureaucrat::signForm(Form &form)
|
|||
return;
|
||||
}
|
||||
std::cout << _name << " signed " << form.getName() << std::endl;
|
||||
|
||||
}
|
||||
void Bureaucrat::executeForm(const AForm &form)
|
||||
{
|
||||
try {
|
||||
form.execute(*this);
|
||||
std::cout << _name << " executed " << form.getName() << std::endl;
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class Form;
|
||||
class AForm;
|
||||
|
||||
class Bureaucrat
|
||||
{
|
||||
|
|
@ -24,7 +24,8 @@ class Bureaucrat
|
|||
int getGrade() const;
|
||||
void incrementGrade();
|
||||
void decrementGrade();
|
||||
void signForm(Form &form);
|
||||
void signForm(AForm &form);
|
||||
void executeForm(const AForm &form);
|
||||
|
||||
class GradeTooHighException : public std::exception
|
||||
{
|
||||
|
|
|
|||
118
ex02/Main.cpp
118
ex02/Main.cpp
|
|
@ -1,102 +1,44 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: toi <toi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/15 10:00:00 by toi #+# #+# */
|
||||
/* Updated: 2025/01/15 10:00:00 by toi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "AForm.hpp"
|
||||
#include "Bureaucrat.hpp"
|
||||
#include "Form.hpp"
|
||||
#include "PresidentialPardonForm.hpp"
|
||||
#include "RobotomyRequestForm.hpp"
|
||||
#include "ShrubberyCreationForm.hpp"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
std::cout << "\033[35m" << std::endl << "Test ex01" << "\033[0m" << std::endl;
|
||||
srand(time(NULL));
|
||||
|
||||
std::cout << "\033[31m" << std::endl << "Test creation trop haut et trop bas" << "\033[0m" << std::endl;
|
||||
try
|
||||
{
|
||||
Bureaucrat Dormeur1("Bernard", 1500);
|
||||
}
|
||||
catch(const std::exception &e)
|
||||
{
|
||||
std::cerr << "Exception catch: " << e.what() << std::endl;
|
||||
}
|
||||
std::cout << "\033[34m" << std::endl << "Test ex02" << "\033[0m" << std::endl;
|
||||
|
||||
try
|
||||
{
|
||||
Bureaucrat Dormeur2("Olivier", -10);
|
||||
}
|
||||
catch(const std::exception &e)
|
||||
{
|
||||
std::cerr << "Exception catch: " << e.what() << std::endl;
|
||||
}
|
||||
std::cout << "\033[35m" << std::endl << "Test ex02 ShrubberyCreationForm" << "\033[0m" << std::endl;
|
||||
Bureaucrat Mr_Shrubby("Mr_Shrubby", 130);
|
||||
ShrubberyCreationForm Shrubby_form("Hello");
|
||||
std::cout << std::endl;
|
||||
std::cout << Shrubby_form;
|
||||
Mr_Shrubby.signForm(Shrubby_form);
|
||||
std::cout << Shrubby_form;
|
||||
Mr_Shrubby.executeForm(Shrubby_form);
|
||||
|
||||
std::cout << "\033[32m" << std::endl << "Test augmentation" << "\033[0m" << std::endl;
|
||||
Bureaucrat robert("Robert", 2);
|
||||
std::cout << robert;
|
||||
try
|
||||
{
|
||||
robert.incrementGrade();
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
std::cerr << "Exception catch: " << e.what() << std::endl;
|
||||
}
|
||||
std::cout << robert;
|
||||
std::cout << "\033[32m" << std::endl << "Test ex02 RobotomyRequestForm" << "\033[0m" << std::endl;
|
||||
RobotomyRequestForm Robo_form("I am a robo form");
|
||||
Bureaucrat Mr_Robo("Mr_Robo", 45);
|
||||
|
||||
try
|
||||
{
|
||||
robert.incrementGrade();
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
std::cerr << "Exception catch: " << e.what() << std::endl;
|
||||
}
|
||||
std::cout << robert;
|
||||
Mr_Robo.executeForm(Robo_form);
|
||||
Mr_Robo.signForm(Robo_form);
|
||||
Mr_Robo.executeForm(Robo_form);
|
||||
Mr_Robo.executeForm(Robo_form);
|
||||
Mr_Robo.executeForm(Robo_form);
|
||||
|
||||
std::cout << "\033[33m" << std::endl << "Test ex02 PresidentialPardonForm" << "\033[0m" << std::endl;
|
||||
PresidentialPardonForm President_form("I am a robo form");
|
||||
Bureaucrat Mr_President("Mr_President", 5);
|
||||
|
||||
Mr_Robo.executeForm(President_form);
|
||||
Mr_Robo.signForm(President_form);
|
||||
|
||||
std::cout << "\033[33m" << std::endl << "Test diminution" << "\033[0m" << std::endl;
|
||||
Bureaucrat thomas("Thomas", 149);
|
||||
std::cout << thomas;
|
||||
try
|
||||
{
|
||||
thomas.decrementGrade();
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
std::cerr << "Exception catch: " << e.what() << std::endl;
|
||||
}
|
||||
std::cout << thomas;
|
||||
|
||||
try
|
||||
{
|
||||
thomas.decrementGrade();
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
std::cerr << "Exception catch: " << e.what() << std::endl;
|
||||
}
|
||||
std::cout << thomas;
|
||||
|
||||
|
||||
std::cout << "\033[34m" << std::endl << "Test signatures de formulaires" << "\033[0m" << std::endl;
|
||||
|
||||
Form formulaire_id("FORMULAIRE IDENTITE", 100, 90);
|
||||
Bureaucrat mr_lent;
|
||||
Bureaucrat mr_identite("MR_IDENTITE", 100);
|
||||
std::cout << formulaire_id;
|
||||
|
||||
mr_lent.signForm(formulaire_id);
|
||||
std::cout << formulaire_id;
|
||||
|
||||
mr_identite.signForm(formulaire_id);
|
||||
std::cout << formulaire_id;
|
||||
Mr_President.executeForm(President_form);
|
||||
Mr_President.signForm(President_form);
|
||||
Mr_President.executeForm(President_form);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
CXX = c++
|
||||
CXXFLAGS = -Wall -Wextra -Werror -std=c++98
|
||||
OBJDIR = obj
|
||||
SOURCES = Main.cpp Bureaucrat.cpp AForm.cpp
|
||||
SOURCES = Main.cpp Bureaucrat.cpp AForm.cpp PresidentialPardonForm.cpp RobotomyRequestForm.cpp ShrubberyCreationForm.cpp
|
||||
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
|
||||
NAME = Form
|
||||
NAME = AForm
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,42 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* PresidentialPardonForm.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cgodecke <cgodecke@student.42wolfsburg. +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/10 10:40:41 by cgodecke #+# #+# */
|
||||
/* Updated: 2023/11/13 15:40:07 by cgodecke ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "PresidentialPardonForm.hpp"
|
||||
#include <string>
|
||||
PresidentialPardonForm::PresidentialPardonForm() : AForm("PresidentialPardonForm", 25, 5), _target("default target")
|
||||
{
|
||||
}
|
||||
|
||||
PresidentialPardonForm::PresidentialPardonForm(const std::string target) : AForm("PresidentialPardonForm", 25, 5), _target(target)
|
||||
{
|
||||
}
|
||||
|
||||
PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm &other) : AForm(other)
|
||||
{
|
||||
*this = other;
|
||||
}
|
||||
|
||||
PresidentialPardonForm &PresidentialPardonForm::operator=(const PresidentialPardonForm &other)
|
||||
{
|
||||
_target = other._target;
|
||||
setIsSigned(other.getSigned());
|
||||
return (*this);
|
||||
}
|
||||
|
||||
PresidentialPardonForm::~PresidentialPardonForm()
|
||||
{
|
||||
}
|
||||
|
||||
void PresidentialPardonForm::performAction() const
|
||||
{
|
||||
std::cout << getName() << " has been pardoned by Zaphod Beeblebrox" << std::endl;
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
#ifndef PRESIDENTIALFORM_HPP
|
||||
#define PRESIDENTIALFORM_HPP
|
||||
|
||||
#include "AForm.hpp"
|
||||
#include <fstream>
|
||||
|
||||
class PresidentialPardonForm : public AForm
|
||||
{
|
||||
private:
|
||||
std::string _target;
|
||||
virtual void performAction() const;
|
||||
|
||||
public:
|
||||
// Constructors
|
||||
PresidentialPardonForm();
|
||||
PresidentialPardonForm(const std::string target);
|
||||
PresidentialPardonForm(const PresidentialPardonForm &other);
|
||||
PresidentialPardonForm &operator=(const PresidentialPardonForm &other);
|
||||
~PresidentialPardonForm();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
#include "RobotomyRequestForm.hpp"
|
||||
#include <string>
|
||||
|
||||
RobotomyRequestForm::RobotomyRequestForm() : AForm("RobotomyRequestForm", 72, 45), _target("default target")
|
||||
{
|
||||
}
|
||||
|
||||
RobotomyRequestForm::RobotomyRequestForm(const std::string target) : AForm("RobotomyRequestForm", 72, 45), _target(target)
|
||||
{
|
||||
}
|
||||
|
||||
RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm &other) : AForm(other)
|
||||
{
|
||||
*this = other;
|
||||
}
|
||||
|
||||
RobotomyRequestForm &RobotomyRequestForm::operator=(const RobotomyRequestForm &other)
|
||||
{
|
||||
_target = other._target;
|
||||
setIsSigned(other.getSigned());
|
||||
return (*this);
|
||||
}
|
||||
|
||||
RobotomyRequestForm::~RobotomyRequestForm()
|
||||
{
|
||||
}
|
||||
|
||||
void RobotomyRequestForm::performAction() const
|
||||
{
|
||||
int rdm;
|
||||
|
||||
rdm = rand() % 2;
|
||||
|
||||
if (rdm == 1)
|
||||
std::cout << "Beep Boop" << getName() << " has been robotomized successfully." << std::endl;
|
||||
else
|
||||
throw RobotizationFailed();
|
||||
}
|
||||
|
||||
const char *RobotomyRequestForm::RobotizationFailed::what() const throw()
|
||||
{
|
||||
return ("Robotomy failed.");
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
#ifndef ROBOTOMYREQUESTFORM_HPP
|
||||
#define ROBOTOMYREQUESTFORM_HPP
|
||||
|
||||
#include "AForm.hpp"
|
||||
#include <fstream>
|
||||
#include <stdlib.h> /* srand, rand */
|
||||
#include <time.h> /* time */
|
||||
|
||||
class RobotomyRequestForm : public AForm
|
||||
{
|
||||
private:
|
||||
std::string _target;
|
||||
virtual void performAction() const;
|
||||
|
||||
public:
|
||||
// Constructors
|
||||
RobotomyRequestForm();
|
||||
RobotomyRequestForm(const std::string target);
|
||||
RobotomyRequestForm(const RobotomyRequestForm &other);
|
||||
RobotomyRequestForm &operator=(const RobotomyRequestForm &other);
|
||||
~RobotomyRequestForm();
|
||||
|
||||
// exceptions
|
||||
class RobotizationFailed : public std::exception
|
||||
{
|
||||
public:
|
||||
virtual const char *what() const throw();
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
#include "ShrubberyCreationForm.hpp"
|
||||
#include <string>
|
||||
ShrubberyCreationForm::ShrubberyCreationForm() : AForm("ShrubberyCreationForm", 145, 137), _target("default target")
|
||||
{
|
||||
}
|
||||
|
||||
ShrubberyCreationForm::ShrubberyCreationForm(const std::string target) : AForm("ShrubberyCreationForm", 145, 137), _target(target)
|
||||
{
|
||||
}
|
||||
|
||||
ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm &other) : AForm(other)
|
||||
{
|
||||
*this = other;
|
||||
}
|
||||
|
||||
ShrubberyCreationForm &ShrubberyCreationForm::operator=(const ShrubberyCreationForm &other)
|
||||
{
|
||||
_target = other._target;
|
||||
setIsSigned(other.getSigned());
|
||||
return (*this);
|
||||
}
|
||||
|
||||
ShrubberyCreationForm::~ShrubberyCreationForm()
|
||||
{
|
||||
}
|
||||
|
||||
void ShrubberyCreationForm::performAction() const
|
||||
{
|
||||
std::ofstream outFile((_target + std::string("_shrubbery")).c_str());
|
||||
|
||||
if (outFile) {
|
||||
outFile << " _-_\n";
|
||||
outFile << " /~~ ~~\\\n";
|
||||
outFile << " /~~ ~~\\\n";
|
||||
outFile << "{ }\n";
|
||||
outFile << " \\ _- -_ /\n";
|
||||
outFile << " ~ \\\\ // ~\n";
|
||||
outFile << "_- - | | _- _\n";
|
||||
outFile << " _ - | | -_\n";
|
||||
outFile << " // \\\\\n";
|
||||
|
||||
outFile.close();
|
||||
} else
|
||||
throw ShrubberyCreationForm::OpenFileExeption();
|
||||
}
|
||||
|
||||
const char *ShrubberyCreationForm::OpenFileExeption::what() const throw()
|
||||
{
|
||||
return ("Could not open the file!");
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
#ifndef SHRUBBERYCREATIONFORM_HPP
|
||||
#define SHRUBBERYCREATIONFORM_HPP
|
||||
|
||||
#include "AForm.hpp"
|
||||
#include <fstream>
|
||||
|
||||
class ShrubberyCreationForm : public AForm
|
||||
{
|
||||
private:
|
||||
std::string _target;
|
||||
virtual void performAction() const;
|
||||
|
||||
public:
|
||||
// Constructors
|
||||
ShrubberyCreationForm();
|
||||
ShrubberyCreationForm(const std::string target);
|
||||
ShrubberyCreationForm(const ShrubberyCreationForm &other);
|
||||
ShrubberyCreationForm &operator=(const ShrubberyCreationForm &other);
|
||||
~ShrubberyCreationForm();
|
||||
|
||||
// exceptions
|
||||
class OpenFileExeption : public std::exception
|
||||
{
|
||||
public:
|
||||
virtual const char *what() const throw();
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,88 @@
|
|||
#include "AForm.hpp"
|
||||
#include "Bureaucrat.hpp"
|
||||
|
||||
AForm::AForm() : _name("Default"), _signed(false), _grade_ex(150), _grade_si(150)
|
||||
{
|
||||
}
|
||||
|
||||
AForm::AForm(const std::string name, const int grade_si, const int grade_ex) : _name(name), _signed(false), _grade_ex(grade_ex), _grade_si(grade_si)
|
||||
{
|
||||
}
|
||||
|
||||
AForm::AForm(const AForm &other) : _name(other._name), _signed(other._signed), _grade_ex(other._grade_ex), _grade_si(other._grade_si)
|
||||
{
|
||||
}
|
||||
|
||||
AForm::~AForm()
|
||||
{
|
||||
}
|
||||
|
||||
std::string AForm::getName() const
|
||||
{
|
||||
return (_name);
|
||||
}
|
||||
|
||||
bool AForm::getSigned() const
|
||||
{
|
||||
return (_signed);
|
||||
}
|
||||
|
||||
int AForm::getExecuteGrade() const
|
||||
{
|
||||
return (_grade_ex);
|
||||
}
|
||||
|
||||
int AForm::getSignGrade() const
|
||||
{
|
||||
return (_grade_si);
|
||||
}
|
||||
|
||||
void AForm::beSigned(const Bureaucrat &bureaucrat)
|
||||
{
|
||||
if (bureaucrat.getGrade() <= _grade_si)
|
||||
_signed = true;
|
||||
else
|
||||
throw GradeTooLowException();
|
||||
}
|
||||
|
||||
AForm &AForm::operator=(const AForm &other)
|
||||
{
|
||||
_signed = other._signed;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
const char *AForm::GradeTooHighException::what() const throw()
|
||||
{
|
||||
return ("Grade too high!");
|
||||
}
|
||||
|
||||
const char *AForm::GradeTooLowException::what() const throw()
|
||||
{
|
||||
return ("Grade too low!");
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, AForm const &AForm)
|
||||
{
|
||||
os << "Name: " << AForm.getName() << " Signed: " << AForm.getSigned() << " SiGrade: " << AForm.getSignGrade() << " ExGrade: " << AForm.getExecuteGrade() << std::endl;
|
||||
return (os);
|
||||
}
|
||||
|
||||
void AForm::setIsSigned(bool i_signed)
|
||||
{
|
||||
_signed = i_signed;
|
||||
}
|
||||
|
||||
void AForm::execute(const Bureaucrat &executor) const
|
||||
{
|
||||
if (_signed == false)
|
||||
throw IsNotSignedException();
|
||||
if (_grade_ex < executor.getGrade())
|
||||
throw GradeTooLowException();
|
||||
|
||||
performAction();
|
||||
}
|
||||
|
||||
const char *AForm::IsNotSignedException::what() const throw()
|
||||
{
|
||||
return ("The form cannot be executed because it is not signed!");
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
#ifndef AFORM_HPP
|
||||
#define AFORM_HPP
|
||||
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class Bureaucrat;
|
||||
|
||||
class AForm
|
||||
{
|
||||
private:
|
||||
const std::string _name;
|
||||
bool _signed;
|
||||
const int _grade_ex;
|
||||
const int _grade_si;
|
||||
virtual void performAction() const = 0;
|
||||
|
||||
public:
|
||||
//cons
|
||||
AForm();
|
||||
AForm(const std::string name, const int grade_si, const int grade_ex);
|
||||
AForm(const AForm &other);
|
||||
~AForm();
|
||||
AForm &operator=(const AForm &other);
|
||||
|
||||
//mem
|
||||
void beSigned(const Bureaucrat &bureaucrat);
|
||||
std::string getName() const;
|
||||
bool getSigned() const;
|
||||
int getExecuteGrade() const;
|
||||
int getSignGrade() const;
|
||||
void setIsSigned(bool i_signed);
|
||||
void execute(const Bureaucrat &executor) const;
|
||||
|
||||
//exept
|
||||
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 IsNotSignedException : public std::exception
|
||||
{
|
||||
public:
|
||||
virtual const char *what() const throw();
|
||||
};
|
||||
};
|
||||
std::ostream &operator<<(std::ostream &os, AForm const &AForm);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,97 @@
|
|||
#include "AForm.hpp"
|
||||
#include "Bureaucrat.hpp"
|
||||
|
||||
Bureaucrat::Bureaucrat() : _name("default"), _grade(150)
|
||||
{
|
||||
}
|
||||
|
||||
Bureaucrat::Bureaucrat(const Bureaucrat ©) : _name(copy._name), _grade(copy._grade)
|
||||
{
|
||||
}
|
||||
|
||||
Bureaucrat::Bureaucrat(const std::string name, int grade) : _name(name)
|
||||
{
|
||||
if (grade < 1)
|
||||
throw GradeTooHighException();
|
||||
else if (grade > 150)
|
||||
throw GradeTooLowException();
|
||||
else
|
||||
_grade = grade;
|
||||
}
|
||||
|
||||
Bureaucrat &Bureaucrat::operator=(const Bureaucrat &other)
|
||||
{
|
||||
if (this != &other)
|
||||
_grade = other._grade;
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Bureaucrat::~Bureaucrat()
|
||||
{
|
||||
}
|
||||
|
||||
std::string Bureaucrat::getName() const
|
||||
{
|
||||
return (_name);
|
||||
}
|
||||
|
||||
int Bureaucrat::getGrade() const
|
||||
{
|
||||
return (_grade);
|
||||
}
|
||||
|
||||
void Bureaucrat::incrementGrade()
|
||||
{
|
||||
if (_grade == 1)
|
||||
throw GradeTooHighException();
|
||||
else
|
||||
_grade--;
|
||||
}
|
||||
|
||||
void Bureaucrat::decrementGrade()
|
||||
{
|
||||
if (_grade == 150)
|
||||
throw GradeTooLowException();
|
||||
else
|
||||
_grade++;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, Bureaucrat const &other)
|
||||
{
|
||||
os << other.getName() << ", bureaucrat grade " << other.getGrade() << std::endl;
|
||||
return (os);
|
||||
}
|
||||
|
||||
const char *Bureaucrat::GradeTooHighException::what() const throw()
|
||||
{
|
||||
return ("Grade too high!");
|
||||
}
|
||||
|
||||
const char *Bureaucrat::GradeTooLowException::what() const throw()
|
||||
{
|
||||
return ("Grade too low!");
|
||||
}
|
||||
|
||||
void Bureaucrat::signForm(AForm &form)
|
||||
{
|
||||
try
|
||||
{
|
||||
form.beSigned(*this);
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
std::cout << _name << " couldn't sign " << form.getName() << " because " << e.what() << std::endl;
|
||||
return;
|
||||
}
|
||||
std::cout << _name << " signed " << form.getName() << std::endl;
|
||||
|
||||
}
|
||||
void Bureaucrat::executeForm(const AForm &form)
|
||||
{
|
||||
try {
|
||||
form.execute(*this);
|
||||
std::cout << _name << " executed " << form.getName() << std::endl;
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
#ifndef BUREAUCRAT_HPP
|
||||
#define BUREAUCRAT_HPP
|
||||
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class AForm;
|
||||
|
||||
class Bureaucrat
|
||||
{
|
||||
private:
|
||||
const std::string _name;
|
||||
int _grade;
|
||||
|
||||
public:
|
||||
Bureaucrat();
|
||||
Bureaucrat(const std::string _name, int _grade);
|
||||
Bureaucrat(const Bureaucrat ©);
|
||||
Bureaucrat &operator=(const Bureaucrat &other);
|
||||
~Bureaucrat();
|
||||
|
||||
std::string getName() const;
|
||||
int getGrade() const;
|
||||
void incrementGrade();
|
||||
void decrementGrade();
|
||||
void signForm(AForm &form);
|
||||
void executeForm(const AForm &form);
|
||||
|
||||
class GradeTooHighException : public std::exception
|
||||
{
|
||||
public:
|
||||
virtual const char *what() const throw();
|
||||
};
|
||||
class GradeTooLowException : public std::exception
|
||||
{
|
||||
public:
|
||||
virtual const char *what() const throw();
|
||||
};
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, Bureaucrat const &other);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
#include "AForm.hpp"
|
||||
#include "Bureaucrat.hpp"
|
||||
#include "PresidentialPardonForm.hpp"
|
||||
#include "RobotomyRequestForm.hpp"
|
||||
#include "ShrubberyCreationForm.hpp"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
srand(time(NULL));
|
||||
|
||||
std::cout << "\033[34m" << std::endl << "Test ex02" << "\033[0m" << std::endl;
|
||||
|
||||
std::cout << "\033[35m" << std::endl << "Test ex02 ShrubberyCreationForm" << "\033[0m" << std::endl;
|
||||
Bureaucrat Mr_Shrubby("Mr_Shrubby", 130);
|
||||
ShrubberyCreationForm Shrubby_form("Hello");
|
||||
std::cout << std::endl;
|
||||
std::cout << Shrubby_form;
|
||||
Mr_Shrubby.signForm(Shrubby_form);
|
||||
std::cout << Shrubby_form;
|
||||
Mr_Shrubby.executeForm(Shrubby_form);
|
||||
|
||||
std::cout << "\033[32m" << std::endl << "Test ex02 RobotomyRequestForm" << "\033[0m" << std::endl;
|
||||
RobotomyRequestForm Robo_form("I am a robo form");
|
||||
Bureaucrat Mr_Robo("Mr_Robo", 45);
|
||||
|
||||
Mr_Robo.executeForm(Robo_form);
|
||||
Mr_Robo.signForm(Robo_form);
|
||||
Mr_Robo.executeForm(Robo_form);
|
||||
Mr_Robo.executeForm(Robo_form);
|
||||
Mr_Robo.executeForm(Robo_form);
|
||||
|
||||
std::cout << "\033[33m" << std::endl << "Test ex02 PresidentialPardonForm" << "\033[0m" << std::endl;
|
||||
PresidentialPardonForm President_form("I am a robo form");
|
||||
Bureaucrat Mr_President("Mr_President", 5);
|
||||
|
||||
Mr_Robo.executeForm(President_form);
|
||||
Mr_Robo.signForm(President_form);
|
||||
|
||||
Mr_President.executeForm(President_form);
|
||||
Mr_President.signForm(President_form);
|
||||
Mr_President.executeForm(President_form);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
CXX = c++
|
||||
CXXFLAGS = -Wall -Wextra -Werror -std=c++98
|
||||
OBJDIR = obj
|
||||
SOURCES = Main.cpp Bureaucrat.cpp AForm.cpp PresidentialPardonForm.cpp RobotomyRequestForm.cpp ShrubberyCreationForm.cpp
|
||||
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
|
||||
NAME = AForm
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(OBJDIR):
|
||||
@echo "📁 Creating obj directory..."
|
||||
@mkdir -p $(OBJDIR)
|
||||
|
||||
$(OBJDIR)/%.o: %.cpp | $(OBJDIR)
|
||||
@echo "🧠 Compiling $< ..."
|
||||
@$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||
@echo "✅ $@ ready!"
|
||||
|
||||
$(NAME): $(OBJECTS)
|
||||
@echo "🔗 Linking $(NAME) ..."
|
||||
@$(CXX) $(CXXFLAGS) $(OBJECTS) -o $(NAME)
|
||||
@echo "🎉 $(NAME) is ready!"
|
||||
|
||||
clean:
|
||||
@echo "🧹 Cleaning object files..."
|
||||
@rm -rf $(OBJDIR)
|
||||
@echo "✨ Objects cleaned!"
|
||||
|
||||
fclean: clean
|
||||
@echo "🗑️ Removing $(NAME)..."
|
||||
@rm -f $(NAME)
|
||||
@echo "💀 Full clean complete!"
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* PresidentialPardonForm.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cgodecke <cgodecke@student.42wolfsburg. +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/10 10:40:41 by cgodecke #+# #+# */
|
||||
/* Updated: 2023/11/13 15:40:07 by cgodecke ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "PresidentialPardonForm.hpp"
|
||||
#include <string>
|
||||
PresidentialPardonForm::PresidentialPardonForm() : AForm("PresidentialPardonForm", 25, 5), _target("default target")
|
||||
{
|
||||
}
|
||||
|
||||
PresidentialPardonForm::PresidentialPardonForm(const std::string target) : AForm("PresidentialPardonForm", 25, 5), _target(target)
|
||||
{
|
||||
}
|
||||
|
||||
PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm &other) : AForm(other)
|
||||
{
|
||||
*this = other;
|
||||
}
|
||||
|
||||
PresidentialPardonForm &PresidentialPardonForm::operator=(const PresidentialPardonForm &other)
|
||||
{
|
||||
_target = other._target;
|
||||
setIsSigned(other.getSigned());
|
||||
return (*this);
|
||||
}
|
||||
|
||||
PresidentialPardonForm::~PresidentialPardonForm()
|
||||
{
|
||||
}
|
||||
|
||||
void PresidentialPardonForm::performAction() const
|
||||
{
|
||||
std::cout << getName() << " has been pardoned by Zaphod Beeblebrox" << std::endl;
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
#ifndef PRESIDENTIALFORM_HPP
|
||||
#define PRESIDENTIALFORM_HPP
|
||||
|
||||
#include "AForm.hpp"
|
||||
#include <fstream>
|
||||
|
||||
class PresidentialPardonForm : public AForm
|
||||
{
|
||||
private:
|
||||
std::string _target;
|
||||
virtual void performAction() const;
|
||||
|
||||
public:
|
||||
// Constructors
|
||||
PresidentialPardonForm();
|
||||
PresidentialPardonForm(const std::string target);
|
||||
PresidentialPardonForm(const PresidentialPardonForm &other);
|
||||
PresidentialPardonForm &operator=(const PresidentialPardonForm &other);
|
||||
~PresidentialPardonForm();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
#include "RobotomyRequestForm.hpp"
|
||||
#include <string>
|
||||
|
||||
RobotomyRequestForm::RobotomyRequestForm() : AForm("RobotomyRequestForm", 72, 45), _target("default target")
|
||||
{
|
||||
}
|
||||
|
||||
RobotomyRequestForm::RobotomyRequestForm(const std::string target) : AForm("RobotomyRequestForm", 72, 45), _target(target)
|
||||
{
|
||||
}
|
||||
|
||||
RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm &other) : AForm(other)
|
||||
{
|
||||
*this = other;
|
||||
}
|
||||
|
||||
RobotomyRequestForm &RobotomyRequestForm::operator=(const RobotomyRequestForm &other)
|
||||
{
|
||||
_target = other._target;
|
||||
setIsSigned(other.getSigned());
|
||||
return (*this);
|
||||
}
|
||||
|
||||
RobotomyRequestForm::~RobotomyRequestForm()
|
||||
{
|
||||
}
|
||||
|
||||
void RobotomyRequestForm::performAction() const
|
||||
{
|
||||
int rdm;
|
||||
|
||||
rdm = rand() % 2;
|
||||
|
||||
if (rdm == 1)
|
||||
std::cout << "Beep Boop" << getName() << " has been robotomized successfully." << std::endl;
|
||||
else
|
||||
throw RobotizationFailed();
|
||||
}
|
||||
|
||||
const char *RobotomyRequestForm::RobotizationFailed::what() const throw()
|
||||
{
|
||||
return ("Robotomy failed.");
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
#ifndef ROBOTOMYREQUESTFORM_HPP
|
||||
#define ROBOTOMYREQUESTFORM_HPP
|
||||
|
||||
#include "AForm.hpp"
|
||||
#include <fstream>
|
||||
#include <stdlib.h> /* srand, rand */
|
||||
#include <time.h> /* time */
|
||||
|
||||
class RobotomyRequestForm : public AForm
|
||||
{
|
||||
private:
|
||||
std::string _target;
|
||||
virtual void performAction() const;
|
||||
|
||||
public:
|
||||
// Constructors
|
||||
RobotomyRequestForm();
|
||||
RobotomyRequestForm(const std::string target);
|
||||
RobotomyRequestForm(const RobotomyRequestForm &other);
|
||||
RobotomyRequestForm &operator=(const RobotomyRequestForm &other);
|
||||
~RobotomyRequestForm();
|
||||
|
||||
// exceptions
|
||||
class RobotizationFailed : public std::exception
|
||||
{
|
||||
public:
|
||||
virtual const char *what() const throw();
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
#include "ShrubberyCreationForm.hpp"
|
||||
#include <string>
|
||||
ShrubberyCreationForm::ShrubberyCreationForm() : AForm("ShrubberyCreationForm", 145, 137), _target("default target")
|
||||
{
|
||||
}
|
||||
|
||||
ShrubberyCreationForm::ShrubberyCreationForm(const std::string target) : AForm("ShrubberyCreationForm", 145, 137), _target(target)
|
||||
{
|
||||
}
|
||||
|
||||
ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm &other) : AForm(other)
|
||||
{
|
||||
*this = other;
|
||||
}
|
||||
|
||||
ShrubberyCreationForm &ShrubberyCreationForm::operator=(const ShrubberyCreationForm &other)
|
||||
{
|
||||
_target = other._target;
|
||||
setIsSigned(other.getSigned());
|
||||
return (*this);
|
||||
}
|
||||
|
||||
ShrubberyCreationForm::~ShrubberyCreationForm()
|
||||
{
|
||||
}
|
||||
|
||||
void ShrubberyCreationForm::performAction() const
|
||||
{
|
||||
std::ofstream outFile((_target + std::string("_shrubbery")).c_str());
|
||||
|
||||
if (outFile) {
|
||||
outFile << " _-_\n";
|
||||
outFile << " /~~ ~~\\\n";
|
||||
outFile << " /~~ ~~\\\n";
|
||||
outFile << "{ }\n";
|
||||
outFile << " \\ _- -_ /\n";
|
||||
outFile << " ~ \\\\ // ~\n";
|
||||
outFile << "_- - | | _- _\n";
|
||||
outFile << " _ - | | -_\n";
|
||||
outFile << " // \\\\\n";
|
||||
|
||||
outFile.close();
|
||||
} else
|
||||
throw ShrubberyCreationForm::OpenFileExeption();
|
||||
}
|
||||
|
||||
const char *ShrubberyCreationForm::OpenFileExeption::what() const throw()
|
||||
{
|
||||
return ("Could not open the file!");
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
#ifndef SHRUBBERYCREATIONFORM_HPP
|
||||
#define SHRUBBERYCREATIONFORM_HPP
|
||||
|
||||
#include "AForm.hpp"
|
||||
#include <fstream>
|
||||
|
||||
class ShrubberyCreationForm : public AForm
|
||||
{
|
||||
private:
|
||||
std::string _target;
|
||||
virtual void performAction() const;
|
||||
|
||||
public:
|
||||
// Constructors
|
||||
ShrubberyCreationForm();
|
||||
ShrubberyCreationForm(const std::string target);
|
||||
ShrubberyCreationForm(const ShrubberyCreationForm &other);
|
||||
ShrubberyCreationForm &operator=(const ShrubberyCreationForm &other);
|
||||
~ShrubberyCreationForm();
|
||||
|
||||
// exceptions
|
||||
class OpenFileExeption : public std::exception
|
||||
{
|
||||
public:
|
||||
virtual const char *what() const throw();
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
Loading…
Reference in New Issue