Advertisement
elinberg

Dragonball Class

Apr 25th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.69 KB | None | 0 0
  1. <?php
  2.   /**
  3.    * Dragonball Class
  4.    *
  5.    * Example of a class that prints a message once 7 calls to iFoundBall method
  6.    * @todo Persist object between calls using serialization, database, or sessions
  7.    * @author     Eric LInberg <elinberg@gmail.com
  8.    */
  9.  
  10.  
  11. class Dragonball {
  12.  
  13.     const MESSAGE = 'You can ask your wish'."\n";
  14.     var $ballCount = 0;
  15.     var $maxBallCount = 0;
  16.  
  17.     public function __construct( $ballCount,  $maxBallCount){
  18.  
  19.         if( !is_integer($ballCount) ){
  20.             throw new \Exception('$ballCount must be of type integer');
  21.         }
  22.  
  23.         if( !is_integer($maxBallCount) ){
  24.             throw new \Exception('$maxBallCount must be of type integer');
  25.         }
  26.  
  27.  
  28.         $this->setBallCount($ballCount);
  29.         $this->setMaxBallCount($maxBallCount);
  30.     }
  31.  
  32.     private function setMaxBallCount( $count){
  33.         $this->maxBallCount = $count;
  34.     }
  35.  
  36.     public function iFoundBall(){
  37.         $this->getBallCount();
  38.     }
  39.  
  40.     private function getCount(){
  41.         return $this->ballCount;
  42.     }
  43.  
  44.     public function getMaxBallCount(){
  45.         return $this->maxBallCount;
  46.     }
  47.  
  48.     private function getBallCount(){
  49.         $currentCount = $this->getCount();
  50.         if($currentCount > -1 && $currentCount < 7){
  51.            
  52.             $this->incrementBallCount();
  53.             return $currentCount;
  54.         } else if( $currentCount == 7 ){
  55.             $this->setBallCount(0);
  56.             echo $this->printMessage();
  57.         }
  58.     }
  59.  
  60.     private function printMessage(){
  61.         echo self::MESSAGE;
  62.     }
  63.  
  64.     private function setBallCount( $count){
  65.         $this->ballCount = $count;
  66.     }
  67.  
  68.     private function incrementBallCount(){
  69.         $this->ballCount++;
  70.     }
  71.  
  72.  
  73. }
  74.  
  75. $ballMin = 0;
  76. $ballMax = 7;
  77. $dragon = new Dragonball($ballMin, $ballMax);
  78. $maxIterations = 17;
  79. for( $i = 0; $i < $maxIterations; $i++) {
  80.     $dragon->iFoundBall();
  81. }
  82.  
  83.  
  84. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement