Guest User

Untitled

a guest
May 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. <?php
  2.  
  3. //Exception Handling
  4. class TodayNoHaveException extends Exception
  5. {
  6. public function errorMessage()
  7. {
  8. //error message
  9. $errorMsg = " Sorry, ".$this->getMessage()." today no have, but tomorrow have sure.<br/>";
  10. return $errorMsg;
  11. }
  12. }
  13.  
  14.  
  15. abstract class Dish
  16. {
  17. public $name;
  18. protected $ingredients;
  19.  
  20. function __construct($name, $ingredients){
  21. $this->name = $name;
  22. $this->ingredients = $ingredients;
  23. }
  24.  
  25. abstract function prepare();
  26. //to be implemented in the subclasses
  27. }
  28.  
  29. class SomTam extends Dish
  30. {
  31. function __construct(){
  32. parent::__construct('Som Tam', array('papaya', 'tomatoes', 'chillies', 'peanuts'));
  33. }
  34.  
  35. public function prepare(){
  36. foreach( $this->ingredients as $ingredient){
  37. echo "Preparing ".$ingredient."...<br/>";
  38. echo "Adding ".$ingredient.'...<br/>';
  39. }
  40. echo "Tam, tam, tam, tok, tok, tok...<br/>";
  41. return $this->name.' for you!<br/>';
  42. }
  43. }
  44.  
  45. class TomYam extends Dish
  46. {
  47. function __construct(){
  48. parent::__construct('Tom Yam', array('lemongrass', 'shrimps', 'galangal', 'water', 'Tom Yam paste'));
  49. }
  50.  
  51. public function prepare(){
  52. echo "Cutting ".$this->ingredients[0]." and ".$this->ingredients[2]."...<br/>";
  53. echo "Boiling ".$this->ingredients[3]." with ".$this->ingredients[4]."...<br/>";
  54. echo "Cleaning ".$this->ingredients[1]."...<br/>";
  55. echo "Adding ".$this->ingredients[0].", ".$this->ingredients[1].", and ".
  56. $this->ingredients[2]." to the boiling ".$this->ingredients[3].
  57. "...<br/>";
  58. echo "Boil, boil, boil...<br/>";
  59. return $this->name.' for you!<br/>';
  60. }
  61. }
  62.  
  63. class Menu
  64. {
  65.  
  66. public $dishes = array();
  67.  
  68. function __construct(){
  69. //empty constructor
  70. $somTam = new SomTam();
  71. $this->dishes[$somTam->name] = $somTam;
  72. $tomYam = new TomYam();
  73. $this->dishes[$tomYam->name] = $tomYam;
  74. }
  75.  
  76.  
  77. private function sendOrderToKitchen($dishName){
  78. if(array_key_exists($dishName, $this->dishes)){
  79. $dish = $this->dishes[$dishName];
  80. return $dish->prepare();
  81. }
  82. else{
  83. throw new TodayNoHaveException($dishName);
  84. }
  85.  
  86. }
  87.  
  88. public function order($dishName){
  89. try{
  90. return $this->sendOrderToKitchen($dishName);
  91. }
  92. catch (TodayNoHaveException $e){
  93. echo $e->errorMessage();
  94. }
  95. }
  96. }
  97.  
  98.  
  99. $menu = new Menu();
  100. echo $menu->order('Swiss chocolate');
  101. echo $menu->order('Som Tam');
Add Comment
Please, Sign In to add comment