From 109f171886e7e93580f5b010f287f27edbec868f Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 16 Jan 2025 16:57:56 +0100 Subject: [PATCH] Add solution to ex01 --- ex01/Bureaucrat.cpp | 122 ++++++++++++++++++++++++++++++++++++++++++++ ex01/Bureaucrat.h | 60 ++++++++++++++++++++++ ex01/Form.cpp | 81 +++++++++++++++++++++++++++++ ex01/Form.h | 34 ++++++++++++ ex01/Makefile | 54 ++++++++++++++++++++ ex01/main.cpp | 51 ++++++++++++++++++ 6 files changed, 402 insertions(+) create mode 100644 ex01/Bureaucrat.cpp create mode 100644 ex01/Bureaucrat.h create mode 100644 ex01/Form.cpp create mode 100644 ex01/Form.h create mode 100644 ex01/Makefile create mode 100644 ex01/main.cpp diff --git a/ex01/Bureaucrat.cpp b/ex01/Bureaucrat.cpp new file mode 100644 index 0000000..20156e1 --- /dev/null +++ b/ex01/Bureaucrat.cpp @@ -0,0 +1,122 @@ +#include "Bureaucrat.h" +#include "Form.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(Form &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"; + } +} + +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/ex01/Bureaucrat.h b/ex01/Bureaucrat.h new file mode 100644 index 0000000..ebadc21 --- /dev/null +++ b/ex01/Bureaucrat.h @@ -0,0 +1,60 @@ +#ifndef BUREAUCRAT_H +# define BUREAUCRAT_H + +#include +#include +#include + +class Form; + +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(Form &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/ex01/Form.cpp b/ex01/Form.cpp new file mode 100644 index 0000000..ad431f8 --- /dev/null +++ b/ex01/Form.cpp @@ -0,0 +1,81 @@ +#include "Form.h" +#include "Bureaucrat.h" + +Form::Form() + : m_name("") + , m_is_signed(0) + , m_signatory_grade(0) + , m_executor_grade(0) +{ +} + +Form::Form(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()); +} + +Form::Form(const Form &other) + : m_name(other.m_name) + , m_is_signed(0) + , m_signatory_grade(other.m_signatory_grade) + , m_executor_grade(other.m_executor_grade) +{ +} + +Form::~Form() { } + +Form &Form::operator=(const Form &other) +{ + if (this == &other) + return (*this); + return (*this); +} + +const std::string &Form::getName() const +{ + return (m_name); +} + +bool Form::isSigned() const +{ + return (m_is_signed); +} + +int Form::getSignatoryGrade() const +{ + return (m_signatory_grade); +} + +int Form::getExecutorGrade() const +{ + return (m_executor_grade); +} + +void Form::beSigned(const Bureaucrat &bureaucrat) +{ + if (bureaucrat.getGrade() > m_signatory_grade) + throw (Bureaucrat::GradeTooLowException()); + if (m_is_signed) + throw (std::runtime_error("Form is already signed")); + m_is_signed = 1; +} + +std::ostream &operator<<(std::ostream &ostream, const Form &form) +{ + ostream << "Form " << 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/ex01/Form.h b/ex01/Form.h new file mode 100644 index 0000000..938955c --- /dev/null +++ b/ex01/Form.h @@ -0,0 +1,34 @@ +#ifndef FORM_H +# define FORM_H + +#include "Bureaucrat.h" +#include +#include + +class Form +{ + private: + const std::string m_name; + bool m_is_signed; + const int m_signatory_grade; + const int m_executor_grade; + + Form(); + + Form &operator=(const Form &other); + + public: + Form(const std::string &name, int signatory_grade, int executor_grade); + Form(const Form &other); + ~Form(); + + const std::string &getName() const; + bool isSigned() const; + int getSignatoryGrade() const; + int getExecutorGrade() const; + void beSigned(const Bureaucrat &bureaucrat); +}; + +std::ostream &operator<<(std::ostream &ostream, const Form &form); + +#endif // FORM_H diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..e81cf8f --- /dev/null +++ b/ex01/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 \ + Form.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/ex01/main.cpp b/ex01/main.cpp new file mode 100644 index 0000000..94d0014 --- /dev/null +++ b/ex01/main.cpp @@ -0,0 +1,51 @@ +#include "Bureaucrat.h" +#include "Form.h" +#include + +void create_form(int signatory_grade, int executor_grade) +{ + Form f("test", signatory_grade, executor_grade); + + std::cout << f << '\n'; +} + +void test_form_creation(int signatory_grade, int executor_grade) +{ + try + { + create_form(signatory_grade, executor_grade); + } + catch (const Bureaucrat::GradeTooLowException &exception) + { + std::cout << exception.what() << '\n'; + } + catch (const Bureaucrat::GradeTooHighException &exception) + { + std::cout << exception.what() << '\n'; + } +} + +int main(void) +{ + Form f("sign test", 30, 100); + Bureaucrat boss("Boss", 1); + Bureaucrat employee("Employee", 110); + + test_form_creation(0, 0); + test_form_creation(0, 1); + test_form_creation(0, 200); + test_form_creation(1, 0); + test_form_creation(1, 1); + test_form_creation(1, 200); + test_form_creation(200, 0); + test_form_creation(200, 1); + test_form_creation(200, 200); + std::cout << f; + employee.signForm(f); + std::cout << f; + boss.signForm(f); + std::cout << f; + boss.signForm(f); + std::cout << f; + return (0); +} -- 2.30.2