42_CPP05/ex02/src/AForm.cpp
Camille Chauvet 3990927c54 add: ex02
2023-09-11 16:18:29 +00:00

112 lines
2.3 KiB
C++

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