Bureaucrat.cpp
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* Bureaucrat.cpp :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: gychoi <gychoi@student.42seoul.kr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2023/11/10 17:36:55 by gychoi #+# #+# */ 9 /* Updated: 2023/11/29 22:28:22 by gychoi ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include "Bureaucrat.hpp" 14 #include "AForm.hpp" 15 16 static void _copyConstWarning(std::string const& object); 17 18 /* ************************************************************************** */ 19 /* Constructor & Destructor */ 20 /* ************************************************************************** */ 21 Bureaucrat::Bureaucrat() 22 : mName(std::string()) 23 , mGrade(0) 24 { 25 // nothing to do 26 } 27 28 Bureaucrat::Bureaucrat(std::string const& name, int grade) 29 : mName(name), 30 mGrade(grade) 31 { 32 if (grade < 1) 33 { 34 throw GradeTooHighException(); 35 } 36 else if (grade > 150) 37 { 38 throw GradeTooLowException(); 39 } 40 else 41 { 42 // nothing to do 43 } 44 } 45 46 Bureaucrat::Bureaucrat(Bureaucrat const& target) 47 : mName(target.getName()), 48 mGrade(target.getGrade()) 49 { 50 // nothing to do 51 } 52 53 Bureaucrat& Bureaucrat::operator=(Bureaucrat const& target) 54 { 55 if (this != &target) 56 { 57 _copyConstWarning("mName"); 58 this->mGrade = target.getGrade(); 59 } 60 return *this; 61 } 62 63 Bureaucrat::~Bureaucrat() 64 { 65 // nothing to do 66 } 67 68 /* ************************************************************************** */ 69 /* Getter & Setter Functions */ 70 /* ************************************************************************** */ 71 std::string const& Bureaucrat::getName() const 72 { 73 return this->mName; 74 } 75 76 int Bureaucrat::getGrade() const 77 { 78 return this->mGrade; 79 } 80 81 void Bureaucrat::setGrade(int grade) 82 { 83 if (grade < 1) 84 { 85 throw GradeTooHighException("Grade too high"); 86 } 87 else if (grade > 150) 88 { 89 throw GradeTooLowException("Grade too low!"); 90 } 91 else 92 { 93 this->mGrade = grade; 94 } 95 } 96 97 /* ************************************************************************** */ 98 /* Public Member Function */ 99 /* ************************************************************************** */ 100 void Bureaucrat::increaseGrade() 101 { 102 int grade = this->getGrade(); 103 104 if (grade > 1) 105 { 106 this->setGrade(--grade); 107 } 108 else 109 { 110 throw GradeTooHighException("Grade too high!"); 111 } 112 } 113 114 void Bureaucrat::decreaseGrade() 115 { 116 int grade = this->getGrade(); 117 118 if (grade < 150) 119 { 120 this->setGrade(++grade); 121 } 122 else 123 { 124 throw GradeTooLowException("Grade too low!"); 125 } 126 } 127 128 void Bureaucrat::signForm(AForm& form) 129 { 130 try 131 { 132 form.beSigned(*this); 133 std::cout 134 << this->getName() 135 << " signed " 136 << form.getName() 137 << std::endl; 138 } 139 catch (std::exception & e) 140 { 141 std::cout 142 << this->getName() 143 << " couldn't sign " 144 << form.getName() 145 << " because " 146 << e.what() 147 << std::endl; 148 } 149 } 150 151 void Bureaucrat::executeForm(AForm const& form) 152 { 153 try 154 { 155 form.execute(*this); 156 std::cout 157 << this->getName() 158 << " executed " 159 << form.getName() 160 << std::endl; 161 } 162 catch (std::exception & e) 163 { 164 std::cout 165 << this->getName() 166 << " couldn't execute " 167 << form.getName() 168 << " because " 169 << e.what() 170 << std::endl; 171 } 172 } 173 174 /* ************************************************************************** */ 175 /* GradeTooHighException : Constructor & Destructor */ 176 /* ************************************************************************** */ 177 Bureaucrat::GradeTooHighException::GradeTooHighException() 178 : mMessage("Bureaucrat::GradeTooHighException") 179 { 180 // nothing to do 181 } 182 183 Bureaucrat::GradeTooHighException::GradeTooHighException(std::string const& msg) 184 : mMessage("Bureaucrat::GradeTooHighException: " + msg) 185 { 186 // nothing to do 187 } 188 189 Bureaucrat::GradeTooHighException::GradeTooHighException 190 (GradeTooHighException const& target) 191 : mMessage(target.getMessage()) 192 { 193 // nothing to do 194 } 195 196 Bureaucrat::GradeTooHighException& 197 Bureaucrat::GradeTooHighException::operator= 198 (GradeTooHighException const& target) 199 { 200 if (this != &target) 201 { 202 this->mMessage = target.getMessage(); 203 } 204 return *this; 205 } 206 207 Bureaucrat::GradeTooHighException::~GradeTooHighException() throw() 208 { 209 // nothing to do 210 } 211 212 /* ************************************************************************** */ 213 /* GradeTooHighException : Getter Function */ 214 /* ************************************************************************** */ 215 std::string const& Bureaucrat::GradeTooHighException::getMessage() const 216 { 217 return this->mMessage; 218 } 219 220 /* ************************************************************************** */ 221 /* GradeTooHighException : Function Override */ 222 /* ************************************************************************** */ 223 const char* Bureaucrat::GradeTooHighException::what() const throw() 224 { 225 return this->getMessage().c_str(); 226 } 227 228 /* ************************************************************************** */ 229 /* GradeTooLowException : Constructor & Destructor */ 230 /* ************************************************************************** */ 231 Bureaucrat::GradeTooLowException::GradeTooLowException() 232 : mMessage("Bureaucrat::GradeTooLowException") 233 { 234 // nothing to do 235 } 236 237 Bureaucrat::GradeTooLowException::GradeTooLowException(std::string const& msg) 238 : mMessage("Bureaucrat::GradeTooLowException: " + msg) 239 { 240 // nothing to do 241 } 242 243 Bureaucrat::GradeTooLowException::GradeTooLowException 244 (GradeTooLowException const& target) 245 : mMessage(target.getMessage()) 246 { 247 // nothing to do 248 } 249 250 Bureaucrat::GradeTooLowException& 251 Bureaucrat::GradeTooLowException::operator= 252 (GradeTooLowException const& target) 253 { 254 if (this != &target) 255 { 256 this->mMessage = target.getMessage(); 257 } 258 return *this; 259 } 260 261 Bureaucrat::GradeTooLowException::~GradeTooLowException() throw() 262 { 263 // nothing to do 264 } 265 266 /* ************************************************************************** */ 267 /* GradeTooLowException : Getter Function */ 268 /* ************************************************************************** */ 269 std::string const& Bureaucrat::GradeTooLowException::getMessage() const 270 { 271 return this->mMessage; 272 } 273 274 /* ************************************************************************** */ 275 /* GradeTooLowException : Function Override */ 276 /* ************************************************************************** */ 277 const char* Bureaucrat::GradeTooLowException::what() const throw() 278 { 279 return this->getMessage().c_str(); 280 } 281 282 /* ************************************************************************** */ 283 /* Global Function */ 284 /* ************************************************************************** */ 285 std::ostream& operator<<(std::ostream& os, Bureaucrat const& target) 286 { 287 os << target.getName() << ", bureaucrat grade " << target.getGrade() << "."; 288 return os; 289 } 290 291 /* ************************************************************************** */ 292 /* Helper Functions */ 293 /* ************************************************************************** */ 294 static void _copyConstWarning(std::string const& variable) 295 { 296 std::cout << "INFO: You are trying to overwrite a const type variable: " << 297 variable << ". " << "This operation will be ignored." << std::endl; 298 }