init ex02
This commit is contained in:
parent
9fb01f9570
commit
2fdad9f4de
26
ex02/Makefile
Normal file
26
ex02/Makefile
Normal file
@ -0,0 +1,26 @@
|
||||
CXX := c++
|
||||
CXXFLAGS := -std=c++98 -Wall -Wextra -Werror -g
|
||||
SRCDIR := src
|
||||
OBJDIR := obj
|
||||
NAME := ex01
|
||||
|
||||
SRCS := $(wildcard $(SRCDIR)/*.cpp)
|
||||
OBJS := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
|
||||
mkdir -p obj
|
||||
$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
$(CXX) $(CXXFLAGS) $^ -o $@
|
||||
|
||||
clean:
|
||||
rm -rf $(OBJDIR)
|
||||
|
||||
fclean: clean
|
||||
rm -fr $(NAME)
|
||||
|
||||
re: fclean
|
||||
@make --no-print-directory all
|
93
ex02/src/AForm.cpp
Normal file
93
ex02/src/AForm.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
#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 == true)
|
||||
{
|
||||
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* Form::GradeTooLowException::what() const throw()
|
||||
{
|
||||
return "grade is to low";
|
||||
}
|
||||
|
||||
const char* Form::GradeTooHighException::what() const throw()
|
||||
{
|
||||
return "grade is to high";
|
||||
}
|
47
ex02/src/AForm.hpp
Normal file
47
ex02/src/AForm.hpp
Normal file
@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include <exception>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
#include "Bureaucrat.hpp"
|
||||
class Bureaucrat;
|
||||
|
||||
class AForm
|
||||
{
|
||||
private:
|
||||
const std::string _name;
|
||||
bool _signed;
|
||||
int _to_sign;
|
||||
int _to_execute;
|
||||
|
||||
public:
|
||||
AForm();
|
||||
AForm(const std::string& name, int to_execute, int to_sign);
|
||||
AForm(const AForm& src);
|
||||
virtual ~AForm();
|
||||
|
||||
AForm& operator=(const AForm& src);
|
||||
|
||||
const std::string& getName() const;
|
||||
bool getSigned() const;
|
||||
int getGradeToBeExecute() const;
|
||||
int getGradeToBeSign() const;
|
||||
|
||||
void beSigned(const Bureaucrat& bureaucrat);
|
||||
|
||||
class GradeTooHighException: public std::exception
|
||||
{
|
||||
public:
|
||||
const char * what() const throw();
|
||||
};
|
||||
|
||||
class GradeTooLowException: public std::exception
|
||||
{
|
||||
public:
|
||||
const char * what() const throw();
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const AForm& src);
|
88
ex02/src/Bureaucrat.cpp
Normal file
88
ex02/src/Bureaucrat.cpp
Normal file
@ -0,0 +1,88 @@
|
||||
#include "Bureaucrat.hpp"
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
Bureaucrat::Bureaucrat()
|
||||
{
|
||||
std::cout << "Bureaucrat()" << std::endl;
|
||||
}
|
||||
|
||||
Bureaucrat::Bureaucrat(const Bureaucrat& src)
|
||||
{
|
||||
*this = src;
|
||||
std::cout << "Bureaucrat(Bureaucrat)" << std::endl;
|
||||
}
|
||||
|
||||
Bureaucrat::Bureaucrat(const std::string& name, int grade):
|
||||
_name(name)
|
||||
{
|
||||
setGrade(grade);
|
||||
std::cout << "Bureaucrat(" << name << ", " << grade << ")" << std::endl;
|
||||
}
|
||||
|
||||
Bureaucrat::~Bureaucrat()
|
||||
{
|
||||
std::cout << "~Bureaucrat()" << std::endl;
|
||||
}
|
||||
|
||||
Bureaucrat& Bureaucrat::operator=(const Bureaucrat &src)
|
||||
{
|
||||
this->_grade = src._grade;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Bureaucrat::setGrade(int new_grade)
|
||||
{
|
||||
if (new_grade > 150)
|
||||
throw Bureaucrat::GradeTooLowException();
|
||||
if (new_grade < 1)
|
||||
throw Bureaucrat::GradeTooHighException();
|
||||
this->_grade = new_grade;
|
||||
}
|
||||
|
||||
const std::string& Bureaucrat::getName(void) const
|
||||
{
|
||||
return this->_name;
|
||||
}
|
||||
|
||||
int Bureaucrat::getGrade(void) const
|
||||
{
|
||||
return this->_grade;
|
||||
}
|
||||
|
||||
void Bureaucrat::increment()
|
||||
{
|
||||
if (this->_grade == 1)
|
||||
throw GradeTooHighException();
|
||||
this->_grade--;
|
||||
}
|
||||
|
||||
void Bureaucrat::decrement()
|
||||
{
|
||||
if (this->_grade == 150)
|
||||
throw GradeTooLowException();
|
||||
this->_grade++;
|
||||
}
|
||||
|
||||
void Bureaucrat::signForm(Form& form) const
|
||||
{
|
||||
form.beSigned(*this);
|
||||
}
|
||||
|
||||
const char* Bureaucrat::GradeTooLowException::what() const throw()
|
||||
{
|
||||
return "Too low grade";
|
||||
}
|
||||
|
||||
const char* Bureaucrat::GradeTooHighException::what() const throw()
|
||||
{
|
||||
return "Too high grade";
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream &stream, Bureaucrat &src)
|
||||
{
|
||||
stream << src.getName() << ", bureaucrat grade " << src.getGrade() << "." << std::endl;
|
||||
return stream;
|
||||
}
|
45
ex02/src/Bureaucrat.hpp
Normal file
45
ex02/src/Bureaucrat.hpp
Normal file
@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
#include "AForm.hpp"
|
||||
class AForm;
|
||||
|
||||
class Bureaucrat
|
||||
{
|
||||
private:
|
||||
const std::string _name;
|
||||
int _grade;
|
||||
|
||||
public:
|
||||
Bureaucrat();
|
||||
~Bureaucrat();
|
||||
Bureaucrat(const Bureaucrat& src);
|
||||
Bureaucrat(const std::string& name, int grade);
|
||||
|
||||
Bureaucrat& operator=(const Bureaucrat& src);
|
||||
|
||||
const std::string& getName() const;
|
||||
int getGrade() const;
|
||||
void setGrade(int new_grade);
|
||||
|
||||
void increment();
|
||||
void decrement();
|
||||
void signForm(AForm& form) const;
|
||||
|
||||
class GradeTooHighException: public std::exception
|
||||
{
|
||||
public:
|
||||
const char* what() const throw();
|
||||
};
|
||||
|
||||
class GradeTooLowException: public std::exception
|
||||
{
|
||||
public:
|
||||
const char* what() const throw();
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& stream, Bureaucrat &src);
|
186
ex02/src/main.cpp
Normal file
186
ex02/src/main.cpp
Normal file
@ -0,0 +1,186 @@
|
||||
#include "Bureaucrat.hpp"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
|
||||
std::cout << "-------------Bureaucrat TEST-----------" << std::endl;
|
||||
std::cout << "Constructor too high grade" << std::endl;
|
||||
{
|
||||
try {
|
||||
Bureaucrat crat("crat", 0);
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "Constructor too low grade" << std::endl;
|
||||
{
|
||||
try {
|
||||
Bureaucrat crat("crat", 151);
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "increment too high" << std::endl;
|
||||
{
|
||||
Bureaucrat crat("crat", 1);
|
||||
try {
|
||||
crat.increment();
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "decrement too low" << std::endl;
|
||||
{
|
||||
Bureaucrat crat("crat", 150);
|
||||
try {
|
||||
crat.decrement();
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "setgrade too low" << std::endl;
|
||||
{
|
||||
Bureaucrat crat("crat", 42);
|
||||
try {
|
||||
crat.setGrade(151);
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "setgrade too high" << std::endl;
|
||||
{
|
||||
Bureaucrat crat("crat", 42);
|
||||
try {
|
||||
crat.setGrade(0);
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "increment normal" << std::endl;
|
||||
{
|
||||
Bureaucrat crat("crat", 42);
|
||||
std::cout << "default grade: " << crat.getGrade() << std::endl;
|
||||
crat.increment();
|
||||
std::cout << "after increment grade: " << crat.getGrade() << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "decrement normal" << std::endl;
|
||||
{
|
||||
Bureaucrat crat("crat", 42);
|
||||
std::cout << "default grade: " << crat.getGrade() << std::endl;
|
||||
crat.decrement();
|
||||
std::cout << "after decrement grade: " << crat.getGrade() << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "print" << std::endl;
|
||||
{
|
||||
Bureaucrat crat("crat", 42);
|
||||
std::cout << crat;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "------------- Form TEST-----------" << std::endl;
|
||||
|
||||
std::cout << "Constructor too high sign grade" << std::endl;
|
||||
{
|
||||
try {
|
||||
Form("form", 42, 0);
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "Constructor too low sign grade" << std::endl;
|
||||
{
|
||||
try {
|
||||
Form("form", 42, 151);
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "Constructor too high exec grade" << std::endl;
|
||||
{
|
||||
try {
|
||||
Form("form", 0, 42);
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "Constructor too low exec grade" << std::endl;
|
||||
{
|
||||
try {
|
||||
Form("form", 151, 42);
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "print" << std::endl;
|
||||
{
|
||||
Form form("form", 42, 42);
|
||||
std::cout << form;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "------------- Form Sign TEST-----------" << std::endl;
|
||||
|
||||
std::cout << "Bureaucrat sign a Form" << std::endl;
|
||||
{
|
||||
Form form("form", 42, 42);
|
||||
Bureaucrat crat("crat", 42);
|
||||
|
||||
crat.signForm(form);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "Too low Bureaucrat sign a Form" << std::endl;
|
||||
{
|
||||
Form form("form", 42, 1);
|
||||
Bureaucrat crat("crat", 42);
|
||||
try {
|
||||
crat.signForm(form);
|
||||
} catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "High Bureaucrat sign a low Form" << std::endl;
|
||||
{
|
||||
Form form("form", 42, 150);
|
||||
Bureaucrat crat("crat", 1);
|
||||
|
||||
crat.signForm(form);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "Form already sign" << std::endl;
|
||||
{
|
||||
Form form("form", 42, 150);
|
||||
Bureaucrat crat("crat", 1);
|
||||
|
||||
crat.signForm(form);
|
||||
crat.signForm(form);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
Loading…
Reference in New Issue
Block a user