Wednesday, December 7, 2011

Everything we need is here

I keep a spreadsheet with our (my wife's and mine) finances. Not super-detailed, just enough to keep track of where the money goes.

I don't mind spreadsheet programs, but there is a lot of mousing around - and I wonder if that's all that necessary. Then it occurred to me that PowerShell has the kinds of data types necessary to keep track of this without adding anything fundamental.

So, I'll see what something like that would look like.

We need a record. That is, every transaction needs to have a certain amount type of information. A hash table stores key value pairs. How about this:

PowerShell: $a = {month=0;day=0;payee=blank;amount=0;category=blank;year=0000}

PowerShell: $a

month=0;day=0;payee=blank;amount=0;category=blank;year=0000


Some design decisions:

Why month and day instead of a date object? Seems like overkill. I guess I can implement it with date objects later.

Why is the year last? I plan on using default values, so that will only have to be set once per year.

So, let's make a function out of this:

PowerShell: function new-transaction ($month = 0, $day=0, $payee="blank", $amount=0, $cat="blank", $year=2011)

>> {


>> @{month=$month;day=$day;payee=$payee;amount=$amount;cat=$cat;year=$year}


>> }


>>



And how does it work?

PowerShell: $a = new-transaction

PowerShell: $a



Name Value


---- -----


year 2011


payee blank


amount 0


cat blank


month 0


day 0




PowerShell: $a = new-transaction 12 8 test 4.35 groceries

PowerShell: $a



Name Value


---- -----


year 2011


payee test


amount 4.35


cat groceries


month 12


day 8


And now this needs to live in a script file - my profile, for now.

Here's what I put in:

function new-transaction ($month = 0, $day=0, $payee="blank", $amount=0, $cat="blank", $year=2011)


{


<#


.SYNOPSIS


Create a new transaction hash table.




.DESCRIPTION


Creates a new hash table with vaues for required parameters. Default values are
provided.




.PARAMETER month


Month from 1-12. No verification




.PARAMETER day


Day from 1-31. No verification.




.PARAMETER payee


Payee string.




.PARAMETER amount


Amount.




.PARAMETER cat


Spending category.




.PARAMETER year


Year.


#>







@{month=$month;day=$day;payee=$payee;amount=$amount;cat=$cat;year=$year}



}



set-alias nt new-transaction



Yes, I wrote help for my function - you should, too.

Here's what the output looks like:


PowerShell: nt




Name Value


---- -----


year 2011


payee blank


amount 0


cat blank


month 0


day 0




PowerShell: get-help nt



NAME


new-transaction




SYNOPSIS


Create a new transaction hash table.






SYNTAX


new-transaction [[-month] Object>] [[-day] Object>] [[-payee]
Object>] [[-amount] Object>] [[-cat] Object>] [[

-year] Object>] []




DESCRIPTION

Creates a new hash table with vaues for required parameters. Default values are provided.




RELATED LINKS



REMARKS


To see the examples, type: "get-help new-transaction -examples".


For more information, type: "get-help new-transaction -detailed".


For technical information, type: "get-help new-transaction -full".





PowerShell:



Next up: Put the transaction records into an array.

No comments:

Post a Comment