class - Creating a PHP checkout system -
i'm trying create fairly simple php checkout system.
the table of information i'm using is:
product code name price
sw1 sandwich £3.11
cl1 chocolate £5.00
cf1 coffee £11.23
code is:
class product { public $name; public $price; public function __construct($name, $price) { $this->item = $name; $this->price = $price; } } class checkout { protected $shopping_cart; public function scan(product $product) { $shopping_cart[] = $product; } public function total() { // goes through shopping cart , sums price of contents return array_reduce( $this->shopping_cart, function($total, $item) { return $total + $item->price; }, 0 ); } } $pay = new checkout(); $pay->scan(new product("sandwich", 3.11)); $pay->scan(new product("chocolate", 5)); $pay->scan(new product("coffee", 11.23)); $price = $pay->total();
i'd add pricing rule items. example, i'd sandwiches buy-one-get-one-free , chocolates have bulk purchase discount. not sure if it'd work, i'm thinking should add this:
$sw1 = new checkout("sandwich", 3.11); $cl1 = new checkout("chocolate", 5); $cf1 = new checkout("coffee", 11.23); if(scan($sw1 % 2 == 0)) { $price = $price / 2; } elseif(scan($sw1 == 1)) { } else { $price = $price / 2 + $price; } if(scan($cl1 >= 3 && $cl1 % 3 == 0)) { $price = $price - .50; } else { // insert logic make sure purchases include more 3 items, though not divisible 3, discounted accordingly. }
appreciated. if knows how test code, that'd great ☺.
i create 'discount' interface can applied product and/or checkout. example:
interface idiscount { public function apply(&$amount); } class discount_halfoff implements idiscount { public function apply(&$amount) { $amount /= 2; } } class discount_fixedamount implements idiscount { private $amount; public function __construct($amount) { $this->amount = $amount; } public function apply(&$amount) { $amount -= $this->amount; } } class product { private $discounts = []; public function __construct($name, $price) { $this->item = $name; $this->price = $price; } public function adddiscount(idiscount $discount) { $this->discounts[] = $discount; } public function getprice() { $price = $this->price; foreach($this->discounts $discount) $discount->apply($price); return $price; } } class checkout { private $contents = []; private $discounts = []; public function adddiscount(idiscount $discount) { $this->discounts[] = $discount; } public function scan(product $product) { $this->contents[] = $product; } public function getcount() { return count($this->contents); } public function gettotal() { $ttl = 0; foreach($this->contents $product) $ttl += $product->getprice(); foreach($this->discounts $discount) $discount->apply($ttl); return $ttl; } } $sandwich = new product('sandwich', 3.11); $sandwich->adddiscount(new discount_halfoff()); $checkout = new checkout(); $checkout->scan($sandwich); // logic included checkout class directly, depends on use case. if ($checkout->getcount() > 3) { if ($checkout->getcount() % 3 == 0) $checkout->adddiscount(new discount_fixedamount(0.50)); } else { // insert logic make sure purchases include more 3 items, though not divisible 3, discounted accordingly. } } echo $checkout->gettotal() . "\n";
Comments
Post a Comment