Created
April 27, 2017 03:09
-
-
Save rambolee/2a16e1ba1389ae581d9f0ff88cee7607 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// A basic shopping cart which contains a list of added products | |
// and the quantity of each product. Includes a method which | |
// calculates the total price of the items in the cart using a | |
// closure as a callback. | |
class Cart | |
{ | |
const PRICE_BUTTER = 1.00; | |
const PRICE_MILK = 3.00; | |
const PRICE_EGGS = 6.95; | |
protected $products = array(); | |
public function add($product, $quantity) | |
{ | |
$this->products[$product] = $quantity; | |
} | |
public function getQuantity($product) | |
{ | |
return isset($this->products[$product]) ? $this->products[$product] : | |
FALSE; | |
} | |
public function getTotal($tax) | |
{ | |
$total = 0.00; | |
$callback = | |
function ($quantity, $product) use ($tax, &$total) | |
{ | |
$pricePerItem = constant(__CLASS__ . "::PRICE_" . | |
strtoupper($product)); | |
$total += ($pricePerItem * $quantity) * ($tax + 1.0); | |
}; | |
array_walk($this->products, $callback); | |
return round($total, 2); | |
} | |
} | |
$my_cart = new Cart; | |
// Add some items to the cart | |
$my_cart->add('butter', 1); | |
$my_cart->add('milk', 3); | |
$my_cart->add('eggs', 6); | |
// Print the total with a 5% sales tax. | |
print $my_cart->getTotal(0.05) . "\n"; | |
// The result is 54.29 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment