on mouseUp
--first, check to make sure there is data
if card field "Data" is empty then
answer "Enter data before calculating or sorting" Â
with "OK, I will"
else
-- initialize a counter variable
put 0 into totalSoFar
-- this is for our convenience, so we don't have to write
-- "number of lines of card field "Data"" twice later
put number of lines of card field "Data" into num
-- go line-by-line through the data field, adding up balances
repeat with count = 1 to num
add word 4 of line count of card field "Data" to totalSoFar
end repeat
set numberFormat to "0.##"
-- So that average will be rounded to two decimal places
put totalSoFar/num into average
-- display the result
put "Total balance of $" & totalSoFar && "for" && num && "accounts" & return & return into card field "Results"
put "Average balance = $" & average after card field "Results"
set the scroll of card field "Results" to 0
end if
-- This is a handler which will find, count, and display
-- information on accounts with greater than average balance
-- and accounts with less than average balance.
displayAccounts average
end mouseUp
-- This is a handler which will display the accounts (and
-- number of accounts) with greater than average balance,
-- and those with less than average balance, where the average
-- balance is specified by the parameter "average"
on displayAccounts average
-- bigAccount is a variable which will store the number
-- of accounts whose balances are greater than the average
-- balance
put 0 into bigAccounts
put return & return after card field "results"
-- go through each line (i.e. each account) of card field
-- "data"
repeat with i = 1 to number of lines of card field "data"
-- check if the balance of the current line is greater
-- than the average balance
if word 4 of line i of card field "data" > average then
-- if the balance of the current line is greater than
-- the average balance, then display the information for
-- that account after whatever is in card field "results"
put word 1 of line i of card field "data" after card field "results"
put " " & word 2 of line i of card field "data" after card field "results"
put " " & word 4 of line i of card field "data" after card field "results"
put return after card field "results"
-- update the number of accounts seen so far with greater
-- than average balance
add 1 to bigAccounts
end if
end repeat
-- display the number of accounts with greater than average
-- balance
put "There are" && bigAccounts && "accounts with above average balance" && return after card field "results"
-- now we will display and count the number of accounts with
-- balance less than or equal to the average balance. The
-- code below is almost identical to the above code.
put 0 into smallAccounts
put return & return after card field "results"
repeat with i = 1 to number of lines of card field "data"
if word 4 of line i of card field "data" <= average then
put word 1 of line i of card field "data" after card field "results"
put " " & word 2 of line i of card field "data" after card field "results"
put " " & word 4 of line i of card field "data" after card field "results"
put return after card field "results"
add 1 to smallAccounts
end if
end repeat
put "There are" && smallAccounts && "accounts with balance less than or equal to average" && return after card field "results"
end displayAccounts