ShrubberyCreationForm.cpp
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* ShrubberyCreationForm.cpp :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: gychoi <gychoi@student.42seoul.kr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2023/11/28 22:09:51 by gychoi #+# #+# */ 9 /* Updated: 2023/12/11 22:21:14 by gychoi ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include "ShrubberyCreationForm.hpp" 14 15 static void _copyConstWarning(std::string const& variable); 16 17 /* ************************************************************************** */ 18 /* Constructor & Destructor */ 19 /* ************************************************************************** */ 20 ShrubberyCreationForm::ShrubberyCreationForm() 21 : AForm(std::string(), 145, 137) 22 { 23 // nothing to do 24 } 25 26 ShrubberyCreationForm::ShrubberyCreationForm(std::string const& targetName) 27 : AForm(targetName, 145, 137) 28 { 29 // nothing to do 30 } 31 32 ShrubberyCreationForm::ShrubberyCreationForm 33 (ShrubberyCreationForm const& target) try 34 : AForm(target.getName(), target.getSignGrade(), target.getExecuteGrade()) 35 { 36 // nothing to do 37 } 38 catch (std::exception & e) 39 { 40 throw; 41 } 42 43 ShrubberyCreationForm& ShrubberyCreationForm::operator= 44 (ShrubberyCreationForm const& target) 45 { 46 if (this != &target) 47 { 48 _copyConstWarning("mName"); 49 this->setIsSigned(target.getIsSigned()); 50 _copyConstWarning("mSignGrade"); 51 _copyConstWarning("mExecuteGrade"); 52 } 53 return *this; 54 } 55 56 ShrubberyCreationForm::~ShrubberyCreationForm() 57 { 58 // nothing to do 59 } 60 61 /* ************************************************************************** */ 62 /* Public Member Function */ 63 /* ************************************************************************** */ 64 void ShrubberyCreationForm::beSigned(Bureaucrat const& bureaucrat) 65 { 66 if (this->getSignGrade() < bureaucrat.getGrade()) 67 { 68 throw GradeTooLowException("Bureaucrat's grade is too low!"); 69 } 70 else 71 { 72 this->setIsSigned(true); 73 } 74 } 75 76 void ShrubberyCreationForm::execute(Bureaucrat const & executor) const 77 { 78 if (this->getExecuteGrade() < executor.getGrade()) 79 { 80 throw GradeTooLowException("Bureaucrat's grade is too low!"); 81 } 82 else if (this->getIsSigned() == false) 83 { 84 throw FormNotSignedException("Form is not signed!"); 85 } 86 else 87 { 88 std::ofstream outfile((this->getName() + "_shrubbery").c_str()); 89 90 if (!outfile.is_open()) 91 { 92 throw std::runtime_error("Unable to create file!"); 93 } 94 95 srand(time(NULL)); 96 97 if (rand() % 2 == 1) 98 { 99 char const* tree = 100 " \n" 101 " .:-:: .- -.- \n" 102 " -===**++++== :-== \n" 103 " -=-::++=+--*+==+==-: \n" 104 " .=++=--=+*+#=+#**=:=*+=. \n" 105 " +#+=*=++*=+**#*+*##=*-=*. \n" 106 " .++-+=*####+=**#==#:###++. \n" 107 " *++=++%+#*+#*++==+**#-*+ \n" 108 " :%%=+*+#*.**++*#+==+*+**+*. \n" 109 " .+++-++#%###+-+****+=#++++=: \n" 110 " :**=###%%##%%***#**###*#*+.-: \n" 111 " -*%*%******-=#=#####.-#%*** \n" 112 " .+#%#*##*#*%*%*##+**#**#. \n" 113 " *%%***#%%%#:*+##-*:= \n" 114 " =-+* *% .-#:*#*##*#: \n" 115 " #% #+ \n" 116 " *# \n" 117 " *+ \n" 118 " =: \n" 119 " .++%-=. \n" 120 " \n" 121 "Created by "; 122 123 outfile << tree 124 << executor.getName() 125 << "[" << executor.getGrade() << "]" 126 << std::endl; 127 } 128 else 129 { 130 char const* tree = 131 " \n" 132 " __ _ \n" 133 " !0@@@0NL \n" 134 " ;@NN|@b0@@0BN]@$@$H \n" 135 " ,;BNW@@0NNN@$@NN@0B@@b@L \n" 136 " PTEX]@BB^}@B0BN_/]y@$KQ@bP+ \n" 137 " !@`]@W8@@@0@$#N@@NE]h]BBb`^ \n" 138 " ` `P''$@@@BRB#B@NEp@p@WB/` \n" 139 " ,ARB@BBBNN@B@@@]RBNE` \n" 140 " 0@@R@@@BK0B@B0B0NNB=_#_ \n" 141 " 0Z$BNBBB$BBBWBR2BN@@E&L_ \n" 142 " B9@RBRRDBBR00BMH F \n" 143 " TBR' 9R@B@NMB6N@H \n" 144 " TTB T`0, \n" 145 " N \n" 146 " L \n" 147 " : \n" 148 " L \n" 149 " L \n" 150 " I \n" 151 " h_ \n" 152 " '` \n" 153 " \n" 154 "Created by "; 155 156 outfile << tree 157 << executor.getName() 158 << "[" << executor.getGrade() << "]" 159 << std::endl; 160 } 161 162 if (!outfile.good()) 163 { 164 throw std::runtime_error("Error writing to file!"); 165 } 166 167 outfile.close(); 168 169 if (outfile.fail()) 170 { 171 throw std::runtime_error("Error closing file!"); 172 } 173 174 throw std::string("Successfully created shrubbery!"); 175 } 176 } 177 178 /* ************************************************************************** */ 179 /* GradeTooHighException : Constructor & Destructor */ 180 /* ************************************************************************** */ 181 ShrubberyCreationForm::GradeTooHighException::GradeTooHighException() 182 : mMessage("ShrubberyCreationForm::GradeTooHighException") 183 { 184 // nothing to do 185 } 186 187 ShrubberyCreationForm::GradeTooHighException::GradeTooHighException 188 (std::string const& msg) 189 : mMessage("ShrubberyCreationForm::GradeTooHighException: " + msg) 190 { 191 // nothing to do 192 } 193 194 ShrubberyCreationForm::GradeTooHighException::GradeTooHighException 195 (GradeTooHighException const& target) 196 : AForm::GradeTooHighException(target.getMessage()) 197 { 198 // nothing to do 199 } 200 201 ShrubberyCreationForm::GradeTooHighException& 202 ShrubberyCreationForm::GradeTooHighException::operator= 203 (GradeTooHighException const& target) 204 { 205 if (this != &target) 206 { 207 this->mMessage = target.getMessage(); 208 } 209 return *this; 210 } 211 212 ShrubberyCreationForm::GradeTooHighException::~GradeTooHighException() throw() 213 { 214 // nothing to do 215 } 216 217 /* ************************************************************************** */ 218 /* GradeTooHighException : Getter Function */ 219 /* ************************************************************************** */ 220 std::string const& ShrubberyCreationForm::GradeTooHighException::getMessage() 221 const 222 { 223 return this->mMessage; 224 } 225 226 /* ************************************************************************** */ 227 /* GradeTooHighException : Function Override */ 228 /* ************************************************************************** */ 229 const char* ShrubberyCreationForm::GradeTooHighException::what() const throw() 230 { 231 return this->getMessage().c_str(); 232 } 233 234 /* ************************************************************************** */ 235 /* GradeTooLowException : Constructor & Destructor */ 236 /* ************************************************************************** */ 237 ShrubberyCreationForm::GradeTooLowException::GradeTooLowException() 238 : mMessage("ShrubberyCreationForm::GradeTooLowException") 239 { 240 // nothing to do 241 } 242 243 ShrubberyCreationForm::GradeTooLowException::GradeTooLowException 244 (std::string const& msg) 245 : mMessage("ShrubberyCreationForm::GradeTooLowException: " + msg) 246 { 247 // nothing to do 248 } 249 250 ShrubberyCreationForm::GradeTooLowException::GradeTooLowException 251 (GradeTooLowException const& target) 252 : AForm::GradeTooLowException(target.getMessage()) 253 { 254 // nothing to do 255 } 256 257 ShrubberyCreationForm::GradeTooLowException& 258 ShrubberyCreationForm::GradeTooLowException::operator= 259 (GradeTooLowException const& target) 260 { 261 if (this != &target) 262 { 263 this->mMessage = target.getMessage(); 264 } 265 return *this; 266 } 267 268 ShrubberyCreationForm::GradeTooLowException::~GradeTooLowException() throw() 269 { 270 // nothing to do 271 } 272 273 /* ************************************************************************** */ 274 /* GradeTooLowException : Getter Function */ 275 /* ************************************************************************** */ 276 std::string const& ShrubberyCreationForm::GradeTooLowException::getMessage() 277 const 278 { 279 return this->mMessage; 280 } 281 282 /* ************************************************************************** */ 283 /* GradeTooLowException : Function Override */ 284 /* ************************************************************************** */ 285 const char* ShrubberyCreationForm::GradeTooLowException::what() const throw() 286 { 287 return this->getMessage().c_str(); 288 } 289 290 /* ************************************************************************** */ 291 /* FormNotSignedException : Constructor & Destructor */ 292 /* ************************************************************************** */ 293 ShrubberyCreationForm::FormNotSignedException::FormNotSignedException() 294 : mMessage("ShrubberyCreationForm::FormNotSignedException") 295 { 296 // nothing to do 297 } 298 299 ShrubberyCreationForm::FormNotSignedException::FormNotSignedException 300 (std::string const& msg) 301 : mMessage("ShrubberyCreationForm::FormNotSignedException: " + msg) 302 { 303 // nothing to do 304 } 305 306 ShrubberyCreationForm::FormNotSignedException::FormNotSignedException 307 (FormNotSignedException const& target) 308 : mMessage(target.getMessage()) 309 { 310 // nothing to do 311 } 312 313 ShrubberyCreationForm::FormNotSignedException& 314 ShrubberyCreationForm::FormNotSignedException::operator= 315 (FormNotSignedException const& target) 316 { 317 if (this != &target) 318 { 319 this->mMessage = target.getMessage(); 320 } 321 return *this; 322 } 323 324 ShrubberyCreationForm::FormNotSignedException::~FormNotSignedException() throw() 325 { 326 // nothing to do 327 } 328 329 /* ************************************************************************** */ 330 /* FormNotSigendException : Getter Function */ 331 /* ************************************************************************** */ 332 std::string const& ShrubberyCreationForm::FormNotSignedException::getMessage() 333 const 334 { 335 return this->mMessage; 336 } 337 338 /* ************************************************************************** */ 339 /* FormNotSigendException : Function Override */ 340 /* ************************************************************************** */ 341 const char* ShrubberyCreationForm::FormNotSignedException::what() const throw() 342 { 343 return this->getMessage().c_str(); 344 } 345 346 /* ************************************************************************** */ 347 /* Global Function */ 348 /* ************************************************************************** */ 349 350 std::ostream& operator<< 351 (std::ostream& os, ShrubberyCreationForm const& target) 352 { 353 std::string isAFormSigned = target.getIsSigned() ? "true" : "false"; 354 355 os << target.getName() 356 << ", ShrubberyCreationForm signed " << isAFormSigned 357 << ", ShrubberyCreationForm sign grade " << target.getSignGrade() 358 << ", ShrubberyCreationForm execution grade " << target.getExecuteGrade() 359 << "."; 360 return os; 361 } 362 363 /* ************************************************************************** */ 364 /* Helper Functions */ 365 /* ************************************************************************** */ 366 static void _copyConstWarning(std::string const& variable) 367 { 368 std::cout << "INFO: You are trying to overwrite a const type variable: " << 369 variable << ". " << "This operation will be ignored." << std::endl; 370 }