-
Notifications
You must be signed in to change notification settings - Fork 0
/
Topping.php
102 lines (95 loc) · 2.58 KB
/
Topping.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
<?php
/**
* Topping.php describes the class Topping and all of it's methods
*
* @author Israel Santiago
* @version 1.0 2016/02/02
* @link neoazareth.com/ITC250/P2EC/index.com
*/
/**
* class creates topping objects, with properties: name and price.
*
* methods display properties ready for table instertion, formatted price as currency
* calculate topping subtotal and get the topping subtotal ready for table format
*
* <code>
* $lettuce = new Topping('lettuce', 0.50)
* </code>
*
* @see Item class; class that creates Item objects
* @see Order class; class that creates Order objects
*/
class Topping
{
//class variables/properties
public $name = ' ';
public $price = 0.0;
/**
* constructor
*
* @param $name string; the name of the topping
* @param $price double; the price of the topping
* @return topping object
*/
public function __construct($name, $price)
{
$this->name = $name;
$this->price = $price;
}
/**
* gets the object's formatted price
*
* @return string e.g. $00.00
*/
public function getFormattedPrice()
{
$price = '$'.number_format($this->price,2);
return $price;
}
/**
* creates a string with a checkbox, name and price properties
*
* @param $key string to be used as the checkbox name
* @param $value string to be used as the checkbox name
* the $key and $value string are concatinated with a dash('-')
* in order to create a unique name
*/
public function toString($key,$value)
{
$string = '
<td>
<input type="checkbox" name="'. $key .'-'. $value .'" value="'. $value .'" >
add '. $this->name .'
'. $this->getFormattedPrice() .'</td>
';
return $string;
}
/**
* creates a string with the topping's properties and topping
* subtotal ready for table insertion
*
* @param $quantity integer topping quantity
* @return string
*/
public function getToppingSubTotalToString($quantity)
{
$string = '
<tr>
<td>' . $quantity .'</td>
<td> extra '. $this->name .'</td>
<td> '. $this->getFormattedPrice() .'</td>
<td> $'. number_format($this->getToppingSubTotal($quantity),2) . '</td>
</tr>';
return $string;
}
/**
* calculates the topping subtotal
*
* @param $quantity integer; topping quantity
* @return integer
*/
public function getToppingSubTotal($quantity)
{
return $this->price * $quantity;
}
}