Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Untitled

zukars3 Mar 28th, 2020 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 setProducts(ProductInterface $product): void
  18.     {
  19.         $this->products[] = $product;
  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();
  55. $shop->setProducts($apple);
  56. $shop->setProducts($banana);
  57. $shop->setProducts($lemon);
  58. echo $shop->getTotalPrice() . PHP_EOL;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top