Refactor Bureaucrat and AForm classes, add form execution logic, and enhance main function for comprehensive testing
This commit is contained in:
parent
d0966d7704
commit
9ec113d630
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@
|
||||
Bureaucrat
|
||||
*.o
|
||||
.cache
|
||||
*_shrubbery
|
||||
|
||||
@ -5,14 +5,14 @@
|
||||
#define AFORM BOLD BACKGROUND5 " AForm: " RESET " "
|
||||
#define SHRUBBERY_CREATION_FORM BOLD BACKGROUND10 " ShrubberyCreationForm: " RESET " "
|
||||
#define ROBOTOMY_REQUEST_FORM BOLD BACKGROUND11 " RobotomyRequestForm: " RESET " "
|
||||
#define PRESIDENTIAL_PARDON_FORM BOLD BACKGROUND9 " PresidentialPardonForm: " RESET " "
|
||||
#define PRESIDENTIAL_PARDON_FORM BOLD BACKGROUND8 " PresidentialPardonForm: " RESET " "
|
||||
|
||||
#define CONSTRUCTOR "Constructor called"
|
||||
#define PARAMETERIZED_CONSTRUCTOR "Parameterized constructor called"
|
||||
#define DESTRUCTOR "Destructor called"
|
||||
#define COPY_CONSTRUCTOR "Copy constructor called"
|
||||
|
||||
#define EXCEPTION BOLD BACKGROUND1 " Exception: " RESET " "
|
||||
#define EXCEPTION BOLD BACKGROUND9 " Exception: " RESET " "
|
||||
#define INFO ITALIC BOLD UNDERLINE GREEN "Info: "
|
||||
#define PRINT BOLD BACKGROUND7 " Print: " RESET " "
|
||||
#define END_SCOPE ITALIC BOLD UNDERLINE RED "End of scope" RESET
|
||||
|
||||
@ -79,7 +79,7 @@ void Bureaucrat::signForm(AForm &form)
|
||||
}
|
||||
catch (std::exception &e)
|
||||
{
|
||||
std::cout << BUREAUCRAT << _name << " cannot sign " << form.getName()
|
||||
std::cout << EXCEPTION << _name << " cannot sign " << form.getName()
|
||||
<< " because: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,40 +8,71 @@
|
||||
|
||||
#include "colors.h"
|
||||
|
||||
int main(void)
|
||||
void test(Bureaucrat &bureaucrat)
|
||||
{
|
||||
try
|
||||
{
|
||||
Bureaucrat bureaucrat("John", 1);
|
||||
std::cout << PRINT << bureaucrat << std::endl;
|
||||
std::cout << INFO << "Testing Bureaucrat: " << bureaucrat.getName()
|
||||
<< ", Grade: " << bureaucrat.getGrade() << std::endl;
|
||||
std::cout << PRINT << bureaucrat << "\n" << std::endl;
|
||||
|
||||
std::cout << INFO << "Creating ShrubberyCreationForm" << std::endl;
|
||||
ShrubberyCreationForm shrubberyForm("Garden");
|
||||
std::cout << PRINT << shrubberyForm << std::endl;
|
||||
std::cout << PRINT << shrubberyForm << "\n" << std::endl;
|
||||
|
||||
std::cout << INFO << "Signing ShrubberyCreationForm" << std::endl;
|
||||
bureaucrat.signForm(shrubberyForm);
|
||||
std::cout << PRINT << shrubberyForm << std::endl;
|
||||
std::cout << PRINT << shrubberyForm << "\n" << std::endl;
|
||||
|
||||
std::cout << INFO << "Executing ShrubberyCreationForm" << std::endl;
|
||||
shrubberyForm.execute(bureaucrat);
|
||||
|
||||
std::cout << INFO << "Creating RobotomyRequestForm" << std::endl;
|
||||
RobotomyRequestForm robotomyForm("Alice");
|
||||
std::cout << PRINT << robotomyForm << std::endl;
|
||||
std::cout << PRINT << robotomyForm << "\n" << std::endl;
|
||||
|
||||
std::cout << INFO << "Signing RobotomyRequestForm" << std::endl;
|
||||
bureaucrat.signForm(robotomyForm);
|
||||
std::cout << PRINT << robotomyForm << std::endl;
|
||||
std::cout << PRINT << robotomyForm << "\n" << std::endl;
|
||||
|
||||
std::cout << INFO << "Executing RobotomyRequestForm" << std::endl;
|
||||
robotomyForm.execute(bureaucrat);
|
||||
std::cout << "\n" << std::endl;
|
||||
|
||||
std::cout << INFO << "Creating PresidentialPardonForm" << std::endl;
|
||||
PresidentialPardonForm pardonForm("Bob");
|
||||
std::cout << PRINT << pardonForm << std::endl;
|
||||
std::cout << PRINT << pardonForm << "\n" << std::endl;
|
||||
|
||||
std::cout << INFO << "Signing PresidentialPardonForm" << std::endl;
|
||||
bureaucrat.signForm(pardonForm);
|
||||
std::cout << PRINT << pardonForm << std::endl;
|
||||
std::cout << PRINT << pardonForm << "\n" << std::endl;
|
||||
|
||||
std::cout << INFO << "Executing PresidentialPardonForm" << std::endl;
|
||||
pardonForm.execute(bureaucrat);
|
||||
std::cout << "\n" << END_SCOPE << std::endl;
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
std::cerr << "Exception: " << e.what() << std::endl;
|
||||
std::cerr << EXCEPTION << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
int main(void)
|
||||
{
|
||||
for (int i = 0; i < 250; i += 10)
|
||||
{
|
||||
std::cout << INFO << "Testing Bureaucrat with grade: " << i << std::endl;
|
||||
try
|
||||
{
|
||||
Bureaucrat bureaucrat("John", i);
|
||||
test(bureaucrat);
|
||||
std::cout << END_SCOPE << std::endl;
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
std::cerr << EXCEPTION << e.what() << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
4
ex03/Makefile
Normal file
4
ex03/Makefile
Normal file
@ -0,0 +1,4 @@
|
||||
NAME = Bureaucrat
|
||||
SRC = $(wildcard *.cpp))
|
||||
|
||||
-include ../common.mk
|
||||
56
ex03/inc/AForm.hpp
Normal file
56
ex03/inc/AForm.hpp
Normal file
@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "Bureaucrat.hpp"
|
||||
|
||||
class AForm
|
||||
{
|
||||
private:
|
||||
const std::string _name;
|
||||
bool _isSigned;
|
||||
const int _signGrade;
|
||||
const int _executeGrade;
|
||||
|
||||
public:
|
||||
AForm() = delete;
|
||||
AForm(std::string name, int signGrade, int executeGrade);
|
||||
AForm(const AForm &other);
|
||||
~AForm();
|
||||
AForm &operator=(const AForm &other) = delete;
|
||||
const std::string &getName() const;
|
||||
bool getIsSigned() const;
|
||||
int getSignGrade() const;
|
||||
int getExecuteGrade() const;
|
||||
void beSigned(const Bureaucrat &bureaucrat);
|
||||
bool isExecutable(const Bureaucrat &executor) const;
|
||||
virtual void execute(const Bureaucrat &executor)
|
||||
const = 0; // Pure virtual function, making AForm an abstract class
|
||||
|
||||
class GradeTooHighException : public std::exception
|
||||
{
|
||||
public:
|
||||
const char *what() const throw();
|
||||
};
|
||||
|
||||
class GradeTooLowException : public std::exception
|
||||
{
|
||||
public:
|
||||
const char *what() const throw();
|
||||
};
|
||||
|
||||
class FormAlreadySignedException : public std::exception
|
||||
{
|
||||
public:
|
||||
const char *what() const throw();
|
||||
};
|
||||
class FormNotSignedException : public std::exception
|
||||
{
|
||||
public:
|
||||
const char *what() const throw();
|
||||
};
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const AForm &form);
|
||||
44
ex03/inc/Bureaucrat.hpp
Normal file
44
ex03/inc/Bureaucrat.hpp
Normal file
@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#define HIGHEST_GRADE 1
|
||||
#define LOWEST_GRADE 150
|
||||
|
||||
class Bureaucrat
|
||||
{
|
||||
private:
|
||||
const std::string _name;
|
||||
int _grade;
|
||||
|
||||
public:
|
||||
Bureaucrat() = delete;
|
||||
Bureaucrat(std::string name, int grade);
|
||||
Bureaucrat(Bureaucrat &other);
|
||||
~Bureaucrat();
|
||||
|
||||
Bureaucrat &operator=(Bureaucrat &other) = delete;
|
||||
|
||||
Bureaucrat &operator++(); // Pre-increment
|
||||
Bureaucrat operator++(int); // Post-increment
|
||||
Bureaucrat &operator--(); // Pre-decrement
|
||||
Bureaucrat operator--(int); // Post-decrement
|
||||
|
||||
int getGrade() const;
|
||||
const std::string getName() const;
|
||||
void signForm(class AForm &form);
|
||||
|
||||
class GradeTooHighException : public std::exception
|
||||
{
|
||||
const char *what() const throw();
|
||||
};
|
||||
|
||||
class GradeTooLowException : public std::exception
|
||||
{
|
||||
const char *what() const throw();
|
||||
};
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat);
|
||||
25
ex03/inc/PresidentialPardonForm.hpp
Normal file
25
ex03/inc/PresidentialPardonForm.hpp
Normal file
@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "AForm.hpp"
|
||||
|
||||
class PresidentialPardonForm : public AForm
|
||||
{
|
||||
private:
|
||||
const std::string _target;
|
||||
|
||||
public:
|
||||
PresidentialPardonForm() = delete;
|
||||
PresidentialPardonForm(std::string target);
|
||||
PresidentialPardonForm(const PresidentialPardonForm &other);
|
||||
~PresidentialPardonForm();
|
||||
PresidentialPardonForm &operator=(const PresidentialPardonForm &other) =
|
||||
delete;
|
||||
|
||||
void execute(const Bureaucrat &executor) const override;
|
||||
const std::string getTarget() const;
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const PresidentialPardonForm &form);
|
||||
24
ex03/inc/RobotomyRequestForm.hpp
Normal file
24
ex03/inc/RobotomyRequestForm.hpp
Normal file
@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "AForm.hpp"
|
||||
|
||||
class RobotomyRequestForm : public AForm
|
||||
{
|
||||
private:
|
||||
const std::string _target;
|
||||
|
||||
public:
|
||||
RobotomyRequestForm() = delete;
|
||||
RobotomyRequestForm(std::string target);
|
||||
RobotomyRequestForm(const RobotomyRequestForm &other);
|
||||
~RobotomyRequestForm();
|
||||
RobotomyRequestForm &operator=(const RobotomyRequestForm &other) = delete;
|
||||
|
||||
void execute(const Bureaucrat &executor) const override;
|
||||
const std::string getTarget() const;
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const RobotomyRequestForm &form);
|
||||
25
ex03/inc/ShrubberyCreationForm.hpp
Normal file
25
ex03/inc/ShrubberyCreationForm.hpp
Normal file
@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "AForm.hpp"
|
||||
|
||||
class ShrubberyCreationForm : public AForm
|
||||
{
|
||||
private:
|
||||
const std::string _target;
|
||||
|
||||
public:
|
||||
ShrubberyCreationForm() = delete; // Default constructor is deleted
|
||||
ShrubberyCreationForm(std::string target);
|
||||
ShrubberyCreationForm(const ShrubberyCreationForm &other);
|
||||
~ShrubberyCreationForm();
|
||||
ShrubberyCreationForm &operator=(const ShrubberyCreationForm &other) =
|
||||
delete;
|
||||
|
||||
void execute(const Bureaucrat &executor) const override;
|
||||
const std::string getTarget() const;
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const ShrubberyCreationForm &form);
|
||||
83
ex03/inc/colors.h
Normal file
83
ex03/inc/colors.h
Normal file
@ -0,0 +1,83 @@
|
||||
#ifndef COLORS_H
|
||||
#define COLORS_H
|
||||
|
||||
#define BUREAUCRAT BOLD BACKGROUND4 " Bureaucrat: " RESET " "
|
||||
#define AFORM BOLD BACKGROUND5 " AForm: " RESET " "
|
||||
#define SHRUBBERY_CREATION_FORM BOLD BACKGROUND10 " ShrubberyCreationForm: " RESET " "
|
||||
#define ROBOTOMY_REQUEST_FORM BOLD BACKGROUND11 " RobotomyRequestForm: " RESET " "
|
||||
#define PRESIDENTIAL_PARDON_FORM BOLD BACKGROUND8 " PresidentialPardonForm: " RESET " "
|
||||
|
||||
#define CONSTRUCTOR "Constructor called"
|
||||
#define PARAMETERIZED_CONSTRUCTOR "Parameterized constructor called"
|
||||
#define DESTRUCTOR "Destructor called"
|
||||
#define COPY_CONSTRUCTOR "Copy constructor called"
|
||||
|
||||
#define EXCEPTION BOLD BACKGROUND9 " Exception: " RESET " "
|
||||
#define INFO ITALIC BOLD UNDERLINE GREEN "Info: "
|
||||
#define PRINT BOLD BACKGROUND7 " Print: " RESET " "
|
||||
#define END_SCOPE ITALIC BOLD UNDERLINE RED "End of scope" RESET
|
||||
#define END_SCOPE_EXCEPTION ITALIC BOLD UNDERLINE RED "End of scope with exception" RESET
|
||||
|
||||
#define RESET "\033[m"
|
||||
|
||||
#define BOLD_UNDERLINE "\033[1;4m"
|
||||
#define BOLD_UNDERLINE_OFF "\033[22;24m"
|
||||
|
||||
#define BOLD "\033[1m"
|
||||
#define UNDERLINE "\033[4m"
|
||||
#define ITALIC "\033[3m"
|
||||
#define BLINK "\033[5m"
|
||||
#define INVERSE "\033[7m"
|
||||
#define STRIKETHROUGH "\033[9m"
|
||||
|
||||
#define BOLD_OFF "\033[22m"
|
||||
#define UNDERLINE_OFF "\033[24m"
|
||||
#define ITALIC_OFF "\033[23m"
|
||||
#define BLINK_OFF "\033[25m"
|
||||
#define INVERSE_OFF "\033[27m"
|
||||
#define STRIKETHROUGH_OFF "\033[29m"
|
||||
|
||||
#define BLACK "\033[30m"
|
||||
#define RED "\033[31m"
|
||||
#define GREEN "\033[32m"
|
||||
#define YELLOW "\033[33m"
|
||||
#define BLUE "\033[34m"
|
||||
#define MAGENTA "\033[35m"
|
||||
#define CYAN "\033[36m"
|
||||
#define WHITE "\033[37m"
|
||||
|
||||
#define COLOR0 "\033[38;5;0m"
|
||||
#define COLOR1 "\033[38;5;1m"
|
||||
#define COLOR2 "\033[38;5;2m"
|
||||
#define COLOR3 "\033[38;5;3m"
|
||||
#define COLOR4 "\033[38;5;4m"
|
||||
#define COLOR5 "\033[38;5;5m"
|
||||
#define COLOR6 "\033[38;5;6m"
|
||||
#define COLOR7 "\033[38;5;7m"
|
||||
#define COLOR8 "\033[38;5;8m"
|
||||
#define COLOR9 "\033[38;5;9m"
|
||||
#define COLOR10 "\033[38;5;10m"
|
||||
#define COLOR11 "\033[38;5;11m"
|
||||
#define COLOR12 "\033[38;5;12m"
|
||||
#define COLOR13 "\033[38;5;13m"
|
||||
#define COLOR14 "\033[38;5;14m"
|
||||
#define COLOR15 "\033[38;5;15m"
|
||||
|
||||
#define BACKGROUND0 "\033[48;5;0m"
|
||||
#define BACKGROUND1 "\033[48;5;1m"
|
||||
#define BACKGROUND2 "\033[48;5;2m"
|
||||
#define BACKGROUND3 "\033[48;5;3m"
|
||||
#define BACKGROUND4 "\033[48;5;4m"
|
||||
#define BACKGROUND5 "\033[48;5;5m"
|
||||
#define BACKGROUND6 "\033[48;5;6m"
|
||||
#define BACKGROUND7 "\033[48;5;7m"
|
||||
#define BACKGROUND8 "\033[48;5;8m"
|
||||
#define BACKGROUND9 "\033[48;5;9m"
|
||||
#define BACKGROUND10 "\033[48;5;10m"
|
||||
#define BACKGROUND11 "\033[48;5;11m"
|
||||
#define BACKGROUND12 "\033[48;5;12m"
|
||||
#define BACKGROUND13 "\033[48;5;13m"
|
||||
#define BACKGROUND14 "\033[48;5;14m"
|
||||
#define BACKGROUND15 "\033[48;5;15m"
|
||||
|
||||
#endif
|
||||
102
ex03/src/AForm.cpp
Normal file
102
ex03/src/AForm.cpp
Normal file
@ -0,0 +1,102 @@
|
||||
#include "AForm.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "colors.h"
|
||||
|
||||
AForm::AForm(std::string name, int signGrade, int executeGrade)
|
||||
: _name(name),
|
||||
_isSigned(false),
|
||||
_signGrade(signGrade),
|
||||
_executeGrade(executeGrade)
|
||||
{
|
||||
std::cout << AFORM PARAMETERIZED_CONSTRUCTOR << std::endl;
|
||||
if (signGrade < 1 || executeGrade < 1)
|
||||
throw GradeTooHighException();
|
||||
if (signGrade > 150 || executeGrade > 150)
|
||||
throw GradeTooLowException();
|
||||
std::cout << AFORM << *this << std::endl;
|
||||
}
|
||||
|
||||
AForm::AForm(const AForm &other)
|
||||
: _name(other._name),
|
||||
_isSigned(other._isSigned),
|
||||
_signGrade(other._signGrade),
|
||||
_executeGrade(other._executeGrade)
|
||||
{
|
||||
std::cout << AFORM COPY_CONSTRUCTOR << std::endl;
|
||||
std::cout << AFORM << *this << std::endl;
|
||||
}
|
||||
|
||||
AForm::~AForm()
|
||||
{
|
||||
std::cout << AFORM DESTRUCTOR << std::endl;
|
||||
}
|
||||
|
||||
const std::string &AForm::getName() const
|
||||
{
|
||||
return _name;
|
||||
}
|
||||
|
||||
bool AForm::getIsSigned() const
|
||||
{
|
||||
return _isSigned;
|
||||
}
|
||||
|
||||
int AForm::getSignGrade() const
|
||||
{
|
||||
return _signGrade;
|
||||
}
|
||||
|
||||
int AForm::getExecuteGrade() const
|
||||
{
|
||||
return _executeGrade;
|
||||
}
|
||||
|
||||
void AForm::beSigned(const Bureaucrat &bureaucrat)
|
||||
{
|
||||
if (_isSigned)
|
||||
throw FormAlreadySignedException();
|
||||
if (bureaucrat.getGrade() > _signGrade)
|
||||
throw GradeTooLowException();
|
||||
_isSigned = true;
|
||||
}
|
||||
|
||||
bool AForm::isExecutable(const Bureaucrat &executor) const
|
||||
{
|
||||
if (!_isSigned)
|
||||
throw FormNotSignedException();
|
||||
if (executor.getGrade() > _executeGrade)
|
||||
throw GradeTooLowException();
|
||||
return true;
|
||||
}
|
||||
|
||||
const char *AForm::GradeTooHighException::what() const throw()
|
||||
{
|
||||
return "Form grade is too high!";
|
||||
}
|
||||
|
||||
const char *AForm::GradeTooLowException::what() const throw()
|
||||
{
|
||||
return "Form grade is too low!";
|
||||
}
|
||||
|
||||
const char *AForm::FormAlreadySignedException::what() const throw()
|
||||
{
|
||||
return "Form is already signed!";
|
||||
}
|
||||
|
||||
const char *AForm::FormNotSignedException::what() const throw()
|
||||
{
|
||||
return "Form is not signed!";
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const AForm &form)
|
||||
{
|
||||
os << "Form Name: " << form.getName()
|
||||
<< ", Is Signed: " << (form.getIsSigned() ? "Yes" : "No")
|
||||
<< ", Sign Grade: " << form.getSignGrade()
|
||||
<< ", Execute Grade: " << form.getExecuteGrade();
|
||||
return os;
|
||||
}
|
||||
102
ex03/src/Bureaucrat.cpp
Normal file
102
ex03/src/Bureaucrat.cpp
Normal file
@ -0,0 +1,102 @@
|
||||
#include "Bureaucrat.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "AForm.hpp"
|
||||
|
||||
#include "colors.h"
|
||||
|
||||
Bureaucrat::Bureaucrat(std::string name, int grade) : _name(name)
|
||||
{
|
||||
std::cout << BUREAUCRAT PARAMETERIZED_CONSTRUCTOR << std::endl;
|
||||
if (grade < HIGHEST_GRADE)
|
||||
throw(Bureaucrat::GradeTooHighException());
|
||||
if (grade > LOWEST_GRADE)
|
||||
throw(Bureaucrat::GradeTooLowException());
|
||||
_grade = grade;
|
||||
std::cout << BUREAUCRAT << *this << std::endl;
|
||||
}
|
||||
|
||||
Bureaucrat::Bureaucrat(Bureaucrat &other)
|
||||
: _name(other._name), _grade(other._grade)
|
||||
{
|
||||
std::cout << BUREAUCRAT COPY_CONSTRUCTOR << std::endl;
|
||||
std::cout << BUREAUCRAT << *this << std::endl;
|
||||
}
|
||||
|
||||
Bureaucrat::~Bureaucrat()
|
||||
{
|
||||
std::cout << BUREAUCRAT DESTRUCTOR << std::endl;
|
||||
}
|
||||
|
||||
Bureaucrat &Bureaucrat::operator++() // Pre-increment
|
||||
{
|
||||
if (_grade <= HIGHEST_GRADE)
|
||||
throw(Bureaucrat::GradeTooHighException());
|
||||
_grade--;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Bureaucrat Bureaucrat::operator++(int) // Post-increment
|
||||
{
|
||||
Bureaucrat temp(*this);
|
||||
++(*this);
|
||||
return temp;
|
||||
}
|
||||
|
||||
Bureaucrat &Bureaucrat::operator--() // Pre-decrement
|
||||
{
|
||||
if (_grade >= LOWEST_GRADE)
|
||||
throw(Bureaucrat::GradeTooLowException());
|
||||
_grade++;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Bureaucrat Bureaucrat::operator--(int) // Post-decrement
|
||||
{
|
||||
Bureaucrat temp(*this);
|
||||
--(*this);
|
||||
return temp;
|
||||
}
|
||||
|
||||
int Bureaucrat::getGrade() const
|
||||
{
|
||||
return _grade;
|
||||
}
|
||||
|
||||
const std::string Bureaucrat::getName() const
|
||||
{
|
||||
return _name;
|
||||
}
|
||||
|
||||
void Bureaucrat::signForm(AForm &form)
|
||||
{
|
||||
try
|
||||
{
|
||||
form.beSigned(*this);
|
||||
std::cout << BUREAUCRAT << _name << " signs " << form.getName()
|
||||
<< std::endl;
|
||||
}
|
||||
catch (std::exception &e)
|
||||
{
|
||||
std::cout << EXCEPTION << _name << " cannot sign " << form.getName()
|
||||
<< " because: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
const char *Bureaucrat::GradeTooHighException::what() const throw()
|
||||
{
|
||||
return "Grade too high!";
|
||||
}
|
||||
|
||||
const char *Bureaucrat::GradeTooLowException::what() const throw()
|
||||
{
|
||||
return "Grade too low!";
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
|
||||
{
|
||||
std::cout << bureaucrat.getName() << ", bureaucrat grade "
|
||||
<< bureaucrat.getGrade();
|
||||
return os;
|
||||
}
|
||||
53
ex03/src/PresidentialPardonForm.cpp
Normal file
53
ex03/src/PresidentialPardonForm.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
#include "PresidentialPardonForm.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "AForm.hpp"
|
||||
|
||||
#include "colors.h"
|
||||
|
||||
PresidentialPardonForm::PresidentialPardonForm(std::string target)
|
||||
: AForm("Presidential Pardon", 25, 5), _target(target)
|
||||
{
|
||||
std::cout << PRESIDENTIAL_PARDON_FORM PARAMETERIZED_CONSTRUCTOR
|
||||
<< std::endl;
|
||||
std::cout << PRESIDENTIAL_PARDON_FORM << *this << std::endl;
|
||||
}
|
||||
|
||||
PresidentialPardonForm::~PresidentialPardonForm()
|
||||
{
|
||||
std::cout << PRESIDENTIAL_PARDON_FORM DESTRUCTOR << std::endl;
|
||||
}
|
||||
|
||||
PresidentialPardonForm::PresidentialPardonForm(
|
||||
const PresidentialPardonForm &other)
|
||||
: AForm(other), _target(other._target)
|
||||
{
|
||||
std::cout << PRESIDENTIAL_PARDON_FORM COPY_CONSTRUCTOR << std::endl;
|
||||
std::cout << PRESIDENTIAL_PARDON_FORM << *this << std::endl;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const PresidentialPardonForm &form)
|
||||
{
|
||||
os << form.getName()
|
||||
<< " Form: " << (form.getIsSigned() ? "Signed" : "Not Signed")
|
||||
<< ", Sign Grade: " << form.getSignGrade()
|
||||
<< ", Execute Grade: " << form.getExecuteGrade()
|
||||
<< ", Target: " << form.getTarget();
|
||||
return os;
|
||||
}
|
||||
|
||||
void PresidentialPardonForm::execute(const Bureaucrat &executor) const
|
||||
{
|
||||
isExecutable(executor);
|
||||
|
||||
std::cout << PRESIDENTIAL_PARDON_FORM << executor.getName()
|
||||
<< " made shure Zaphod Beeblebrox pardoned " << _target << "."
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
const std::string PresidentialPardonForm::getTarget() const
|
||||
{
|
||||
return _target;
|
||||
}
|
||||
55
ex03/src/RobotomyRequestForm.cpp
Normal file
55
ex03/src/RobotomyRequestForm.cpp
Normal file
@ -0,0 +1,55 @@
|
||||
#include "RobotomyRequestForm.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "colors.h"
|
||||
|
||||
RobotomyRequestForm::RobotomyRequestForm(std::string target)
|
||||
: AForm("Robotomy Request", 72, 45), _target(target)
|
||||
{
|
||||
std::cout << ROBOTOMY_REQUEST_FORM PARAMETERIZED_CONSTRUCTOR << std::endl;
|
||||
}
|
||||
|
||||
RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm &other)
|
||||
: AForm(other), _target(other._target)
|
||||
{
|
||||
std::cout << ROBOTOMY_REQUEST_FORM COPY_CONSTRUCTOR << std::endl;
|
||||
}
|
||||
|
||||
RobotomyRequestForm::~RobotomyRequestForm()
|
||||
{
|
||||
std::cout << ROBOTOMY_REQUEST_FORM DESTRUCTOR << std::endl;
|
||||
}
|
||||
|
||||
void RobotomyRequestForm::execute(const Bureaucrat &executor) const
|
||||
{
|
||||
isExecutable(executor);
|
||||
|
||||
std::cout << ROBOTOMY_REQUEST_FORM << "Drrr, Drrr. Drilling noises... "
|
||||
<< std::endl;
|
||||
std::srand(static_cast<unsigned int>(std::time(0)));
|
||||
if (std::rand() % 2)
|
||||
std::cout << ROBOTOMY_REQUEST_FORM << _target
|
||||
<< " has been robotomized!" << std::endl;
|
||||
else
|
||||
std::cout << ROBOTOMY_REQUEST_FORM << _target << "'s robotomy failed. "
|
||||
<< _target << " is not the same anymore..." << std::endl;
|
||||
}
|
||||
|
||||
const std::string RobotomyRequestForm::getTarget() const
|
||||
{
|
||||
return _target;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const RobotomyRequestForm &form)
|
||||
{
|
||||
os << form.getName()
|
||||
<< " Form: " << (form.getIsSigned() ? "Signed" : "Not Signed")
|
||||
<< ", Sign Grade: " << form.getSignGrade()
|
||||
<< ", Execute Grade: " << form.getExecuteGrade()
|
||||
<< ", Target: " << form.getTarget();
|
||||
return os;
|
||||
}
|
||||
75
ex03/src/ShrubberyCreationForm.cpp
Normal file
75
ex03/src/ShrubberyCreationForm.cpp
Normal file
@ -0,0 +1,75 @@
|
||||
#include "ShrubberyCreationForm.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include "AForm.hpp"
|
||||
#include "Bureaucrat.hpp"
|
||||
|
||||
#include "colors.h"
|
||||
|
||||
ShrubberyCreationForm::ShrubberyCreationForm(std::string target)
|
||||
: AForm("Shrubbery Creation", 145, 137), _target(target)
|
||||
{
|
||||
std::cout << SHRUBBERY_CREATION_FORM PARAMETERIZED_CONSTRUCTOR << std::endl;
|
||||
std::cout << SHRUBBERY_CREATION_FORM << *this << std::endl;
|
||||
}
|
||||
|
||||
ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm &other)
|
||||
: AForm(other), _target(other._target)
|
||||
{
|
||||
std::cout << SHRUBBERY_CREATION_FORM COPY_CONSTRUCTOR << std::endl;
|
||||
std::cout << SHRUBBERY_CREATION_FORM << *this << std::endl;
|
||||
}
|
||||
|
||||
ShrubberyCreationForm::~ShrubberyCreationForm()
|
||||
{
|
||||
std::cout << SHRUBBERY_CREATION_FORM DESTRUCTOR << std::endl;
|
||||
}
|
||||
|
||||
void ShrubberyCreationForm::execute(const Bureaucrat &executor) const
|
||||
{
|
||||
isExecutable(executor);
|
||||
std::ofstream ofs(_target + "_shrubbery");
|
||||
if (!ofs)
|
||||
{
|
||||
std::cerr << "Error creating file: " << _target << "_shrubbery"
|
||||
<< std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
ofs << " # #### ####\n"
|
||||
<< " ### \\/#|### |/####\n"
|
||||
<< " ##\\/#/ \\||/##/_/##/_#\n"
|
||||
<< " ### \\/###|/ \\/ # ###\n"
|
||||
<< " ##_\\_#\\_\\## | #/###_/_####\n"
|
||||
<< " ## #### # \\ #| / #### ##/##\n"
|
||||
<< " __#_--###` |{,###---###-~\n"
|
||||
<< " \\ }{\n"
|
||||
<< " }}{\n"
|
||||
<< " }}{\n"
|
||||
<< " {{}\n"
|
||||
<< " , -=-~{ .-^- _\n"
|
||||
<< " `}\n"
|
||||
<< " {\n";
|
||||
|
||||
ofs.close();
|
||||
std::cout << SHRUBBERY_CREATION_FORM
|
||||
<< "Shrubbery created in file: " << _target << "_shrubbery"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
const std::string ShrubberyCreationForm::getTarget() const
|
||||
{
|
||||
return _target;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const ShrubberyCreationForm &form)
|
||||
{
|
||||
os << form.getName()
|
||||
<< " Form: " << (form.getIsSigned() ? "Signed" : "Not Signed")
|
||||
<< ", Sign Grade: " << form.getSignGrade()
|
||||
<< ", Execute Grade: " << form.getExecuteGrade()
|
||||
<< ", Target: " << form.getTarget();
|
||||
return os;
|
||||
}
|
||||
79
ex03/src/main.cpp
Normal file
79
ex03/src/main.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "Bureaucrat.hpp"
|
||||
#include "PresidentialPardonForm.hpp"
|
||||
#include "RobotomyRequestForm.hpp"
|
||||
#include "ShrubberyCreationForm.hpp"
|
||||
|
||||
#include "colors.h"
|
||||
|
||||
void test(Bureaucrat &bureaucrat)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::cout << INFO << "Testing Bureaucrat: " << bureaucrat.getName()
|
||||
<< ", Grade: " << bureaucrat.getGrade() << std::endl;
|
||||
std::cout << PRINT << bureaucrat << "\n" << std::endl;
|
||||
|
||||
std::cout << INFO << "Creating ShrubberyCreationForm" << std::endl;
|
||||
ShrubberyCreationForm shrubberyForm("Garden");
|
||||
std::cout << PRINT << shrubberyForm << "\n" << std::endl;
|
||||
|
||||
std::cout << INFO << "Signing ShrubberyCreationForm" << std::endl;
|
||||
bureaucrat.signForm(shrubberyForm);
|
||||
std::cout << PRINT << shrubberyForm << "\n" << std::endl;
|
||||
|
||||
std::cout << INFO << "Executing ShrubberyCreationForm" << std::endl;
|
||||
shrubberyForm.execute(bureaucrat);
|
||||
|
||||
std::cout << INFO << "Creating RobotomyRequestForm" << std::endl;
|
||||
RobotomyRequestForm robotomyForm("Alice");
|
||||
std::cout << PRINT << robotomyForm << "\n" << std::endl;
|
||||
|
||||
std::cout << INFO << "Signing RobotomyRequestForm" << std::endl;
|
||||
bureaucrat.signForm(robotomyForm);
|
||||
std::cout << PRINT << robotomyForm << "\n" << std::endl;
|
||||
|
||||
std::cout << INFO << "Executing RobotomyRequestForm" << std::endl;
|
||||
robotomyForm.execute(bureaucrat);
|
||||
std::cout << "\n" << std::endl;
|
||||
|
||||
std::cout << INFO << "Creating PresidentialPardonForm" << std::endl;
|
||||
PresidentialPardonForm pardonForm("Bob");
|
||||
std::cout << PRINT << pardonForm << "\n" << std::endl;
|
||||
|
||||
std::cout << INFO << "Signing PresidentialPardonForm" << std::endl;
|
||||
bureaucrat.signForm(pardonForm);
|
||||
std::cout << PRINT << pardonForm << "\n" << std::endl;
|
||||
|
||||
std::cout << INFO << "Executing PresidentialPardonForm" << std::endl;
|
||||
pardonForm.execute(bureaucrat);
|
||||
std::cout << "\n" << END_SCOPE << std::endl;
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
std::cerr << EXCEPTION << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
int main(void)
|
||||
{
|
||||
for (int i = 0; i < 250; i += 10)
|
||||
{
|
||||
std::cout << INFO << "Testing Bureaucrat with grade: " << i << std::endl;
|
||||
try
|
||||
{
|
||||
Bureaucrat bureaucrat("John", i);
|
||||
test(bureaucrat);
|
||||
std::cout << END_SCOPE << std::endl;
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
std::cerr << EXCEPTION << e.what() << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user