diff --git a/ex01/Form.cpp b/ex01/Form.cpp index ef0f238..70552fe 100644 --- a/ex01/Form.cpp +++ b/ex01/Form.cpp @@ -65,4 +65,4 @@ std::ostream &operator<<(std::ostream &os, Form const &form) { os << "Name: " << form.getName() << " Signed: " << form.getSigned() << " SiGrade: " << form.getSignGrade() << " ExGrade: " << form.getExecuteGrade() << std::endl; return (os); -} \ No newline at end of file +} diff --git a/ex01/Main.cpp b/ex01/Main.cpp index c56f7b1..0bf1292 100644 --- a/ex01/Main.cpp +++ b/ex01/Main.cpp @@ -24,7 +24,7 @@ int main(void) } catch(const std::exception &e) { - std::cerr << "Exception attrapée: " << e.what() << std::endl; + std::cerr << "Exception catch: " << e.what() << std::endl; } try @@ -33,7 +33,7 @@ int main(void) } catch(const std::exception &e) { - std::cerr << "Exception attrapée: " << e.what() << std::endl; + std::cerr << "Exception catch: " << e.what() << std::endl; } std::cout << "\033[32m" << std::endl << "Test augmentation" << "\033[0m" << std::endl; @@ -45,17 +45,17 @@ int main(void) } catch(const std::exception& e) { - std::cerr << "Exception attrapée: " << e.what() << std::endl; + std::cerr << "Exception catch: " << e.what() << std::endl; } std::cout << robert; - + try { robert.incrementGrade(); } catch(const std::exception& e) { - std::cerr << "Exception attrapée: " << e.what() << std::endl; + std::cerr << "Exception catch: " << e.what() << std::endl; } std::cout << robert; @@ -70,17 +70,17 @@ int main(void) } catch(const std::exception& e) { - std::cerr << "Exception attrapée: " << e.what() << std::endl; + std::cerr << "Exception catch: " << e.what() << std::endl; } std::cout << thomas; - + try { thomas.decrementGrade(); } catch(const std::exception& e) { - std::cerr << "Exception attrapée: " << e.what() << std::endl; + std::cerr << "Exception catch: " << e.what() << std::endl; } std::cout << thomas; @@ -91,12 +91,12 @@ int main(void) 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; - + return (0); -} \ No newline at end of file +} diff --git a/ex02/AForm.cpp b/ex02/AForm.cpp new file mode 100644 index 0000000..12d54b5 --- /dev/null +++ b/ex02/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/ex02/AForm.hpp b/ex02/AForm.hpp new file mode 100644 index 0000000..851172b --- /dev/null +++ b/ex02/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/ex02/Bureaucrat.cpp b/ex02/Bureaucrat.cpp new file mode 100644 index 0000000..700237d --- /dev/null +++ b/ex02/Bureaucrat.cpp @@ -0,0 +1,87 @@ +#include "Form.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(Form &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; +} diff --git a/ex02/Bureaucrat.hpp b/ex02/Bureaucrat.hpp new file mode 100644 index 0000000..045fb49 --- /dev/null +++ b/ex02/Bureaucrat.hpp @@ -0,0 +1,43 @@ +#ifndef BUREAUCRAT_HPP +#define BUREAUCRAT_HPP + +#include +#include +#include + +class Form; + +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(Form &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/ex02/Main.cpp b/ex02/Main.cpp new file mode 100644 index 0000000..0bf1292 --- /dev/null +++ b/ex02/Main.cpp @@ -0,0 +1,102 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: toi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/15 10:00:00 by toi #+# #+# */ +/* Updated: 2025/01/15 10:00:00 by toi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" +#include "Form.hpp" + +int main(void) +{ + std::cout << "\033[35m" << std::endl << "Test ex01" << "\033[0m" << std::endl; + + 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; + } + + try + { + Bureaucrat Dormeur2("Olivier", -10); + } + catch(const std::exception &e) + { + std::cerr << "Exception catch: " << e.what() << std::endl; + } + + 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; + + try + { + robert.incrementGrade(); + } + catch(const std::exception& e) + { + std::cerr << "Exception catch: " << e.what() << std::endl; + } + std::cout << robert; + + + + 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; + + return (0); +} diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..9f7228a --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,36 @@ +CXX = c++ +CXXFLAGS = -Wall -Wextra -Werror -std=c++98 +OBJDIR = obj +SOURCES = Main.cpp Bureaucrat.cpp AForm.cpp +OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o)) +NAME = Form + +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