CPP05/ex01/src/Form.cpp

95 lines
2.1 KiB
C++

#include "Form.hpp"
#include <iostream>
#include <string>
#include "colors.h"
#define FORM BOLD BACKGROUND5 " Form: " RESET " "
#define CONSTRUCTOR "Constructor called"
#define PARAMETERIZED_CONSTRUCTOR "Parameterized constructor called"
#define DESTRUCTOR "Destructor called"
#define COPY_CONSTRUCTOR "Copy constructor called"
Form::Form(std::string name, int signGrade, int executeGrade)
: _name(name),
_isSigned(false),
_signGrade(signGrade),
_executeGrade(executeGrade)
{
std::cout << FORM PARAMETERIZED_CONSTRUCTOR << std::endl;
if (signGrade < 1 || executeGrade < 1)
throw GradeTooHighException();
if (signGrade > 150 || executeGrade > 150)
throw GradeTooLowException();
std::cout << FORM << *this << std::endl;
}
Form::Form(const Form &other)
: _name(other._name),
_isSigned(other._isSigned),
_signGrade(other._signGrade),
_executeGrade(other._executeGrade)
{
std::cout << FORM COPY_CONSTRUCTOR << std::endl;
std::cout << FORM << *this << std::endl;
}
Form::~Form()
{
std::cout << FORM DESTRUCTOR << std::endl;
}
const std::string &Form::getName() const
{
return _name;
}
bool Form::getIsSigned() const
{
return _isSigned;
}
int Form::getSignGrade() const
{
return _signGrade;
}
int Form::getExecuteGrade() const
{
return _executeGrade;
}
void Form::beSigned(const Bureaucrat &bureaucrat)
{
if (_isSigned)
throw FormAlreadySignedException();
if (bureaucrat.getGrade() > _signGrade)
throw GradeTooLowException();
_isSigned = true;
}
const char *Form::GradeTooHighException::what() const throw()
{
return "Form grade is too high!";
}
const char *Form::GradeTooLowException::what() const throw()
{
return "Form grade is too low!";
}
const char *Form::FormAlreadySignedException::what() const throw()
{
return "Form is already signed!";
}
std::ostream &operator<<(std::ostream &os, const Form &form)
{
os << "Form Name: " << form.getName()
<< ", Is Signed: " << (form.getIsSigned() ? "Yes" : "No")
<< ", Sign Grade: " << form.getSignGrade()
<< ", Execute Grade: " << form.getExecuteGrade();
return os;
}