From e90df5667deba3c763a9c105b210b38b71e5c33c Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Sun, 19 Jan 2025 16:13:10 +0100 Subject: [PATCH] Implement robotomy execution --- ex02/AForm.cpp | 5 +++++ ex02/AForm.h | 1 + ex02/RobotomyRequestForm.cpp | 19 +++++++++++++++++++ ex02/RobotomyRequestForm.h | 2 ++ ex02/main.cpp | 11 +++++++++++ 5 files changed, 38 insertions(+) diff --git a/ex02/AForm.cpp b/ex02/AForm.cpp index 5d77859..8297ad0 100644 --- a/ex02/AForm.cpp +++ b/ex02/AForm.cpp @@ -59,6 +59,11 @@ int AForm::getExecutorGrade() const return (m_executor_grade); } +const std::string &AForm::getTarget() const +{ + return (m_target); +} + void AForm::beSigned(const Bureaucrat &bureaucrat) { if (bureaucrat.getGrade() > m_signatory_grade) diff --git a/ex02/AForm.h b/ex02/AForm.h index 81c657f..c6f867c 100644 --- a/ex02/AForm.h +++ b/ex02/AForm.h @@ -30,6 +30,7 @@ class AForm bool isSigned() const; int getSignatoryGrade() const; int getExecutorGrade() const; + const std::string &getTarget() const; void beSigned(const Bureaucrat &bureaucrat); void checkExecutorGrade(const Bureaucrat &bureaucrat) const; void virtual execute(const Bureaucrat &bureaucrat) const = 0; diff --git a/ex02/RobotomyRequestForm.cpp b/ex02/RobotomyRequestForm.cpp index a814a90..964eabd 100644 --- a/ex02/RobotomyRequestForm.cpp +++ b/ex02/RobotomyRequestForm.cpp @@ -1,4 +1,7 @@ #include "RobotomyRequestForm.h" +#include +#include +#include RobotomyRequestForm::RobotomyRequestForm() : AForm() @@ -22,4 +25,20 @@ RobotomyRequestForm::~RobotomyRequestForm() void RobotomyRequestForm::execute(Bureaucrat const &executor) const { checkExecutorGrade(executor); + if (getRandomBit()) + std::cout << "The robotomy on " << getTarget() << " was successful.\n"; + else + std::cout << "The robotomy on " << getTarget() << " failed.\n"; +} + +bool RobotomyRequestForm::getRandomBit() +{ + static bool is_seeded = 0; + + if (!is_seeded) + { + std::srand(std::time(0)); + is_seeded = 1; + } + return (std::rand() > (RAND_MAX / 2)); } diff --git a/ex02/RobotomyRequestForm.h b/ex02/RobotomyRequestForm.h index b81ccfa..98f287b 100644 --- a/ex02/RobotomyRequestForm.h +++ b/ex02/RobotomyRequestForm.h @@ -6,6 +6,8 @@ class RobotomyRequestForm : public AForm { + private: + static bool getRandomBit(); // These are protected only so that inheriting classes can use them // in their "non implementations" of there functions protected: diff --git a/ex02/main.cpp b/ex02/main.cpp index 95f808d..0d5150c 100644 --- a/ex02/main.cpp +++ b/ex02/main.cpp @@ -37,7 +37,18 @@ int main(void) employee1.executeForm(pres); std::cout << '\n'; employee2.executeForm(shrub); + std::cout << '\n'; + employee1.executeForm(robot); + employee1.executeForm(robot); + employee1.executeForm(robot); + employee1.executeForm(robot); + employee1.executeForm(robot); employee1.executeForm(robot); + employee1.executeForm(robot); + employee1.executeForm(robot); + employee1.executeForm(robot); + employee1.executeForm(robot); + std::cout << '\n'; employee0.executeForm(pres); return (0); } -- 2.30.2