Refactor Bureaucrat and AForm classes for improved formatting and consistency
This commit is contained in:
parent
525d4e2beb
commit
09e719000c
@ -11,7 +11,7 @@ class Bureaucrat
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
const std::string _name;
|
const std::string _name;
|
||||||
int _grade;
|
int _grade;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Bureaucrat() = delete;
|
Bureaucrat() = delete;
|
||||||
@ -21,12 +21,12 @@ class Bureaucrat
|
|||||||
|
|
||||||
Bureaucrat &operator=(Bureaucrat &other) = delete;
|
Bureaucrat &operator=(Bureaucrat &other) = delete;
|
||||||
|
|
||||||
Bureaucrat &operator++(); // Pre-increment
|
Bureaucrat &operator++(); // Pre-increment
|
||||||
Bureaucrat operator++(int); // Post-increment
|
Bureaucrat operator++(int); // Post-increment
|
||||||
Bureaucrat &operator--(); // Pre-decrement
|
Bureaucrat &operator--(); // Pre-decrement
|
||||||
Bureaucrat operator--(int); // Post-decrement
|
Bureaucrat operator--(int); // Post-decrement
|
||||||
|
|
||||||
int getGrade() const;
|
int getGrade() const;
|
||||||
const std::string getName() const;
|
const std::string getName() const;
|
||||||
|
|
||||||
class GradeTooHighException : public std::exception
|
class GradeTooHighException : public std::exception
|
||||||
|
|||||||
@ -23,7 +23,8 @@ Bureaucrat::Bureaucrat(std::string name, int grade) : _name(name)
|
|||||||
std::cout << BUREAUCRAT << *this << std::endl;
|
std::cout << BUREAUCRAT << *this << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
Bureaucrat::Bureaucrat(Bureaucrat &other) : _name(other._name), _grade(other._grade)
|
Bureaucrat::Bureaucrat(Bureaucrat &other)
|
||||||
|
: _name(other._name), _grade(other._grade)
|
||||||
{
|
{
|
||||||
std::cout << COPY_CONSTRUCTOR << std::endl;
|
std::cout << COPY_CONSTRUCTOR << std::endl;
|
||||||
std::cout << BUREAUCRAT << *this << std::endl;
|
std::cout << BUREAUCRAT << *this << std::endl;
|
||||||
@ -86,6 +87,7 @@ const char *Bureaucrat::GradeTooLowException::what() const throw()
|
|||||||
|
|
||||||
std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
|
std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
|
||||||
{
|
{
|
||||||
std::cout << bureaucrat.getName() << ", bureaucrat grade " << bureaucrat.getGrade();
|
std::cout << bureaucrat.getName() << ", bureaucrat grade "
|
||||||
|
<< bureaucrat.getGrade();
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "Bureaucrat.hpp"
|
#include "Bureaucrat.hpp"
|
||||||
|
|
||||||
#include "colors.h"
|
#include "colors.h"
|
||||||
|
|
||||||
#define EXCEPTION_COLOR BOLD BACKGROUND1
|
#define EXCEPTION_COLOR BOLD BACKGROUND1
|
||||||
@ -13,11 +14,13 @@ int main(void)
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::cout << INFO << "Creating Bureaucrat with name 'jan' and grade 3" << RESET << std::endl;
|
std::cout << INFO << "Creating Bureaucrat with name 'jan' and grade 3"
|
||||||
|
<< RESET << std::endl;
|
||||||
Bureaucrat jan("jan", 3);
|
Bureaucrat jan("jan", 3);
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::cout << INFO << "Creating a copy of Bureaucrat 'jan'" << RESET << std::endl;
|
std::cout << INFO << "Creating a copy of Bureaucrat 'jan'" << RESET
|
||||||
|
<< std::endl;
|
||||||
Bureaucrat copy(jan);
|
Bureaucrat copy(jan);
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
@ -34,7 +37,8 @@ int main(void)
|
|||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::cout << INFO << "Creating Bureaucrat which grade is too high" << RESET << std::endl;
|
std::cout << INFO << "Creating Bureaucrat which grade is too high" << RESET
|
||||||
|
<< std::endl;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Bureaucrat bob("bob", 0); // This should throw an exception
|
Bureaucrat bob("bob", 0); // This should throw an exception
|
||||||
@ -45,7 +49,8 @@ int main(void)
|
|||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::cout << INFO << "Creating Bureaucrat which grade is too low" << RESET << std::endl;
|
std::cout << INFO << "Creating Bureaucrat which grade is too low" << RESET
|
||||||
|
<< std::endl;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Bureaucrat alice("alice", 151); // This should also throw an exception
|
Bureaucrat alice("alice", 151); // This should also throw an exception
|
||||||
@ -56,7 +61,8 @@ int main(void)
|
|||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::cout << INFO << "Prefix incrementing Bureaucrat's grade..." << RESET << std::endl;
|
std::cout << INFO << "Prefix incrementing Bureaucrat's grade..." << RESET
|
||||||
|
<< std::endl;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Bureaucrat charlie("charlie", 1);
|
Bureaucrat charlie("charlie", 1);
|
||||||
@ -68,11 +74,12 @@ int main(void)
|
|||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::cout << INFO << "Prefix decrementing Bureaucrat's grade..." << RESET << std::endl;
|
std::cout << INFO << "Prefix decrementing Bureaucrat's grade..." << RESET
|
||||||
|
<< std::endl;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Bureaucrat dave("dave", 150);
|
Bureaucrat dave("dave", 150);
|
||||||
--dave; // This should throw an exception
|
--dave; // This should throw an exception
|
||||||
}
|
}
|
||||||
catch (std::exception &e)
|
catch (std::exception &e)
|
||||||
{
|
{
|
||||||
@ -80,7 +87,8 @@ int main(void)
|
|||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::cout << INFO << "Postfix incrementing Bureaucrat's grade..." << RESET << std::endl;
|
std::cout << INFO << "Postfix incrementing Bureaucrat's grade..." << RESET
|
||||||
|
<< std::endl;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Bureaucrat charlie("charlie", 1);
|
Bureaucrat charlie("charlie", 1);
|
||||||
@ -92,17 +100,16 @@ int main(void)
|
|||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::cout << INFO << "Postfix decrementing Bureaucrat's grade..." << RESET << std::endl;
|
std::cout << INFO << "Postfix decrementing Bureaucrat's grade..." << RESET
|
||||||
|
<< std::endl;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Bureaucrat dave("dave", 150);
|
Bureaucrat dave("dave", 150);
|
||||||
dave--; // This should throw an exception
|
dave--; // This should throw an exception
|
||||||
}
|
}
|
||||||
catch (std::exception &e)
|
catch (std::exception &e)
|
||||||
{
|
{
|
||||||
std::cerr << EXCEPTION << e.what() << std::endl;
|
std::cerr << EXCEPTION << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,7 +11,7 @@ class Bureaucrat
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
const std::string _name;
|
const std::string _name;
|
||||||
int _grade;
|
int _grade;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Bureaucrat() = delete;
|
Bureaucrat() = delete;
|
||||||
@ -21,14 +21,14 @@ class Bureaucrat
|
|||||||
|
|
||||||
Bureaucrat &operator=(Bureaucrat &other) = delete;
|
Bureaucrat &operator=(Bureaucrat &other) = delete;
|
||||||
|
|
||||||
Bureaucrat &operator++(); // Pre-increment
|
Bureaucrat &operator++(); // Pre-increment
|
||||||
Bureaucrat operator++(int); // Post-increment
|
Bureaucrat operator++(int); // Post-increment
|
||||||
Bureaucrat &operator--(); // Pre-decrement
|
Bureaucrat &operator--(); // Pre-decrement
|
||||||
Bureaucrat operator--(int); // Post-decrement
|
Bureaucrat operator--(int); // Post-decrement
|
||||||
|
|
||||||
int getGrade() const;
|
int getGrade() const;
|
||||||
const std::string getName() const;
|
const std::string getName() const;
|
||||||
void signForm(class Form &form);
|
void signForm(class Form &form);
|
||||||
|
|
||||||
class GradeTooHighException : public std::exception
|
class GradeTooHighException : public std::exception
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <string>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "Bureaucrat.hpp"
|
#include "Bureaucrat.hpp"
|
||||||
|
|
||||||
@ -10,21 +10,21 @@ class Form
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
const std::string _name;
|
const std::string _name;
|
||||||
bool _isSigned;
|
bool _isSigned;
|
||||||
const int _signGrade;
|
const int _signGrade;
|
||||||
const int _executeGrade;
|
const int _executeGrade;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Form() = delete;
|
Form() = delete;
|
||||||
Form(std::string name, int signGrade, int executeGrade);
|
Form(std::string name, int signGrade, int executeGrade);
|
||||||
Form(const Form &other);
|
Form(const Form &other);
|
||||||
~Form();
|
~Form();
|
||||||
Form &operator=(const Form &other) = delete;
|
Form &operator=(const Form &other) = delete;
|
||||||
const std::string &getName() const;
|
const std::string &getName() const;
|
||||||
bool getIsSigned() const;
|
bool getIsSigned() const;
|
||||||
int getSignGrade() const;
|
int getSignGrade() const;
|
||||||
int getExecuteGrade() const;
|
int getExecuteGrade() const;
|
||||||
void beSigned(const Bureaucrat &bureaucrat);
|
void beSigned(const Bureaucrat &bureaucrat);
|
||||||
|
|
||||||
class GradeTooHighException : public std::exception
|
class GradeTooHighException : public std::exception
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,16 +1,16 @@
|
|||||||
#include "Bureaucrat.hpp"
|
#include "Bureaucrat.hpp"
|
||||||
#include "Form.hpp"
|
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "Form.hpp"
|
||||||
|
|
||||||
#include "colors.h"
|
#include "colors.h"
|
||||||
|
|
||||||
#define BUREAUCRAT BOLD BACKGROUND4 " Bureaucrat: " RESET " "
|
#define BUREAUCRAT BOLD BACKGROUND4 " Bureaucrat: " RESET " "
|
||||||
#define CONSTRUCTOR "Constructor called"
|
#define CONSTRUCTOR "Constructor called"
|
||||||
#define PARAMETERIZED_CONSTRUCTOR "Parameterized constructor called"
|
#define PARAMETERIZED_CONSTRUCTOR "Parameterized constructor called"
|
||||||
#define DESTRUCTOR "Destructor called"
|
#define DESTRUCTOR "Destructor called"
|
||||||
#define COPY_CONSTRUCTOR "Copy constructor called"
|
#define COPY_CONSTRUCTOR "Copy constructor called"
|
||||||
|
|
||||||
|
|
||||||
Bureaucrat::Bureaucrat(std::string name, int grade) : _name(name)
|
Bureaucrat::Bureaucrat(std::string name, int grade) : _name(name)
|
||||||
{
|
{
|
||||||
@ -23,7 +23,8 @@ Bureaucrat::Bureaucrat(std::string name, int grade) : _name(name)
|
|||||||
std::cout << BUREAUCRAT << *this << std::endl;
|
std::cout << BUREAUCRAT << *this << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
Bureaucrat::Bureaucrat(Bureaucrat &other) : _name(other._name), _grade(other._grade)
|
Bureaucrat::Bureaucrat(Bureaucrat &other)
|
||||||
|
: _name(other._name), _grade(other._grade)
|
||||||
{
|
{
|
||||||
std::cout << BUREAUCRAT COPY_CONSTRUCTOR << std::endl;
|
std::cout << BUREAUCRAT COPY_CONSTRUCTOR << std::endl;
|
||||||
std::cout << BUREAUCRAT << *this << std::endl;
|
std::cout << BUREAUCRAT << *this << std::endl;
|
||||||
@ -76,11 +77,16 @@ const std::string Bureaucrat::getName() const
|
|||||||
|
|
||||||
void Bureaucrat::signForm(Form &form)
|
void Bureaucrat::signForm(Form &form)
|
||||||
{
|
{
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
form.beSigned(*this);
|
form.beSigned(*this);
|
||||||
std::cout << BUREAUCRAT << _name << " signs " << form.getName() << std::endl;
|
std::cout << BUREAUCRAT << _name << " signs " << form.getName()
|
||||||
} catch (std::exception &e) {
|
<< std::endl;
|
||||||
std::cout << BUREAUCRAT << _name << " cannot sign " << form.getName() << " because: " << e.what() << std::endl;
|
}
|
||||||
|
catch (std::exception &e)
|
||||||
|
{
|
||||||
|
std::cout << BUREAUCRAT << _name << " cannot sign " << form.getName()
|
||||||
|
<< " because: " << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,6 +102,7 @@ const char *Bureaucrat::GradeTooLowException::what() const throw()
|
|||||||
|
|
||||||
std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
|
std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
|
||||||
{
|
{
|
||||||
std::cout << bureaucrat.getName() << ", bureaucrat grade " << bureaucrat.getGrade();
|
std::cout << bureaucrat.getName() << ", bureaucrat grade "
|
||||||
|
<< bureaucrat.getGrade();
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
|
#include "Form.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "Form.hpp"
|
|
||||||
#include "colors.h"
|
#include "colors.h"
|
||||||
|
|
||||||
|
|
||||||
#define FORM BOLD BACKGROUND5 " Form: " RESET " "
|
#define FORM BOLD BACKGROUND5 " Form: " RESET " "
|
||||||
#define CONSTRUCTOR "Constructor called"
|
#define CONSTRUCTOR "Constructor called"
|
||||||
#define PARAMETERIZED_CONSTRUCTOR "Parameterized constructor called"
|
#define PARAMETERIZED_CONSTRUCTOR "Parameterized constructor called"
|
||||||
@ -12,7 +12,10 @@
|
|||||||
#define COPY_CONSTRUCTOR "Copy constructor called"
|
#define COPY_CONSTRUCTOR "Copy constructor called"
|
||||||
|
|
||||||
Form::Form(std::string name, int signGrade, int executeGrade)
|
Form::Form(std::string name, int signGrade, int executeGrade)
|
||||||
: _name(name), _isSigned(false), _signGrade(signGrade), _executeGrade(executeGrade)
|
: _name(name),
|
||||||
|
_isSigned(false),
|
||||||
|
_signGrade(signGrade),
|
||||||
|
_executeGrade(executeGrade)
|
||||||
{
|
{
|
||||||
std::cout << FORM PARAMETERIZED_CONSTRUCTOR << std::endl;
|
std::cout << FORM PARAMETERIZED_CONSTRUCTOR << std::endl;
|
||||||
if (signGrade < 1 || executeGrade < 1)
|
if (signGrade < 1 || executeGrade < 1)
|
||||||
@ -23,7 +26,10 @@ Form::Form(std::string name, int signGrade, int executeGrade)
|
|||||||
}
|
}
|
||||||
|
|
||||||
Form::Form(const Form &other)
|
Form::Form(const Form &other)
|
||||||
: _name(other._name), _isSigned(other._isSigned), _signGrade(other._signGrade), _executeGrade(other._executeGrade)
|
: _name(other._name),
|
||||||
|
_isSigned(other._isSigned),
|
||||||
|
_signGrade(other._signGrade),
|
||||||
|
_executeGrade(other._executeGrade)
|
||||||
{
|
{
|
||||||
std::cout << FORM COPY_CONSTRUCTOR << std::endl;
|
std::cout << FORM COPY_CONSTRUCTOR << std::endl;
|
||||||
std::cout << FORM << *this << std::endl;
|
std::cout << FORM << *this << std::endl;
|
||||||
@ -86,4 +92,3 @@ std::ostream &operator<<(std::ostream &os, const Form &form)
|
|||||||
<< ", Execute Grade: " << form.getExecuteGrade();
|
<< ", Execute Grade: " << form.getExecuteGrade();
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,19 +3,22 @@
|
|||||||
|
|
||||||
#include "Bureaucrat.hpp"
|
#include "Bureaucrat.hpp"
|
||||||
#include "Form.hpp"
|
#include "Form.hpp"
|
||||||
|
|
||||||
#include "colors.h"
|
#include "colors.h"
|
||||||
|
|
||||||
#define EXCEPTION BOLD BACKGROUND1 " Exception: " RESET " "
|
#define EXCEPTION BOLD BACKGROUND1 " Exception: " RESET " "
|
||||||
#define INFO ITALIC BOLD UNDERLINE GREEN "Info: "
|
#define INFO ITALIC BOLD UNDERLINE GREEN "Info: "
|
||||||
#define PRINT BOLD BACKGROUND3 " Print: " RESET " "
|
#define PRINT BOLD BACKGROUND3 " Print: " RESET " "
|
||||||
#define END_SCOPE ITALIC BOLD UNDERLINE RED "End of scope" 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 END_SCOPE_EXCEPTION \
|
||||||
|
ITALIC BOLD UNDERLINE RED "End of scope with exception" RESET
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::cout << INFO << "Creating Bureaucrat with name 'jan' and grade 3" << RESET << std::endl;
|
std::cout << INFO << "Creating Bureaucrat with name 'jan' and grade 3"
|
||||||
|
<< RESET << std::endl;
|
||||||
Bureaucrat jan("Jan", 41);
|
Bureaucrat jan("Jan", 41);
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
@ -23,8 +26,10 @@ int main(void)
|
|||||||
std::cout << PRINT << jan << std::endl;
|
std::cout << PRINT << jan << std::endl;
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::cout << INFO << "Creating a Form with name 'Form1', sign grade 40, and execute grade 30" << RESET
|
std::cout << INFO
|
||||||
<< std::endl;
|
<< "Creating a Form with name 'Form1', sign grade 40, and "
|
||||||
|
"execute grade 30"
|
||||||
|
<< RESET << std::endl;
|
||||||
Form form1("Form1", 40, 30);
|
Form form1("Form1", 40, 30);
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
@ -36,27 +41,33 @@ int main(void)
|
|||||||
jan.signForm(form1);
|
jan.signForm(form1);
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::cout << INFO << "Printing Form after signing..." << RESET << std::endl;
|
std::cout << INFO << "Printing Form after signing..." << RESET
|
||||||
|
<< std::endl;
|
||||||
std::cout << PRINT << form1 << std::endl;
|
std::cout << PRINT << form1 << std::endl;
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::cout << INFO << "Incrementing Jan's grade..." << RESET << std::endl;
|
std::cout << INFO << "Incrementing Jan's grade..." << RESET
|
||||||
|
<< std::endl;
|
||||||
++jan;
|
++jan;
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::cout << INFO << "Printing Bureaucrat after incrementing..." << RESET << std::endl;
|
std::cout << INFO << "Printing Bureaucrat after incrementing..."
|
||||||
|
<< RESET << std::endl;
|
||||||
std::cout << PRINT << jan << std::endl;
|
std::cout << PRINT << jan << std::endl;
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::cout << INFO << "Jan tries to sign Form1 again..." << RESET << std::endl;
|
std::cout << INFO << "Jan tries to sign Form1 again..." << RESET
|
||||||
|
<< std::endl;
|
||||||
jan.signForm(form1);
|
jan.signForm(form1);
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::cout << INFO << "Printing Form after second signing attempt..." << RESET << std::endl;
|
std::cout << INFO << "Printing Form after second signing attempt..."
|
||||||
|
<< RESET << std::endl;
|
||||||
std::cout << PRINT << form1 << std::endl;
|
std::cout << PRINT << form1 << std::endl;
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::cout << INFO << "Jan tries to sign Form1 again" << RESET << std::endl;
|
std::cout << INFO << "Jan tries to sign Form1 again" << RESET
|
||||||
|
<< std::endl;
|
||||||
jan.signForm(form1);
|
jan.signForm(form1);
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
@ -74,7 +85,9 @@ int main(void)
|
|||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::cout << INFO << "Creating Bureaucrat with name 'Invalid' and grade 0" << RESET << std::endl;
|
std::cout << INFO
|
||||||
|
<< "Creating Bureaucrat with name 'Invalid' and grade 0"
|
||||||
|
<< RESET << std::endl;
|
||||||
Bureaucrat invalid("Invalid", 0);
|
Bureaucrat invalid("Invalid", 0);
|
||||||
std::cout << END_SCOPE << std::endl;
|
std::cout << END_SCOPE << std::endl;
|
||||||
}
|
}
|
||||||
@ -87,7 +100,9 @@ int main(void)
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::cout << INFO << "Creating Bureaucrat with name 'Invalid' and grade 151" << RESET << std::endl;
|
std::cout << INFO
|
||||||
|
<< "Creating Bureaucrat with name 'Invalid' and grade 151"
|
||||||
|
<< RESET << std::endl;
|
||||||
Bureaucrat invalid("Invalid", 151);
|
Bureaucrat invalid("Invalid", 151);
|
||||||
std::cout << END_SCOPE << std::endl;
|
std::cout << END_SCOPE << std::endl;
|
||||||
}
|
}
|
||||||
@ -100,8 +115,10 @@ int main(void)
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::cout << INFO << "Creating Form with name 'InvalidForm', sign grade 0, and execute grade 30" << RESET
|
std::cout << INFO
|
||||||
<< std::endl;
|
<< "Creating Form with name 'InvalidForm', sign grade 0, and "
|
||||||
|
"execute grade 30"
|
||||||
|
<< RESET << std::endl;
|
||||||
Form invalidForm("InvalidForm", 0, 30);
|
Form invalidForm("InvalidForm", 0, 30);
|
||||||
std::cout << END_SCOPE << std::endl;
|
std::cout << END_SCOPE << std::endl;
|
||||||
}
|
}
|
||||||
@ -114,8 +131,10 @@ int main(void)
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::cout << INFO << "Creating Form with name 'InvalidForm', sign grade 40, and execute grade 151" << RESET
|
std::cout << INFO
|
||||||
<< std::endl;
|
<< "Creating Form with name 'InvalidForm', sign grade 40, "
|
||||||
|
"and execute grade 151"
|
||||||
|
<< RESET << std::endl;
|
||||||
Form invalidForm("InvalidForm", 40, 151);
|
Form invalidForm("InvalidForm", 40, 151);
|
||||||
std::cout << END_SCOPE << std::endl;
|
std::cout << END_SCOPE << std::endl;
|
||||||
}
|
}
|
||||||
@ -126,20 +145,27 @@ int main(void)
|
|||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::cout << INFO << "Testing signing with Bureaucrat with grade too low using Form::beSigned()" << RESET
|
std::cout << INFO
|
||||||
<< std::endl;
|
<< "Testing signing with Bureaucrat with grade too low using "
|
||||||
|
"Form::beSigned()"
|
||||||
|
<< RESET << std::endl;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::cout << INFO << "Creating Bureaucrat with name 'Peter' and grade 50" << RESET << std::endl;
|
std::cout << INFO
|
||||||
|
<< "Creating Bureaucrat with name 'Peter' and grade 50"
|
||||||
|
<< RESET << std::endl;
|
||||||
Bureaucrat peter("Peter", 50);
|
Bureaucrat peter("Peter", 50);
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::cout << INFO << "Creating Form with name 'Form2', sign grade 40, and execute grade 30" << RESET
|
std::cout << INFO
|
||||||
<< std::endl;
|
<< "Creating Form with name 'Form2', sign grade 40, and "
|
||||||
|
"execute grade 30"
|
||||||
|
<< RESET << std::endl;
|
||||||
Form form2("Form2", 40, 30);
|
Form form2("Form2", 40, 30);
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
std::cout << INFO << "Peter tries to sign Form2..." << RESET << std::endl;
|
std::cout << INFO << "Peter tries to sign Form2..." << RESET
|
||||||
|
<< std::endl;
|
||||||
form2.beSigned(peter);
|
form2.beSigned(peter);
|
||||||
std::cout << END_SCOPE << std::endl;
|
std::cout << END_SCOPE << std::endl;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <string>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "Bureaucrat.hpp"
|
#include "Bureaucrat.hpp"
|
||||||
|
|
||||||
|
|||||||
@ -11,7 +11,7 @@ class Bureaucrat
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
const std::string _name;
|
const std::string _name;
|
||||||
int _grade;
|
int _grade;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Bureaucrat() = delete;
|
Bureaucrat() = delete;
|
||||||
@ -21,14 +21,14 @@ class Bureaucrat
|
|||||||
|
|
||||||
Bureaucrat &operator=(Bureaucrat &other) = delete;
|
Bureaucrat &operator=(Bureaucrat &other) = delete;
|
||||||
|
|
||||||
Bureaucrat &operator++(); // Pre-increment
|
Bureaucrat &operator++(); // Pre-increment
|
||||||
Bureaucrat operator++(int); // Post-increment
|
Bureaucrat operator++(int); // Post-increment
|
||||||
Bureaucrat &operator--(); // Pre-decrement
|
Bureaucrat &operator--(); // Pre-decrement
|
||||||
Bureaucrat operator--(int); // Post-decrement
|
Bureaucrat operator--(int); // Post-decrement
|
||||||
|
|
||||||
int getGrade() const;
|
int getGrade() const;
|
||||||
const std::string getName() const;
|
const std::string getName() const;
|
||||||
void signForm(class AForm &form);
|
void signForm(class AForm &form);
|
||||||
|
|
||||||
class GradeTooHighException : public std::exception
|
class GradeTooHighException : public std::exception
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,11 +1,15 @@
|
|||||||
|
#include "AForm.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "AForm.hpp"
|
|
||||||
#include "colors.h"
|
#include "colors.h"
|
||||||
|
|
||||||
AForm::AForm(std::string name, int signGrade, int executeGrade)
|
AForm::AForm(std::string name, int signGrade, int executeGrade)
|
||||||
: _name(name), _isSigned(false), _signGrade(signGrade), _executeGrade(executeGrade)
|
: _name(name),
|
||||||
|
_isSigned(false),
|
||||||
|
_signGrade(signGrade),
|
||||||
|
_executeGrade(executeGrade)
|
||||||
{
|
{
|
||||||
std::cout << AFORM PARAMETERIZED_CONSTRUCTOR << std::endl;
|
std::cout << AFORM PARAMETERIZED_CONSTRUCTOR << std::endl;
|
||||||
if (signGrade < 1 || executeGrade < 1)
|
if (signGrade < 1 || executeGrade < 1)
|
||||||
@ -16,7 +20,10 @@ AForm::AForm(std::string name, int signGrade, int executeGrade)
|
|||||||
}
|
}
|
||||||
|
|
||||||
AForm::AForm(const AForm &other)
|
AForm::AForm(const AForm &other)
|
||||||
: _name(other._name), _isSigned(other._isSigned), _signGrade(other._signGrade), _executeGrade(other._executeGrade)
|
: _name(other._name),
|
||||||
|
_isSigned(other._isSigned),
|
||||||
|
_signGrade(other._signGrade),
|
||||||
|
_executeGrade(other._executeGrade)
|
||||||
{
|
{
|
||||||
std::cout << AFORM COPY_CONSTRUCTOR << std::endl;
|
std::cout << AFORM COPY_CONSTRUCTOR << std::endl;
|
||||||
std::cout << AFORM << *this << std::endl;
|
std::cout << AFORM << *this << std::endl;
|
||||||
@ -87,7 +94,9 @@ const char *AForm::FormNotSignedException::what() const throw()
|
|||||||
|
|
||||||
std::ostream &operator<<(std::ostream &os, const AForm &form)
|
std::ostream &operator<<(std::ostream &os, const AForm &form)
|
||||||
{
|
{
|
||||||
os << "Form Name: " << form.getName() << ", Is Signed: " << (form.getIsSigned() ? "Yes" : "No")
|
os << "Form Name: " << form.getName()
|
||||||
<< ", Sign Grade: " << form.getSignGrade() << ", Execute Grade: " << form.getExecuteGrade();
|
<< ", Is Signed: " << (form.getIsSigned() ? "Yes" : "No")
|
||||||
|
<< ", Sign Grade: " << form.getSignGrade()
|
||||||
|
<< ", Execute Grade: " << form.getExecuteGrade();
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,9 @@
|
|||||||
#include "Bureaucrat.hpp"
|
#include "Bureaucrat.hpp"
|
||||||
#include "AForm.hpp"
|
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "AForm.hpp"
|
||||||
|
|
||||||
#include "colors.h"
|
#include "colors.h"
|
||||||
|
|
||||||
Bureaucrat::Bureaucrat(std::string name, int grade) : _name(name)
|
Bureaucrat::Bureaucrat(std::string name, int grade) : _name(name)
|
||||||
@ -16,7 +17,8 @@ Bureaucrat::Bureaucrat(std::string name, int grade) : _name(name)
|
|||||||
std::cout << BUREAUCRAT << *this << std::endl;
|
std::cout << BUREAUCRAT << *this << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
Bureaucrat::Bureaucrat(Bureaucrat &other) : _name(other._name), _grade(other._grade)
|
Bureaucrat::Bureaucrat(Bureaucrat &other)
|
||||||
|
: _name(other._name), _grade(other._grade)
|
||||||
{
|
{
|
||||||
std::cout << BUREAUCRAT COPY_CONSTRUCTOR << std::endl;
|
std::cout << BUREAUCRAT COPY_CONSTRUCTOR << std::endl;
|
||||||
std::cout << BUREAUCRAT << *this << std::endl;
|
std::cout << BUREAUCRAT << *this << std::endl;
|
||||||
@ -72,11 +74,13 @@ void Bureaucrat::signForm(AForm &form)
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
form.beSigned(*this);
|
form.beSigned(*this);
|
||||||
std::cout << BUREAUCRAT << _name << " signs " << form.getName() << std::endl;
|
std::cout << BUREAUCRAT << _name << " signs " << form.getName()
|
||||||
|
<< std::endl;
|
||||||
}
|
}
|
||||||
catch (std::exception &e)
|
catch (std::exception &e)
|
||||||
{
|
{
|
||||||
std::cout << BUREAUCRAT << _name << " cannot sign " << form.getName() << " because: " << e.what() << std::endl;
|
std::cout << BUREAUCRAT << _name << " cannot sign " << form.getName()
|
||||||
|
<< " because: " << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,6 +96,7 @@ const char *Bureaucrat::GradeTooLowException::what() const throw()
|
|||||||
|
|
||||||
std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
|
std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
|
||||||
{
|
{
|
||||||
std::cout << bureaucrat.getName() << ", bureaucrat grade " << bureaucrat.getGrade();
|
std::cout << bureaucrat.getName() << ", bureaucrat grade "
|
||||||
|
<< bureaucrat.getGrade();
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -42,8 +42,9 @@ void PresidentialPardonForm::execute(const Bureaucrat &executor) const
|
|||||||
{
|
{
|
||||||
isExecutable(executor);
|
isExecutable(executor);
|
||||||
|
|
||||||
std::cout << PRESIDENTIAL_PARDON_FORM<< executor.getName() << " made shure Zaphod Beeblebrox pardoned "
|
std::cout << PRESIDENTIAL_PARDON_FORM << executor.getName()
|
||||||
<< _target << "." << std::endl;
|
<< " made shure Zaphod Beeblebrox pardoned " << _target << "."
|
||||||
|
<< std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string PresidentialPardonForm::getTarget() const
|
const std::string PresidentialPardonForm::getTarget() const
|
||||||
|
|||||||
@ -28,16 +28,15 @@ void RobotomyRequestForm::execute(const Bureaucrat &executor) const
|
|||||||
{
|
{
|
||||||
isExecutable(executor);
|
isExecutable(executor);
|
||||||
|
|
||||||
std::cout << ROBOTOMY_REQUEST_FORM
|
std::cout << ROBOTOMY_REQUEST_FORM << "Drrr, Drrr. Drilling noises... "
|
||||||
<< "Drrr, Drrr. Drilling noises... " << std::endl;
|
<< std::endl;
|
||||||
std::srand(static_cast<unsigned int>(std::time(0)));
|
std::srand(static_cast<unsigned int>(std::time(0)));
|
||||||
if (std::rand() % 2)
|
if (std::rand() % 2)
|
||||||
std::cout << ROBOTOMY_REQUEST_FORM << _target
|
std::cout << ROBOTOMY_REQUEST_FORM << _target
|
||||||
<< " has been robotomized!" << std::endl;
|
<< " has been robotomized!" << std::endl;
|
||||||
else
|
else
|
||||||
std::cout << ROBOTOMY_REQUEST_FORM << _target
|
std::cout << ROBOTOMY_REQUEST_FORM << _target << "'s robotomy failed. "
|
||||||
<< "'s robotomy failed. " << _target
|
<< _target << " is not the same anymore..." << std::endl;
|
||||||
<< " is not the same anymore..." << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string RobotomyRequestForm::getTarget() const
|
const std::string RobotomyRequestForm::getTarget() const
|
||||||
|
|||||||
@ -54,7 +54,8 @@ void ShrubberyCreationForm::execute(const Bureaucrat &executor) const
|
|||||||
<< " {\n";
|
<< " {\n";
|
||||||
|
|
||||||
ofs.close();
|
ofs.close();
|
||||||
std::cout << SHRUBBERY_CREATION_FORM << "Shrubbery created in file: " << _target << "_shrubbery"
|
std::cout << SHRUBBERY_CREATION_FORM
|
||||||
|
<< "Shrubbery created in file: " << _target << "_shrubbery"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,13 +2,14 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "Bureaucrat.hpp"
|
#include "Bureaucrat.hpp"
|
||||||
#include "ShrubberyCreationForm.hpp"
|
|
||||||
#include "RobotomyRequestForm.hpp"
|
|
||||||
#include "PresidentialPardonForm.hpp"
|
#include "PresidentialPardonForm.hpp"
|
||||||
|
#include "RobotomyRequestForm.hpp"
|
||||||
|
#include "ShrubberyCreationForm.hpp"
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
Bureaucrat bureaucrat("John", 1);
|
Bureaucrat bureaucrat("John", 1);
|
||||||
std::cout << bureaucrat << std::endl;
|
std::cout << bureaucrat << std::endl;
|
||||||
|
|
||||||
@ -35,10 +36,11 @@ int main(void)
|
|||||||
std::cout << pardonForm << std::endl;
|
std::cout << pardonForm << std::endl;
|
||||||
|
|
||||||
pardonForm.execute(bureaucrat);
|
pardonForm.execute(bureaucrat);
|
||||||
} catch (const std::exception &e) {
|
}
|
||||||
|
catch (const std::exception &e)
|
||||||
|
{
|
||||||
std::cerr << "Exception: " << e.what() << std::endl;
|
std::cerr << "Exception: " << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user