-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBureaucrat.cpp
More file actions
102 lines (87 loc) · 2.85 KB
/
Bureaucrat.cpp
File metadata and controls
102 lines (87 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zelhajou <zelhajou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/26 16:36:47 by zelhajou #+# #+# */
/* Updated: 2024/12/30 16:30:50 by zelhajou ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
#include "AForm.hpp"
Bureaucrat::Bureaucrat() : _name("default"), _grade(150) {}
Bureaucrat::Bureaucrat(const std::string &name, int grade) : _name(name)
{
if (grade < 1)
throw GradeTooHighException();
if (grade > 150)
throw GradeTooLowException();
_grade = grade;
}
Bureaucrat::Bureaucrat(const Bureaucrat &src) : _name(src._name), _grade(src._grade) {}
Bureaucrat::~Bureaucrat() {}
Bureaucrat &Bureaucrat::operator=(const Bureaucrat &src)
{
if (this != &src)
_grade = src._grade;
return *this;
}
const std::string &Bureaucrat::getName() const
{
return _name;
}
int Bureaucrat::getGrade() const
{
return _grade;
}
void Bureaucrat::incrementGrade()
{
if (_grade <= 1)
throw GradeTooHighException();
_grade--;
}
void Bureaucrat::decrementGrade()
{
if (_grade >= 150)
throw GradeTooLowException();
_grade++;
}
void Bureaucrat::signForm(AForm &form)
{
try
{
form.beSigned(*this);
std::cout << _name << " signed " << form.getName() << std::endl;
}
catch (const std::exception &e)
{
std::cout << _name << " couldn't sign " << form.getName() << " because " << e.what() << std::endl;
}
}
void Bureaucrat::executeForm(AForm const &form) const
{
try
{
form.execute(*this);
std::cout << _name << " executed " << form.getName() << std::endl;
}
catch (const std::exception &e)
{
std::cout << _name << " couldn't execute " << form.getName() << " because " << e.what() << std::endl;
}
}
const char *Bureaucrat::GradeTooHighException::what() const throw()
{
return "Grade too high (must be between 1 and 150)";
}
const char *Bureaucrat::GradeTooLowException::what() const throw()
{
return "Grade too low (must be between 1 and 150)";
}
std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
{
os << bureaucrat.getName() << ", bureaucrat grade " << bureaucrat.getGrade();
return os;
}