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:20:35 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 char const* tree = 96 " \n" 97 " .:-:: .- -.- \n" 98 " -===**++++== :-== \n" 99 " -=-::++=+--*+==+==-: \n" 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 "Created by "; 118 119 outfile << tree 120 << executor.getName() 121 << "[" << executor.getGrade() << "]" 122 << std::endl; 123 124 if (!outfile.good()) 125 { 126 throw std::runtime_error("Error writing to file!"); 127 } 128 129 outfile.close(); 130 131 if (outfile.fail()) 132 { 133 throw std::runtime_error("Error closing file!"); 134 } 135 136 std::cout << "INFO: Successfully created shrubbery!" << std::endl; 137 } 138 } 139 140 /* ************************************************************************** */ 141 /* GradeTooHighException : Constructor & Destructor */ 142 /* ************************************************************************** */ 143 ShrubberyCreationForm::GradeTooHighException::GradeTooHighException() 144 : mMessage("ShrubberyCreationForm::GradeTooHighException") 145 { 146 // nothing to do 147 } 148 149 ShrubberyCreationForm::GradeTooHighException::GradeTooHighException 150 (std::string const& msg) 151 : mMessage("ShrubberyCreationForm::GradeTooHighException: " + msg) 152 { 153 // nothing to do 154 } 155 156 ShrubberyCreationForm::GradeTooHighException::GradeTooHighException 157 (GradeTooHighException const& target) 158 : AForm::GradeTooHighException(target.getMessage()) 159 { 160 // nothing to do 161 } 162 163 ShrubberyCreationForm::GradeTooHighException& 164 ShrubberyCreationForm::GradeTooHighException::operator= 165 (GradeTooHighException const& target) 166 { 167 if (this != &target) 168 { 169 this->mMessage = target.getMessage(); 170 } 171 return *this; 172 } 173 174 ShrubberyCreationForm::GradeTooHighException::~GradeTooHighException() throw() 175 { 176 // nothing to do 177 } 178 179 /* ************************************************************************** */ 180 /* GradeTooHighException : Getter Function */ 181 /* ************************************************************************** */ 182 std::string const& ShrubberyCreationForm::GradeTooHighException::getMessage() 183 const 184 { 185 return this->mMessage; 186 } 187 188 /* ************************************************************************** */ 189 /* GradeTooHighException : Function Override */ 190 /* ************************************************************************** */ 191 const char* ShrubberyCreationForm::GradeTooHighException::what() const throw() 192 { 193 return this->getMessage().c_str(); 194 } 195 196 /* ************************************************************************** */ 197 /* GradeTooLowException : Constructor & Destructor */ 198 /* ************************************************************************** */ 199 ShrubberyCreationForm::GradeTooLowException::GradeTooLowException() 200 : mMessage("ShrubberyCreationForm::GradeTooLowException") 201 { 202 // nothing to do 203 } 204 205 ShrubberyCreationForm::GradeTooLowException::GradeTooLowException 206 (std::string const& msg) 207 : mMessage("ShrubberyCreationForm::GradeTooLowException: " + msg) 208 { 209 // nothing to do 210 } 211 212 ShrubberyCreationForm::GradeTooLowException::GradeTooLowException 213 (GradeTooLowException const& target) 214 : AForm::GradeTooLowException(target.getMessage()) 215 { 216 // nothing to do 217 } 218 219 ShrubberyCreationForm::GradeTooLowException& 220 ShrubberyCreationForm::GradeTooLowException::operator= 221 (GradeTooLowException const& target) 222 { 223 if (this != &target) 224 { 225 this->mMessage = target.getMessage(); 226 } 227 return *this; 228 } 229 230 ShrubberyCreationForm::GradeTooLowException::~GradeTooLowException() throw() 231 { 232 // nothing to do 233 } 234 235 /* ************************************************************************** */ 236 /* GradeTooLowException : Getter Function */ 237 /* ************************************************************************** */ 238 std::string const& ShrubberyCreationForm::GradeTooLowException::getMessage() 239 const 240 { 241 return this->mMessage; 242 } 243 244 /* ************************************************************************** */ 245 /* GradeTooLowException : Function Override */ 246 /* ************************************************************************** */ 247 const char* ShrubberyCreationForm::GradeTooLowException::what() const throw() 248 { 249 return this->getMessage().c_str(); 250 } 251 252 /* ************************************************************************** */ 253 /* FormNotSignedException : Constructor & Destructor */ 254 /* ************************************************************************** */ 255 ShrubberyCreationForm::FormNotSignedException::FormNotSignedException() 256 : mMessage("ShrubberyCreationForm::FormNotSignedException") 257 { 258 // nothing to do 259 } 260 261 ShrubberyCreationForm::FormNotSignedException::FormNotSignedException 262 (std::string const& msg) 263 : mMessage("ShrubberyCreationForm::FormNotSignedException: " + msg) 264 { 265 // nothing to do 266 } 267 268 ShrubberyCreationForm::FormNotSignedException::FormNotSignedException 269 (FormNotSignedException const& target) 270 : mMessage(target.getMessage()) 271 { 272 // nothing to do 273 } 274 275 ShrubberyCreationForm::FormNotSignedException& 276 ShrubberyCreationForm::FormNotSignedException::operator= 277 (FormNotSignedException const& target) 278 { 279 if (this != &target) 280 { 281 this->mMessage = target.getMessage(); 282 } 283 return *this; 284 } 285 286 ShrubberyCreationForm::FormNotSignedException::~FormNotSignedException() throw() 287 { 288 // nothing to do 289 } 290 291 /* ************************************************************************** */ 292 /* FormNotSigendException : Getter Function */ 293 /* ************************************************************************** */ 294 std::string const& ShrubberyCreationForm::FormNotSignedException::getMessage() 295 const 296 { 297 return this->mMessage; 298 } 299 300 /* ************************************************************************** */ 301 /* FormNotSigendException : Function Override */ 302 /* ************************************************************************** */ 303 const char* ShrubberyCreationForm::FormNotSignedException::what() const throw() 304 { 305 return this->getMessage().c_str(); 306 } 307 308 /* ************************************************************************** */ 309 /* Global Function */ 310 /* ************************************************************************** */ 311 312 std::ostream& operator<< 313 (std::ostream& os, ShrubberyCreationForm const& target) 314 { 315 std::string isAFormSigned = target.getIsSigned() ? "true" : "false"; 316 317 os << target.getName() 318 << ", ShrubberyCreationForm signed " << isAFormSigned 319 << ", ShrubberyCreationForm sign grade " << target.getSignGrade() 320 << ", ShrubberyCreationForm execution grade " << target.getExecuteGrade() 321 << "."; 322 return os; 323 } 324 325 /* ************************************************************************** */ 326 /* Helper Functions */ 327 /* ************************************************************************** */ 328 static void _copyConstWarning(std::string const& variable) 329 { 330 std::cout << "INFO: You are trying to overwrite a const type variable: " << 331 variable << ". " << "This operation will be ignored." << std::endl; 332 }