From 50ae5fa4e5f22933f3b9720af2db6a484c51143f Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 16 Jan 2025 17:35:39 +0100 Subject: [PATCH] Copy ex01, change Form to AForm, add functions --- ex02/AForm.cpp | 87 ++++++++++++++++++++++ ex02/AForm.h | 36 +++++++++ ex02/Bureaucrat.cpp | 137 +++++++++++++++++++++++++++++++++++ ex02/Bureaucrat.h | 61 ++++++++++++++++ ex02/Makefile | 54 ++++++++++++++ ex02/ShruberryCreationForm.h | 25 +++++++ ex02/main.cpp | 11 +++ 7 files changed, 411 insertions(+) create mode 100644 ex02/AForm.cpp create mode 100644 ex02/AForm.h create mode 100644 ex02/Bureaucrat.cpp create mode 100644 ex02/Bureaucrat.h create mode 100644 ex02/Makefile create mode 100644 ex02/ShruberryCreationForm.h create mode 100644 ex02/main.cpp diff --git a/ex02/AForm.cpp b/ex02/AForm.cpp new file mode 100644 index 0000000..56f714b --- /dev/null +++ b/ex02/AForm.cpp @@ -0,0 +1,87 @@ +#include "AForm.h" +#include "Bureaucrat.h" + +AForm::AForm() + : m_name("") + , m_is_signed(0) + , m_signatory_grade(0) + , m_executor_grade(0) +{ +} + +AForm::AForm(const std::string &name, int signatory_grade, int executor_grade) + : m_name(name) + , m_is_signed(0) + , m_signatory_grade(signatory_grade) + , m_executor_grade(executor_grade) +{ + if (signatory_grade < Bureaucrat::HIGHEST_GRADE || executor_grade < Bureaucrat::HIGHEST_GRADE) + throw (Bureaucrat::GradeTooHighException()); + else if (signatory_grade > Bureaucrat::LOWEST_GRADE || executor_grade > Bureaucrat::LOWEST_GRADE) + throw (Bureaucrat::GradeTooLowException()); +} + +AForm::AForm(const AForm &other) + : m_name(other.m_name) + , m_is_signed(0) + , m_signatory_grade(other.m_signatory_grade) + , m_executor_grade(other.m_executor_grade) +{ +} + +AForm::~AForm() { } + +AForm &AForm::operator=(const AForm &other) +{ + if (this == &other) + return (*this); + return (*this); +} + +const std::string &AForm::getName() const +{ + return (m_name); +} + +bool AForm::isSigned() const +{ + return (m_is_signed); +} + +int AForm::getSignatoryGrade() const +{ + return (m_signatory_grade); +} + +int AForm::getExecutorGrade() const +{ + return (m_executor_grade); +} + +void AForm::beSigned(const Bureaucrat &bureaucrat) +{ + if (bureaucrat.getGrade() > m_signatory_grade) + throw (Bureaucrat::GradeTooLowException()); + if (m_is_signed) + throw (std::runtime_error("AForm is already signed")); + m_is_signed = 1; +} + +void AForm::checkExecutorGrade(const Bureaucrat &bureaucrat) const +{ + if (bureaucrat.getGrade() > m_executor_grade) + throw (Bureaucrat::GradeTooLowException()); +} + +std::ostream &operator<<(std::ostream &ostream, const AForm &form) +{ + ostream << "AForm " << form.getName() << ":\n\n"; + ostream << "\tMinimum grade of signatory:\t" << form.getSignatoryGrade() << '\n'; + ostream << "\tMinimum grade of executor:\t" << form.getExecutorGrade() << "\n\n"; + if (form.isSigned()) + ostream << "\t\t\t\tBureaucrat\n"; + else + ostream << "\n"; + ostream << "\t\t\t\tSignature\n"; + return (ostream); +} diff --git a/ex02/AForm.h b/ex02/AForm.h new file mode 100644 index 0000000..052f11d --- /dev/null +++ b/ex02/AForm.h @@ -0,0 +1,36 @@ +#ifndef AFORM_H +# define AFORM_H + +#include "Bureaucrat.h" +#include +#include + +class AForm +{ + private: + const std::string m_name; + bool m_is_signed; + const int m_signatory_grade; + const int m_executor_grade; + + AForm(); + + AForm &operator=(const AForm &other); + + public: + AForm(const std::string &name, int signatory_grade, int executor_grade); + AForm(const AForm &other); + virtual ~AForm(); + + const std::string &getName() const; + bool isSigned() const; + int getSignatoryGrade() const; + int getExecutorGrade() const; + void beSigned(const Bureaucrat &bureaucrat); + void checkExecutorGrade(const Bureaucrat &bureaucrat) const; + void virtual execute(const Bureaucrat &bureaucrat) const = 0; +}; + +std::ostream &operator<<(std::ostream &ostream, const AForm &form); + +#endif // AFORM_H diff --git a/ex02/Bureaucrat.cpp b/ex02/Bureaucrat.cpp new file mode 100644 index 0000000..ae6a380 --- /dev/null +++ b/ex02/Bureaucrat.cpp @@ -0,0 +1,137 @@ +#include "Bureaucrat.h" +#include "AForm.h" + +Bureaucrat::Bureaucrat() +{ +} + +Bureaucrat::Bureaucrat(const std::string &name, int grade) + : m_name(name) + , m_grade(grade) +{ + if (grade < HIGHEST_GRADE) + throw (Bureaucrat::GradeTooHighException()); + else if (grade > LOWEST_GRADE) + throw (Bureaucrat::GradeTooLowException()); +} + +Bureaucrat::Bureaucrat(const Bureaucrat &other) +{ + *this = other; +} + +Bureaucrat::~Bureaucrat() +{ +} + +Bureaucrat &Bureaucrat::operator=(const Bureaucrat &other) +{ + if (this == &other) + return (*this); + return (*this); +} + +const std::string &Bureaucrat::getName() const +{ + return (m_name); +} + +int Bureaucrat::getGrade() const +{ + return (m_grade); +} + +void Bureaucrat::promote() +{ + if (m_grade == HIGHEST_GRADE) + throw (Bureaucrat::GradeTooHighException()); + --m_grade; +} + +void Bureaucrat::demote() +{ + if (m_grade == LOWEST_GRADE) + throw (Bureaucrat::GradeTooLowException()); + ++m_grade; +} + +void Bureaucrat::signForm(AForm &form) const +{ + try + { + form.beSigned(*this); + std::cout << m_name << " signed the form " << form.getName() << '\n'; + } + catch (const Bureaucrat::GradeTooLowException &exception) + { + std::cout << m_name << " couldn't sign the form " << form.getName() + << " because his grade " << m_grade << " is lower than the necessary " + << form.getSignatoryGrade() << '\n'; + } + catch (const std::runtime_error &exception) + { + std::cout << m_name << " couldn't sign the form " << form.getName() + << " because it is already signed.\n"; + } +} + +void Bureaucrat::executeForm(const AForm &form) const +{ + try + { + form.execute(*this); + std::cout << m_name << " executed the form " << form.getName() << '\n'; + } + catch (const Bureaucrat::GradeTooLowException &exception) + { + std::cout << m_name << " couldn't execute the form " << form.getName() + << " because his grade " << m_grade << " is lower than the necessary " + << form.getSignatoryGrade() << '\n'; + } +} + +std::ostream &operator<<(std::ostream &ostream, const Bureaucrat &bureaucrat) +{ + ostream << bureaucrat.getName() << ", bureaucrat of grade " << bureaucrat.getGrade(); + return (ostream); +} + +Bureaucrat::GradeTooLowException::GradeTooLowException() + : std::runtime_error::runtime_error("Grade cannot be lower") +{ +} + +Bureaucrat::GradeTooLowException::GradeTooLowException(const Bureaucrat::GradeTooLowException &other) + : std::runtime_error::runtime_error(other.what()) +{ +} + +Bureaucrat::GradeTooLowException::~GradeTooLowException() throw() +{ +} + +Bureaucrat::GradeTooLowException &Bureaucrat::GradeTooLowException::operator=(const Bureaucrat::GradeTooLowException &other) +{ + std::runtime_error::operator = (other); + return (*this); +} + +Bureaucrat::GradeTooHighException::GradeTooHighException() + : std::runtime_error::runtime_error("Grade cannot be higher") +{ +} + +Bureaucrat::GradeTooHighException::GradeTooHighException(const Bureaucrat::GradeTooHighException &other) + : std::runtime_error::runtime_error(other.what()) +{ +} + +Bureaucrat::GradeTooHighException::~GradeTooHighException() throw() +{ +} + +Bureaucrat::GradeTooHighException &Bureaucrat::GradeTooHighException::operator=(const Bureaucrat::GradeTooHighException &other) +{ + std::runtime_error::operator = (other); + return (*this); +} diff --git a/ex02/Bureaucrat.h b/ex02/Bureaucrat.h new file mode 100644 index 0000000..31a74e1 --- /dev/null +++ b/ex02/Bureaucrat.h @@ -0,0 +1,61 @@ +#ifndef BUREAUCRAT_H +# define BUREAUCRAT_H + +#include +#include +#include + +class AForm; + +class Bureaucrat +{ + private: + const std::string m_name; + int m_grade; + + Bureaucrat(); + Bureaucrat(const Bureaucrat &other); + + Bureaucrat &operator=(const Bureaucrat &other); + + public: + Bureaucrat(const std::string &name, int grade); + ~Bureaucrat(); + + static const int LOWEST_GRADE = 150; + static const int HIGHEST_GRADE = 1; + + const std::string &getName() const; + int getGrade() const; + void promote(); + void demote(); + void signForm(AForm &form) const; + void executeForm(const AForm &form) const; + + class GradeTooLowException; + class GradeTooHighException; +}; + +std::ostream &operator<<(std::ostream &ostream, const Bureaucrat &bureaucrat); + +class Bureaucrat::GradeTooLowException : public std::runtime_error +{ + public: + GradeTooLowException(); + GradeTooLowException(const GradeTooLowException &other); + ~GradeTooLowException() throw(); + + GradeTooLowException &operator=(const GradeTooLowException &other); +}; + +class Bureaucrat::GradeTooHighException : public std::runtime_error +{ + public: + GradeTooHighException(); + GradeTooHighException(const GradeTooHighException &other); + ~GradeTooHighException() throw(); + + GradeTooHighException &operator=(const GradeTooHighException &other); +}; + +#endif // BUREAUCRAT_H diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..9261211 --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,54 @@ +CC := c++ +CFLAGS = -std=c++98 -Wall -Wextra -Werror -Wpedantic + +ifneq ("$(wildcard .debug)","") + CFLAGS += -g +endif + +RM := rm -f + +INCDIR := inc +INCDIR += $(addsuffix /inc, $(SUBPROJECTS)); +ifneq ($(INCLUDE),) + INCLUDE := $(addprefix -I, $(INCDIR)) +endif + +SRCDIR := . + +SOURCES := main.cpp \ + Bureaucrat.cpp \ + AForm.cpp \ + +SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES)) + +OBJECTS := $(SOURCES:.cpp=.o) + +NAME := bureaucrat + +all : $(NAME) + +debug : .debug + $(MAKE) all + +nodebug : + $(RM) .debug + $(MAKE) re + +.% : + $(MAKE) fclean + touch $@ + +$(NAME) : $(OBJECTS) + $(CC) $(CFLAGS) -o $@ $^ $(LINKS) + +%.o : %.cpp + $(CC) $(CFLAGS) -o $@ -c $< $(INCLUDE) + +clean : + $(RM) $(OBJECTS) + +fclean : clean + $(RM) $(NAME) + +re : fclean + $(MAKE) all diff --git a/ex02/ShruberryCreationForm.h b/ex02/ShruberryCreationForm.h new file mode 100644 index 0000000..0c47e1c --- /dev/null +++ b/ex02/ShruberryCreationForm.h @@ -0,0 +1,25 @@ +#ifndef SHRUBERRYCREATIONFORM_H +# define SHRUBERRYCREATIONFORM_H + +class ShruberryCreationForm +{ + private: + + public: + using l + ShruberryCreationForm(const ShruberryCreationForm &other); + ~ShruberryCreationForm(); + + ShruberryCreationForm &operator=(const ShruberryCreationForm &other); +}; + +ShruberryCreationForm::ShruberryCreationForm(const std::string &target) + : AForm("Shruberry Creation Form", 145, 137, target) +{ +} + +ShruberryCreationForm::~ShruberryCreationForm() +{ +} + +#endif // SHRUBERRYCREATIONFORM_H diff --git a/ex02/main.cpp b/ex02/main.cpp new file mode 100644 index 0000000..fda8686 --- /dev/null +++ b/ex02/main.cpp @@ -0,0 +1,11 @@ +#include "Bureaucrat.h" +#include "AForm.h" +#include + +int main(void) +{ + Bureaucrat boss("Boss", 1); + Bureaucrat employee("Employee", 110); + + return (0); +} -- 2.30.2