Skip to content

Commit b1d95b2

Browse files
committed
Implement AForm, Bureaucrat, and Intern classes with associated functionality and error handling; add form execution tests in main.
1 parent cc7d385 commit b1d95b2

17 files changed

Lines changed: 646 additions & 2 deletions

05/ex02/AForm.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ class AForm {
1313
const int _gradeToExecute;
1414
std::string _target;
1515

16-
public:
16+
protected:
1717
AForm();
1818
AForm(std::string name, int gradeToSign, int gradeToExecute, std::string target);
19-
virtual ~AForm();
2019
AForm(const AForm& other);
2120
AForm& operator=(const AForm& other);
2221

22+
public:
23+
virtual ~AForm();
24+
2325
std::string getName() const;
2426
bool isSigned() const;
2527
int getGradeToSign() const;

05/ex02/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ $(BUILD_DIR):
1717

1818
clean:
1919
@rm -rf $(NAME)
20+
@rm -rf *_shrubbery 2>/dev/null || true
2021

2122
fclean: clean
2223
@rm -rf $(BUILD_DIR)

05/ex03/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bureaucrat
2+
*_shrubbery

05/ex03/AForm.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include "AForm.hpp"
2+
3+
#include "Bureaucrat.hpp"
4+
5+
AForm::AForm(std::string name, int gradeToSign, int gradeToExecute, std::string target)
6+
: _name(name), _signed(false), _gradeToSign(gradeToSign), _gradeToExecute(gradeToExecute), _target(target) {
7+
if (gradeToSign < 1 || gradeToExecute < 1) {
8+
throw GradeTooHighException();
9+
}
10+
if (gradeToSign > 150 || gradeToExecute > 150) {
11+
throw GradeTooLowException();
12+
}
13+
}
14+
15+
AForm::AForm(const AForm& other)
16+
: _name(other._name),
17+
_signed(other._signed),
18+
_gradeToSign(other._gradeToSign),
19+
_gradeToExecute(other._gradeToExecute),
20+
_target(other._target) {}
21+
22+
AForm& AForm::operator=(const AForm& other) {
23+
if (this != &other) {
24+
_signed = other.isSigned();
25+
}
26+
return *this;
27+
}
28+
29+
AForm::~AForm() {}
30+
31+
std::string AForm::getName() const { return _name; }
32+
33+
bool AForm::isSigned() const { return _signed; }
34+
35+
int AForm::getGradeToSign() const { return _gradeToSign; }
36+
37+
int AForm::getGradeToExecute() const { return _gradeToExecute; }
38+
39+
std::string AForm::getTarget() const { return _target; }
40+
41+
void AForm::beSigned(const Bureaucrat& bureaucrat) {
42+
if (bureaucrat.getGrade() <= _gradeToSign) {
43+
_signed = true;
44+
} else {
45+
throw GradeTooLowException();
46+
}
47+
}
48+
49+
void AForm::checkExecutability(const Bureaucrat& executor) const {
50+
if (!_signed) {
51+
throw FormNotSignedException();
52+
}
53+
if (executor.getGrade() > _gradeToExecute) {
54+
throw GradeTooLowException();
55+
}
56+
}
57+
58+
const char* AForm::GradeTooHighException::what() const throw() { return "Grade is too high!"; }
59+
60+
const char* AForm::GradeTooLowException::what() const throw() { return "Grade is too low!"; }
61+
62+
const char* AForm::FormNotSignedException::what() const throw() { return "Form is not signed!"; }
63+
64+
std::ostream& operator<<(std::ostream& os, const AForm& form) {
65+
os << "Form: " << form.getName() << ", Signed: " << (form.isSigned() ? "Yes" : "No")
66+
<< ", Grade to Sign: " << form.getGradeToSign() << ", Grade to Execute: " << form.getGradeToExecute();
67+
return os;
68+
}

05/ex03/AForm.hpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#pragma once
2+
3+
#include <iostream>
4+
#include <string>
5+
6+
class Bureaucrat;
7+
8+
class AForm {
9+
private:
10+
const std::string _name;
11+
bool _signed;
12+
const int _gradeToSign;
13+
const int _gradeToExecute;
14+
std::string _target;
15+
16+
protected:
17+
AForm();
18+
AForm(std::string name, int gradeToSign, int gradeToExecute, std::string target);
19+
AForm(const AForm& other);
20+
AForm& operator=(const AForm& other);
21+
22+
public:
23+
virtual ~AForm();
24+
25+
std::string getName() const;
26+
bool isSigned() const;
27+
int getGradeToSign() const;
28+
int getGradeToExecute() const;
29+
std::string getTarget() const;
30+
void beSigned(const Bureaucrat& bureaucrat);
31+
void checkExecutability(const Bureaucrat& executor) const;
32+
virtual void execute(const Bureaucrat& executor) const = 0;
33+
34+
class GradeTooHighException : public std::exception {
35+
public:
36+
virtual const char* what() const throw();
37+
};
38+
class GradeTooLowException : public std::exception {
39+
public:
40+
virtual const char* what() const throw();
41+
};
42+
class FormNotSignedException : public std::exception {
43+
public:
44+
virtual const char* what() const throw();
45+
};
46+
};
47+
48+
std::ostream& operator<<(std::ostream& os, const AForm& form);

05/ex03/Bureaucrat.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "./Bureaucrat.hpp"
2+
3+
#include "AForm.hpp"
4+
5+
Bureaucrat::Bureaucrat() : _name("default"), _grade(150) {}
6+
7+
Bureaucrat::Bureaucrat(std::string name, int grade) : _name(name) {
8+
if (grade < 1) throw Bureaucrat::GradeTooHighException();
9+
if (grade > 150) throw Bureaucrat::GradeTooLowException();
10+
_grade = grade;
11+
}
12+
13+
Bureaucrat::~Bureaucrat() {}
14+
15+
Bureaucrat::Bureaucrat(const Bureaucrat& other) : _name(other._name), _grade(other._grade) {}
16+
17+
Bureaucrat& Bureaucrat::operator=(const Bureaucrat& other) {
18+
if (this != &other) {
19+
_grade = other._grade;
20+
}
21+
return *this;
22+
}
23+
24+
std::string Bureaucrat::getName() const { return _name; }
25+
26+
int Bureaucrat::getGrade() const { return _grade; }
27+
28+
void Bureaucrat::incrementGrade() {
29+
if (_grade == 1) throw Bureaucrat::GradeTooHighException();
30+
_grade--;
31+
}
32+
33+
void Bureaucrat::decrementGrade() {
34+
if (_grade == 150) throw Bureaucrat::GradeTooLowException();
35+
_grade++;
36+
}
37+
38+
const char* Bureaucrat::GradeTooHighException::what() const throw() { return "Grade is too high!"; }
39+
40+
const char* Bureaucrat::GradeTooLowException::what() const throw() { return "Grade is too low!"; }
41+
42+
void Bureaucrat::signForm(AForm& form) {
43+
try {
44+
form.beSigned(*this);
45+
std::cout << _name << " signed " << form.getName() << std::endl;
46+
} catch (const std::exception& e) {
47+
std::cout << _name << " couldn't sign " << form.getName() << " because " << e.what() << std::endl;
48+
}
49+
}
50+
51+
void Bureaucrat::executeForm(const AForm& form) const {
52+
try {
53+
form.execute(*this);
54+
std::cout << _name << " executed " << form.getName() << std::endl;
55+
} catch (const std::exception& e) {
56+
std::cout << _name << " couldn't execute " << form.getName() << " because " << e.what() << std::endl;
57+
}
58+
}
59+
60+
std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat) {
61+
os << bureaucrat.getName() << ", bureaucrat grade " << bureaucrat.getGrade();
62+
return os;
63+
}

05/ex03/Bureaucrat.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
3+
#include <iostream>
4+
#include <string>
5+
6+
#include "AForm.hpp"
7+
8+
class Bureaucrat {
9+
private:
10+
std::string const _name;
11+
int _grade;
12+
13+
public:
14+
Bureaucrat();
15+
Bureaucrat(std::string name, int grade);
16+
~Bureaucrat();
17+
Bureaucrat(const Bureaucrat& other);
18+
Bureaucrat& operator=(const Bureaucrat& other);
19+
20+
std::string getName() const;
21+
int getGrade() const;
22+
void incrementGrade();
23+
void decrementGrade();
24+
void signForm(AForm& form);
25+
void executeForm(const AForm& form) const;
26+
27+
class GradeTooHighException : public std::exception {
28+
public:
29+
virtual const char* what() const throw();
30+
};
31+
class GradeTooLowException : public std::exception {
32+
public:
33+
virtual const char* what() const throw();
34+
};
35+
};
36+
37+
std::ostream& operator<<(std::ostream& os, const Bureaucrat& bureaucrat);

05/ex03/Intern.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "Intern.hpp"
2+
3+
#include <iostream>
4+
5+
#include "AForm.hpp"
6+
#include "PresidentialPardonForm.hpp"
7+
#include "RobotomyRequestForm.hpp"
8+
#include "ShrubberyCreationForm.hpp"
9+
10+
Intern::Intern() { std::cout << "Intern default Constructor called" << std::endl; }
11+
12+
Intern::~Intern() { std::cout << "Intern Destructor called" << std::endl; }
13+
14+
Intern &Intern::operator=(const Intern &obj) {
15+
std::cout << "Intern Assignment Operator called" << std::endl;
16+
if (this == &obj) return *this;
17+
return *this;
18+
}
19+
20+
Intern::Intern(const Intern &obj) {
21+
std::cout << "Intern Copy Constructor called" << std::endl;
22+
*this = obj;
23+
}
24+
25+
AForm *Intern::makeForm(const std::string &formName, const std::string &target) const {
26+
auto createShrubbery = [](const std::string &target) -> AForm * { return new ShrubberyCreationForm(target); };
27+
auto createRobotomy = [](const std::string &target) -> AForm * { return new RobotomyRequestForm(target); };
28+
auto createPresidential = [](const std::string &target) -> AForm * { return new PresidentialPardonForm(target); };
29+
30+
std::string formTypes[3] = {"shrubbery creation", "robotomy request", "presidential pardon"};
31+
AForm *(*formCreators[3])(const std::string &) = {createShrubbery, createRobotomy, createPresidential};
32+
33+
for (int i = 0; i < 3; i++) {
34+
if (formName == formTypes[i]) {
35+
std::cout << "Intern creates " << formName << " form" << std::endl;
36+
return formCreators[i](target);
37+
}
38+
}
39+
40+
std::cout << "Intern cannot create form: " << formName << std::endl;
41+
return nullptr;
42+
}

05/ex03/Intern.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
#include "AForm.hpp"
6+
7+
class Intern {
8+
public:
9+
Intern();
10+
~Intern();
11+
Intern(const Intern &obj);
12+
Intern &operator=(const Intern &obj);
13+
14+
AForm *makeForm(const std::string &formName, const std::string &target) const;
15+
};

05/ex03/Makefile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
NAME = bureaucrat
2+
SRC = Bureaucrat.cpp main.cpp AForm.cpp PresidentialPardonForm.cpp RobotomyRequestForm.cpp ShrubberyCreationForm.cpp Intern.cpp
3+
HEADERS = Bureaucrat.hpp AForm.hpp PresidentialPardonForm.hpp RobotomyRequestForm.hpp ShrubberyCreationForm.hpp Intern.hpp
4+
BUILD_DIR = build
5+
BUILD = $(addprefix $(BUILD_DIR)/, $(SRC:.cpp=.o))
6+
7+
all: $(NAME)
8+
9+
$(NAME): $(BUILD_DIR) $(BUILD)
10+
@c++ $(BUILD) -o $(NAME)
11+
12+
$(BUILD_DIR)/%.o: %.cpp | $(BUILD_DIR)
13+
@c++ -Werror -Wall -Wextra -Wno-implicit-fallthrough -std=c++11 -c $< -o $@
14+
15+
$(BUILD_DIR):
16+
@mkdir -p $(BUILD_DIR)
17+
18+
clean:
19+
@rm -rf $(NAME)
20+
@rm -rf *_shrubbery 2>/dev/null || true
21+
22+
fclean: clean
23+
@rm -rf $(BUILD_DIR)
24+
25+
re: fclean all
26+
27+
format:
28+
@clang-format -i $(SRC) $(HEADERS)
29+
30+
run: all
31+
@printf "\n🤖 05/ex03 $(NAME) output:\n\n"
32+
@./$(NAME)
33+
34+
.PHONY: all clean fclean re

0 commit comments

Comments
 (0)