diff --git a/ex02/AForm/AForm.cpp b/ex02/AForm/AForm.cpp new file mode 100644 index 0000000..974a5f3 --- /dev/null +++ b/ex02/AForm/AForm.cpp @@ -0,0 +1,124 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* AForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos 150 || gradeToExecute > 150) + throw GradeTooLowException(); + else if (gradeToSign < 1 || gradeToExecute < 1) + throw GradeTooHighException(); + //std::cout << "AForm constructor with params called" << std::endl; +} + +AForm::AForm(const AForm &other) : + name (other.getName()), isSigned(other.getIsSigned()), + gradeToSign(other.getGradeToSign()), + gradeToExecute(other.getGradeToExecute()) +{ + //std::cout << "AForm copy constructor called" << std::endl; +} + +AForm& AForm::operator=(const AForm &other) +{ + if (this != &other) + this->isSigned = other.getIsSigned(); + //std::cout << "AForm copy assigment operator called" << std::endl; + return (*this); +} + +AForm::~AForm() +{ + //std::cout << "Destructor called" +} + +////////////////// +// Getters // +////////////////// + +std::string AForm::getName() const +{ + return (this->name); +} + +bool AForm::getIsSigned() const +{ + return (this->isSigned); +} + +int AForm::getGradeToSign() const +{ + return (this->gradeToSign); +} + +int AForm::getGradeToExecute() const +{ + return (this->gradeToExecute); +} + + +////////////////// +// SignAForm // +////////////////// + +void AForm::beSigned(const Bureaucrat &br) +{ + if (this->gradeToSign < br.getGrade()) + throw GradeTooLowException(); + this->isSigned = true; +} + + +////////////////// +// << // +////////////////// + +std::ostream& operator<<(std::ostream &os, const AForm &form) +{ + std::string isItSigned; + + if (form.getIsSigned()) + { + isItSigned = " is signed."; + } else { + isItSigned = " is not signed."; + } + + os << form.getName() << isItSigned; + return (os); +} + + +////////////////// +// Excepciones // +////////////////// +const char* AForm::GradeTooLowException::what() const throw() +{ + return ("grade too low"); +} + +const char* AForm::GradeTooHighException::what() const throw() +{ + return ("grade too high"); +} \ No newline at end of file diff --git a/ex02/AForm/AForm.hpp b/ex02/AForm/AForm.hpp new file mode 100644 index 0000000..b7bc942 --- /dev/null +++ b/ex02/AForm/AForm.hpp @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* AForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +# include "../Bureaucrat/Bureaucrat.hpp" + +class AForm +{ + private: + const std::string name; + bool isSigned; + const int gradeToSign; + const int gradeToExecute; + + public: + AForm(); + AForm(const AForm &other); + AForm& operator=(const AForm &other); + ~AForm(); + + AForm(std::string name, bool isSigned, int gradeToSign, int gradeToExecute); + + std::string getName() const; + bool getIsSigned() const; + int getGradeToSign() const; + int getGradeToExecute() const; + + void beSigned(const Bureaucrat &br); + + class GradeTooLowException : public std::exception { + public: + virtual const char *what() const throw(); + }; + + class GradeTooHighException : public std::exception { + public: + virtual const char *what() const throw(); + }; +}; + +std::ostream& operator<<(std::ostream &os, const AForm &AForm); + +#endif diff --git a/ex02/AForm/AForm.o b/ex02/AForm/AForm.o new file mode 100644 index 0000000..959189e Binary files /dev/null and b/ex02/AForm/AForm.o differ diff --git a/ex02/Bureaucrat/Bureaucrat.cpp b/ex02/Bureaucrat/Bureaucrat.cpp new file mode 100644 index 0000000..cf31d17 --- /dev/null +++ b/ex02/Bureaucrat/Bureaucrat.cpp @@ -0,0 +1,136 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos grade = other.getGrade(); + //std::cout << "Copy operator called" << std::endl; + return (*this); +} + +Bureaucrat::~Bureaucrat() +{ + //std::cout << "Bureaucrat destroyed" << std::endl; +} + +Bureaucrat::Bureaucrat(std::string name, int grade) : name(name) +{ + if (grade > 150) + throw GradeTooLowException(); + else if (grade < 1) + throw GradeTooHighException(); + this->grade = grade; + //std::cout << "Bureaucrat with params has been created" << std::endl; +} + + +////////////////// +// Getters // +////////////////// + +std::string Bureaucrat::getName() const +{ + return (this->name); +} + +int Bureaucrat::getGrade() const +{ + return (this->grade); +} + + +////////////////// +// Grade // +////////////////// + +void Bureaucrat::incrementGrade() +{ + if (this->grade <= 1) + throw GradeTooHighException(); + this->grade--; +} + +void Bureaucrat::decrementGrade() +{ + if (this->grade >= 150) + throw GradeTooLowException(); + this->grade++; +} + +////////////////// +// Sign // +////////////////// + +void Bureaucrat::signForm(AForm &form) +{ + try { + form.beSigned(*this); + std::cout << this->name + << " signed " + << form.getName() + << std::endl; + } catch (std::exception &e) { + std::cout << this->name + << " couldn't sign " + << form.getName() + << " because " + << e.what() + << std::endl; + } +} + + +////////////////// +// << // +////////////////// + +std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat) +{ + os << bureaucrat.getName() + << ", bureaucrat grade " + << bureaucrat.getGrade() + << std::endl; + return (os); +} + + +////////////////// +// Excepciones // +////////////////// + +const char* Bureaucrat::GradeTooHighException::what() const throw() +{ + return ("Grade is too high!"); +} + +const char* Bureaucrat::GradeTooLowException::what() const throw() +{ + return ("Grade is too low!"); +} diff --git a/ex02/Bureaucrat/Bureaucrat.hpp b/ex02/Bureaucrat/Bureaucrat.hpp new file mode 100644 index 0000000..1b9a493 --- /dev/null +++ b/ex02/Bureaucrat/Bureaucrat.hpp @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos + +class AForm; + +class Bureaucrat +{ + private: + const std::string name; + int grade; + + public: + Bureaucrat(); + Bureaucrat(const Bureaucrat &other); + Bureaucrat &operator=(const Bureaucrat &other); + ~Bureaucrat(); + + Bureaucrat(std::string name, int grade); + + std::string getName() const; + int getGrade() const; + + void incrementGrade(); + void decrementGrade(); + + void signForm(AForm &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, const Bureaucrat &bureaucrat); + +#endif diff --git a/ex02/Bureaucrat/Bureaucrat.o b/ex02/Bureaucrat/Bureaucrat.o new file mode 100644 index 0000000..b281c93 Binary files /dev/null and b/ex02/Bureaucrat/Bureaucrat.o differ diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..e2077aa --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,43 @@ +NAME = form + +SRC = main.cpp \ + Bureaucrat/Bureaucrat.cpp AForm/AForm.cpp + +OBJ = $(SRC:.cpp=.o) + +CC = c++ +CFLAGS = -Wall -Wextra -Werror -std=c++98 + +GREEN = \033[0;32m +RED = \033[0;31m +RESET = \033[0m + +TOTAL := $(words $(SRC)) +COUNT = 0 + +all: $(NAME) + +$(NAME): $(OBJ) + @rm -f .build_start + @$(CC) $(CFLAGS) $(OBJ) -o $(NAME) + @printf "\n$(GREEN)✔ Listo (100%%)\n$(RESET)" + +%.o: %.cpp + @if [ ! -f .build_start ]; then printf "$(GREEN)Compilando archivos...\n$(RESET)"; touch .build_start; fi + @$(eval COUNT = $(shell echo $$(($(COUNT)+1)))) + @PERCENT=$$(($(COUNT)*100/$(TOTAL))); \ + BAR=$$(printf "%0.s#" $$(seq 1 $$((PERCENT/5)))); \ + SPACE=$$(printf "%0.s " $$(seq 1 $$((20-PERCENT/5)))); \ + printf "\r [$$BAR$$SPACE] %3d%% (%d/%d) $< " $$PERCENT $(COUNT) $(TOTAL) + @$(CC) $(CFLAGS) -c $< -o $@ + +clean: + @printf "$(RED)Eliminando...\n$(RESET)" + @rm -f $(OBJ) + +fclean: clean + @rm -f $(NAME) + +re: fclean all + +.PHONY: all clean fclean re diff --git a/ex02/form b/ex02/form new file mode 100755 index 0000000..8ca75c8 Binary files /dev/null and b/ex02/form differ diff --git a/ex02/main.cpp b/ex02/main.cpp new file mode 100644 index 0000000..81b7573 --- /dev/null +++ b/ex02/main.cpp @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hadi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/02/16 22:32:41 by hadi #+# #+# */ +/* Updated: 2026/02/17 09:56:07 by hadi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat/Bureaucrat.hpp" +#include "AForm/AForm.hpp" + +int main() +{ + std::cout << "---- ex01 tests ----" << std::endl; + + try { + AForm form("AngelForm", false, 139, 5); + Bureaucrat angel("Angel", 140); + + std::cout << form << std::endl; + angel.signForm(form); + std::cout << form << std::endl; + + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + + std::cout << std::endl; + + return (0); +} \ No newline at end of file diff --git a/ex02/main.o b/ex02/main.o new file mode 100644 index 0000000..0b0265a Binary files /dev/null and b/ex02/main.o differ