feat: finishing the form part
This commit is contained in:
parent
6790f1c03c
commit
5829da2bcb
7 changed files with 516 additions and 0 deletions
126
cpp05/ex01/Bureaucrat.cpp
Normal file
126
cpp05/ex01/Bureaucrat.cpp
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Bureaucrat.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/16 14:49:52 by rparodi #+# #+# */
|
||||
/* Updated: 2025/03/17 16:59:03 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Bureaucrat.hpp"
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
|
||||
/**
|
||||
* @brief Constructor by default of Bureaucrat
|
||||
*/
|
||||
Bureaucrat::Bureaucrat(void) : _name("Roger"), _grade(150) {
|
||||
std::cout << "[Bureaucrat] Default constructor called" << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Better constructor easier to test
|
||||
*
|
||||
* @param name the name of the Bureaucrat
|
||||
* @param grade grade to start the carreer
|
||||
*/
|
||||
Bureaucrat::Bureaucrat(std::string name, int grade) : _name(name) {
|
||||
std::cout << "[Bureaucrat] Smart constructor called" << std::endl;
|
||||
if (grade < 1) {
|
||||
throw GradeTooHighException();
|
||||
} else if (grade > 150) {
|
||||
throw GradeTooLowException();
|
||||
} else {
|
||||
this->_grade = grade;
|
||||
}
|
||||
}
|
||||
|
||||
Bureaucrat::Bureaucrat(Bureaucrat const & copy) : _name(copy._name), _grade(copy._grade) {
|
||||
std::cout << "[Bureaucrat] Copy constructor called" << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Constructor with the operator '=' (Overload)
|
||||
*
|
||||
* @param assign Bureaucrat to copie
|
||||
* @return It's a constructor
|
||||
*/
|
||||
Bureaucrat& Bureaucrat::operator=(Bureaucrat const & assign) {
|
||||
std::cout << "[Bureaucrat] Assign constructor called" << std::endl;
|
||||
this->_grade = assign._grade;
|
||||
this->_name = assign._name;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Destructor for the Bureaucrat
|
||||
*/
|
||||
Bureaucrat::~Bureaucrat() {
|
||||
std::cout << "[Bureaucrat] Desctuctor called" << std::endl;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Getter for the grade
|
||||
*
|
||||
* @return grade (unsigned int 8bits)
|
||||
*/
|
||||
int Bureaucrat::getGrade() const {
|
||||
return (this->_grade);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Getter for the name
|
||||
*
|
||||
* @return name (string from standard)
|
||||
*/
|
||||
std::string Bureaucrat::getName() const {
|
||||
return (this->_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get a promotion for the Bureaucrat
|
||||
*/
|
||||
void Bureaucrat::promote() {
|
||||
if (this->_grade == 1) {
|
||||
throw GradeTooHighException();
|
||||
} else {
|
||||
--this->_grade;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get a demotion for the Bureaucrat
|
||||
*/
|
||||
void Bureaucrat::demote() {
|
||||
if (this->_grade == 150) {
|
||||
throw GradeTooLowException();
|
||||
} else {
|
||||
++this->_grade;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @brief Overload of the '<<' operator to print
|
||||
*
|
||||
* @param output previous output
|
||||
* @param toPrint Bureaucrat to print
|
||||
* @return the output with the Bureaucrat
|
||||
*/
|
||||
std::ostream& operator<<(std::ostream& output, Bureaucrat const &toPrint) {
|
||||
output << toPrint.getName() << ", bureaucrat grade " << toPrint.getGrade() << ".";
|
||||
return (output);
|
||||
}
|
||||
|
||||
void Bureaucrat::signForm(Form const &form) {
|
||||
if (form.isSigned()) {
|
||||
std::cerr << this->getName() << " couldn’t sign " << form.getName() << " because form is already signed !" << std::endl;
|
||||
} else {
|
||||
if (form.getSign() < this->_grade) {
|
||||
throw GradeTooLowException();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
54
cpp05/ex01/Bureaucrat.hpp
Normal file
54
cpp05/ex01/Bureaucrat.hpp
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Bureaucrat.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/16 14:48:33 by rparodi #+# #+# */
|
||||
/* Updated: 2025/03/17 17:06:04 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef BUREAUCRAT_HPP
|
||||
#define BUREAUCRAT_HPP
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include "Form.hpp"
|
||||
|
||||
#define RESET "\033[0m"
|
||||
#define YELLOW "\033[0;33m"
|
||||
#define RED "\033[0;31m"
|
||||
|
||||
class Bureaucrat {
|
||||
public:
|
||||
Bureaucrat(void);
|
||||
Bureaucrat(std::string name, int grade);
|
||||
Bureaucrat(Bureaucrat const ©);
|
||||
Bureaucrat& operator=(Bureaucrat const &assign);
|
||||
~Bureaucrat(void);
|
||||
std::string getName(void) const;
|
||||
int getGrade(void) const;
|
||||
void promote(void);
|
||||
void demote(void);
|
||||
void signForm(Form const &form);
|
||||
class GradeTooHighException : public std::exception {
|
||||
virtual const char *what() const throw() {
|
||||
return ("Error:\n> Grade is too high (have to be lower than 1)");
|
||||
}
|
||||
};
|
||||
class GradeTooLowException : public std::exception {
|
||||
virtual const char *what() const throw() {
|
||||
return ("Error:\n> Grade is too low (have to be higher than 150)");
|
||||
}
|
||||
};
|
||||
private:
|
||||
std::string _name;
|
||||
int _grade;
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& output, Bureaucrat const &bureaucrat);
|
||||
|
||||
#endif
|
||||
89
cpp05/ex01/Form.cpp
Normal file
89
cpp05/ex01/Form.cpp
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Form.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/17 14:19:41 by rparodi #+# #+# */
|
||||
/* Updated: 2025/03/17 20:17:10 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Form.hpp"
|
||||
#include "Bureaucrat.hpp"
|
||||
|
||||
/**
|
||||
* @brief Constructor by default of Form
|
||||
*/
|
||||
Form::Form(void) : _name("Default"), _signed(false), _to_execute(150), _to_sign(150) {
|
||||
std::cout << "[Form] Default constructor called" << std::endl;
|
||||
}
|
||||
Form::Form(std::string const name, int execute, int sign) : _name(name), _signed(false) {
|
||||
if (execute < 1) {
|
||||
throw GradeTooHighException();
|
||||
} else if (execute > 150) {
|
||||
throw GradeTooLowException();
|
||||
} else {
|
||||
this->_to_execute = execute;
|
||||
}
|
||||
if (sign < 1) {
|
||||
throw GradeTooHighException();
|
||||
} else if (sign > 150) {
|
||||
throw GradeTooLowException();
|
||||
} else {
|
||||
this->_to_sign = sign;
|
||||
}
|
||||
std::cout << "[Form] Smart constructor called" << std::endl;
|
||||
}
|
||||
|
||||
Form::Form(Form const ©) {
|
||||
std::cout << "[Form] Copy constructor called" << std::endl;
|
||||
this->_name = copy._name;
|
||||
this->_to_sign = copy._to_sign;
|
||||
this->_to_execute = copy._to_execute;
|
||||
_signed = copy._signed;
|
||||
}
|
||||
|
||||
Form& Form::operator=(Form const &assign) {
|
||||
std::cout << "[Form] Assign constructor called" << std::endl;
|
||||
this->_name = assign._name;
|
||||
this->_to_sign = assign._to_sign;
|
||||
this->_to_execute = assign._to_execute;
|
||||
this->_signed = assign._signed;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Form::~Form() {
|
||||
std::cout << "[Form] Destructor called" << std::endl;
|
||||
}
|
||||
|
||||
std::string Form::getName() const {
|
||||
return this->_name;
|
||||
}
|
||||
|
||||
int Form::getExecute() const {
|
||||
return this->_to_execute;
|
||||
}
|
||||
|
||||
int Form::getSign() const {
|
||||
return this->_to_sign;
|
||||
}
|
||||
|
||||
bool Form::isSigned() const {
|
||||
return this->_signed;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& output, Form const &toPrint) {
|
||||
output << toPrint.getName() << ", Form\n\tTo execute: " << toPrint.getExecute() << "\n\tTo sign: " << toPrint.getSign() << "\n\tAlready signed: " << (toPrint.isSigned() ? "✅ | Yes" : "❌ | No");
|
||||
return (output);
|
||||
}
|
||||
|
||||
void Form::beSigned(Bureaucrat &bureaucrat) {
|
||||
try {
|
||||
bureaucrat.signForm(*this);
|
||||
} catch (std::exception & err) {
|
||||
std::cerr << err.what() << std::endl;
|
||||
}
|
||||
_signed = true;
|
||||
}
|
||||
52
cpp05/ex01/Form.hpp
Normal file
52
cpp05/ex01/Form.hpp
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Form.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/17 14:20:06 by rparodi #+# #+# */
|
||||
/* Updated: 2025/03/17 20:17:37 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef FORM_HPP
|
||||
#define FORM_HPP
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
class Bureaucrat;
|
||||
|
||||
class Form {
|
||||
public:
|
||||
Form();
|
||||
Form(std::string const name, int execute, int sign);
|
||||
Form(Form const ©);
|
||||
Form& operator=(Form const &assign);
|
||||
~Form();
|
||||
class GradeTooHighException : public std::exception {
|
||||
virtual const char *what() const throw() {
|
||||
return ("Error:\n> Grade is too high (have to be lower than 1)");
|
||||
}
|
||||
};
|
||||
class GradeTooLowException : public std::exception {
|
||||
virtual const char *what() const throw() {
|
||||
return ("Error:\n> Grade is too low (have to be higher than 150)");
|
||||
}
|
||||
};
|
||||
std::string getName() const;
|
||||
void beSigned(Bureaucrat &bureaucrat);
|
||||
int getExecute() const;
|
||||
int getSign() const;
|
||||
bool isSigned() const;
|
||||
private:
|
||||
std::string _name;
|
||||
bool _signed;
|
||||
int _to_execute;
|
||||
int _to_sign;
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& output, Form const &Form);
|
||||
|
||||
#endif
|
||||
126
cpp05/ex01/Makefile
Normal file
126
cpp05/ex01/Makefile
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
# **************************************************************************** #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
|
||||
# Updated: 2024/12/21 19:50:58 by rparodi ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
# Variables
|
||||
|
||||
# Name
|
||||
NAME = form
|
||||
|
||||
# Commands
|
||||
CXX = c++
|
||||
RM = rm -rf
|
||||
|
||||
# Flags
|
||||
# Mandatory flags for 42
|
||||
CXXFLAGS = -Werror -Wextra -Wall -std=c++98 -I./includes/
|
||||
# Flags to have the dependences (can be removed for correction)
|
||||
CXXFLAGS += -MMD
|
||||
# Flags to have the debug (can be removed for correction)
|
||||
DEBUG += -g3 -fsanitize=address
|
||||
CXXFLAGS += $(DEBUG)
|
||||
|
||||
# Sources
|
||||
SRC = Bureaucrat.cpp \
|
||||
Form.cpp \
|
||||
main.cpp
|
||||
|
||||
# Objects
|
||||
OBJDIRNAME = ./build
|
||||
OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.cpp=.o))
|
||||
|
||||
# Colors
|
||||
GREEN = \033[32m
|
||||
GREY = \033[0;90m
|
||||
RED = \033[0;31m
|
||||
GOLD = \033[38;5;220m
|
||||
END = \033[0m
|
||||
|
||||
# Rules
|
||||
|
||||
# All (make all)
|
||||
all: header $(NAME) footer
|
||||
|
||||
# Bonus (make bonus)
|
||||
bonus: header $(OBJ) footer
|
||||
@mkdir -p $(OBJDIRNAME)
|
||||
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
|
||||
@$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ)
|
||||
|
||||
# Clean (make clean)
|
||||
clean:
|
||||
@printf '$(GREY) Removing $(END)$(RED)Objects$(END)\n'
|
||||
@printf '$(GREY) Removing $(END)$(RED)Objects Folder$(END)\n'
|
||||
@$(RM) $(OBJDIRNAME)
|
||||
|
||||
# Clean (make fclean)
|
||||
fclean: clean
|
||||
@printf '$(GREY) Removing $(END)$(RED)Program$(END)\n'
|
||||
@$(RM) $(NAME)
|
||||
@echo ""
|
||||
|
||||
# Restart (make re)
|
||||
re: header fclean all
|
||||
|
||||
# Dependences for all
|
||||
$(NAME): $(OBJ)
|
||||
@mkdir -p $(OBJDIRNAME)
|
||||
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
|
||||
@$(CXX) $(CXXFLAGS) -o $(NAME) $(OBJ)
|
||||
|
||||
# Creating the objects
|
||||
$(OBJDIRNAME)/%.o: %.cpp
|
||||
@mkdir -p $(dir $@)
|
||||
@printf '$(GREY) Compiling $(END)$(GREEN)$<$(END)\n'
|
||||
@$(CXX) $(CXXFLAGS) -o $@ -c $<
|
||||
|
||||
# Header
|
||||
header:
|
||||
@clear
|
||||
@printf '\n\n'
|
||||
@printf '$(GOLD) ******* ****** ******* $(END)\n'
|
||||
@printf '$(GOLD) ****** *** ******* $(END)\n'
|
||||
@printf '$(GOLD) ******* * ******* $(END)\n'
|
||||
@printf '$(GOLD) ****** ******* $(END)\n'
|
||||
@printf '$(GOLD) ******* ******* $(END)\n'
|
||||
@printf '$(GOLD) ******************* ******* * $(END)\n'
|
||||
@printf '$(GOLD) ******************* ******* *** $(END)\n'
|
||||
@printf '$(GOLD) ****** ******* ****** $(END)\n'
|
||||
@printf '$(GOLD) ****** $(END)\n'
|
||||
@printf '$(GOLD) ****** $(END)\n'
|
||||
@printf '$(GREY) Made by rparodi$(END)\n\n'
|
||||
|
||||
# Footer
|
||||
footer:
|
||||
@printf "\n"
|
||||
@printf "$(GOLD) ,_ _,$(END)\n"
|
||||
@printf "$(GOLD) | \\___//|$(END)\n"
|
||||
@printf "$(GOLD) |=6 6=|$(END)\n"
|
||||
@printf "$(GOLD) \\=._Y_.=/$(END)\n"
|
||||
@printf "$(GOLD) ) \` ( ,$(END)\n"
|
||||
@printf "$(GOLD) / \\ (('$(END)\n"
|
||||
@printf "$(GOLD) | | ))$(END)\n"
|
||||
@printf "$(GOLD) /| | | |\\_//$(END)\n"
|
||||
@printf "$(GOLD) \\| |._.| |/-\`$(END)\n"
|
||||
@printf "$(GOLD) '\"' '\"'$(END)\n"
|
||||
@printf ' $(GREY)The compilation is$(END) $(GOLD)finish$(END)\n $(GREY)Have a good $(END)$(GOLD)correction !$(END)\n'
|
||||
|
||||
clangd:
|
||||
@echo \
|
||||
"CompileFlags:\n" \
|
||||
" Add:\n" \
|
||||
" - \"-std=c++98 -Wall -Wextra -Werror\"\n" \
|
||||
" - \"-I"$(shell pwd)"/includes\"\n" \
|
||||
" - \"-xc++\"\n" \
|
||||
> .clangd
|
||||
|
||||
# Phony
|
||||
.PHONY: all bonus clean fclean re clangd
|
||||
-include ${OBJ:.o=.d}
|
||||
68
cpp05/ex01/main.cpp
Normal file
68
cpp05/ex01/main.cpp
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/16 14:47:26 by rparodi #+# #+# */
|
||||
/* Updated: 2025/03/17 16:37:04 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Bureaucrat.hpp"
|
||||
#include "Form.hpp"
|
||||
#include <ostream>
|
||||
|
||||
int main(void) {
|
||||
|
||||
std::cout << YELLOW << "\t\t[ Testing with the grade 0 ]" << RESET << std::endl;
|
||||
try {
|
||||
Bureaucrat b1("Roger", 0);
|
||||
} catch (std::exception & err) {
|
||||
std::cerr << RED << err.what() << RESET << std::endl;
|
||||
}
|
||||
|
||||
std::cout << std::endl << YELLOW << "\t\t[ Testing with the grade 151 ]" << RESET << std::endl;
|
||||
try {
|
||||
Bureaucrat b1("Robert", 151);
|
||||
} catch (std::exception & err) {
|
||||
std::cerr << RED << err.what() << RESET << std::endl;
|
||||
}
|
||||
|
||||
Bureaucrat b1("Bob", 150);
|
||||
std::cout << std::endl << YELLOW << "\t\t[ Testing with the demote a Bureaucrate level 150 ]" << RESET << std::endl;
|
||||
try {
|
||||
b1.demote();
|
||||
} catch (std::exception & err) {
|
||||
std::cerr << RED << err.what() << RESET << std::endl;
|
||||
}
|
||||
std::cout << std::endl << std::endl << b1 << std::endl << std::endl;
|
||||
std::cout << std::endl << YELLOW << "\t\t[ Testing with the promote a Bureaucrate level 150 ]" << RESET << std::endl;
|
||||
try {
|
||||
b1.promote();
|
||||
} catch (std::exception & err) {
|
||||
std::cerr << RED << err.what() << RESET << std::endl;
|
||||
}
|
||||
std::cout << std::endl << std::endl << b1 << std::endl << std::endl;
|
||||
|
||||
Bureaucrat b2("Norminet", 1);
|
||||
std::cout << std::endl << YELLOW << "\t\t[ Testing with the promote a Bureaucrate level 1 ]" << RESET << std::endl;
|
||||
try {
|
||||
b2.promote();
|
||||
} catch (std::exception & err) {
|
||||
std::cerr << RED << err.what() << RESET << std::endl;
|
||||
}
|
||||
std::cout << std::endl << std::endl << b2 << std::endl << std::endl;
|
||||
std::cout << std::endl << YELLOW << "\t\t[ Testing with the demote a Bureaucrate level 1 ]" << RESET << std::endl;
|
||||
try {
|
||||
b2.demote();
|
||||
} catch (std::exception & err) {
|
||||
std::cerr << RED << err.what() << RESET << std::endl;
|
||||
}
|
||||
std::cout << std::endl << std::endl << b2 << std::endl << std::endl;
|
||||
|
||||
std::cout << std::endl << YELLOW << "\t\t[ Testing the overload of the '<<' operator ]" << RESET << std::endl;
|
||||
Form f1("Norme to cpp", 1, 1);
|
||||
std::cout << f1 << std::endl;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue