return $this->getTitle() . " is a " . $this->getGenre() . " movie directed by " . $this->getDirector();
    }
}
class ItemDescriber
{
    public function outputDescription($Product)
    {
        if ($Product instanceof Describable) {
            return $Product->provideDescription();
        } else {
            throw new Exception("Item is not describable");
        }
    }
}
$kramericaTV = new Television("Giant TV", "Kramerica", 3900.9, "LED", "100in");
$shirt = new Clothing("Button Down Shirt", "J Peterman", '29.88', "shirt", "medium", "red", "male");
$pulpFiction = new CloudMovieFile("Pulp Fiction", "Quentin Tarantino", "Drama");
$describer = new ItemDescriber();
$objects = array($shirt, $kramericaTV, $pulpFiction);
try {
    foreach ($objects as $object) {
        echo $describer->outputDescription($object);
        echo "</br>";
    }
} catch (Exception $e) {
    echo $e->getMessage();
}
?>
    </p>
  </body>
</html>
Exemple #2
0
    public function findProductByName($name)
    {
        $itemKey = null;
        foreach ($this->items as $key => $item) {
            if ($name == $item->getName()) {
                $itemKey = $key + 1;
                echo "{$name} is number {$itemKey} in the array";
                break;
            }
        }
        if ($itemKey == null) {
            throw new Exception("Item was not found in cart");
        }
    }
}
$describer = new ItemDescriber();
$kramericaTV = new Television("Giant TV", "Kramerica", 3900.9, "LED", "100in");
$shirt = new Clothing("Button Down Shirt", "J Peterman", '29.88', "shirt", "medium", "red", "male");
$cart = new ShoppingCart();
$cart->addProduct($kramericaTV);
$cart->addProduct($shirt);
$cart->provideDescription();
echo "</br>";
echo "The cart total price is \$" . $cart->getTotalPrice();
$cart->removeOne($shirt);
$cart->findProductByName("Giant TV");
$describer->outputDescription($cart);
?>
    </p>
  </body>
</html>
        }
    }
    public function getSize()
    {
        if (empty($this->size)) {
            throw new Exception("No size entered");
        } else {
            return $this->size;
        }
    }
    public function provideDescriptionForProductType()
    {
        return 'This is a ' . $this->getSize() . ' ' . $this->getBrand() . ' ' . $this->getname() . ' ' . $this->getDisplayType() . ' Television <br />';
    }
}
$itemDescriber = new ItemDescriber();
$cart = new ShoppingCart();
$hoodie = new Clothing("Hoodie", "Nike", 19.99, "large", "red", "shirt", "male");
$plasma = new Television("Plasma", "Sony", 1000.0, plasma, "50in");
$cart->addProduct($hoodie);
$cart->addProduct($plasma);
echo implode('<br />', $cart->provideDescription());
echo "<br />";
echo $cart->getTotalPrice();
echo "<br />";
$cart->removeProduct($hoodie);
foreach ($cart->provideDescription() as $productDescription) {
    echo $productDescription;
    echo '<br />';
}
var_dump($cart->findProductByName("Plasma"));