diff --git a/d05/ex00/Bureaucrate.hpp b/d05/ex00/Bureaucrate.hpp deleted file mode 100644 index 46677db..0000000 --- a/d05/ex00/Bureaucrate.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef BUREAUCRATE_HPP -# define BUREAUCRATE_HPP - -class Bureaucrate { -public: - Bureaucrate(); - Bureaucrate(std::string name, int grade); - -private: - std::string const _name; - int _grade; -} - -#endif diff --git a/d05/ex00/Makefile b/d05/ex00/Makefile index ee31047..deba33f 100644 --- a/d05/ex00/Makefile +++ b/d05/ex00/Makefile @@ -4,7 +4,7 @@ # . name is case sensitive . ?= set if not already set # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # -NAME = bureaucrate +NAME = bureaucrat #CC = gcc CXX = c++ @@ -21,12 +21,12 @@ LIBS = INCLUDES = -I$(D_HEADERS) -D_SRCS = . +D_SRCS = srcs SRCS = main.cpp \ - Bureaucrate.cpp + Bureaucrat.cpp -D_HEADERS = . -HEADERS = Bureaucrate.hpp +D_HEADERS = headers +HEADERS = Bureaucrat.hpp D_OBJS = builds OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) diff --git a/d05/ex00/bureaucrat b/d05/ex00/bureaucrat new file mode 100755 index 0000000..7a8a4e8 Binary files /dev/null and b/d05/ex00/bureaucrat differ diff --git a/d05/ex00/headers/Bureaucrat.hpp b/d05/ex00/headers/Bureaucrat.hpp new file mode 100644 index 0000000..40359b4 --- /dev/null +++ b/d05/ex00/headers/Bureaucrat.hpp @@ -0,0 +1,46 @@ +#ifndef BUREAUCRAT_HPP +# define BUREAUCRAT_HPP + +# include "color.h" +# include +# include +# include + +class Bureaucrat { + +public: + + Bureaucrat(std::string name, int grade); + Bureaucrat( Bureaucrat const & src ); + ~Bureaucrat(); + Bureaucrat & operator=( Bureaucrat const & rhs ); + + class GradeTooHighException : public std::exception { + public: + const char * what() const throw(); + }; + class GradeTooLowException : public std::exception { + public: + const char * what() const throw(); + }; + + std::string getName() const; + int getGrade() const; + + void gradeUp(); + void gradeDown(); + +protected: + + std::string const _name; + int _grade; + +private: + + Bureaucrat(); +}; + +std::ostream & operator<<(std::ostream & o, Bureaucrat const & rhs); + +#endif + diff --git a/d05/ex00/headers/color.h b/d05/ex00/headers/color.h new file mode 100644 index 0000000..f40596b --- /dev/null +++ b/d05/ex00/headers/color.h @@ -0,0 +1,25 @@ +#ifndef COLOR_H +# define COLOR_H + +# define GRAY "\e[0;30m" +# define RED "\e[0;31m" +# define GREEN "\e[0;32m" +# define YELLOW "\e[0;33m" +# define BLUE "\e[0;34m" +# define PURPLE "\e[0;35m" +# define CYAN "\e[0;36m" +# define WHITE "\e[0;37m" + +# define B_GRAY "\e[1;30m" +# define B_RED "\e[1;31m" +# define B_GREEN "\e[1;32m" +# define B_YELLOW "\e[1;33m" +# define B_BLUE "\e[1;34m" +# define B_PURPLE "\e[1;35m" +# define B_CYAN "\e[1;36m" +# define B_WHITE "\e[1;37m" + +# define RESET "\e[0m" + +#endif + diff --git a/d05/ex00/main.cpp b/d05/ex00/main.cpp index 8f37d92..8b07aa5 100644 --- a/d05/ex00/main.cpp +++ b/d05/ex00/main.cpp @@ -1,10 +1,79 @@ +#include "Bureaucrat.hpp" +#define N_TEST "5" + int main() { - try + + std::cout << B_YELLOW "\n[1/" N_TEST "] test too high :" RESET "\n"; { - + try { + Bureaucrat b("clarence", 151); + } + catch (std::exception& e) { + std::cout << e.what() << "\n"; + } } - catch (std::exception & e) + + std::cout << B_YELLOW "\n[2/" N_TEST "] test too low :" RESET "\n"; { - + try { + Bureaucrat b("ellen", 0); + } + catch (std::exception& e) { + std::cout << e.what() << "\n"; + } } + + std::cout << B_YELLOW "\n[3/" N_TEST "] test change grade :" RESET "\n"; + { + try { + Bureaucrat b("pacome", 51); + std::cout << b << "\n"; + b.gradeDown(); + std::cout << b << "\n"; + b.gradeUp(); + std::cout << b << "\n"; + } + catch (std::exception& e) { + std::cout << e.what() << "\n"; + } + } + + std::cout << B_YELLOW "\n[4/" N_TEST "] test reduce grade :" RESET "\n"; + { + try + { + Bureaucrat b("colin", 145); + + while (true) + { + b.gradeDown(); + std::cout << b << "\n"; + } + } + catch (std::exception& e) + { + std::cout << e.what() << "\n"; + } + } + + std::cout << B_YELLOW "\n[5/" N_TEST "] test increase grade :" RESET "\n"; + { + try + { + Bureaucrat b("colin", 5); + + while (true) + { + b.gradeUp(); + std::cout << b << "\n"; + } + } + catch (std::exception& e) + { + std::cout << e.what() << "\n"; + } + } + + std::cout << "\n"; + return 0; } diff --git a/d05/ex00/srcs/Bureaucrat.cpp b/d05/ex00/srcs/Bureaucrat.cpp new file mode 100644 index 0000000..8d683d8 --- /dev/null +++ b/d05/ex00/srcs/Bureaucrat.cpp @@ -0,0 +1,87 @@ +#include "Bureaucrat.hpp" + +#define COPLIEN_COLOR B_CYAN + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +Bureaucrat::Bureaucrat( std::string name, int grade ) +: _name(name) +, _grade(grade) { + if (grade > 150) + throw GradeTooLowException(); + if (grade < 1) + throw GradeTooHighException(); + std::cout << COPLIEN_COLOR "Bureaucrat constructor" RESET "\n"; + return; +} + +Bureaucrat::Bureaucrat( Bureaucrat const & src ) { + std::cout << COPLIEN_COLOR "Bureaucrat copy constructor" RESET "\n"; + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +Bureaucrat::~Bureaucrat() { + std::cout << COPLIEN_COLOR "Bureaucrat destructor" RESET "\n"; + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +Bureaucrat & Bureaucrat::operator=( Bureaucrat const & rhs ) { + if ( this != &rhs ) + _grade = rhs.getGrade(); + return *this; +} + +std::ostream & operator<<(std::ostream & o, Bureaucrat const & rhs) +{ + o + << rhs.getName() << ", bureaucrat grade " + << rhs.getGrade(); + return (o); +} + +/********************************************* + * ACCESSORS + *********************************************/ + +std::string Bureaucrat::getName() const {return _name;} +int Bureaucrat::getGrade() const {return _grade;} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +void Bureaucrat::gradeUp() { + if (_grade > 1) + _grade--; + else + throw GradeTooHighException(); +} + +void Bureaucrat::gradeDown() { + if (_grade < 150) + _grade++; + else + throw GradeTooLowException(); +} + +/********************************************* + * NESTED CLASS + *********************************************/ + +const char * Bureaucrat::GradeTooHighException::what() const throw() { + return ("grade higher than 1"); +} +const char * Bureaucrat::GradeTooLowException::what() const throw() { + return ("grade lower than 150"); +}