KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > example > ajax > actions > ShowCart


1 /*
2  * Copyright (c) 2005 Your Corporation. All Rights Reserved.
3  */

4 package com.opensymphony.webwork.example.ajax.actions;
5
6 import com.opensymphony.webwork.example.ajax.cart.ShoppingCart;
7 import com.opensymphony.webwork.example.ajax.cart.ShoppingCartAware;
8 import com.opensymphony.xwork.ActionSupport;
9
10 import java.util.Iterator JavaDoc;
11
12 /**
13  * ShowCart
14  *
15  * @author Jason Carreira <jcarreira@eplus.com>
16  */

17 public class ShowCart extends ActionSupport implements ShoppingCartAware {
18     private ShoppingCart cart;
19
20     public void setShoppingCart(ShoppingCart cart) {
21         this.cart = cart;
22     }
23
24     public ShoppingCart getCart() {
25         return cart;
26     }
27
28     public int getNumCartItems() {
29         ShoppingCart cart = getCart();
30         if ((cart == null) || (cart.getContents() == null)) {
31             return 0;
32         }
33         return cart.getContents().size();
34     }
35
36     public double getCartTotal() {
37         ShoppingCart cart = getCart();
38         if ((cart == null) || (cart.getContents() == null)) {
39             return 0;
40         }
41         double total = 0.0;
42         for (Iterator JavaDoc iterator = cart.getContents().iterator(); iterator.hasNext();) {
43             ShoppingCart.CartEntry cartEntry = (ShoppingCart.CartEntry) iterator.next();
44             total += (cartEntry.getQuantity() * cartEntry.getProduct().getPrice());
45         }
46         return total;
47     }
48 }
49
Popular Tags