/**
 * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Component of: Red Hat Application Server
 * 
 * Initial Developers: Aizaz Ahmed
 *                     Vivek Lakshmanan
 *                     Andrew Overholt
 *                     Matthew Wringe
 *
 */
package olstore.form;

import java.util.ArrayList;
import java.math.BigDecimal;

import olstore.dto.ShoppingCartItem;

public class CheckoutForm extends DemoBaseForm {
    
    private BigDecimal totalCost = new BigDecimal(0.00f);
    private int numItems = 0;
    private String submitType = null;
    private ArrayList cartEntries = new ArrayList() ;
    
    
    public BigDecimal getTotalCost () {
        return totalCost;
    }
    
    public void setTotalCost ( BigDecimal cost ) {
        totalCost = cost;
    }
    
    public int getNumItems () {
        return numItems;
    }
    
    public void setNumItems ( int num ) {
        numItems = num;
    }
    
    // If return is "update" then the form is being updated, if "submit" then 
    // the form is being submitted.
    public String getSubmitType () {
        return submitType;
    }
    
    public void setSubmitType ( String submit ) {
        this.submitType = submit;
    }
    
    /**
     * This autoincrements the size of the vector, so will not return null
     * . This is necessary as the struts auto-populater expects
     * non-null to be retured, and we don't know how many it will
     * request.
     * 
     */
    public ShoppingCartItem getCartItem (int index) {
        if ( index >= cartEntries.size() ) {
            int size = cartEntries.size();
            for ( int i =0; i < (index-size) + 1 ; i++ ) {
                cartEntries.add ( new ShoppingCartItem() );
            }
        }
        return (ShoppingCartItem) cartEntries.get ( index );
    }
    
    public void setCartItem ( int index, ShoppingCartItem item ) {
        numItems++;
        cartEntries.set ( index, item );
    }
    
    public void reset(){
        totalCost = new BigDecimal(0.00f);
        numItems = 0;
        submitType = null;
        cartEntries.clear();	
    }
    
    /**
     * Returns a reference to the internal Properties, use wisely
     */
    public ArrayList getCartEntries( ) {
        return cartEntries;
    }
    
    public void setCartEntries ( ArrayList cartEntries ) {
        this.cartEntries = cartEntries;
        setNumItems(cartEntries.size());
    }
    
    
}
