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