I want to explore the getTotal() method in detail.
function getTotal():Number{
var total = 0;
var i;
for(i=0;i<elementCount;i++){
var tempProduct:Product = cart[i];
total += tempProduct.getTotal();
}
return total;
}
This method will return the total of all of the products in the cart. We first create a local variable named total. We also create a local variable named i for use in our for loop.
The for loop will examine each of the Products in the Shopping cart. We create a temporary Product named tempProduct and cast it to a Product.(When we get the item out of the cart, the computer does not know what type of object it is. We must tell it that it is a Product. This is done by the :Product).
Once we have gotten the Product out of the cart, we get its total using the Product getTotal() method. This total is added to our local variable total. (total += tempProduct.getTotal(); is the same as total = total + tempProduct.getTotal(); )
The size() method just returns the number of elements in the array. The getElement just returns the object at that position.
SUPPORT THIS SITE If this tutorial helped you, show your support and send me something from my wish list. Click on an item below and then choose Adam Hayes (Gift Registry Address) for the Ship to Address. It is that easy!