From 2fdad9f4de0bed8a6a96675c6c62fc6914fdcd19 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Sun, 3 Sep 2023 13:57:59 +0000 Subject: [PATCH] init ex02 --- ex02/Makefile | 26 ++++++ ex02/src/AForm.cpp | 93 ++++++++++++++++++++ ex02/src/AForm.hpp | 47 ++++++++++ ex02/src/Bureaucrat.cpp | 88 +++++++++++++++++++ ex02/src/Bureaucrat.hpp | 45 ++++++++++ ex02/src/main.cpp | 186 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 485 insertions(+) create mode 100644 ex02/Makefile create mode 100644 ex02/src/AForm.cpp create mode 100644 ex02/src/AForm.hpp create mode 100644 ex02/src/Bureaucrat.cpp create mode 100644 ex02/src/Bureaucrat.hpp create mode 100644 ex02/src/main.cpp diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..7f950fe --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,26 @@ +CXX := c++ +CXXFLAGS := -std=c++98 -Wall -Wextra -Werror -g +SRCDIR := src +OBJDIR := obj +NAME := ex01 + +SRCS := $(wildcard $(SRCDIR)/*.cpp) +OBJS := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS)) + +all: $(NAME) + +$(OBJDIR)/%.o: $(SRCDIR)/%.cpp + mkdir -p obj + $(CXX) $(CXXFLAGS) -c $< -o $@ + +$(NAME): $(OBJS) + $(CXX) $(CXXFLAGS) $^ -o $@ + +clean: + rm -rf $(OBJDIR) + +fclean: clean + rm -fr $(NAME) + +re: fclean + @make --no-print-directory all diff --git a/ex02/src/AForm.cpp b/ex02/src/AForm.cpp new file mode 100644 index 0000000..9449555 --- /dev/null +++ b/ex02/src/AForm.cpp @@ -0,0 +1,93 @@ +#include "Form.hpp" + +#include +#include +#include + +Form::Form() +{ + std::cout << "Form()" << std::endl; +} + +Form::Form(const Form& src) +{ + std::cout << "Form(Form)" << std::endl; + *this = src; +} + +Form::Form(const std::string& name, int to_execute, int to_sign): + _name(name), + _signed(0), + _to_sign(to_sign), + _to_execute(to_execute) +{ + std::cout << "Form(" << name << ", " << to_execute << ", " << to_sign << ")" << std::endl; + if (to_execute > 150 || to_sign < 1) + throw GradeTooLowException(); + if (to_execute < 1 || to_sign < 1) + throw GradeTooHighException(); +} + +Form::~Form() +{ + std::cout << "~Form()" << std::endl; +} + +Form& Form::operator=(const Form& src) +{ + this->_signed = src._signed; + this->_to_sign = src._to_sign; + this->_to_execute = src._to_execute; + + return *this; +} + +void Form::beSigned(const Bureaucrat &bureaucrat) +{ + if (this->_signed == true) + { + std::cout << bureaucrat.getName() << " couldn't sign " << this->_name << " because " << "the form is already sign" << std::endl; + return; + } + if (bureaucrat.getGrade() > this->_to_sign) + throw GradeTooLowException(); + this->_signed = true; + std::cout << bureaucrat.getName() << " signed " << this->_name << std::endl; +} + +const std::string& Form::getName() const +{ + return this->_name; +} + +bool Form::getSigned() const +{ + return this->_signed; +} + +int Form::getGradeToBeExecute() const +{ + return this->_to_execute; +} + +int Form::getGradeToBeSign() const +{ + return this->_to_sign; +} + +std::ostream& operator<<(std::ostream& os, const Form& src) +{ + os << src.getName() << " form signed: " << src.getSigned() << ", grade to be execute: " << src.getGradeToBeExecute() << ", grade to be sign: " << src.getGradeToBeSign() << "." << std::endl; + + return os; +} + +const char* Form::GradeTooLowException::what() const throw() +{ + return "grade is to low"; +} + +const char* Form::GradeTooHighException::what() const throw() +{ + return "grade is to high"; +} diff --git a/ex02/src/AForm.hpp b/ex02/src/AForm.hpp new file mode 100644 index 0000000..b7098f6 --- /dev/null +++ b/ex02/src/AForm.hpp @@ -0,0 +1,47 @@ +#pragma once + +#include +#include +#include + +#include "Bureaucrat.hpp" +class Bureaucrat; + +class AForm +{ + private: + const std::string _name; + bool _signed; + int _to_sign; + int _to_execute; + + public: + AForm(); + AForm(const std::string& name, int to_execute, int to_sign); + AForm(const AForm& src); + virtual ~AForm(); + + AForm& operator=(const AForm& src); + + const std::string& getName() const; + bool getSigned() const; + int getGradeToBeExecute() const; + int getGradeToBeSign() const; + + void beSigned(const Bureaucrat& bureaucrat); + + class GradeTooHighException: public std::exception + { + public: + const char * what() const throw(); + }; + + class GradeTooLowException: public std::exception + { + public: + const char * what() const throw(); + }; + +}; + +std::ostream& operator<<(std::ostream& os, const AForm& src); diff --git a/ex02/src/Bureaucrat.cpp b/ex02/src/Bureaucrat.cpp new file mode 100644 index 0000000..f2a0e98 --- /dev/null +++ b/ex02/src/Bureaucrat.cpp @@ -0,0 +1,88 @@ +#include "Bureaucrat.hpp" +#include +#include +#include + +Bureaucrat::Bureaucrat() +{ + std::cout << "Bureaucrat()" << std::endl; +} + +Bureaucrat::Bureaucrat(const Bureaucrat& src) +{ + *this = src; + std::cout << "Bureaucrat(Bureaucrat)" << std::endl; +} + +Bureaucrat::Bureaucrat(const std::string& name, int grade): + _name(name) +{ + setGrade(grade); + std::cout << "Bureaucrat(" << name << ", " << grade << ")" << std::endl; +} + +Bureaucrat::~Bureaucrat() +{ + std::cout << "~Bureaucrat()" << std::endl; +} + +Bureaucrat& Bureaucrat::operator=(const Bureaucrat &src) +{ + this->_grade = src._grade; + + return *this; +} + +void Bureaucrat::setGrade(int new_grade) +{ + if (new_grade > 150) + throw Bureaucrat::GradeTooLowException(); + if (new_grade < 1) + throw Bureaucrat::GradeTooHighException(); + this->_grade = new_grade; +} + +const std::string& Bureaucrat::getName(void) const +{ + return this->_name; +} + +int Bureaucrat::getGrade(void) const +{ + return this->_grade; +} + +void Bureaucrat::increment() +{ + if (this->_grade == 1) + throw GradeTooHighException(); + this->_grade--; +} + +void Bureaucrat::decrement() +{ + if (this->_grade == 150) + throw GradeTooLowException(); + this->_grade++; +} + +void Bureaucrat::signForm(Form& form) const +{ + form.beSigned(*this); +} + +const char* Bureaucrat::GradeTooLowException::what() const throw() +{ + return "Too low grade"; +} + +const char* Bureaucrat::GradeTooHighException::what() const throw() +{ + return "Too high grade"; +} + +std::ostream& operator<<(std::ostream &stream, Bureaucrat &src) +{ + stream << src.getName() << ", bureaucrat grade " << src.getGrade() << "." << std::endl; + return stream; +} diff --git a/ex02/src/Bureaucrat.hpp b/ex02/src/Bureaucrat.hpp new file mode 100644 index 0000000..2b4d8b4 --- /dev/null +++ b/ex02/src/Bureaucrat.hpp @@ -0,0 +1,45 @@ +#pragma once + +#include +#include + +#include "AForm.hpp" +class AForm; + +class Bureaucrat +{ + private: + const std::string _name; + int _grade; + + public: + Bureaucrat(); + ~Bureaucrat(); + Bureaucrat(const Bureaucrat& src); + Bureaucrat(const std::string& name, int grade); + + Bureaucrat& operator=(const Bureaucrat& src); + + const std::string& getName() const; + int getGrade() const; + void setGrade(int new_grade); + + void increment(); + void decrement(); + void signForm(AForm& form) const; + + class GradeTooHighException: public std::exception + { + public: + const char* what() const throw(); + }; + + class GradeTooLowException: public std::exception + { + public: + const char* what() const throw(); + }; + +}; + +std::ostream& operator<<(std::ostream& stream, Bureaucrat &src); diff --git a/ex02/src/main.cpp b/ex02/src/main.cpp new file mode 100644 index 0000000..b7ec015 --- /dev/null +++ b/ex02/src/main.cpp @@ -0,0 +1,186 @@ +#include "Bureaucrat.hpp" +#include + +int main() { + + std::cout << "-------------Bureaucrat TEST-----------" << std::endl; + std::cout << "Constructor too high grade" << std::endl; + { + try { + Bureaucrat crat("crat", 0); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "Constructor too low grade" << std::endl; + { + try { + Bureaucrat crat("crat", 151); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "increment too high" << std::endl; + { + Bureaucrat crat("crat", 1); + try { + crat.increment(); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "decrement too low" << std::endl; + { + Bureaucrat crat("crat", 150); + try { + crat.decrement(); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "setgrade too low" << std::endl; + { + Bureaucrat crat("crat", 42); + try { + crat.setGrade(151); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "setgrade too high" << std::endl; + { + Bureaucrat crat("crat", 42); + try { + crat.setGrade(0); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "increment normal" << std::endl; + { + Bureaucrat crat("crat", 42); + std::cout << "default grade: " << crat.getGrade() << std::endl; + crat.increment(); + std::cout << "after increment grade: " << crat.getGrade() << std::endl; + } + std::cout << std::endl; + + std::cout << "decrement normal" << std::endl; + { + Bureaucrat crat("crat", 42); + std::cout << "default grade: " << crat.getGrade() << std::endl; + crat.decrement(); + std::cout << "after decrement grade: " << crat.getGrade() << std::endl; + } + std::cout << std::endl; + + std::cout << "print" << std::endl; + { + Bureaucrat crat("crat", 42); + std::cout << crat; + } + std::cout << std::endl; + + std::cout << "------------- Form TEST-----------" << std::endl; + + std::cout << "Constructor too high sign grade" << std::endl; + { + try { + Form("form", 42, 0); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "Constructor too low sign grade" << std::endl; + { + try { + Form("form", 42, 151); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "Constructor too high exec grade" << std::endl; + { + try { + Form("form", 0, 42); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "Constructor too low exec grade" << std::endl; + { + try { + Form("form", 151, 42); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "print" << std::endl; + { + Form form("form", 42, 42); + std::cout << form; + } + std::cout << std::endl; + + std::cout << "------------- Form Sign TEST-----------" << std::endl; + + std::cout << "Bureaucrat sign a Form" << std::endl; + { + Form form("form", 42, 42); + Bureaucrat crat("crat", 42); + + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "Too low Bureaucrat sign a Form" << std::endl; + { + Form form("form", 42, 1); + Bureaucrat crat("crat", 42); + try { + crat.signForm(form); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + } + std::cout << std::endl; + + std::cout << "High Bureaucrat sign a low Form" << std::endl; + { + Form form("form", 42, 150); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + } + std::cout << std::endl; + + std::cout << "Form already sign" << std::endl; + { + Form form("form", 42, 150); + Bureaucrat crat("crat", 1); + + crat.signForm(form); + crat.signForm(form); + } + std::cout << std::endl; +}