Bureaucrat.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* Bureaucrat.cpp :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2025/12/26 17:51:13 by bchanot #+# #+# */
  9. /* Updated: 2025/12/30 13:59:30 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "Bureaucrat.hpp"
  13. #include "Form.hpp"
  14. /*
  15. ** ------------------------------- CONSTRUCTOR --------------------------------
  16. */
  17. Bureaucrat::Bureaucrat() : _name("DefaultName"), _grade(150)
  18. {
  19. }
  20. Bureaucrat::Bureaucrat( std::string const name, u_int8_t grade ) : _name(name), _grade(grade)
  21. {
  22. if (this->_grade > 150)
  23. throw Bureaucrat::GradeTooHighException();
  24. else if (this->_grade < 1)
  25. throw Bureaucrat::GradeTooLowException();
  26. }
  27. Bureaucrat::Bureaucrat( const Bureaucrat & src ) : _name(src._name), _grade(src._grade)
  28. {
  29. if (this->_grade > 150)
  30. throw Bureaucrat::GradeTooHighException();
  31. else if (this->_grade < 1)
  32. throw Bureaucrat::GradeTooLowException();
  33. }
  34. /*
  35. ** -------------------------------- DESTRUCTOR --------------------------------
  36. */
  37. Bureaucrat::~Bureaucrat()
  38. {
  39. }
  40. /*
  41. ** --------------------------------- OVERLOAD ---------------------------------
  42. */
  43. Bureaucrat & Bureaucrat::operator=( Bureaucrat const & rhs )
  44. {
  45. (void)rhs;
  46. //if ( this != &rhs )
  47. //{
  48. //this->_value = rhs.getValue();
  49. //}
  50. return *this;
  51. }
  52. std::ostream & operator<<( std::ostream & o, Bureaucrat const & i )
  53. {
  54. o << i.getName() << ", bureaucrat grade " << (int)i.getGrade();
  55. return o;
  56. }
  57. Bureaucrat &Bureaucrat::operator++(void) {
  58. if (this->_grade > 1)
  59. this->_grade--;
  60. else
  61. throw Bureaucrat::GradeTooHighException();
  62. return *this;
  63. }
  64. Bureaucrat &Bureaucrat::operator--(void) {
  65. if (this->_grade < 150)
  66. this->_grade++;
  67. else
  68. throw Bureaucrat::GradeTooLowException();
  69. return *this;
  70. }
  71. /*
  72. ** --------------------------------- METHODS ----------------------------------
  73. */
  74. void Bureaucrat::decrementGrade(void)
  75. {
  76. if (this->_grade < 150)
  77. this->_grade++;
  78. else
  79. throw Bureaucrat::GradeTooHighException();
  80. }
  81. void Bureaucrat::incrementGrade(void) {
  82. if (this->_grade > 1)
  83. this->_grade--;
  84. else
  85. throw Bureaucrat::GradeTooLowException();
  86. }
  87. void Bureaucrat::signForm(Form &form)
  88. {
  89. try {
  90. form.beSigned(*this);
  91. std::cout << this->_name << " signed " << form.getName() << std::endl;
  92. } catch (std::exception &e) {
  93. std::cout << this->_name << " couldn't sign " << form.getName() << " because " << e.what() << std::endl;
  94. }
  95. }
  96. /*
  97. ** --------------------------------- ACCESSOR ---------------------------------
  98. */
  99. uint8_t Bureaucrat::getGrade(void) const {
  100. return this->_grade;
  101. }
  102. std::string Bureaucrat::getName(void) const {
  103. return this->_name;
  104. }
  105. /* ************************************************************************** */