A Shopping Cart Class

Dec 01, 2009 Author: LinuxAdmin

First, we need to define the properties and behavior of a shopping cartclass. This shopping cart will be kept fairly simple for demonstration purposes,but will certainly be sufficient for use and expansion.

The shopping cart will track all of the items it contains by using an array.We’ll assume that each item has an item number (001, 002, and so on), abrief name, and a price.

We’ll want the shopper to be able to choose whateverquantity he wants, so this will also be stored in the array.Aside from that array, we will also track the total number of items stored inthe array.

This number will be stored in an integer variable.Now that we know the variables we want, we need to think about whatfunctions we’ll need.The obvious ones, Add() and Update(), would add andremove items from the cart. (To remove an item, you would use Update() toset the new desired quantity.)

In addition to those functions, we’ll defineTotalItems() and NumItems(), the first of which will return the total numberof items in the cart and the second of which will return how many of agiven item are in the cart.Our class comes out looking like this:

class shopping_cart
{
var $arrItems,
$intNumItems;
function shopping_cart()
{
$this->
arrItems = Array();
$this->intNumItems = 0;
}
function Add($intItemNumber, $strName, $dblPrice, $intQuantity)
/*$strName is the textual name of the item,
$dblPrice is the price of the item,
$intQuantity is the number of this item we are addingto the shopping cartThe item information (name, price) for $intItemNumberis updated to match the newest data,
$intQuantity items are added to the previous quantity
$intNumItems is updated appropriately*/
{
// Set item’s name/price information
$this->arrItems[$intItemNumber][‘name’] = $strName;
$this->arrItems[$intItemNumber][‘price’] = $dblPrice;
// Add the appropriate number of items to the quantity$this->arrItems[$intItemNumber][‘quantity’] += $intQuantity;
// Update the intNumbItems variable$this->intNumItems += $intQuantity;
}
function Update($intItemNumber, $intQuantity)
/*
 $intItemNumber is the 3-digit item number code assignedto this item,
$intQuantity is the new quantity of this item that shouldbe in the shopping cartThe item’s quantity is updated to match $intQuantity
$intNumItems is updated appropriately
*/
{
// Update intNumItems$this->intNumItems += $intQuantity -
$this->arrItems[$intItemNumber][‘quantity’];
// Update arrItems
$this->arrItems[$intItemNumber][‘quantity’] = $intQuantity;
}
function TotalItems()
/*
 nonereturns the total number of items in the shopping cart(stored in $intNumItems)
*/
{
return $this->intNumItems;
}
function NumItems($intItemNumber)
/*
 $intItemNumber is a 3-digit item identification numberthe quantity of items with the $intItemNumber itemnumber is returned
*/
{
return $this->arrItems[$intItemNumber][‘quantity’];
}
}
Notice that a basic constructor function has been added to explicitly set theclass’s member variables to beginning values.

Although this isn’t necessary, telling other programmers that these values always begin at this starting point is good programming practice.When given a choice, it’s better not to leave matters in question; the process wastes time when adding a short function, because the constructor clarifies matters instantly.

From this class you can see why black boxing is helpful.Now, instead of dealing with a multidimensional array, which could easily get confusing,we’re working with a few simple functions that handle all the details for us.

As a by-product, the class’s member variables are less likely to be corruptedby a programming mistake; they’re as reliable as the class definition.As you can see from these functions, the workings of this shopping cart arefairly simple.

The Add() function adds items to the shopping cart, theUpdate() function allows us to update an item’s quantity by either increasingor decreasing its value, and the TotalItems() and NumItems() functionsgive us important information about the contents of the class.


views 10267
  1. Add New Comment