push for save

This commit is contained in:
lfirmin
2025-11-18 20:51:13 +01:00
parent 93a5094a0a
commit 2fef2f8dfc
22 changed files with 848 additions and 95 deletions
+12 -2
View File
@@ -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';
}
}
+4 -3
View File
@@ -5,7 +5,7 @@
#include <iostream>
#include <string>
class Form;
class AForm;
class Bureaucrat
{
@@ -18,13 +18,14 @@ class Bureaucrat
Bureaucrat(const std::string _name, int _grade);
Bureaucrat(const Bureaucrat &copy);
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
{
+30 -88
View File
@@ -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);
}
+2 -2
View File
@@ -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)
+42
View File
@@ -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;
}
+22
View File
@@ -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
+43
View File
@@ -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.");
}
+31
View File
@@ -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
+50
View File
@@ -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!");
}
+29
View 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