Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Product{
- private $name;
- private $price;
- private $quantity;
- public function __construct($name,$price,$quantity)
- {
- $this -> name = $name;
- $this -> price = $price;
- $this -> quantity = $quantity;
- }
- public function getName(){
- return $this -> name;
- }
- public function getPrice(){
- return $this -> price;
- }
- public function getQuantity(){
- return $this -> quantity;
- }
- public function getCost(){
- return $this->price * $this -> quantity;
- }
- }
- class Cart{
- public $products = [];
- public function add($products){
- $this -> products[] = $products;
- }
- public function remove($name_remove){
- foreach($this -> products as &$value){
- if($value -> getName() === $name_remove){
- unset($value);
- }
- }
- }
- public function show(){
- foreach($this->products as $value){
- echo $value->getName()." ".$value->getPrice()."Р. ".$value->getQuantity()." штук</br>";
- }
- }
- public function getTotalQuantity(){
- $sum = 0;
- foreach($this -> products as $value){
- $sum += $value->getQuantity();
- }
- return $sum;
- }
- }
- $cart = new Cart;
- $cart -> add(new Product("Макароны",60,30));
- $cart -> add(new Product("Сосиски",200,40));
- $cart -> add(new Product("Котлеты",180,20));
- $cart -> add(new Product("Рис",55,30));
- $cart -> add(new Product("Гречка",48,20));
- $cart -> remove("Рис");
- $cart -> show();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement