aboutsummaryrefslogtreecommitdiff
path: root/cpp05/ex03/Bureaucrat.cpp
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-11-17 19:02:35 +0100
committerCharles Cabergs <me@cacharle.xyz>2020-11-17 19:02:35 +0100
commit452e6bfa7bb4bca75dc4659bf9d707101b411977 (patch)
treef4ee27a96437d6d17a89bbbbc135ae45f9f3faae /cpp05/ex03/Bureaucrat.cpp
parenta92013c92bfcd50b0e2561280c9eaa604843ade0 (diff)
downloadpiscine_cpp-452e6bfa7bb4bca75dc4659bf9d707101b411977.tar.gz
piscine_cpp-452e6bfa7bb4bca75dc4659bf9d707101b411977.tar.bz2
piscine_cpp-452e6bfa7bb4bca75dc4659bf9d707101b411977.zip
Added cpp05/ex02 main, Added cpp05/ex03
Diffstat (limited to 'cpp05/ex03/Bureaucrat.cpp')
-rw-r--r--cpp05/ex03/Bureaucrat.cpp127
1 files changed, 127 insertions, 0 deletions
diff --git a/cpp05/ex03/Bureaucrat.cpp b/cpp05/ex03/Bureaucrat.cpp
new file mode 100644
index 0000000..64bbd25
--- /dev/null
+++ b/cpp05/ex03/Bureaucrat.cpp
@@ -0,0 +1,127 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* Bureaucrat.cpp :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/04/14 18:08:13 by charles #+# #+# */
+/* Updated: 2020/11/17 13:37:49 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "Bureaucrat.hpp"
+
+Bureaucrat::Bureaucrat(Bureaucrat const& other) { *this = other; }
+
+Bureaucrat& Bureaucrat::operator=(Bureaucrat const& other)
+{
+ m_name = other.m_name;
+ m_grade = other.m_grade;
+ return *this;
+}
+
+Bureaucrat::~Bureaucrat() {}
+
+Bureaucrat::Bureaucrat(std::string const& name, int grade)
+ : m_name(name), m_grade(grade)
+{
+ checkGrade();
+}
+
+std::string const& Bureaucrat::getName() const { return m_name; }
+int Bureaucrat::getGrade() const { return m_grade; }
+
+void Bureaucrat::incrementGrade()
+{
+ m_grade--;
+ checkGrade();
+}
+
+void Bureaucrat::decrementGrade()
+{
+ m_grade++;
+ checkGrade();
+}
+
+void Bureaucrat::signForm(Form& form)
+{
+ try
+ {
+ form.beSigned(*this);
+ std::cout << m_name << " signs " << form.getName() << std::endl;
+ }
+ catch (std::exception &e)
+ {
+ std::cout << m_name << " cannot sign " << form.getName() << " " << e.what() << std::endl;
+ }
+}
+
+void Bureaucrat::executeForm(Form& form)
+{
+ try
+ {
+ form.execute(*this);
+ std::cout << m_name << " executes " << form.getName() << std::endl;
+ }
+ catch (std::exception &e)
+ {
+ std::cout << m_name << " cannot execute " << form.getName() << " " << e.what() << std::endl;
+ }
+}
+
+void Bureaucrat::checkGrade()
+{
+ if (m_grade > 150)
+ throw Bureaucrat::GradeTooLowException();
+ if (m_grade < 1)
+ throw Bureaucrat::GradeTooHighException();
+}
+
+std::ostream& operator<<(std::ostream& out, Bureaucrat const& b)
+{
+ std::cout << b.getName() << ", bureaucrat grade " << b.getGrade() << std::endl;
+ return out;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// Exception grade too high
+///////////////////////////////////////////////////////////////////////////////
+
+Bureaucrat::GradeTooHighException::GradeTooHighException() : std::exception() {}
+
+Bureaucrat::GradeTooHighException::GradeTooHighException(GradeTooHighException const& other)
+ : std::exception(other) {}
+
+Bureaucrat::GradeTooHighException&
+Bureaucrat::GradeTooHighException::operator=(GradeTooHighException const& other)
+{
+ std::exception::operator=(other);
+ return *this;
+}
+
+Bureaucrat::GradeTooHighException::~GradeTooHighException() throw() {}
+
+char const* Bureaucrat::GradeTooHighException::what() const throw() { return "Grade is too high"; }
+
+///////////////////////////////////////////////////////////////////////////////
+// Exception grade too low
+///////////////////////////////////////////////////////////////////////////////
+
+Bureaucrat::GradeTooLowException::GradeTooLowException() : std::exception() {}
+
+Bureaucrat::GradeTooLowException::GradeTooLowException(GradeTooLowException const& other)
+ : std::exception(other) {}
+
+Bureaucrat::GradeTooLowException&
+Bureaucrat::GradeTooLowException::operator=(GradeTooLowException const& other)
+{
+ std::exception::operator=(other);
+ return *this;
+}
+
+Bureaucrat::GradeTooLowException::~GradeTooLowException() throw() {}
+
+char const* Bureaucrat::GradeTooLowException::what() const throw() { return "Grade is too low"; }
+
+Bureaucrat::Bureaucrat() {}