Now let's say that we want to modify the quantity of an item in our Shopping cart. We will use the getElement method for this.
import com.ahfx.Product;
import com.ahfx.Shopping;
var myProduct = new Product();
myProduct.setQuantity(3);
myProduct.setPrice(2.99);
myProduct.setDesc("Teddy Bear");
var myOtherProduct = new Product();
myOtherProduct.setQuantity(1);
myOtherProduct.setPrice(12.99);
myOtherProduct.setDesc("Cool Sunglasses");
var shop_cart = new Shopping();
shop_cart.addElement(myProduct);
shop_cart.addElement(myOtherProduct);
trace(shop_cart.getTotal());
var tempProduct:Product = shop_cart.getElement(1);
tempProduct.setQuantity(2);
trace(shop_cart.getTotal());
Our customer has decided to buy another pair of sunglasses (because they are so cool). We use the getElement() method to return the sunglasses. Notice that this is a zero based Shopping Cart. The first item is 0, the second is 1, etc. After we get the Product out of the cart, we can modify its quanity to 2. This outputs the following:
21.96
34.95
We can see that the total cost of all the items in the Shopping cart has increased to 34.95.
This ends the intermediate Actionscript 2.0 lesson. Although this has been a simple walkthrough, this is a solid base that we wish to build upon. The Shopping cart still needs a remove method for taking things out of the cart and other enhancing methods. If you continue to the advanced page, you will learn how to take the shopping cart you have created and display it in a DataGrid object.
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!