Skip to content

Commit 36b299b

Browse files
committed
05/ex02 pretty much done
1 parent 0d4bff7 commit 36b299b

13 files changed

+496
-0
lines changed

05/ex02/.gitignore

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

05/ex02/AForm.cpp

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

05/ex02/AForm.hpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
public:
17+
AForm();
18+
AForm(std::string name, int gradeToSign, int gradeToExecute, std::string target);
19+
virtual ~AForm();
20+
AForm(const AForm& other);
21+
AForm& operator=(const AForm& other);
22+
23+
std::string getName() const;
24+
bool isSigned() const;
25+
int getGradeToSign() const;
26+
int getGradeToExecute() const;
27+
std::string getTarget() const;
28+
void beSigned(const Bureaucrat& bureaucrat);
29+
void checkExecutability(const Bureaucrat& executor) const;
30+
virtual void execute(const Bureaucrat& executor) const = 0;
31+
32+
class GradeTooHighException : public std::exception {
33+
public:
34+
virtual const char* what() const throw();
35+
};
36+
class GradeTooLowException : public std::exception {
37+
public:
38+
virtual const char* what() const throw();
39+
};
40+
class FormNotSignedException : public std::exception {
41+
public:
42+
virtual const char* what() const throw();
43+
};
44+
};
45+
46+
std::ostream& operator<<(std::ostream& os, const AForm& form);

05/ex02/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/ex02/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/ex02/Makefile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
NAME = bureaucrat
2+
SRC = Bureaucrat.cpp main.cpp AForm.cpp PresidentialPardonForm.cpp RobotomyRequestForm.cpp ShrubberyCreationForm.cpp
3+
HEADERS = Bureaucrat.hpp AForm.hpp PresidentialPardonForm.hpp RobotomyRequestForm.hpp ShrubberyCreationForm.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+
21+
fclean: clean
22+
@rm -rf $(BUILD_DIR)
23+
24+
re: fclean all
25+
26+
format:
27+
@clang-format -i $(SRC) $(HEADERS)
28+
29+
run: all
30+
@printf "\n🤖 05/ex00 $(NAME) output:\n\n"
31+
@./$(NAME)
32+
33+
.PHONY: all clean fclean re

05/ex02/PresidentialPardonForm.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "PresidentialPardonForm.hpp"
2+
#include "Bureaucrat.hpp"
3+
4+
PresidentialPardonForm::PresidentialPardonForm(std::string target)
5+
: AForm("PresidentialPardonForm", 25, 5, target) {}
6+
7+
PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm& other)
8+
: AForm(other) {}
9+
10+
PresidentialPardonForm::~PresidentialPardonForm() {}
11+
12+
PresidentialPardonForm& PresidentialPardonForm::operator=(const PresidentialPardonForm& other) {
13+
AForm::operator=(other);
14+
return *this;
15+
}
16+
17+
void PresidentialPardonForm::execute(const Bureaucrat& executor) const {
18+
checkExecutability(executor);
19+
std::cout << getTarget() << " has been pardoned by Zaphod Beeblebrox." << std::endl;
20+
}

05/ex02/PresidentialPardonForm.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include "AForm.hpp"
4+
5+
class PresidentialPardonForm : public AForm {
6+
public:
7+
PresidentialPardonForm(std::string target);
8+
PresidentialPardonForm(const PresidentialPardonForm& other);
9+
~PresidentialPardonForm();
10+
PresidentialPardonForm& operator=(const PresidentialPardonForm& other);
11+
12+
virtual void execute(const Bureaucrat& executor) const;
13+
};

05/ex02/RobotomyRequestForm.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "RobotomyRequestForm.hpp"
2+
#include "Bureaucrat.hpp"
3+
#include <cstdlib>
4+
#include <ctime>
5+
6+
RobotomyRequestForm::RobotomyRequestForm(std::string target)
7+
: AForm("RobotomyRequestForm", 72, 45, target) {
8+
std::srand(std::time(0));
9+
}
10+
11+
RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm& other)
12+
: AForm(other) {}
13+
14+
RobotomyRequestForm::~RobotomyRequestForm() {}
15+
16+
RobotomyRequestForm& RobotomyRequestForm::operator=(const RobotomyRequestForm& other) {
17+
AForm::operator=(other);
18+
return *this;
19+
}
20+
21+
void RobotomyRequestForm::execute(const Bureaucrat& executor) const {
22+
checkExecutability(executor);
23+
24+
std::cout << "* DRILLING NOISES *" << std::endl;
25+
if (std::rand() % 2) {
26+
std::cout << getTarget() << " has been robotomized successfully!" << std::endl;
27+
} else {
28+
std::cout << "Robotomy failed for " << getTarget() << "." << std::endl;
29+
}
30+
}

05/ex02/RobotomyRequestForm.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include "AForm.hpp"
4+
5+
class RobotomyRequestForm : public AForm {
6+
public:
7+
RobotomyRequestForm(std::string target);
8+
RobotomyRequestForm(const RobotomyRequestForm& other);
9+
~RobotomyRequestForm();
10+
RobotomyRequestForm& operator=(const RobotomyRequestForm& other);
11+
12+
virtual void execute(const Bureaucrat& executor) const;
13+
};

0 commit comments

Comments
 (0)