transactions calculations

The fourth section contains functions to calculate the grand totals and final balances for raw and cooked transactions, as follows:

void calculateGrandTotals(float *totalRaw, float *totalCooked) {
struct Node *node = transactionsHead;
while (node != NULL) {
*totalRaw += node->rawAmount;
*totalCooked += node->cookedAmount;
node = node->next;
}
}

float getGrandTotalForType(AmountType type) {
float totalRaw = 0;
float totalCooked = 0;
calculateGrandTotals(&totalRaw, &totalCooked);

if (type == RAW) return totalRaw;
if (type == COOKED) return totalCooked;
return 0;
}

float getFinalBalanceForType(AmountType type, float initialBalance) {
float totalForType = getGrandTotalForType(type);
return initialBalance + totalForType;
}

The AmountType enum we declared in the declarations section is used here to avoid magic numbers. It makes it easy to remember that 1 represents raw transactions and 2 represents cooked transactions. The grand totals for both raw and cooked transactions are calculated in the calculateGrandTotals() function, even though we're only asking for one type in getGrandTotalForType(). Since we can only return a single value from a Wasm function, we end up looping through all of the transactions twice when we call getGrandTotalForType() for both raw and cooked transactions. With a relatively small amount of transactions and the simplicity of the calculation, this doesn't present any issues. The getFinalBalanceForType() returns the grand total plus the specified initialBalance. You'll see this in action when we add the ability to change initial balances in the web application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset