Advertisement
zukars3

Untitled

Mar 28th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.97 KB | None | 0 0
  1. <?php
  2.  
  3. interface ShopInterface
  4. {
  5.     public function getTotalPrice(): float;
  6. }
  7.  
  8. interface ProductInterface
  9. {
  10.     public function getPrice(): float;
  11. }
  12.  
  13. class Shop implements ShopInterface
  14. {
  15.     private $products;
  16.  
  17.     public function __construct(array $products)
  18.     {
  19.         $this->products = $products;
  20.     }
  21.  
  22.     public function getTotalPrice(): float
  23.     {
  24.         $totalPrice = 0;
  25.  
  26.         foreach ($this->products as $product) {
  27.             $totalPrice+=$product->getPrice();
  28.         }
  29.  
  30.         return $totalPrice;
  31.     }
  32. }
  33.  
  34. class Product implements ProductInterface
  35. {
  36.     private $price;
  37.  
  38.     public function __construct(float $price)
  39.     {
  40.         $this->price = $price;
  41.     }
  42.  
  43.     public function getPrice(): float
  44.     {
  45.         return $this->price;
  46.     }
  47.  
  48. }
  49.  
  50. $apple = new Product(1.99);
  51. $banana = new Product(2.99);
  52. $lemon = new Product(3.99);
  53.  
  54. $shop = new Shop([$apple, $banana, $lemon]);
  55. echo $shop->getTotalPrice() . PHP_EOL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement