<?php
//my php classes by Arman G. de Castro
//donate at armandecastro@gmail.com paypal.
class Item {
protected $id;
protected $name;
protected $price;
protected $quantity;
protected $totalperitem;
public function __construct($id, $quantity, $name, $price) {
$this->id = $id;
$this->name = $name;
$this->price = $price;
$this->quantity = $quantity;
$this->totalperitem = $this->getTotalPerItem();
}
public function getId() {
return $this->id;
}
public function getName() {
return $this->name;
}
public function getPrice() {
return $this->price;
}
public function getQuantity(){
return $this->quantity;
}
public function getTotalPerItem(){
$this->totalperitem = $this->getQuantity() * $this->getPrice();
return $this->totalperitem;
}
}//End of Item class.
//ITEM CLASS USAGE
// item qty desc amount
$PO1 = new Item('ADC123', 2, 'Cat', 23.45);
$PO2 = new Item('ADC456', 2, 'Dog', 12.39);
$PO3 = new Item('ADC789', 1, 'Rat', 5.00);
$PO4 = new Item('ADC101', 1, 'Bat', 15.00);
class ShoppingCart implements Iterator, Countable {
protected $items = array();
protected $ids = array();
public function __construct(){
$this->items = array();
$this->ids = array();
}
public function addItem(Item $item) {
$id = $item->getId();
$this->items[$id] = array('kasmot' => $item);
$this->ids[] = $id;
}
public function updateItem(Item $item) {
$id = $item->getId();
$qty = $item->getQuantity();
$totalperitem = $item->getTotalPerItem();
if ($qty === 0) {
$this->deleteItem($item);
} elseif ( ($qty > 0) && ($qty != $this->items[$id]['qty'])) {
$this->items[$id]['qty'] = $qty;
$this->items[$id]['totalperitem'] = $totalperitem;
}
}
public function deleteItem(Item $item) {
$id = $item->getId();
if (isset($this->items[$id])) {
unset($this->items[$id]);
$index = array_search($id, $this->ids);
unset($this->ids[$index]);
$this->ids = array_values($this->ids);
}
}
public function isEmpty() {
return (empty($this->items));
}
//ITERATION IN PHP
public function current() {
$index = $this->ids[$this->position];
return $this->items[$index];
}
public function key() {
return $this->position;
}
public function next() {
$this->position++;
}
public function rewind() {
$this->position = 0;
}
public function valid() {
return (isset($this->ids[$this->position]));
}
public function count() {
return count($this->items);
}
}//End of ShoppingCart Class
//SHOPPING CART USAGE
$cart = new ShoppingCart();
// Add the items to the cart:
$cart->addItem($PO1);
$cart->addItem($PO2);
$cart->addItem($PO3);
$cart->addItem($PO4);
//$cart->deleteItem($PO1);
//$cart->deleteItem($PO2);
//$cart->deleteItem($PO3);
$cart->deleteItem($PO4);
echo '<h2>There are 2 options running in this cart. <br><br> 1. is "WITHOUT" updateItem() <br>2. is "WITH" updateItem()<br><br>';
echo 'OPTION #1.</h2>';
// Show the cart contents:
echo '<h2>Cart Contents (' . count($cart) . ' items)</h2>';
//print_r($cart);die();
//SHOW THE CART COMPLETE INFO WITHOUT USING THE updateitem();
if (!$cart->isEmpty()) {
$total = 0;
foreach ($cart as $arr) {
// Get the item object:
$item = $arr['kasmot'];
// Print the item:
$format = '<p> %d <strong>%s</strong> @ $%0.2f each = $%0.2f</p>';
printf($format, $item->getQuantity(), $item->getName(), $item->getPrice(), $item->getTotalPerItem() );
//add the total
$total += $item->getTotalPerItem();
} // End of foreach loop!
$formatTotal = '<p><strong>TOTAL: = </strong>$%0.2f<p>';
printf($formatTotal, $total);
} // End of IF.
echo '<h2>OPTION #2. try to commented the updateItem to see the effects.</h2>';
//TAKENOTE!! IF YOU NEED TO UPDATE OR EDIT THE QUANTITY updateItem() below:
$cart->updateItem($PO1);
$cart->updateItem($PO2);
$cart->updateItem($PO3);
//$cart->updateItem($PO4); //deleted
echo '<h2>Cart Contents (' . count($cart) . ' items)</h2>';
if (!$cart->isEmpty()) {
$total = 0;
foreach ($cart as $arr) {
$item = $arr['kasmot'];
$format = '<p> %d <strong>%s</strong> @ $%0.2f each = $%0.2f</p>';
printf($format, $arr['qty'], $item->getName(), $item->getPrice(), $arr['totalperitem'] );
$total += $arr['totalperitem'];
}
$formatTotal = '<p><strong>TOTAL: = </strong>$%0.2f<p>';
printf($formatTotal, $total);
} // End of IF.
?>
|