Guest User

Untitled

a guest
Nov 20th, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.20 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Class FizzBuzz
  4.  *
  5.  * FizzBuzz class implemented in the OOP style.  This class will handle all aspects of the FizzBuzz requirements.  The user can also
  6.  * create additional divisors and result strings as required.  The only divisor implemented by default is 3 for "Fizz".
  7.  *
  8.  * @author  Keith Jones
  9.  * @copyright   Keith Jones 11/20/2014
  10.  * @version 1.0
  11.  * @since   11/20/2014
  12.  * @package OOPFB
  13.  */
  14. class FizzBuzz {
  15.  
  16.     private $count = 0;
  17.     private $outputSent = false;
  18.     private $divisors = array();
  19.  
  20.     public function FizzBuzz() {
  21.  
  22.         $this->addDivisor(3, 'Fizz');
  23.  
  24.     }
  25.  
  26.     private function setOutputSent ($bool = true) {
  27.  
  28.         if(is_bool($bool)) {
  29.             $this->outputSent = $bool;
  30.         } else {
  31.             throw new Exception('OutputSent value not boolean');
  32.         }
  33.  
  34.     }
  35.  
  36.     private function getOutputSent () {
  37.  
  38.         return $this->outputSent;
  39.  
  40.     }
  41.  
  42.     private function CountIsDivisibleBy($number) {
  43.  
  44.         return $this->count%$number==0 ? true : false;
  45.  
  46.     }
  47.  
  48.  
  49.     public function addDivisor($divisor, $outputString) {
  50.  
  51.         if(!is_int($divisor)) {
  52.             throw new Exception('Bad divisor');
  53.         } elseif(!is_string($outputString)) {
  54.             throw new Exception('Bad outputString');
  55.         } else {
  56.             $this->divisors[$divisor] = $outputString;
  57.             return true;
  58.         }
  59.  
  60.         return false;
  61.     }
  62.  
  63.     public function Increment($number) {
  64.  
  65.         if(is_int($number) && $number > 0) {
  66.             $this->count += $number;
  67.         } else {
  68.             throw new Exception('Bad incrementer');
  69.         }
  70.  
  71.     }
  72.  
  73.     public function getNumber () {
  74.  
  75.         return $this->count;
  76.  
  77.     }
  78.  
  79.     public function printNumber () {
  80.  
  81.         $output = '';
  82.  
  83.         foreach($this->divisors as $divisor => $outputString) {
  84.             if($this->CountIsDivisibleBy($divisor)) {
  85.                 echo $outputString;
  86.                 $this->setOutputSent();
  87.             }
  88.         }
  89.  
  90.         if($this->outputSent == false) {
  91.             echo $this->count;
  92.         }
  93.  
  94.         echo "\n";
  95.         $this->setOutputSent(false);
  96.  
  97.     }
  98.  
  99. }
  100.  
  101. $maxNum = 100;
  102.  
  103. try {
  104.     $fb = new FizzBuzz();
  105.     $fb->addDivisor(5, 'Buzz');
  106. } catch (Exception $e) {
  107.     echo 'Instantiation Exception: ' . $e->getMessage() . "\n";
  108. }
  109.  
  110.  
  111. do {
  112.  
  113.     try {
  114.         $fb->Increment(1);
  115.         $fb->printNumber();
  116.     } catch (Exception $e) {
  117.         echo 'Exception: ' . $e->getMessage() . "\n";
  118.     }
  119.  
  120. } while ($fb->getNumber() < $maxNum);
  121.  
  122.  
  123. ?>
Advertisement
Add Comment
Please, Sign In to add comment