This commit is contained in:
root 2025-11-15 01:14:20 +00:00
parent df15fdffed
commit ecd930d8a1
6 changed files with 163 additions and 48 deletions

View File

@ -1,3 +1,4 @@
#include "Form.hpp"
#include "Bureaucrat.hpp" #include "Bureaucrat.hpp"
Bureaucrat::Bureaucrat() : _name("default"), _grade(150) Bureaucrat::Bureaucrat() : _name("default"), _grade(150)
@ -70,3 +71,17 @@ const char *Bureaucrat::GradeTooLowException::what() const throw()
{ {
return ("Grade too low!"); 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;
}

View File

@ -5,6 +5,8 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
class Form;
class Bureaucrat class Bureaucrat
{ {
private: private:
@ -16,11 +18,13 @@ class Bureaucrat
Bureaucrat(const std::string _name, int _grade); Bureaucrat(const std::string _name, int _grade);
Bureaucrat(const Bureaucrat &copy); Bureaucrat(const Bureaucrat &copy);
Bureaucrat &operator=(const Bureaucrat &other); Bureaucrat &operator=(const Bureaucrat &other);
~Bureaucrat();
std::string getName() const; std::string getName() const;
int getGrade() const; int getGrade() const;
void incrementGrade(); void incrementGrade();
void decrementGrade(); void decrementGrade();
void signForm(Form &form);
class GradeTooHighException : public std::exception class GradeTooHighException : public std::exception
{ {

View File

@ -1,18 +1,15 @@
#include "Form.hpp" #include "Form.hpp"
#include "Bureaucrat.hpp"
Bureaucrat::Bureaucrat() Form::Form() : _name("Default"), _signed(false), _grade_ex(150), _grade_si(150)
{ {
} }
Form::Form() : _name("Default"), _signed(false), _grade_si(150), _grade_ex(150) Form::Form(const std::string name, const int grade_si, const int grade_ex) : _name(name), _signed(false), _grade_ex(grade_ex), _grade_si(grade_si)
{ {
} }
Form::Form(const std::string name, const int grade_si, const int grade_ex) : _name(name), _signed(false), _grade_si(grade_si), _grade_ex(grade_ex) Form::Form(const Form &other) : _name(other._name), _signed(other._signed), _grade_ex(other._grade_ex), _grade_si(other._grade_si)
{
}
Form::Form(const Form &other) : _name(other._name), _signed(other._signed), _grade_si(other._grade_si), _grade_ex(other._grade_ex)
{ {
} }
@ -39,3 +36,33 @@ int Form::getSignGrade() const
{ {
return (_grade_si); return (_grade_si);
} }
void Form::beSigned(const Bureaucrat &bureaucrat)
{
if (bureaucrat.getGrade() <= _grade_si)
_signed = true;
else
throw GradeTooLowException();
}
Form &Form::operator=(const Form &other)
{
_signed = other._signed;
return (*this);
}
const char *Form::GradeTooHighException::what() const throw()
{
return ("Grade too high!");
}
const char *Form::GradeTooLowException::what() const throw()
{
return ("Grade too low!");
}
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);
}

View File

@ -4,7 +4,8 @@
#include <exception> #include <exception>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include "Bureaucrat.hpp"
class Bureaucrat;
class Form class Form
{ {
@ -26,5 +27,19 @@ class Form
bool getSigned() const; bool getSigned() const;
int getExecuteGrade() const; int getExecuteGrade() const;
int getSignGrade() const; int getSignGrade() const;
class GradeTooLowException : public std::exception
{
public:
virtual const char *what() const throw();
};
class GradeTooHighException : public std::exception
{
public:
virtual const char *what() const throw();
};
}; };
#endif std::ostream &operator<<(std::ostream &os, Form const &form);
#endif

View File

@ -1,48 +1,102 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 "Bureaucrat.hpp" #include "Bureaucrat.hpp"
#include "Form.hpp"
int main(void) int main(void)
{ {
Bureaucrat cat("Moulinette", 1); std::cout << "\033[35m" << std::endl << "Test ex01" << "\033[0m" << std::endl;
Bureaucrat stud("Student", 150);
std::cout << std::endl << "---Test too high/low create---" << std::endl; std::cout << "\033[31m" << std::endl << "Test creation trop haut et trop bas" << "\033[0m" << std::endl;
try { try
Bureaucrat test("test", 1500); {
} catch (const std::exception &e) { Bureaucrat Dormeur1("Bernard", 1500);
std::cerr << "Exception caught: " << e.what() << std::endl;
} }
try { catch(const std::exception &e)
Bureaucrat test1("test2", -10); {
} catch (const std::exception &e) { std::cerr << "Exception attrapée: " << e.what() << std::endl;
std::cerr << "Exception caught: " << e.what() << std::endl;
} }
std::cout << std::endl << "---Test too high/low in/de---" << std::endl;
std::cout << cat; try
try { {
cat.incrementGrade(); Bureaucrat Dormeur2("Olivier", -10);
} catch (const std::exception &e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
} }
std::cout << stud; catch(const std::exception &e)
try { {
stud.decrementGrade(); std::cerr << "Exception attrapée: " << e.what() << std::endl;
} catch (const std::exception &e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
} }
std::cout << std::endl << "---Test increment---" << std::endl;
std::cout << stud; std::cout << "\033[32m" << std::endl << "Test augmentation" << "\033[0m" << std::endl;
try { Bureaucrat robert("Robert", 2);
stud.incrementGrade(); std::cout << robert;
} catch (const std::exception &e) { try
std::cerr << "Exception caught: " << e.what() << std::endl; {
robert.incrementGrade();
} }
std::cout << stud; catch(const std::exception& e)
std::cout << std::endl << "---Test decrement---" << std::endl; {
std::cout << cat; std::cerr << "Exception attrapée: " << e.what() << std::endl;
try {
cat.decrementGrade();
} catch (const std::exception &e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
} }
std::cout << cat; std::cout << robert;
try
{
robert.incrementGrade();
}
catch(const std::exception& e)
{
std::cerr << "Exception attrapée: " << 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 attrapée: " << 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::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); return (0);
} }

View File

@ -1,9 +1,9 @@
CXX = c++ CXX = c++
CXXFLAGS = -Wall -Wextra -Werror -std=c++98 CXXFLAGS = -Wall -Wextra -Werror -std=c++98
OBJDIR = obj OBJDIR = obj
SOURCES = Main.cpp Bureaucrat.cpp SOURCES = Main.cpp Bureaucrat.cpp Form.cpp
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o)) OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
NAME = Bureaucrat NAME = Form
all: $(NAME) all: $(NAME)