From 9bea74be7ed2be4edaa30bb42897567dbaa31114 Mon Sep 17 00:00:00 2001 From: aortigos Date: Mon, 16 Feb 2026 23:36:12 +0100 Subject: [PATCH] ex00 --- ex00/Bureaucrat/Bureaucrat.cpp | 89 ++++++++++++++++++++++++++++ ex00/Bureaucrat/Bureaucrat.hpp | 52 +++++++++++++++++ ex00/Makefile | 43 ++++++++++++++ ex00/main.cpp | 104 +++++++++++++++++++++++++++++++++ 4 files changed, 288 insertions(+) create mode 100644 ex00/Bureaucrat/Bureaucrat.cpp create mode 100644 ex00/Bureaucrat/Bureaucrat.hpp create mode 100644 ex00/Makefile create mode 100644 ex00/main.cpp diff --git a/ex00/Bureaucrat/Bureaucrat.cpp b/ex00/Bureaucrat/Bureaucrat.cpp new file mode 100644 index 0000000..5c11c68 --- /dev/null +++ b/ex00/Bureaucrat/Bureaucrat.cpp @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 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; +} + +std::string Bureaucrat::getName() const +{ + return (this->name); +} + +int Bureaucrat::getGrade() const +{ + return (this->grade); +} + +void Bureaucrat::incrementGrade() +{ + if (this->grade <= 1) + throw GradeTooHighException(); + this->grade--; +} + +void Bureaucrat::decrementGrade() +{ + if (this->grade >= 150) + throw GradeTooLowException(); + this->grade++; +} + +const char* Bureaucrat::GradeTooHighException::what() const throw() +{ + return ("Grade is too high!"); +} + +const char* Bureaucrat::GradeTooLowException::what() const throw() +{ + return ("Grade is too low!"); +} + +std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat) +{ + os << bureaucrat.getName() + << ", bureaucrat grade " + << bureaucrat.getGrade() + << std::endl; + return (os); +} \ No newline at end of file diff --git a/ex00/Bureaucrat/Bureaucrat.hpp b/ex00/Bureaucrat/Bureaucrat.hpp new file mode 100644 index 0000000..39847ec --- /dev/null +++ b/ex00/Bureaucrat/Bureaucrat.hpp @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos + +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(); + + 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/ex00/Makefile b/ex00/Makefile new file mode 100644 index 0000000..2095011 --- /dev/null +++ b/ex00/Makefile @@ -0,0 +1,43 @@ +NAME = bureaucrat + +SRC = main.cpp \ + Bureaucrat/Bureaucrat.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/ex00/main.cpp b/ex00/main.cpp new file mode 100644 index 0000000..21efe7e --- /dev/null +++ b/ex00/main.cpp @@ -0,0 +1,104 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hadi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/02/16 22:32:41 by hadi #+# #+# */ +/* Updated: 2026/02/16 23:35:56 by hadi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat/Bureaucrat.hpp" + +int main() +{ + std::cout << "---- ex00 tests ----" << std::endl; + + try { + std::cout << "-> Bureaucrat with grade 0" << std::endl; + Bureaucrat hadi("Hadi", 0); + } catch (std::exception &e) { + std::cout << e.what() << std::endl << std::endl; + } + + try { + std::cout << "-> Bureaucrat with grade 1" << std::endl; + Bureaucrat hadi("Hadi", 1); + + std::cout << hadi << std::endl; + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + + try { + std::cout << "-> Bureaucrat with grade 150" << std::endl; + Bureaucrat hadi("Hadi", 150); + + std::cout << hadi << std::endl; + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } + + try { + std::cout << "-> Bureaucrat with grade 151" << std::endl; + Bureaucrat hadi("Hadi", 151); + + std::cout << hadi << std::endl; + } catch (std::exception &e) { + std::cout << e.what() << std::endl << std::endl; + } + + try { + std::cout << "-> Try increment grade from 1" << std::endl; + Bureaucrat hadi("Hadi", 1); + + std::cout << hadi; + + hadi.incrementGrade(); + std::cout << hadi; + } catch (std::exception &e) { + std::cout << e.what() << std::endl << std::endl; + } + + try { + std::cout << "-> Try increment grade from 2" << std::endl; + Bureaucrat hadi("Hadi", 2); + + std::cout << hadi; + + hadi.incrementGrade(); + std::cout << hadi; + } catch (std::exception &e) { + std::cout << e.what() << std::endl << std::endl; + } + + try { + std::cout << std::endl + << "-> Try decrement grade from 150" << std::endl; + Bureaucrat hadi("Hadi", 150); + + std::cout << hadi; + + hadi.decrementGrade(); + std::cout << hadi; + } catch (std::exception &e) { + std::cout << e.what() << std::endl << std::endl; + } + + try { + std::cout << "-> Try decrement grade from 149" << std::endl; + Bureaucrat hadi("Hadi", 149); + + std::cout << hadi; + hadi.decrementGrade(); + std::cout << hadi; + + } catch (std::exception &e) { + std::cout << e.what() << std::endl << std::endl; + } + + + return (0); +} \ No newline at end of file