Thursday, December 8, 2011

A Pilot Project


Now that I have a transaction record, I need something to use for all the transactions. I thought about using a hash table again, but then I would have to figure out what to use for a key and that seems unnecessary. PowerShell arrays are OK for little stuff, but there's a lot more built into System.Collections.ArrayList.

PowerShell: $a = new System.Collections.ArrayList
PowerShell:
PowerShell: $b1 = nt
PowerShell: $b2 = nt 12 7 -cat groceries

Create an array list and create two transaction objects. This is what they have in them:

PowerShell: $b1

Name Value
---- -----
year 2011
payee blank
amount 0
cat blank
month 0
day 0


PowerShell: $b2

Name Value
---- -----
year 2011
payee blank
amount 0
cat groceries
month 12
day 7

Going to add one and then the other:

PowerShell: $a.Add($b1)
0
PowerShell: $a

Name Value
---- -----
year 2011
payee blank
amount 0
cat blank
month 0
day 0


PowerShell: $a.Add($b2)
1

And here's what the array looks like now:

PowerShell: $a

Name Value
---- -----
year 2011
payee blank
amount 0
cat blank
month 0
day 0
year 2011
payee blank
amount 0
cat groceries
month 12
day 7

One of the sheets on my spreadsheet has the current balance of our checking account, a list of outstanding payments, and remaining balance. I'm going to try to implement that now.

OK, here's my first draft:

PowerShell: function currentBalance ($checkbook) {
>> foreach ($a in $checkbook) { $total = $total - $a.amount}
>> $total
>> }
>>

I know you can see the problems with it.

  • It assumes $total will be blank.
  • It assumes that the amount in the first entry of $checkbook will be a negative number.
I could make a function with two parameters: one for the bank balance and one for the outstanding transactions, but I don't want to – running that is almost as complicated as just using a calculator. Let's try something else.

I added a payee field to the bank_balance transaction:

PowerShell: $bank_balance = @{amount=75;payee="balance"}

And I wrote the function as follows:

PowerShell: function get-balance ($checkbook) {
>> $total = 0
>> foreach ($a in $checkbook) {
>> if ($a.payee -eq "balance") { $total = $total + $a.amount }
>> else { $total = $total - $a.amount }
>> }
>> $total
>> }
>>

And now I'm going to test it:

PowerShell: get-balance $checkbook
-43.56

And that is correct.

So all I need to do is tidy it up and save it in my profile, for now.

function get-balance ($checkbook) {
<#
.SYNOPSIS
Calculate the outstanding balance on an object representing the known balance and outstanding transations.

.DESCRIPTION
Assume that transation amounts with the payee of "balance" is positive and that other payees are to be subtracted from that value.

.PARAMETER month
Array of transactions.
#>
$total = 0
foreach ($a in $checkbook) {
if ($a.payee -eq "balance") { $total = $total + $a.amount }
else { $total = $total - $a.amount }
}
$total
}

set-alias gb get-balance


Next up: some housekeeping. I want to save these functions to their own script and set up a directory for this stuff.

After that, I will be saving the accounts as XML files. Hey, this stuff is no good if I have to enter it every time I want to enter things into my accounts.

No comments:

Post a Comment