From 34eb56c9cc8fe3c42830258d684977cfc0f2991d Mon Sep 17 00:00:00 2001 From: "Luna Mira Lage (Desoroxxx)" Date: Mon, 6 Jul 2026 15:55:01 +0200 Subject: [PATCH] ex01 Signed-off-by: Luna Mira Lage (Desoroxxx) --- ex01/Makefile | 4 + ex01/src/Bureaucrat.cpp | 81 +++++++++++++++++++ ex01/src/Bureaucrat.hpp | 49 ++++++++++++ ex01/src/Form.cpp | 68 ++++++++++++++++ ex01/src/Form.hpp | 49 ++++++++++++ ex01/src/main.cpp | 167 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 418 insertions(+) create mode 100644 ex01/Makefile create mode 100644 ex01/src/Bureaucrat.cpp create mode 100644 ex01/src/Bureaucrat.hpp create mode 100644 ex01/src/Form.cpp create mode 100644 ex01/src/Form.hpp create mode 100644 ex01/src/main.cpp diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..9b7ba35 --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,4 @@ +override NAME = ex01.out +override SRC_FILES = main.cpp Bureaucrat.cpp Form.cpp + +include ../Makefile diff --git a/ex01/src/Bureaucrat.cpp b/ex01/src/Bureaucrat.cpp new file mode 100644 index 0000000..7b872be --- /dev/null +++ b/ex01/src/Bureaucrat.cpp @@ -0,0 +1,81 @@ +#include +#include + +Bureaucrat::Bureaucrat() : name("Franck Leboeuf") { + std::cout << "Bureaucrat default constructor called" << std::endl; + setGrade(150); +} + +Bureaucrat::Bureaucrat(const Bureaucrat& other) : name(other.name), grade(other.grade) { + std::cout << "Bureaucrat copy constructor called" << std::endl; +} + +Bureaucrat::Bureaucrat(const std::string& name) : name(name) { + std::cout << "Bureaucrat (name) constructor called" << std::endl; + setGrade(150); +} + +Bureaucrat::Bureaucrat(const std::string& name, const unsigned short grade) : name(name) { + std::cout << "Bureaucrat (name, grade) constructor called" << std::endl; + setGrade(grade); +} + +void Bureaucrat::incrementGrade() { + std::cout << "Increment grade called" << std::endl; + setGrade(grade - 1); +} + +void Bureaucrat::decrementGrade() { + std::cout << "Decrement grade called" << std::endl; + setGrade(grade + 1); +} + +void Bureaucrat::signForm(Form& form) { + if (form.getIsSigned()) { + std::cout << name << " couldn't sign " << form.getName() << " because it is already signed" << std::endl; + return; + } + + try { + form.beSigned(*this); + std::cout << name << " signed " << form.getName() << std::endl; + } catch (const std::exception& exception) { + std::cout << name << " couldn't sign " << form.getName() << " because " << exception.what() << std::endl; + } +} + +const std::string& Bureaucrat::getName() const { + return name; +} + +unsigned short Bureaucrat::getGrade() const { + return grade; +} + +Bureaucrat& Bureaucrat::operator=(const Bureaucrat& other) { + std::cout << "Bureaucrat assignment operator called" << std::endl; + + if (this != &other) + grade = other.grade; + + return *this; +} + +Bureaucrat::~Bureaucrat() { + std::cout << "Bureaucrat destructor called" << std::endl; +} + +void Bureaucrat::setGrade(const unsigned short grade) { + if (grade > 150) + throw GradeTooLowException(); + + if (grade < 1) + throw GradeTooHighException(); + + this->grade = grade; +} + +std::ostream& operator<<(std::ostream& ostream, const Bureaucrat& bureaucrat) { + ostream << bureaucrat.getName() << ", bureaucrat grade " << bureaucrat.getGrade() << "."; + return ostream; +} diff --git a/ex01/src/Bureaucrat.hpp b/ex01/src/Bureaucrat.hpp new file mode 100644 index 0000000..144c127 --- /dev/null +++ b/ex01/src/Bureaucrat.hpp @@ -0,0 +1,49 @@ +#pragma once + +#include +#include +#include + +class Form; + +class Bureaucrat { + public: + Bureaucrat(); + Bureaucrat(const Bureaucrat& other); + Bureaucrat(const std::string& name); + Bureaucrat(const std::string& name, const unsigned short grade); + + void incrementGrade(); + void decrementGrade(); + + void signForm(Form& form); + + const std::string& getName() const; + unsigned short getGrade() const; + + Bureaucrat& operator=(const Bureaucrat& other); + + virtual ~Bureaucrat(); + private: + const std::string name; + + unsigned short grade; + + void setGrade(const unsigned short grade); + + class GradeTooHighException : public std::exception { + public: + const char* what() const throw() { + return "Grade too high"; + } + }; + + class GradeTooLowException : public std::exception { + public: + const char* what() const throw() { + return "Grade too low"; + } + }; +}; + +std::ostream& operator<<(std::ostream& ostream, const Bureaucrat& bureaucrat); diff --git a/ex01/src/Form.cpp b/ex01/src/Form.cpp new file mode 100644 index 0000000..d19f10c --- /dev/null +++ b/ex01/src/Form.cpp @@ -0,0 +1,68 @@ +#include +#include + +Form::Form() : name("Vente"), isSigned(false), gradeToSign(150), gradeToExecute(150) { + std::cout << "Form default constructor called" << std::endl; +} + +Form::Form(const Form& other) : name(other.name), isSigned(other.isSigned), gradeToSign(other.gradeToSign), gradeToExecute(other.gradeToExecute) { + std::cout << "Form copy constructor called" << std::endl; +} + +Form::Form(const std::string& name) : name(name), isSigned(false), gradeToSign(150), gradeToExecute(150) { + std::cout << "Form (name) constructor called" << std::endl; +} + +Form::Form(const std::string& name, const unsigned short gradeToSign, const unsigned short gradeToExecute) : name(name), isSigned(false), gradeToSign(gradeToSign), gradeToExecute(gradeToExecute) { + std::cout << "Form (name, gradeToSign, gradeToExecute) constructor called" << std::endl; + + if (gradeToSign > 150 || gradeToExecute > 150) + throw GradeTooLowException(); + + if (gradeToSign < 1 || gradeToExecute < 1) + throw GradeTooHighException(); +} + +void Form::beSigned(Bureaucrat& bureaucrat) { + std::cout << "Be signed called" << std::endl; + + if (bureaucrat.getGrade() > gradeToSign) + throw GradeTooLowException(); + + isSigned = true; +} + +const std::string& Form::getName() const { + return name; +} + +bool Form::getIsSigned() const { + return isSigned; +} + +unsigned short Form::getGradeToSign() const { + return gradeToSign; +} + +unsigned short Form::getGradeToExecute() const { + return gradeToExecute; +} + +Form& Form::operator=(const Form& other) { + std::cout << "Form assignment operator called" << std::endl; + + if (this != &other) + isSigned = other.isSigned; + + return *this; +} + +Form::~Form() { + std::cout << "Form destructor called" << std::endl; +} + +std::ostream& operator<<(std::ostream& ostream, const Form& form) { + ostream << form.getName() << ", grade to sign " << form.getGradeToSign() << ", grade to execute " << form.getGradeToExecute() << "."; + return ostream; +} + diff --git a/ex01/src/Form.hpp b/ex01/src/Form.hpp new file mode 100644 index 0000000..47273a0 --- /dev/null +++ b/ex01/src/Form.hpp @@ -0,0 +1,49 @@ +#pragma once + +#include +#include +#include + +class Bureaucrat; + +class Form { + public: + Form(); + Form(const Form& other); + Form(const std::string& name); + Form(const std::string& name, const unsigned short gradeToSign, const unsigned short gradeToExecute); + + void beSigned(Bureaucrat& bureaucrat); + + const std::string& getName() const; + bool getIsSigned() const; + unsigned short getGradeToSign() const; + unsigned short getGradeToExecute() const; + + Form& operator=(const Form& other); + + virtual ~Form(); + private: + const std::string name; + + bool isSigned; + + const unsigned short gradeToSign; + const unsigned short gradeToExecute; + + class GradeTooHighException : public std::exception { + public: + const char* what() const throw() { + return "Grade too high"; + } + }; + + class GradeTooLowException : public std::exception { + public: + const char* what() const throw() { + return "Grade too low"; + } + }; +}; + +std::ostream& operator<<(std::ostream& ostream, const Form& form); diff --git a/ex01/src/main.cpp b/ex01/src/main.cpp new file mode 100644 index 0000000..9ed43f0 --- /dev/null +++ b/ex01/src/main.cpp @@ -0,0 +1,167 @@ +#include +#include +#include +#include + +int main() { + { + Bureaucrat franck; + std::cout << franck << std::endl; + franck.incrementGrade(); + std::cout << franck << std::endl; + + Bureaucrat alain("Alain"); + std::cout << alain << std::endl; + alain.incrementGrade(); + std::cout << alain << std::endl; + + Bureaucrat james("James", 1); + std::cout << james << std::endl; + james.decrementGrade(); + std::cout << james << std::endl; + } + + std::cout << "================ Exceptions ================" << std::endl; + { + std::cout << "=== Decrement too low ===" << std::endl; + try { + Bureaucrat franckAlainJamesLeboeuf; + std::cout << franckAlainJamesLeboeuf << std::endl; + franckAlainJamesLeboeuf.decrementGrade(); + } catch (const std::exception& exception) { + std::cout << exception.what() << std::endl; + } + + std::cout << "=== Increment too high ===" << std::endl; + try { + Bureaucrat franckAlainJamesLeboeuf("Franck Alain James Leboeuf", 1); + std::cout << franckAlainJamesLeboeuf << std::endl; + franckAlainJamesLeboeuf.incrementGrade(); + } catch (const std::exception& exception) { + std::cout << exception.what() << std::endl; + } + + std::cout << "=== Construct too low ===" << std::endl; + try { + Bureaucrat franckAlainJamesLeboeuf("Franck Alain James Leboeuf", 151); + } catch (const std::exception& exception) { + std::cout << exception.what() << std::endl; + } + + std::cout << "=== Construct too high ===" << std::endl; + try { + Bureaucrat franckAlainJamesLeboeuf("Franck Alain James Leboeuf", 0); + } catch (const std::exception& exception) { + std::cout << exception.what() << std::endl; + } + } + + std::cout << "================================ Forms ================================" << std::endl; + { + Bureaucrat franck; + Form vente; + std::cout << vente << std::endl; + vente.beSigned(franck); + std::cout << vente << std::endl; + Form vente2; + std::cout << vente2 << std::endl; + franck.signForm(vente2); + std::cout << vente2 << std::endl; + + Bureaucrat alain("Alain"); + Form contrat("Contrat"); + std::cout << contrat << std::endl; + contrat.beSigned(alain); + std::cout << contrat << std::endl; + Form contrat2("Contrat2"); + std::cout << contrat2 << std::endl; + alain.signForm(contrat2); + std::cout << contrat2 << std::endl; + + Bureaucrat james("James", 1); + Form achat("Achat", 1, 1); + std::cout << achat << std::endl; + achat.beSigned(james); + std::cout << achat << std::endl; + Form achat2("Achat2", 1, 1); + std::cout << achat2 << std::endl; + james.signForm(achat2); + std::cout << achat2 << std::endl; + + Bureaucrat franckAlainJamesLeboeuf("Franck Alain James Leboeuf", 1); + Form contratAlternance("Contrat Alternance", 1, 1); + std::cout << contratAlternance << std::endl; + contratAlternance.beSigned(franckAlainJamesLeboeuf); + std::cout << contratAlternance << std::endl; + contratAlternance.beSigned(franckAlainJamesLeboeuf); + std::cout << contratAlternance << std::endl; + Form contractAlternance2("Contrat Alternance 2", 1, 1); + franckAlainJamesLeboeuf.signForm(contractAlternance2); + std::cout << contractAlternance2 << std::endl; + franckAlainJamesLeboeuf.signForm(contractAlternance2); + std::cout << contractAlternance2 << std::endl; + } + + std::cout << "================ Exceptions ================" << std::endl; + { + std::cout << "=== Construct both grade too low ===" << std::endl; + try { + Form vente("Vente", 151, 151); + } catch (const std::exception& exception) { + std::cout << exception.what() << std::endl; + } + + std::cout << "=== Construct both grade too high ===" << std::endl; + try { + Form vente("Vente", 0, 0); + } catch (const std::exception& exception) { + std::cout << exception.what() << std::endl; + } + + std::cout << "=== Construct to sign grade too low ===" << std::endl; + try { + Form vente("Vente", 151, 150); + } catch (const std::exception& exception) { + std::cout << exception.what() << std::endl; + } + + std::cout << "=== Construct to sign grade too high ===" << std::endl; + try { + Form vente("Vente", 0, 0); + } catch (const std::exception& exception) { + std::cout << exception.what() << std::endl; + } + + std::cout << "=== Construct to execute grade too low ===" << std::endl; + try { + Form vente("Vente", 150, 151); + } catch (const std::exception& exception) { + std::cout << exception.what() << std::endl; + } + + std::cout << "=== Construct to execute grade too high ===" << std::endl; + try { + Form vente("Vente", 150, 0); + } catch (const std::exception& exception) { + std::cout << exception.what() << std::endl; + } + + std::cout << "=== Grade to be signed too low ===" << std::endl; + try { + Bureaucrat franck; + Form vente("Vente", 1, 1); + vente.beSigned(franck); + } catch (const std::exception& exception) { + std::cout << exception.what() << std::endl; + } + + std::cout << "=== Grade to sign form too low ===" << std::endl; + try { + Bureaucrat franck; + Form vente("Vente", 1, 1); + franck.signForm(vente); + } catch (const std::exception& exception) { + std::cout << exception.what() << std::endl; + } + } +} -- 2.51.2