1<?php
2class Cart
3{
4 private $items_;
5
6 /**
7 * コンストラクタ
8 */
9 public function Cart()
10 {
11 $this->items_ = array();
12 }
13
14 /**
15 * 商品の追加
16 */
17 public function add($item_cd, $amount)
18 {
19 $this->_check($item_cd);
20 if ($amount > 0) {
21 $this->items_[$item_cd] += $amount;
22 }
23 }
24
25 /**
26 * 特定商品の削除
27 */
28 public function remove($item_cd, $amount)
29 {
30 $this->_check($item_cd);
31 if ($amount > 0) {
32 $this->items_[$item_cd] -= $amount;
33 if ($this->items_[$item_cd] < 0) {
34 $this->items_[$item_cd] = 0;
35 }
36 }
37 }
38
39 /**
40 * 全商品のクリア
41 */
42 public function clear()
43 {
44 unset($this->items_);
45 }
46
47 /**
48 * 特定商品の個数を返す
49 */
50 public function getAmount($item_cd)
51 {
52 $this->_check($item_cd);
53 return $this->items_[$item_cd];
54 }
55
56 /**
57 * 商品個数のチェック&初期化
58 */
59 public function _check($item_cd)
60 {
61 if (!isset($this->items_[$item_cd])) {
62 $this->items_[$item_cd] = 0;
63 }
64 }
65}
66?>