Advertisement
Nikita051

Untitled

Oct 13th, 2022
1,266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.44 KB | None | 0 0
  1. <?php
  2.     class Product{
  3.         private $name;
  4.         private $price;
  5.         private $quantity;
  6.  
  7.         public function __construct($name,$price,$quantity)
  8.         {
  9.             $this -> name = $name;
  10.             $this -> price = $price;
  11.             $this -> quantity = $quantity;
  12.         }
  13.         public function getName(){
  14.             return $this -> name;
  15.         }
  16.         public function getPrice(){
  17.             return $this -> price;
  18.         }
  19.         public function getQuantity(){
  20.             return $this -> quantity;
  21.         }
  22.         public function getCost(){
  23.             return $this->price * $this -> quantity;
  24.         }
  25.     }
  26.     class Cart{
  27.         public $products = [];
  28.  
  29.         public function add($products){
  30.             $this -> products[] = $products;
  31.         }
  32.         public function remove($name_remove){
  33.             foreach($this -> products as &$value){
  34.                 if($value -> getName() === $name_remove){
  35.                     unset($value);
  36.                 }
  37.             }
  38.         }
  39.         public function show(){
  40.             foreach($this->products as $value){
  41.                 echo $value->getName()." ".$value->getPrice()."Р. ".$value->getQuantity()." штук</br>";
  42.             }
  43.         }
  44.         public function getTotalQuantity(){
  45.             $sum = 0;
  46.             foreach($this -> products as $value){
  47.                 $sum += $value->getQuantity();
  48.             }
  49.             return $sum;
  50.         }
  51.     }
  52.     $cart = new Cart;
  53.     $cart -> add(new Product("Макароны",60,30));
  54.     $cart -> add(new Product("Сосиски",200,40));
  55.     $cart -> add(new Product("Котлеты",180,20));
  56.     $cart -> add(new Product("Рис",55,30));
  57.     $cart -> add(new Product("Гречка",48,20));
  58.     $cart -> remove("Рис");
  59.     $cart -> show();
  60. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement