Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Exception Handling
- class TodayNoHaveException extends Exception
- {
- public function errorMessage()
- {
- //error message
- $errorMsg = " Sorry, ".$this->getMessage()." today no have, but tomorrow have sure.<br/>";
- return $errorMsg;
- }
- }
- abstract class Dish
- {
- public $name;
- protected $ingredients;
- function __construct($name, $ingredients){
- $this->name = $name;
- $this->ingredients = $ingredients;
- }
- abstract function prepare();
- //to be implemented in the subclasses
- }
- class SomTam extends Dish
- {
- function __construct(){
- parent::__construct('Som Tam', array('papaya', 'tomatoes', 'chillies', 'peanuts'));
- }
- public function prepare(){
- foreach( $this->ingredients as $ingredient){
- echo "Preparing ".$ingredient."...<br/>";
- echo "Adding ".$ingredient.'...<br/>';
- }
- echo "Tam, tam, tam, tok, tok, tok...<br/>";
- return $this->name.' for you!<br/>';
- }
- }
- class TomYam extends Dish
- {
- function __construct(){
- parent::__construct('Tom Yam', array('lemongrass', 'shrimps', 'galangal', 'water', 'Tom Yam paste'));
- }
- public function prepare(){
- echo "Cutting ".$this->ingredients[0]." and ".$this->ingredients[2]."...<br/>";
- echo "Boiling ".$this->ingredients[3]." with ".$this->ingredients[4]."...<br/>";
- echo "Cleaning ".$this->ingredients[1]."...<br/>";
- echo "Adding ".$this->ingredients[0].", ".$this->ingredients[1].", and ".
- $this->ingredients[2]." to the boiling ".$this->ingredients[3].
- "...<br/>";
- echo "Boil, boil, boil...<br/>";
- return $this->name.' for you!<br/>';
- }
- }
- class Menu
- {
- public $dishes = array();
- function __construct(){
- //empty constructor
- $somTam = new SomTam();
- $this->dishes[$somTam->name] = $somTam;
- $tomYam = new TomYam();
- $this->dishes[$tomYam->name] = $tomYam;
- }
- private function sendOrderToKitchen($dishName){
- if(array_key_exists($dishName, $this->dishes)){
- $dish = $this->dishes[$dishName];
- return $dish->prepare();
- }
- else{
- throw new TodayNoHaveException($dishName);
- }
- }
- public function order($dishName){
- try{
- return $this->sendOrderToKitchen($dishName);
- }
- catch (TodayNoHaveException $e){
- echo $e->errorMessage();
- }
- }
- }
- $menu = new Menu();
- echo $menu->order('Swiss chocolate');
- echo $menu->order('Som Tam');
Add Comment
Please, Sign In to add comment