-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.php
122 lines (95 loc) · 3.24 KB
/
order.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
setlocale(LC_MONETARY, 'en_US.UTF-8');
require_once('products/hotdog.php');
require_once('products/taco.php');
require_once('products/hamburger.php');
require_once('products/pizza.php');
class Order {
protected $bIsCalculated = false;
protected $fTaxRate = 0.075;
protected $aProducts = ['hotdog', 'taco', 'hamburger', 'pizza'];
// protected $aCondiments = ['ketchup'];
protected $aErrors = [];
public $fTotalCost = 0;
public $fTaxCost = 0;
public $fGrandTotalCost = 0;
public $iTotalNumOrdered = 0;
public function calculateOrder() {
$aProducts = $this->getAllProducts();
foreach ($aProducts as $oProduct) {
if (!isset($_POST[$oProduct->getAttrName()])) continue;
if ($this->validateEntryValidValue($_POST[$oProduct->getAttrName()])) {
$this->fTotalCost += $oProduct->getPrice($_POST[$oProduct->getAttrName()]);
if (isset($_POST[$oProduct->getAttrName()."_ketchup"])) {
if (! $this->validateEntryValidValue($_POST[$oProduct->getAttrName()."_ketchup"]))
$this->generateError($oProduct, "condiment_valid_value");
if (! $this->validateEntryLimit($_POST[$oProduct->getAttrName()], $_POST[$oProduct->getAttrName()."_ketchup"]))
$this->generateError($oProduct, "condiment_more_than_product");
}
} else {
$this->generateError($oProduct);
}
}
$this->calculateTax();
$this->calculateGrandTotal();
}
protected function validateEntryValidValue($entry) {
return is_numeric($entry) && $entry >= 0;
}
protected function validateEntryLimit($productEntry, $condimentEntry) {
return $condimentEntry <= $productEntry;
}
protected function generateError($oProduct, $sErrorType="product_valid_value") {
switch ($sErrorType) {
case "product_valid_value":
$this->aErrors[$oProduct->getAttrName()] = [
"message" => "You cannot order {$_POST[$oProduct->getAttrName()]} {$oProduct->getName()}. Please enter a valid value."
];
break;
case "condiment_valid_value":
$this->aErrors[$oProduct->getAttrName()."_ketchup"] = [
"message" => "You cannot order {$_POST[$oProduct->getAttrName()."_ketchup"]} ketchup. Please enter a valid value."
];
break;
case "condiment_more_than_product";
$this->aErrors[$oProduct->getAttrName()."_ketchup"] = [
"message" => "You cannot put ketchup on more {$oProduct->getName()} than you ordered. Please enter a valid value."
];
break;
default:
break;
}
}
protected function calculateTax() {
$this->fTaxCost = $this->fTotalCost * $this->fTaxRate;
}
protected function calculateGrandTotal() {
$this->fGrandTotalCost = $this->fTotalCost + $this->fTaxCost;
}
public function getCountOrdered($sType) {
if (!empty($_POST[$sType])) {
return $_POST[$sType];
}
return 0;
}
public function getSubTotal() {
return money_format('%.2n', $this->fTotalCost);
}
public function getTaxTotal() {
return money_format('%.2n', $this->fTaxCost);
}
public function getGrandTotal() {
return money_format('%.2n', $this->fGrandTotalCost);
}
public function getAllProducts() {
$aProductInstances = [];
foreach ($this->aProducts as $product) {
$sClassName = ucfirst($product);
$aProductInstances[] = new $sClassName;
}
return $aProductInstances;
}
public function getErrors() {
return $this->aErrors;
}
}