/ CPP_Modules / CPP_Module_05 / ex03 / bonus / 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 18:30:02 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  		srand(time(NULL));
 88  
 89  		if (rand() % 2 == 1)
 90  		{
 91  			std::string	message = "Successfully robotomized "
 92  								+ this->getName()
 93  								+ ".";
 94  
 95  			throw message;
 96  		}
 97  		else
 98  		{
 99  			std::string	message = "Sorry. Robotomize "
100  								+ this->getName()
101  								+ " Failed.";
102  
103  			throw message;
104  		}
105  	}
106  }
107  
108  /* ************************************************************************** */
109  /*              GradeTooHighException : Constructor & Destructor              */
110  /* ************************************************************************** */
111  RobotomyRequestForm::GradeTooHighException::GradeTooHighException()
112  	: mMessage("RobotomyRequestForm::GradeTooHighException")
113  {
114  	// nothing to do
115  }
116  
117  RobotomyRequestForm::GradeTooHighException::GradeTooHighException
118  (std::string const& msg)
119  	: mMessage("RobotomyRequestForm::GradeTooHighException: " + msg)
120  {
121  	// nothing to do
122  }
123  
124  RobotomyRequestForm::GradeTooHighException::GradeTooHighException
125  (GradeTooHighException const& target)
126  	: AForm::GradeTooHighException(target.getMessage())
127  {
128  	// nothing to do
129  }
130  
131  RobotomyRequestForm::GradeTooHighException&
132  RobotomyRequestForm::GradeTooHighException::operator=
133  (GradeTooHighException const& target)
134  {
135  	if (this != &target)
136  	{
137  		this->mMessage = target.getMessage();
138  	}
139  	return *this;
140  }
141  
142  RobotomyRequestForm::GradeTooHighException::~GradeTooHighException() throw()
143  {
144  	// nothing to do
145  }
146  
147  /* ************************************************************************** */
148  /*                  GradeTooHighException : Getter Function                   */
149  /* ************************************************************************** */
150  std::string const&	RobotomyRequestForm::GradeTooHighException::getMessage()
151  	const
152  {
153  	return this->mMessage;
154  }
155  
156  /* ************************************************************************** */
157  /*                 GradeTooHighException : Function Override                  */
158  /* ************************************************************************** */
159  const char*	RobotomyRequestForm::GradeTooHighException::what() const throw()
160  {
161  	return this->getMessage().c_str();
162  }
163  
164  /* ************************************************************************** */
165  /*              GradeTooLowException : Constructor & Destructor               */
166  /* ************************************************************************** */
167  RobotomyRequestForm::GradeTooLowException::GradeTooLowException()
168  	: mMessage("RobotomyRequestForm::GradeTooLowException")
169  {
170  	// nothing to do
171  }
172  
173  RobotomyRequestForm::GradeTooLowException::GradeTooLowException
174  (std::string const& msg)
175  	: mMessage("RobotomyRequestForm::GradeTooLowException: " + msg)
176  {
177  	// nothing to do
178  }
179  
180  RobotomyRequestForm::GradeTooLowException::GradeTooLowException
181  (GradeTooLowException const& target)
182  	: AForm::GradeTooLowException(target.getMessage())
183  {
184  	// nothing to do
185  }
186  
187  RobotomyRequestForm::GradeTooLowException&
188  RobotomyRequestForm::GradeTooLowException::operator=
189  (GradeTooLowException const& target)
190  {
191  	if (this != &target)
192  	{
193  		this->mMessage = target.getMessage();
194  	}
195  	return *this;
196  }
197  
198  RobotomyRequestForm::GradeTooLowException::~GradeTooLowException() throw()
199  {
200  	// nothing to do
201  }
202  
203  /* ************************************************************************** */
204  /*                   GradeTooLowException : Getter Function                   */
205  /* ************************************************************************** */
206  std::string const&	RobotomyRequestForm::GradeTooLowException::getMessage()
207  	const
208  {
209  	return this->mMessage;
210  }
211  
212  /* ************************************************************************** */
213  /*                  GradeTooLowException : Function Override                  */
214  /* ************************************************************************** */
215  const char*	RobotomyRequestForm::GradeTooLowException::what() const throw()
216  {
217  	return this->getMessage().c_str();
218  }
219  
220  /* ************************************************************************** */
221  /*             FormNotSignedException : Constructor & Destructor              */
222  /* ************************************************************************** */
223  RobotomyRequestForm::FormNotSignedException::FormNotSignedException()
224  	: mMessage("RobotomyRequestForm::FormNotSignedException")
225  {
226  	// nothing to do
227  }
228  
229  RobotomyRequestForm::FormNotSignedException::FormNotSignedException
230  (std::string const& msg)
231  	: mMessage("RobotomyRequestForm::FormNotSignedException: " + msg)
232  {
233  	// nothing to do
234  }
235  
236  RobotomyRequestForm::FormNotSignedException::FormNotSignedException
237  (FormNotSignedException const& target)
238  	: mMessage(target.getMessage())
239  {
240  	// nothing to do
241  }
242  
243  RobotomyRequestForm::FormNotSignedException&
244  RobotomyRequestForm::FormNotSignedException::operator=
245  (FormNotSignedException const& target)
246  {
247  	if (this != &target)
248  	{
249  		this->mMessage = target.getMessage();
250  	}
251  	return *this;
252  }
253  
254  RobotomyRequestForm::FormNotSignedException::~FormNotSignedException() throw()
255  {
256  	// nothing to do
257  }
258  
259  /* ************************************************************************** */
260  /*                  FormNotSigendException : Getter Function                  */
261  /* ************************************************************************** */
262  std::string const&	RobotomyRequestForm::FormNotSignedException::getMessage()
263  	const
264  {
265  	return this->mMessage;
266  }
267  
268  /* ************************************************************************** */
269  /*                 FormNotSigendException : Function Override                 */
270  /* ************************************************************************** */
271  const char*	RobotomyRequestForm::FormNotSignedException::what() const throw()
272  {
273  	return this->getMessage().c_str();
274  }
275  
276  /* ************************************************************************** */
277  /*                              Global Function                               */
278  /* ************************************************************************** */
279  
280  std::ostream&	operator<<
281  (std::ostream& os, RobotomyRequestForm const& target)
282  {
283  	std::string	isAFormSigned = target.getIsSigned() ? "true" : "false";
284  
285  	os << target.getName()
286  	<< ", RobotomyRequestForm signed " << isAFormSigned
287  	<< ", RobotomyRequestForm sign grade " << target.getSignGrade()
288  	<< ", RobotomyRequestForm execution grade " << target.getExecuteGrade()
289  	<< ".";
290  	return os;
291  }
292  
293  /* ************************************************************************** */
294  /*                              Helper Functions                              */
295  /* ************************************************************************** */
296  static void	_copyConstWarning(std::string const& variable)
297  {
298  	std::cout << "INFO: You are trying to overwrite a const type variable: " <<
299  		variable << ". " << "This operation will be ignored." << std::endl;
300  }