-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInventoryClass.php
86 lines (71 loc) · 2.08 KB
/
InventoryClass.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
include 'ProductClass.php';
class Inventory {
private $inventory;
public function __construct() {
$this->inventory = array();
}
public function add($price, $stock, $code, $volumeSale, $volumePrice, $volumeSize, $buy1Get1, $withWhich) {
try {
$this->checkIsValid($price);
if($volumeSale){
$this->checkIsValid($volumeSize);
$this->checkIsValid($volumePrice);
}
if (!$this->isInInventory($code))
$this->inventory[$code] = new Product($price, $stock, $code, $volumeSale, $volumePrice, $volumeSize, $buy1Get1, $withWhich);
} catch (Exception $e) {
echo nl2br($e->getMessage() . " for Product <b>" . $code. "</b>\n");
echo nl2br("Product <b>" . $code . "</b> cannot added into the cart\n");
}
}
private function checkIsValid($price) {
if (!is_numeric($price)) {
throw new Exception("some feilds should be numbers");
}
}
public function get($code) {
if ($this->isInInventory($code))
return $this->inventory[$code];
}
public function update($product){
$this->inventory[$product->getCode()] = $product;
}
public function remove($code) {
unset($this->inventory[$code]);
}
public function isInInventory($code) {
return array_key_exists($code, $this->inventory);
}
public function setPrice($code, $price){
if ($this->isInInventory($code)){
$product = $this->inventory[$code];
$product->setPrice($price);
$this->update($product);
}
}
public function setVolumePrices($code, $volume_prices){
if ($this->isInInventory($code)){
$product = $this->inventory[$code];
$product->setVolumePrice($volume_prices);
$product->setVolumeSale(True);
$this->update($product);
}
}
public function setVolumeSize($code, $volume_size){
if ($this->isInInventory($code)){
$product = $this->inventory[$code];
$product->setVolumeSize($volume_size);
$product->setVolumeSale(True);
$this->inventory[$code] = $product;
}
}
public function setStock($code, $stock){
if ($this->isInInventory($code)){
$product = $this->inventory[$code];
$product->setStock($stock);
$this->inventory[$code] = $product;
}
}
}
?>