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