42_CPP05/ex01/src/Form.cpp
2023-09-03 11:23:21 +00:00

94 lines
1.9 KiB
C++

#include "Form.hpp"
#include <iostream>
#include <ostream>
#include <string>
Form::Form()
{
std::cout << "Form()" << std::endl;
}
Form::Form(const Form& src)
{
std::cout << "Form(Form)" << std::endl;
*this = src;
}
Form::Form(const std::string& name, int to_execute, int to_sign):
_name(name),
_signed(0),
_to_sign(to_sign),
_to_execute(to_execute)
{
std::cout << "Form(" << name << ", " << to_execute << ", " << to_sign << ")" << std::endl;
if (to_execute > 150 || to_sign < 1)
throw GradeTooLowException();
if (to_execute < 1 || to_sign < 1)
throw GradeTooHighException();
}
Form::~Form()
{
std::cout << "~Form()" << std::endl;
}
Form& Form::operator=(const Form& src)
{
this->_signed = src._signed;
this->_to_sign = src._to_sign;
this->_to_execute = src._to_execute;
return *this;
}
void Form::beSigned(const Bureaucrat &bureaucrat)
{
if (this->_signed == false)
{
std::cout << bureaucrat.getName() << " couldn't sign " << this->_name << " because " << "the form is already sign" << std::endl;
return;
}
if (bureaucrat.getGrade() > this->_to_sign)
throw GradeTooLowException();
this->_signed = true;
std::cout << bureaucrat.getName() << " signed " << this->_name << std::endl;
}
const std::string& Form::getName() const
{
return this->_name;
}
bool Form::getSigned() const
{
return this->_signed;
}
int Form::getGradeToBeExecute() const
{
return this->_to_execute;
}
int Form::getGradeToBeSign() const
{
return this->_to_sign;
}
std::ostream& operator<<(std::ostream& os, const Form& src)
{
os << src.getName() << " Form signed: " << src.getSigned() << ", grade to be execute " << src.getGradeToBeExecute() << ", grade to be sign: " << src.getGradeToBeSign() << std::endl;
return os;
}
const char* Bureaucrat::GradeTooLowException::what() const throw()
{
return "grade is to low";
}
const char* Bureaucrat::GradeTooHighException::what() const throw()
{
return "grade is to high";
}