开发者

Object dependencies, member variables (homework example)

开发者 https://www.devze.com 2023-03-21 02:45 出处:网络
I\'m in the process of creating a wool farm, where sheep are sheared in the machine if they are old. The wool is counted by the color, there is black and white.

I'm in the process of creating a wool farm, where sheep are sheared in the machine if they are old. The wool is counted by the color, there is black and white.

Each mentioned attribute is random:

  • Age: Young = 0, Old = 1
  • Color (Black, White).

I understand the logic, but I don't know how to send groups of sheep to the machine, and I can't make the counter to increase if I make more sheep. (During the assignation I was asked to do this).

I commented the code for further understanding: http://gabrielmeono.com/theFarm.zip

<?php 
class Sheep{
    //Atributes
    const COLOR_WHITE = 'white';
    const COLOR_BLACK = 'black';
    const AGE_YOUNG = 0;
    const AGE_OLD = 1;

    private $_color;
    private $_age;

    //Random age and color
    public static function makeRandom(){
    if(rand(0, 1)){
        $color = self::COLOR_WHITE;
    }else{
        $color = self::COLOR_BLACK;
    }
        $age = rand(0, 1);
        return new self($color, $age);
    }

    public function __construct($color, $age){
        $this->_color = $color;
        $this->_age = $age;
    }
    //To check if the sheep was created and had all the atributes.
    public function report(){
        echo '<br>This sheep is '.$this->_color.' and '.$this->_age.'<br/>';//Old age is 1. 

        }

}


class machine {
    //The machine should shear wool only if the sheep is old. Old equals 1.
    public function Shear($collector){
        switch($collector)
        {
            case $sheep->_age = 1;
                echo 'Sheep sheared <br/>';
            break;
            default:
                echo 'This sheep is not ready to be sheared <br/>';
            break;

        }
    }
    //Here I should be able to count and separate wool by color.
    public function Counter($whiteWool, $blackWool, $count){ //This is my notion how it should start...Correct me if I'm wrong.


        }
    }

$sheep = Sheep::makeRandom();
$sheep -> report();
$machine = new machine ();
$machine -> Shear(0);
//I don't know how to connect 开发者_运维百科the machine class with the sheep class.
//Batch creation and processing of sheep is beyond my understanding.
?>


As others have said, you can add the sheep to the machine through injection. The shear method actually makes the most sense to me.

public function Shear(Sheep $sheep) {
   switch ($sheep->age) {} // etc.
}

In fact, I would take this a step further by having the sheep react to the shearing.

public static function makeRandom() {
   // ...
   return new $age ? OldSheep($color) : YoungSheep($color);
}

Or even better:

private static $ages = array(0 => 'Old', 1 => 'Young')
public static function makeRandom() {
   // ...
   $sheepclass = $ages[$age] . 'Sheep';
   return new $sheepclass($color);
}

Then when it's time for shearing:

interface Sheepable {
   function prepareToShear(machine $mach);
}

class OldSheep implements Sheepable {
   public function prepareToShear(machine $mach) {
      echo "Sheep is ready to be shorn";
      $mach->shear($this);
   }
}

class YoungSheep implements Sheepable {
   public function prepareToShear(machine $mach) {
      echo "Sheep is not ready to be shorn";
   }
}

class machine {
   //... etc.
   public function Shear(Sheepable $sheep) {
      $sheep->prepareToShear($this);
   }
}

By the way, asking for help with homework tends to get a negative response around here.


You can inject the Sheep object to the Machine by using dependency injection (todo: google for it).

e.g.

$machine->addSheep($sheep);

addSheep method should save the $sheep object in the class member variable. Other methods may use this variable later.


You can just send $sheep (the object you created) as an argument to a function in the machine class.


I hate farming.
Don't just copypasta. Read through it, ask questions (but not duplicates) and we'll be able to assist.

interface Animal{
    const AGE_YOUNG = 0;
    const AGE_OLD = 1;
    const COLOR_WHITE = 'white';
    const COLOR_BLACK = 'black';
    public function getAge();
    public function getColor();
}

interface Shearable extends Animal{
    public function shear();
    public function isSheared();
}

class Gorilla implements Shearable{
    // ♫ seeeeeee my vest, see my vest, made of real gorilla chest ♫
}

class Sheep implements Shearable{

    private $_isSheared = false;
    private $_age = null;
    private $_color = null;

    public static function create(){
        $age = rand(0, 1);
        $color = rand(0, 1)
            ? self::COLOR_WHITE
            : self::COLOR_BLACK;
        return new self($age, $color);
    }

    public function __construct($age, $color){
        $this->_age = $age;
        $this->_color = $color;
    }

    public function shear(){
        $this->_isSheared = true;
        return $this;
    }

    public function isSheared(){
        return $this->_isSheared;
    }

    public function getAge(){
        return $this->_age;
    }

    public function getColor(){
        return $this->_color;
    }

}

class Machine{

    private $_wool = array();

    public function shearLine(Array $line){
        $unshearable = array();
        foreach($line as $animal){
            if($this->shear($animal) !== true){
                $unshearable[] = $animal;
            }
        }
        return $unshearable;
    }

    public function shear(Shearable $animal){
        if(!$animal->isSheared() && $animal->getAge() == Shearable::AGE_OLD){
            $this->_wool[] = $animal->shear()->getColor();
            return true;
        }
        return $animal;
    }

    public function getWool(){
        return count($this->_wool);
    }

    public function getWoolByColor($color){
        return count(array_keys($this->_wool, $color));
    }

}

// build a machine
$machine = new Machine();

// brew up some sheep
$lineOfSheep = array();
for($i = 0; $i < 200; $i++){
    $lineOfSheep[] = Sheep::create();
}

//make some underwear
$unshearable = $machine->shearLine($lineOfSheep);

// see how many sheep still need water and sunlight
var_dump(count($unshearable));

// see how much and of what color your underwear will be
var_dump($machine->getWool(), $machine->getWoolByColor(Shearable::COLOR_WHITE));
0

精彩评论

暂无评论...
验证码 换一张
取 消