-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cart.php
41 lines (37 loc) · 1.34 KB
/
Cart.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
namespace Cart;
class Cart{
public $countProduct = [];
public function addProduct($product){
$product->numberProduct = 1;
if(array_key_exists($product->title, $this->countProduct)){
$this->countProduct[$product->title]->numberProduct++;
}
else{
$this->countProduct[$product->title] = $product;
}
}
public function deleteOneProduct($product){ //убрать 1 одинаковый продукт
if(array_key_exists($product->title, $this->countProduct)){
if($this->countProduct[$product->title]->numberProduct > 0){
$this->countProduct[$product->title]->numberProduct--;
echo 'Name ' . $this->countProduct[$product->title]->title. ' Delete<br>';
}
}
}
public function showAllProduct(){
$resCountProduct = 0;
foreach($this->countProduct as $key => $value){
echo 'Name:' . $key . ',<br>';
$resCountProduct = $resCountProduct + $value->numberProduct;
}echo 'Count: ' . $resCountProduct;
}
public function sum(){ //подсчитать общую сумму продукта
$res = 0;
foreach($this->countProduct as $key => $value){
$res = $res + ($value->price * $value->numberProduct);
}
return $res;
}
}
?>