-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompleteclass.php
More file actions
31 lines (31 loc) · 992 Bytes
/
completeclass.php
File metadata and controls
31 lines (31 loc) · 992 Bytes
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
<?php
class Cart {
var $items;
var $name;
function __construct($myName) {
echo "$myName 's cart, charge process: <br>";
$this->name = $myName;
}
function add_item($artnr, $num) {
$this->items[$artnr] += $num;
echo "<br>add $num $artnr, $artnr 's total num:" . $this->items[$artnr];
}
function remove_item($artnr, $num) {
if ($this->items[$artnr] > $num) {
$this->items[$artnr] -= $num;
echo "<br>remove $num $artnr, $artnr 's total num: " . $this->items[$artnr];
return true;
} elseif ($this->items[$artnr] == $num) {
unset($this->items[$artnr]);
echo "<br>remove $num $artnr, $artnr 's total num is zero";
} else {
return false;
}
}
}
$myCart = new Cart("zhang san");
$myCart->add_item("apple", 9);
$myCart->add_item("banana", 8);
$myCart->remove_item("apple", 2);
$myCart->remove_item("banana", 3);
?>