From 452e6bfa7bb4bca75dc4659bf9d707101b411977 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Tue, 17 Nov 2020 19:02:35 +0100 Subject: Added cpp05/ex02 main, Added cpp05/ex03 --- cpp05/ex02/Form.cpp | 26 +++++- cpp05/ex02/Form.hpp | 13 ++- cpp05/ex02/PresidentialPardonForm.cpp | 35 ++++++++ cpp05/ex02/PresidentialPardonForm.hpp | 34 ++++++++ cpp05/ex02/PresidentialPardonFrom.cpp | 35 -------- cpp05/ex02/PresidentialPardonFrom.hpp | 34 -------- cpp05/ex02/RobotomyRequestForm.cpp | 4 +- cpp05/ex02/ShrubberyCreationForm.cpp | 20 ++--- cpp05/ex02/main.cpp | 158 +++++++++++++++++++++++++++++++--- 9 files changed, 262 insertions(+), 97 deletions(-) create mode 100644 cpp05/ex02/PresidentialPardonForm.cpp create mode 100644 cpp05/ex02/PresidentialPardonForm.hpp delete mode 100644 cpp05/ex02/PresidentialPardonFrom.cpp delete mode 100644 cpp05/ex02/PresidentialPardonFrom.hpp (limited to 'cpp05/ex02') diff --git a/cpp05/ex02/Form.cpp b/cpp05/ex02/Form.cpp index 8f3afc0..9208f7f 100644 --- a/cpp05/ex02/Form.cpp +++ b/cpp05/ex02/Form.cpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 19:26:39 by charles #+# #+# */ -/* Updated: 2020/11/17 13:35:10 by cacharle ### ########.fr */ +/* Updated: 2020/11/17 17:09:53 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,7 +53,9 @@ void Form::beSigned(Bureaucrat const& b) void Form::execute(Bureaucrat const& executor) const { - if (executor.getGrade() <= m_gradeExecute && m_signed) + if (!m_signed) + throw Form::NoSignatureException(); + if (executor.getGrade() <= m_gradeExecute) executeUnsafe(); else throw Form::GradeTooLowException(); @@ -126,3 +128,23 @@ Form::GradeTooLowException::operator=(GradeTooLowException const& other) Form::GradeTooLowException::~GradeTooLowException() throw() {} char const* Form::GradeTooLowException::what() const throw() { return "Grade is too low for form"; } + +/////////////////////////////////////////////////////////////////////////////// +// Exception signature +/////////////////////////////////////////////////////////////////////////////// + +Form::NoSignatureException::NoSignatureException() : std::exception() {} + +Form::NoSignatureException::NoSignatureException(NoSignatureException const& other) + : std::exception(other) {} + +Form::NoSignatureException& +Form::NoSignatureException::operator=(NoSignatureException const& other) +{ + std::exception::operator=(other); + return *this; +} + +Form::NoSignatureException::~NoSignatureException() throw() {} + +char const* Form::NoSignatureException::what() const throw() { return "No signature"; } diff --git a/cpp05/ex02/Form.hpp b/cpp05/ex02/Form.hpp index a371426..78828da 100644 --- a/cpp05/ex02/Form.hpp +++ b/cpp05/ex02/Form.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 19:19:45 by charles #+# #+# */ -/* Updated: 2020/11/17 12:56:36 by cacharle ### ########.fr */ +/* Updated: 2020/11/17 17:09:15 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,6 +68,17 @@ private: ~GradeTooLowException() throw(); virtual char const* what() const throw(); }; + + class NoSignatureException : public std::exception + { + public: + NoSignatureException(); + NoSignatureException(NoSignatureException const& other); + NoSignatureException& operator=(NoSignatureException const& other); + ~NoSignatureException() throw(); + virtual char const* what() const throw(); + }; + }; std::ostream& operator<<(std::ostream& out, Form const& f); diff --git a/cpp05/ex02/PresidentialPardonForm.cpp b/cpp05/ex02/PresidentialPardonForm.cpp new file mode 100644 index 0000000..3855841 --- /dev/null +++ b/cpp05/ex02/PresidentialPardonForm.cpp @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/11/17 13:01:25 by cacharle #+# #+# */ +/* Updated: 2020/11/17 17:02:14 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "PresidentialPardonForm.hpp" + +PresidentialPardonForm::PresidentialPardonForm(std::string const& target) + : Form("presidential pardon", 25, 5), m_target(target) {} + +PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm& other) + : Form(other) { *this = other; } + +PresidentialPardonForm& PresidentialPardonForm::operator=(const PresidentialPardonForm& other) +{ + Form::operator=(other); + m_target = other.m_target; + return *this; +} + +PresidentialPardonForm::~PresidentialPardonForm() {} + +void PresidentialPardonForm::executeUnsafe() const +{ + std::cout << m_target << " has been pardoned by Zafod Beeblebrox" << std::endl; +} + +PresidentialPardonForm::PresidentialPardonForm() : Form("", 0, 0) {} diff --git a/cpp05/ex02/PresidentialPardonForm.hpp b/cpp05/ex02/PresidentialPardonForm.hpp new file mode 100644 index 0000000..fcf8a70 --- /dev/null +++ b/cpp05/ex02/PresidentialPardonForm.hpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/19 13:42:46 by cacharle #+# #+# */ +/* Updated: 2020/11/17 17:02:03 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PRESIDENTIALPARDONFORM_HPP +# define PRESIDENTIALPARDONFORM_HPP + +# include "Form.hpp" + +class PresidentialPardonForm : public Form +{ +public: + PresidentialPardonForm(const PresidentialPardonForm& other); + PresidentialPardonForm& operator=(const PresidentialPardonForm& other); + ~PresidentialPardonForm(); + + PresidentialPardonForm(std::string const& target); + +private: + PresidentialPardonForm(); + virtual void executeUnsafe() const; + + std::string m_target; +}; + +#endif diff --git a/cpp05/ex02/PresidentialPardonFrom.cpp b/cpp05/ex02/PresidentialPardonFrom.cpp deleted file mode 100644 index 46e2d3c..0000000 --- a/cpp05/ex02/PresidentialPardonFrom.cpp +++ /dev/null @@ -1,35 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* PresidentialPardonFrom.cpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/17 13:01:25 by cacharle #+# #+# */ -/* Updated: 2020/11/17 13:04:10 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "PresidentialPardonFrom.hpp" - -PresidentialPardonFrom::PresidentialPardonFrom(std::string const& target) - : Form("presidential pardon", 25, 5), m_target(target) {} - -PresidentialPardonFrom::PresidentialPardonFrom(const PresidentialPardonFrom& other) - : Form(other) { *this = other; } - -PresidentialPardonFrom& PresidentialPardonFrom::operator=(const PresidentialPardonFrom& other) -{ - Form::operator=(other); - m_target = other.m_target; - return *this; -} - -PresidentialPardonFrom::~PresidentialPardonFrom() {} - -void PresidentialPardonFrom::executeUnsafe() const -{ - std::cout << m_target << " has been pardoned by Zafod Beeblebrox" << std::endl; -} - -PresidentialPardonFrom::PresidentialPardonFrom() : Form("", 0, 0) {} diff --git a/cpp05/ex02/PresidentialPardonFrom.hpp b/cpp05/ex02/PresidentialPardonFrom.hpp deleted file mode 100644 index 3993685..0000000 --- a/cpp05/ex02/PresidentialPardonFrom.hpp +++ /dev/null @@ -1,34 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* PresidentialPardonFrom.hpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/10/19 13:42:46 by cacharle #+# #+# */ -/* Updated: 2020/11/17 12:57:57 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef PRESIDENTIALPARDONFROM_HPP -# define PRESIDENTIALPARDONFROM_HPP - -# include "Form.hpp" - -class PresidentialPardonFrom : public Form -{ -public: - PresidentialPardonFrom(const PresidentialPardonFrom& other); - PresidentialPardonFrom& operator=(const PresidentialPardonFrom& other); - ~PresidentialPardonFrom(); - - PresidentialPardonFrom(std::string const& target); - -private: - PresidentialPardonFrom(); - virtual void executeUnsafe() const; - - std::string m_target; -}; - -#endif diff --git a/cpp05/ex02/RobotomyRequestForm.cpp b/cpp05/ex02/RobotomyRequestForm.cpp index 113bcd1..ed05d5f 100644 --- a/cpp05/ex02/RobotomyRequestForm.cpp +++ b/cpp05/ex02/RobotomyRequestForm.cpp @@ -6,14 +6,14 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/10/19 13:45:45 by cacharle #+# #+# */ -/* Updated: 2020/11/17 13:27:05 by cacharle ### ########.fr */ +/* Updated: 2020/11/17 17:20:03 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "RobotomyRequestForm.hpp" RobotomyRequestForm::RobotomyRequestForm(std::string const& target) - : Form("robotomy request", 145, 137), m_target(target) {} + : Form("robotomy request", 72, 45), m_target(target) {} RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm& other) : Form(other) { *this = other; } diff --git a/cpp05/ex02/ShrubberyCreationForm.cpp b/cpp05/ex02/ShrubberyCreationForm.cpp index 70bc570..262f166 100644 --- a/cpp05/ex02/ShrubberyCreationForm.cpp +++ b/cpp05/ex02/ShrubberyCreationForm.cpp @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/10/19 13:45:45 by cacharle #+# #+# */ -/* Updated: 2020/11/17 16:36:09 by charles ### ########.fr */ +/* Updated: 2020/11/17 17:15:43 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,15 +36,15 @@ void ShrubberyCreationForm::executeUnsafe() const return; } file << - " ## " - " #### " - " ###### " - " ########## " - " ############## " - " ######ntm####### " - " ################### " - " ##################### " - " |___| "; + " ## \n" + " #### \n" + " ###### \n" + " ########## \n" + " ############## \n" + " ######ntm####### \n" + " ################### \n" + " ##################### \n" + " |___| \n"; file.close(); } diff --git a/cpp05/ex02/main.cpp b/cpp05/ex02/main.cpp index ae272c9..0f3a893 100644 --- a/cpp05/ex02/main.cpp +++ b/cpp05/ex02/main.cpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 18:13:05 by charles #+# #+# */ -/* Updated: 2020/11/17 16:35:40 by charles ### ########.fr */ +/* Updated: 2020/11/17 17:50:20 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,9 @@ #include #include #include "Bureaucrat.hpp" +#include "ShrubberyCreationForm.hpp" +#include "RobotomyRequestForm.hpp" +#include "PresidentialPardonForm.hpp" int main() { @@ -78,32 +81,161 @@ int main() } } - // Bureaucrat c("baraucrat", 10); - // std::cout << "############### SIGN FORM" << std::endl; - // Form f4("foo", 150, 1); c.signForm(f4); - // Form f3("foo", 10, 1); c.signForm(f3); - // Form f2("foo", 9, 1); c.signForm(f2); - // Form f1("foo", 1, 1); c.signForm(f1); - } + { + std::cout << "############### SHRUBERRY SIGN FORM" << std::endl; + ShrubberyCreationForm sh("shi"); + Bureaucrat c1("foo", 150); c1.signForm(sh); + Bureaucrat c2("foo", 146); c2.signForm(sh); + Bureaucrat c3("foo", 145); c3.signForm(sh); + Bureaucrat c4("foo", 1); c4.signForm(sh); + } + { + std::cout << "############### PRESIDENTIAL SIGN FORM" << std::endl; + PresidentialPardonForm pr("fu"); + Bureaucrat c1("foo", 150); c1.signForm(pr); + Bureaucrat c2("foo", 26); c2.signForm(pr); + Bureaucrat c3("foo", 25); c3.signForm(pr); + Bureaucrat c4("foo", 1); c4.signForm(pr); + } + { + std::cout << "############### ROBOTOMY SIGN FORM" << std::endl; + RobotomyRequestForm ro("mi"); + Bureaucrat c1("foo", 150); c1.signForm(ro); + Bureaucrat c2("foo", 73); c2.signForm(ro); + Bureaucrat c3("foo", 72); c3.signForm(ro); + Bureaucrat c4("foo", 1); c4.signForm(ro); + } + { + std::cout << "############### SHRUBERRY EXECUTE FORM" << std::endl; + ShrubberyCreationForm sh("shi"); + sh.beSigned(Bureaucrat("foo", 1)); + Bureaucrat c1("foo", 150); c1.executeForm(sh); + Bureaucrat c2("foo", 138); c2.executeForm(sh); + Bureaucrat c3("foo", 137); c3.executeForm(sh); + Bureaucrat c4("foo", 1); c4.executeForm(sh); + } + { + std::cout << "############### PRESIDENTIAL SIGN FORM" << std::endl; + PresidentialPardonForm pr("fu"); + pr.beSigned(Bureaucrat("foo", 1)); + Bureaucrat c1("foo", 150); c1.executeForm(pr); + Bureaucrat c2("foo", 6); c2.executeForm(pr); + Bureaucrat c3("foo", 5); c3.executeForm(pr); + Bureaucrat c4("foo", 1); c4.executeForm(pr); + } + { + std::cout << "############### ROBOTOMY SIGN FORM" << std::endl; + RobotomyRequestForm ro("mi"); + ro.beSigned(Bureaucrat("foo", 1)); + Bureaucrat c1("foo", 150); c1.executeForm(ro); + Bureaucrat c2("foo", 46); c2.executeForm(ro); + Bureaucrat c3("foo", 45); c3.executeForm(ro); + Bureaucrat c4("foo", 1); c4.executeForm(ro); + } + } + std::cout << std::endl; { - std::cout << "================= SHRUBERRY CREATION =================" << std::endl; - + std::cout << "================= SHRUBBERY CREATION =================" << std::endl; + ShrubberyCreationForm sh("home"); + ShrubberyCreationForm sh2(sh); + ShrubberyCreationForm sh3("SHOULD NOT BE PRINTED"); + sh3 = sh; + std::cout << sh; + std::cout << sh2; + std::cout << sh3; + + try { sh.execute(Bureaucrat("", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + + try { sh.beSigned(Bureaucrat("foo", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { sh.beSigned(Bureaucrat("foo", 145)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { sh.beSigned(Bureaucrat("foo", 146)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { sh.beSigned(Bureaucrat("foo", 150)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + std::cout << sh; + + try { sh.execute(Bureaucrat("bar", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { sh.execute(Bureaucrat("bar", 137)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { sh.execute(Bureaucrat("bar", 138)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { sh.execute(Bureaucrat("bar", 150)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } } std::cout << std::endl; - { std::cout << "================= PRESIDENTIAL PARDON =================" << std::endl; + PresidentialPardonForm pr("Didier"); + PresidentialPardonForm pr2(pr); + PresidentialPardonForm pr3("SHOULD NOT BE PRINTED"); + pr3 = pr; + std::cout << pr; + std::cout << pr2; + std::cout << pr3; + + try { pr.execute(Bureaucrat("", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + + try { pr.beSigned(Bureaucrat("foo", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { pr.beSigned(Bureaucrat("foo", 25)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { pr.beSigned(Bureaucrat("foo", 26)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { pr.beSigned(Bureaucrat("foo", 150)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + std::cout << pr; + + try { pr.execute(Bureaucrat("bar", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { pr.execute(Bureaucrat("bar", 5)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { pr.execute(Bureaucrat("bar", 6)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { pr.execute(Bureaucrat("bar", 150)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } } std::cout << std::endl; - { std::cout << "================= ROBOTOMY REQUEST =================" << std::endl; + RobotomyRequestForm ro("Jonathan"); + RobotomyRequestForm ro2(ro); + RobotomyRequestForm ro3("SHOULD NOT BE PRINTED"); + ro3 = ro; + std::cout << ro; + std::cout << ro2; + std::cout << ro3; + + try { ro.execute(Bureaucrat("", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + + try { ro.beSigned(Bureaucrat("foo", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { ro.beSigned(Bureaucrat("foo", 72)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { ro.beSigned(Bureaucrat("foo", 73)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { ro.beSigned(Bureaucrat("foo", 150)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + std::cout << ro; + + try { ro.execute(Bureaucrat("bar", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { ro.execute(Bureaucrat("bar", 45)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { ro.execute(Bureaucrat("bar", 46)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { ro.execute(Bureaucrat("bar", 150)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } } - return 0; } -- cgit