From 2fef2f8dfc541ffa6b20cb1bdf73d22043bc876a Mon Sep 17 00:00:00 2001 From: lfirmin Date: Tue, 18 Nov 2025 20:51:13 +0100 Subject: [PATCH] push for save --- ex02/Bureaucrat.cpp | 14 +++- ex02/Bureaucrat.hpp | 7 +- ex02/Main.cpp | 118 ++++++++------------------------ ex02/Makefile | 4 +- ex02/PresidentialPardonForm.cpp | 42 ++++++++++++ ex02/PresidentialPardonForm.hpp | 22 ++++++ ex02/RobotomyRequestForm.cpp | 43 ++++++++++++ ex02/RobotomyRequestForm.hpp | 31 +++++++++ ex02/ShrubberyCreationForm.cpp | 50 ++++++++++++++ ex02/ShrubberyCreationForm.hpp | 29 ++++++++ ex03/AForm.cpp | 88 ++++++++++++++++++++++++ ex03/AForm.hpp | 57 +++++++++++++++ ex03/Bureaucrat.cpp | 97 ++++++++++++++++++++++++++ ex03/Bureaucrat.hpp | 44 ++++++++++++ ex03/Main.cpp | 44 ++++++++++++ ex03/Makefile | 36 ++++++++++ ex03/PresidentialPardonForm.cpp | 42 ++++++++++++ ex03/PresidentialPardonForm.hpp | 22 ++++++ ex03/RobotomyRequestForm.cpp | 43 ++++++++++++ ex03/RobotomyRequestForm.hpp | 31 +++++++++ ex03/ShrubberyCreationForm.cpp | 50 ++++++++++++++ ex03/ShrubberyCreationForm.hpp | 29 ++++++++ 22 files changed, 848 insertions(+), 95 deletions(-) create mode 100644 ex02/PresidentialPardonForm.cpp create mode 100644 ex02/PresidentialPardonForm.hpp create mode 100644 ex02/RobotomyRequestForm.cpp create mode 100644 ex02/RobotomyRequestForm.hpp create mode 100644 ex02/ShrubberyCreationForm.cpp create mode 100644 ex02/ShrubberyCreationForm.hpp create mode 100644 ex03/AForm.cpp create mode 100644 ex03/AForm.hpp create mode 100644 ex03/Bureaucrat.cpp create mode 100644 ex03/Bureaucrat.hpp create mode 100644 ex03/Main.cpp create mode 100644 ex03/Makefile create mode 100644 ex03/PresidentialPardonForm.cpp create mode 100644 ex03/PresidentialPardonForm.hpp create mode 100644 ex03/RobotomyRequestForm.cpp create mode 100644 ex03/RobotomyRequestForm.hpp create mode 100644 ex03/ShrubberyCreationForm.cpp create mode 100644 ex03/ShrubberyCreationForm.hpp diff --git a/ex02/Bureaucrat.cpp b/ex02/Bureaucrat.cpp index 700237d..fe77b9a 100644 --- a/ex02/Bureaucrat.cpp +++ b/ex02/Bureaucrat.cpp @@ -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'; + } } diff --git a/ex02/Bureaucrat.hpp b/ex02/Bureaucrat.hpp index 045fb49..42c982e 100644 --- a/ex02/Bureaucrat.hpp +++ b/ex02/Bureaucrat.hpp @@ -5,7 +5,7 @@ #include #include -class Form; +class AForm; class Bureaucrat { @@ -18,13 +18,14 @@ class Bureaucrat Bureaucrat(const std::string _name, int _grade); Bureaucrat(const Bureaucrat ©); Bureaucrat &operator=(const Bureaucrat &other); - ~Bureaucrat(); + ~Bureaucrat(); std::string getName() const; 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 { diff --git a/ex02/Main.cpp b/ex02/Main.cpp index 0bf1292..ce95e9f 100644 --- a/ex02/Main.cpp +++ b/ex02/Main.cpp @@ -1,102 +1,44 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* Main.cpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: toi +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* 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); } diff --git a/ex02/Makefile b/ex02/Makefile index 9f7228a..cede060 100644 --- a/ex02/Makefile +++ b/ex02/Makefile @@ -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) diff --git a/ex02/PresidentialPardonForm.cpp b/ex02/PresidentialPardonForm.cpp new file mode 100644 index 0000000..485b588 --- /dev/null +++ b/ex02/PresidentialPardonForm.cpp @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cgodecke +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; +} diff --git a/ex02/PresidentialPardonForm.hpp b/ex02/PresidentialPardonForm.hpp new file mode 100644 index 0000000..12ae12b --- /dev/null +++ b/ex02/PresidentialPardonForm.hpp @@ -0,0 +1,22 @@ +#ifndef PRESIDENTIALFORM_HPP +#define PRESIDENTIALFORM_HPP + +#include "AForm.hpp" +#include + +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 diff --git a/ex02/RobotomyRequestForm.cpp b/ex02/RobotomyRequestForm.cpp new file mode 100644 index 0000000..88f5873 --- /dev/null +++ b/ex02/RobotomyRequestForm.cpp @@ -0,0 +1,43 @@ +#include "RobotomyRequestForm.hpp" +#include + +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."); +} diff --git a/ex02/RobotomyRequestForm.hpp b/ex02/RobotomyRequestForm.hpp new file mode 100644 index 0000000..593fbb4 --- /dev/null +++ b/ex02/RobotomyRequestForm.hpp @@ -0,0 +1,31 @@ +#ifndef ROBOTOMYREQUESTFORM_HPP +#define ROBOTOMYREQUESTFORM_HPP + +#include "AForm.hpp" +#include +#include /* srand, rand */ +#include /* 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 diff --git a/ex02/ShrubberyCreationForm.cpp b/ex02/ShrubberyCreationForm.cpp new file mode 100644 index 0000000..739a0d9 --- /dev/null +++ b/ex02/ShrubberyCreationForm.cpp @@ -0,0 +1,50 @@ +#include "ShrubberyCreationForm.hpp" +#include +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!"); +} diff --git a/ex02/ShrubberyCreationForm.hpp b/ex02/ShrubberyCreationForm.hpp new file mode 100644 index 0000000..38ae284 --- /dev/null +++ b/ex02/ShrubberyCreationForm.hpp @@ -0,0 +1,29 @@ +#ifndef SHRUBBERYCREATIONFORM_HPP +#define SHRUBBERYCREATIONFORM_HPP + +#include "AForm.hpp" +#include + +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 diff --git a/ex03/AForm.cpp b/ex03/AForm.cpp new file mode 100644 index 0000000..12d54b5 --- /dev/null +++ b/ex03/AForm.cpp @@ -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!"); +} diff --git a/ex03/AForm.hpp b/ex03/AForm.hpp new file mode 100644 index 0000000..851172b --- /dev/null +++ b/ex03/AForm.hpp @@ -0,0 +1,57 @@ +#ifndef AFORM_HPP +#define AFORM_HPP + +#include +#include +#include + +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 diff --git a/ex03/Bureaucrat.cpp b/ex03/Bureaucrat.cpp new file mode 100644 index 0000000..fe77b9a --- /dev/null +++ b/ex03/Bureaucrat.cpp @@ -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'; + } +} diff --git a/ex03/Bureaucrat.hpp b/ex03/Bureaucrat.hpp new file mode 100644 index 0000000..42c982e --- /dev/null +++ b/ex03/Bureaucrat.hpp @@ -0,0 +1,44 @@ +#ifndef BUREAUCRAT_HPP +#define BUREAUCRAT_HPP + +#include +#include +#include + +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 diff --git a/ex03/Main.cpp b/ex03/Main.cpp new file mode 100644 index 0000000..ce95e9f --- /dev/null +++ b/ex03/Main.cpp @@ -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); +} diff --git a/ex03/Makefile b/ex03/Makefile new file mode 100644 index 0000000..cede060 --- /dev/null +++ b/ex03/Makefile @@ -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 diff --git a/ex03/PresidentialPardonForm.cpp b/ex03/PresidentialPardonForm.cpp new file mode 100644 index 0000000..485b588 --- /dev/null +++ b/ex03/PresidentialPardonForm.cpp @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cgodecke +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; +} diff --git a/ex03/PresidentialPardonForm.hpp b/ex03/PresidentialPardonForm.hpp new file mode 100644 index 0000000..12ae12b --- /dev/null +++ b/ex03/PresidentialPardonForm.hpp @@ -0,0 +1,22 @@ +#ifndef PRESIDENTIALFORM_HPP +#define PRESIDENTIALFORM_HPP + +#include "AForm.hpp" +#include + +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 diff --git a/ex03/RobotomyRequestForm.cpp b/ex03/RobotomyRequestForm.cpp new file mode 100644 index 0000000..88f5873 --- /dev/null +++ b/ex03/RobotomyRequestForm.cpp @@ -0,0 +1,43 @@ +#include "RobotomyRequestForm.hpp" +#include + +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."); +} diff --git a/ex03/RobotomyRequestForm.hpp b/ex03/RobotomyRequestForm.hpp new file mode 100644 index 0000000..593fbb4 --- /dev/null +++ b/ex03/RobotomyRequestForm.hpp @@ -0,0 +1,31 @@ +#ifndef ROBOTOMYREQUESTFORM_HPP +#define ROBOTOMYREQUESTFORM_HPP + +#include "AForm.hpp" +#include +#include /* srand, rand */ +#include /* 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 diff --git a/ex03/ShrubberyCreationForm.cpp b/ex03/ShrubberyCreationForm.cpp new file mode 100644 index 0000000..739a0d9 --- /dev/null +++ b/ex03/ShrubberyCreationForm.cpp @@ -0,0 +1,50 @@ +#include "ShrubberyCreationForm.hpp" +#include +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!"); +} diff --git a/ex03/ShrubberyCreationForm.hpp b/ex03/ShrubberyCreationForm.hpp new file mode 100644 index 0000000..38ae284 --- /dev/null +++ b/ex03/ShrubberyCreationForm.hpp @@ -0,0 +1,29 @@ +#ifndef SHRUBBERYCREATIONFORM_HPP +#define SHRUBBERYCREATIONFORM_HPP + +#include "AForm.hpp" +#include + +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