This commit is contained in:
Ladebeze66 2024-02-22 16:34:34 +01:00
parent bce000d3ca
commit 9aa5e8f9c5
18 changed files with 160 additions and 79 deletions

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */
/* Updated: 2024/02/16 16:39:04 by fgras-ca ### ########.fr */
/* Updated: 2024/02/22 12:40:07 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -33,13 +33,13 @@ public:
class GradeTooHighException : public std::exception
{
public:
const char* what() const throw() override;
const char* what() const throw();
};
class GradeTooLowException : public std::exception
{
public:
const char* what() const throw() override;
const char* what() const throw();
};
Bureaucrat();

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */
/* Updated: 2024/02/16 17:30:30 by fgras-ca ### ########.fr */
/* Updated: 2024/02/22 12:45:13 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -34,13 +34,13 @@ public:
class GradeTooHighException : public std::exception
{
public:
const char* what() const throw() override;
const char* what() const throw();
};
class GradeTooLowException : public std::exception
{
public:
const char* what() const throw() override;
const char* what() const throw();
};
Bureaucrat();

View File

@ -6,19 +6,13 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:39:51 by fgras-ca #+# #+# */
/* Updated: 2024/02/16 18:19:00 by fgras-ca ### ########.fr */
/* Updated: 2024/02/22 12:48:40 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
#include "Form.hpp"
Form::Form()
: name(name), isSigned(false), gradeRequiredToSign(1), gradeRequiredToExecute(1)
{
std::cout << GREEN << "Form default constructor called!" << RESET << std::endl;
}
Form::Form(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute)
: name(name), isSigned(false), gradeRequiredToSign(gradeRequiredToSign), gradeRequiredToExecute(gradeRequiredToExecute)
{
@ -84,12 +78,12 @@ void Form::beSigned(const Bureaucrat& bureaucrat)
}
}
const char* Form::GradeTooHighException::what() const noexcept
const char* Form::GradeTooHighException::what() const throw()
{
return ("Grade too hight\n");
}
const char* Form::GradeTooLowException::what() const noexcept
const char* Form::GradeTooLowException::what() const throw()
{
return ("Grade too low\n");
}

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:37:14 by fgras-ca #+# #+# */
/* Updated: 2024/02/16 18:26:09 by fgras-ca ### ########.fr */
/* Updated: 2024/02/22 12:48:27 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -29,7 +29,6 @@ private:
public:
//constructueur
Form();
Form(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute);
// Constructeur par copie
Form(const Form& other);
@ -49,13 +48,13 @@ public:
class GradeTooHighException : public std::exception
{
public:
const char* what() const noexcept override;
const char* what() const throw();
};
class GradeTooLowException : public std::exception
{
public:
const char* what() const noexcept override;
const char* what() const throw();
};
};

View File

@ -6,7 +6,7 @@
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
# Updated: 2024/02/11 14:58:41 by fgras-ca ### ########.fr #
# Updated: 2024/02/22 12:41:21 by fgras-ca ### ########.fr #
# #
# **************************************************************************** #
@ -29,14 +29,16 @@ SRC = Bureaucrat.cpp \
Form.cpp \
main.cpp \
OBJS = ${SRC:.cpp=.o}
CC = c++
CFLAGS = -Wall -Werror -Wextra -std=c++98
RM = rm -f
OBJS = ${SRC:.cpp=.o}
all : $(NAME)
%.o: %.cpp
$(CC) $(CFLAGS) -c $< -o $@
$(NAME) : $(OBJS)
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)

View File

@ -6,19 +6,13 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:39:51 by fgras-ca #+# #+# */
/* Updated: 2024/02/21 14:25:05 by fgras-ca ### ########.fr */
/* Updated: 2024/02/22 14:36:36 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
#include "AForm.hpp"
AForm::AForm()
: name(name), isSigned(false), gradeRequiredToSign(1), gradeRequiredToExecute(1)
{
std::cout << GREEN << "AForm default constructor called!" << RESET << std::endl;
}
AForm::AForm(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute)
: name(name), isSigned(false), gradeRequiredToSign(gradeRequiredToSign), gradeRequiredToExecute(gradeRequiredToExecute)
{
@ -84,20 +78,21 @@ void AForm::beSigned(const Bureaucrat& bureaucrat)
}
}
const char* AForm::GradeTooHighException::what() const noexcept
const char* AForm::GradeTooHighException::what() const throw()
{
return ("Grade too hight\n");
return ("Grade too high\n");
}
const char* AForm::GradeTooLowException::what() const noexcept
const char* AForm::GradeTooLowException::what() const throw()
{
return ("Grade too low\n");
}
const char* AForm::NotSignedException::what() const noexcept
const char* AForm::NotSignedException::what() const throw()
{
return ("The forpm is not signed.\n");
return ("The form is not signed.\n");
}
std::ostream& operator<<(std::ostream& os, const AForm& aform)
{
os << "AForm: " << aform.getName()

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:37:14 by fgras-ca #+# #+# */
/* Updated: 2024/02/21 15:53:16 by fgras-ca ### ########.fr */
/* Updated: 2024/02/22 13:49:48 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -32,7 +32,6 @@ protected:
public:
//constructueur
AForm();
AForm(const std::string &name, int gradeRequiredToSign, int gradeRequiredToExecute);
// Constructeur par copie
AForm(const AForm& other);
@ -54,19 +53,19 @@ public:
class GradeTooHighException : public std::exception
{
public:
const char* what() const noexcept override;
virtual const char* what() const throw();
};
class GradeTooLowException : public std::exception
{
public:
const char* what() const noexcept override;
virtual const char* what() const throw();
};
class NotSignedException : public std::exception
{
public:
const char* what() const noexcept override;
virtual const char* what() const throw();
};
};
// Surcharge de l'opérateur d'insertion

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 13:16:26 by fgras-ca #+# #+# */
/* Updated: 2024/02/21 16:02:52 by fgras-ca ### ########.fr */
/* Updated: 2024/02/22 15:24:37 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -102,14 +102,14 @@ void Bureaucrat::executeForm(AForm const &form)
}
catch (std::exception& e)
{
std::cout << name << " couldnt execute " << RED << form.getName() << BLUE << " (" << form.getTarget() << ")" << RESET << " because: " << e.what() << std::endl;
std::cerr << name << " couldnt execute " << RED << form.getName() << BLUE << " (" << form.getTarget() << ")" << RESET << " because: " << e.what() << std::endl;
}
}
// Méthodes pour les exceptions
const char* Bureaucrat::GradeTooHighException::what() const throw()
{
return ("Grade too hight\n");
return ("Grade too high\n");
}
const char* Bureaucrat::GradeTooLowException::what() const throw()

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:57:28 by fgras-ca #+# #+# */
/* Updated: 2024/02/21 13:09:39 by fgras-ca ### ########.fr */
/* Updated: 2024/02/22 15:29:48 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -36,13 +36,13 @@ public:
class GradeTooHighException : public std::exception
{
public:
const char* what() const throw() override;
virtual const char* what() const throw();
};
class GradeTooLowException : public std::exception
{
public:
const char* what() const throw() override;
virtual const char* what() const throw();
};
Bureaucrat();
@ -56,7 +56,7 @@ public:
void incrementGrade();
void decrementGrade();
void signForm(AForm& form);
void executeForm(AForm const &form);
virtual void executeForm(AForm const &form);
private:
const std::string name;

View File

@ -1 +0,0 @@
ASCII trees

View File

@ -6,7 +6,7 @@
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# #
# Updated: 2024/02/21 13:06:08 by fgras-ca ### ########.fr #
# Updated: 2024/02/22 15:36:34 by fgras-ca ### ########.fr #
# #
# **************************************************************************** #
@ -32,14 +32,16 @@ SRC = Bureaucrat.cpp \
RobotomyRequestForm.cpp \
ShrubberyCreationForm.cpp \
OBJS = ${SRC:.cpp=.o}
CC = c++
CFLAGS = -Wall -Werror -Wextra -std=c++98
RM = rm -f
OBJS = ${SRC:.cpp=.o}
all : $(NAME)
%.o: %.cpp
$(CC) $(CFLAGS) -c $< -o $@
$(NAME) : $(OBJS)
@echo "$(RED)Compilation fixed... $(DEF_COLOR)"
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME)

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/21 11:31:13 by fgras-ca #+# #+# */
/* Updated: 2024/02/21 15:18:45 by fgras-ca ### ########.fr */
/* Updated: 2024/02/22 15:42:00 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/20 16:56:48 by fgras-ca #+# #+# */
/* Updated: 2024/02/21 15:17:46 by fgras-ca ### ########.fr */
/* Updated: 2024/02/22 15:39:59 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,7 @@
#define PRESIDENTIALPARDONFORM_HPP
#include "AForm.hpp"
#include "Bureaucrat.hpp"
#include <iostream>
#define RESET "\033[0m"
@ -32,7 +33,7 @@ public:
PresidentialPardonForm& operator=(const PresidentialPardonForm& other);
virtual ~PresidentialPardonForm();
void execute(Bureaucrat const &executor) const override;
void execute(Bureaucrat const &executor) const;
std::string getTarget() const;
};

View File

@ -6,15 +6,26 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/21 11:25:21 by fgras-ca #+# #+# */
/* Updated: 2024/02/21 16:00:03 by fgras-ca ### ########.fr */
/* Updated: 2024/02/22 16:13:37 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "RobotomyRequestForm.hpp"
void RobotomyRequestForm::initializeRandomSeed()
{
static bool seeded = false;
if (!seeded)
{
std::srand(static_cast<unsigned int>(std::time(NULL))); // Utilisez NULL au lieu de nullptr
seeded = true;
}
}
RobotomyRequestForm::RobotomyRequestForm(const std::string& target)
: AForm("RobotomyRequestForm", 72, 45), target(target)
{
initializeRandomSeed();
std::cout << GREEN << "RobotomyRequestForm constructor called for target: " << target << RESET << std::endl;
}
@ -51,14 +62,15 @@ void RobotomyRequestForm::execute(Bureaucrat const &executor) const
{
throw GradeTooLowException();
}
std::cout << "Drilling noises... Zzzzzz..." << std::endl;
if (std::rand() % 2 == 0)
{
std::cout << target << " has been robotomized successfully." << std::endl;
std::cout << GREEN << target << " has been robotomized successfully." << RESET << std::endl;
}
else
{
std::cout << "The robotomy on " << target << " failed." << std::endl;
std::cout << RED << "The robotomy on " << target << " failed." << RESET << std::endl;
}
}

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/20 16:55:26 by fgras-ca #+# #+# */
/* Updated: 2024/02/21 15:58:32 by fgras-ca ### ########.fr */
/* Updated: 2024/02/22 16:10:22 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,7 @@
#define ROBOTOMYREQUESTFORM_HPP
#include "AForm.hpp"
#include "Bureaucrat.hpp"
#include <iostream>
#include <cstdlib> // Pour std::rand() et std::srand()
#include <ctime> // Pour std::time()
@ -33,9 +34,10 @@ public:
RobotomyRequestForm(const RobotomyRequestForm& other);
RobotomyRequestForm& operator=(const RobotomyRequestForm& other);
virtual ~RobotomyRequestForm();
static void initializeRandomSeed();
void execute(Bureaucrat const &executor) const override;
std::string getTarget() const override;
void execute(Bureaucrat const &executor) const;
std::string getTarget() const;
};
#endif

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/20 16:54:52 by fgras-ca #+# #+# */
/* Updated: 2024/02/21 15:39:36 by fgras-ca ### ########.fr */
/* Updated: 2024/02/22 16:33:23 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -45,16 +45,33 @@ void ShrubberyCreationForm::execute(Bureaucrat const &executor) const
// Vérification si le formulaire est signé et si le grade de l'exécuteur est suffisant
if (!getIsSigned())
throw NotSignedException(); // Assurez-vous que cette exception est correctement définie dans AForm
if (executor.getGrade() > getGradeRequiredToExecute())
else if (executor.getGrade() > this->getGradeRequiredToExecute())
throw GradeTooLowException();
std::ofstream ofs(target + "_shrubbery");
if (!ofs)
else
{
std::cerr << "Failed to create file " << target << "_shrubbery" << std::endl;
return ;
std::ofstream ofs((target + "_shrubbery").c_str());
if (!ofs)
{
std::cerr << "Failed to create file " << target << "_shrubbery" << std::endl;
return ;
}
ofs << " . 🅻🅰🅳🅴🅱🅴🆉🅴 . +" << "\n"
<<" . . . # ." << "\n"
<<" . . ### . ." << "\n"
<<" . . .#:. .:##°##:. .:#. . ." << "\n"
<<" . . ####°###°#### ." << "\n"
<<" . #:. .:#.###.#:. .:# . ." << "\n"
<<" #########°######### . ." << "\n"
<<" . #:. ####°###°#### .:# . ." << "\n"
<<" . . °#######°°##°##°°#######° ." << "\n"
<<" .°##°#####°#####°##° . ." << "\n"
<<". #:. ... .:##°###°###°##:. ... .:# ." << "\n"
<<" . #######°##°#####°##°####### . ." << "\n"
<<". . #####°°#######°°##### . ." << "\n"
<<" . 000 . ." << "\n"
<<" . . . 000 . . ." << "\n"
<<". ..................O000O................." << std::endl;
}
ofs << "ASCII trees" << std::endl;
}
std::string ShrubberyCreationForm::getTarget() const

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/20 16:41:49 by fgras-ca #+# #+# */
/* Updated: 2024/02/21 15:51:24 by fgras-ca ### ########.fr */
/* Updated: 2024/02/22 15:35:33 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,7 @@
#define SHRUBBERYCREATIONFORM_HPP
#include "AForm.hpp"
#include "Bureaucrat.hpp"
#include <fstream>
#include <iostream>
@ -32,7 +33,7 @@ public:
ShrubberyCreationForm& operator=(const ShrubberyCreationForm& other);
virtual ~ShrubberyCreationForm();
void execute(Bureaucrat const &executor) const override;
void execute(Bureaucrat const &executor) const;
std::string getTarget() const;
};

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 14:55:46 by fgras-ca #+# #+# */
/* Updated: 2024/02/21 15:21:24 by fgras-ca ### ########.fr */
/* Updated: 2024/02/22 15:54:24 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,31 +19,89 @@
int main()
{
// Création des bureaucrates
Bureaucrat john("John", 2);
Bureaucrat doe("Doe", 150);
// Création des formulaires
ShrubberyCreationForm shrub("Home");
RobotomyRequestForm robot("Robot");
PresidentialPardonForm pardon("Criminal");
try {
john.signForm(shrub);
john.executeForm(shrub);
// Tentatives de signature et d'exécution des formulaires par John
std::cout << "\n--- John tries to sign and execute forms ---\n";
john.signForm(shrub);
john.executeForm(shrub);
john.signForm(robot);
john.executeForm(robot);
john.signForm(robot);
john.executeForm(robot);
john.signForm(pardon);
john.executeForm(pardon);
john.signForm(pardon);
john.executeForm(pardon);
// Tester avec un bureaucrate de grade insuffisant
// Tentatives de signature et d'exécution des formulaires par Doe
std::cout << "\n--- Doe tries to sign and execute forms ---\n";
try
{
doe.signForm(shrub);
}
catch (std::exception& e)
{
std::cout << "Error: " << e.what() << std::endl;
}
try
{
doe.executeForm(shrub);
}
catch (std::exception& e)
{
std::cout << e.what() << std::endl;
std::cout << "Error: " << e.what() << std::endl;
}
try
{
doe.executeForm(robot);
}
catch (std::exception& e)
{
std::cout << "Error: " << e.what() << std::endl;
}
// Tentatives de signature et d'exécution des formulaires avec gestion des exceptions
std::cout << "\n--- Additional tests with exception handling ---\n";
try
{
doe.signForm(robot);
doe.executeForm(robot);
}
catch (std::exception& e)
{
std::cout << "Error: " << e.what() << std::endl;
}
try
{
doe.signForm(pardon);
doe.executeForm(pardon);
}
catch (std::exception& e)
{
std::cout << "Error: " << e.what() << std::endl;
}
// Test avec un formulaire déjà signé
std::cout << "\n--- Testing with already signed form ---\n";
try
{
john.signForm(shrub); // shrub est déjà signé par John plus tôt
john.executeForm(shrub);
}
catch (std::exception& e)
{
std::cout << "Error: " << e.what() << std::endl;
}
return (0);
}