Implement robotomy execution
authorLukas Jiriste <ljiriste@student.42prague.com>
Sun, 19 Jan 2025 15:13:10 +0000 (16:13 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sun, 19 Jan 2025 15:13:10 +0000 (16:13 +0100)
ex02/AForm.cpp
ex02/AForm.h
ex02/RobotomyRequestForm.cpp
ex02/RobotomyRequestForm.h
ex02/main.cpp

index 5d77859d14bc0d8b1d853e421c61430bd0f0a7e8..8297ad0bf1d5860fc8db614496ca4c0f02633f89 100644 (file)
@@ -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)
index 81c657fb69527a1bba8fcca315e24f00d466afb6..c6f867ccf7c3afc75380477adf35d86422fd7304 100644 (file)
@@ -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;
index a814a90dd438f899ed23cb80c01075e3b7e79414..964eabd05693a593211aa141a5647d089b91fad6 100644 (file)
@@ -1,4 +1,7 @@
 #include "RobotomyRequestForm.h"
+#include <iostream>
+#include <cstdlib>
+#include <ctime>
 
 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));
 }
index b81ccfa02031e6aa9fbac2add80a16d0531594d8..98f287b9e7b4df4cd21c9fc7c700c85d03116dd7 100644 (file)
@@ -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:
index 95f808dd12e3c6641b1702a9369d5e4d15314a96..0d5150c50e6ffdab2f6804cd3468db213f2b309a 100644 (file)
@@ -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);
 }