-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorator.php
More file actions
101 lines (80 loc) · 2.21 KB
/
decorator.php
File metadata and controls
101 lines (80 loc) · 2.21 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
interface iProduct
{
public function setPrice();
public function getPrice();
}
class Product implements iProduct
{
protected $_product;
protected $_price;
protected $_size;
protected $_weight;
public function __construct($price)
{
$this->_price = $price;
$this->_size = 5;
$this->_weight = 5;
}
public function setPrice(){}
public function getPrice(){
return $this->_price;
}
}
class ProdWithVat extends Product
{
public function __construct(Product $product)
{
$this->_product = $product;
$this->setPrice();
}
public function setPrice()
{
$price = $this->_product->getPrice();
$this->_price = $price + $this->getVat();
}
public function getVat(){
//5% tax
return ($this->_product->getPrice()*5)/100;
}
}
class ProdWithShippingCost extends Product
{
public function __construct(Product $product)
{
$this->_product = $product;
$this->setPrice();
}
public function setPrice()
{
$price = $this->_product->getPrice();
$this->_price = $price + $this->getShippingCost();
}
public function getShippingCost(){
return $this->_product->getPrice() + $this->_size + $this->_weight;
}
}
class ProdWithDiscount extends Product
{
public function __construct(Product $product)
{
$this->_product = $product;
$this->setPrice();
}
public function setPrice()
{
$price = $this->_product->getPrice();
$this->_price = $price - $this->getDiscount();
}
public function getDiscount(){
return 2;
}
}
$product = new Product(10);
echo "Standard Product Price: ".$product->getPrice();
$productWithVat = new ProdWithVat($product);
echo "<br/>Price After addind 5% VAT: ".$productWithVat->getPrice();
$productWithShipping = new ProdWithShippingCost($productWithVat);
echo "<br/>Price After addind Shipping Cost: ".$productWithShipping->getPrice();
$productWithDiscount = new ProdWithShippingCost(new ProdWithVat(new ProdWithDiscount($product)));
echo '<br/><br/>Final Price after discount: '.$productWithDiscount->getPrice();