Example: Adding values that are contained in an indexed global variable

This example adds the values that are contained in an indexed global variable and stores the sum in another, non-indexed global variable. It assumes that you have stored strings representing numbers in the indexed global variable subtotals.

The previous example included the names of the input and output global variables (theDate) on the set calls. This example sets the names of the input and output variables into local string variables and uses those strings on calls to get and set the global variable values. Because the name of the global variable is being passed as a variable, it is not put into quotes:

setGlobalVariable(blInfo,gvOutputName, new Float(myTotal));

////////////////////////////////////////////////////////////////////////////////
//  This sample is provided AS IS.
//  Permission to use, copy and modify this software for any purpose and
//  without fee is hereby granted. provided that the name of IBM not be used in
//  advertising or publicity pertaining to distribution of the software without
//  specific written permission.
//
//  IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SAMPLE, INCLUDING ALL
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL IBM
//  BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
//  DAMAGES WZIETransOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
//  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
//  OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SAMPLE.
////////////////////////////////////////////////////////////////////////////////
import com.ibm.hats.common.IBusinessLogicInformation;
import com.ibm.hats.common.GlobalVariable;
import com.ibm.hats.common.IGlobalVariable;

  public static void execute(IBusinessLogicInformation blInfo)
    {
    	// Name of indexed global variable to be read in
        String gvInputName = "subtotals";
        // Name of global variable to be calculated and saved
        String gvOutputName = "total";
        
        // The indexed global variable where each index is a subtotal to be summed
        GlobalVariable gvSubtotals = 
					((GlobalVariable)getGlobalVariable(blInfo, gvInputName));
        
        float myTotal = 0;
        
        // Calculate the total by adding all subtotals
        for (int i = 0; i < gvSubtotals.size(); i++)
        {
        	myTotal = myTotal + Float.valueOf(gvSubtotals.getString(i)).floatValue();
        }
		
        // Save the total as a non-indexed local variable
        setGlobalVariable(blInfo,gvOutputName, new Float(myTotal));
    }