Jon Aquino's Mental Garden

Engineering beautiful software jon aquino labs | personal blog

Tuesday, December 05, 2006

cashflow.rb

Here's a little Ruby script that you can use to see how your bank balance will go up and down. Enter recurring payments at the top and run it; it will print out a list of balances at various dates. You can then paste the output into a spreadsheet to visualize how it goes up and down.

It's a free alternative to the graphing functionality in MS Money.

def main

balance = 500
days_to_compute = 60
events = [
# 1st of each month,etc. [Jon Aquino 2006-12-05]
RegexEvent.new(/.*-.*-01/, "Job", +1000),
RegexEvent.new(/.*-.*-01/, "Bank service charge", -5),
RegexEvent.new(/.*-.*-01/, "Virgin Mobile", -30),
RegexEvent.new(/.*-.*-01/, "TextDrive", -15),
RegexEvent.new(/.*-06-15/, "Order of the Water Buffaloes", -500),
# Every 14 days, etc. [Jon Aquino 2006-12-05]
EveryNDaysEvent.new(14, "Jim's Mowing", -32, Date.new(2006, 10, 11)),
EveryNDaysEvent.new(14, "Rent", -300, Date.new(2006, 11, 25)),
]

start = Date.today
1.upto(days_to_compute) do |i|
date = start + i
events.each do |event|
change = event.change(date)
if change != 0 then
balance += change
puts date.to_s + ', ' + event.name + ', ' + change.to_s + ', ' + balance.to_s
end
end
end

end

class EveryNDaysEvent
attr_reader :name
def initialize(n, name, change, start)
@n = n
@change = change
@start = start
@name = name
end
def change(date)
return (date - @start).modulo(@n) == 0 ? @change : 0
end
end

class RegexEvent
attr_reader :name
def initialize(regex, name, change)
@regex = regex
@change = change
@name = name
end
def change(date)
# e.g. 2006-12-05 Tuesday [Jon Aquino 2006-12-05]
string = date.to_s + ' ' + Date::DAYNAMES[date.wday]
return string =~ @regex ? @change : 0
end
end

main

Output:

2006-12-06, Jim's Mowing, -32, 468
2006-12-09, Rent, -300, 168
2006-12-20, Jim's Mowing, -32, 136
2006-12-23, Rent, -300, -164
2007-01-01, Job, 1000, 836
2007-01-01, Bank service charge, -5, 831
2007-01-01, Virgin Mobile, -30, 801
2007-01-01, TextDrive, -15, 786
2007-01-03, Jim's Mowing, -32, 754
2007-01-06, Rent, -300, 454
2007-01-17, Jim's Mowing, -32, 422
2007-01-20, Rent, -300, 122
2007-01-31, Jim's Mowing, -32, 90
2007-02-01, Job, 1000, 1090
2007-02-01, Bank service charge, -5, 1085
2007-02-01, Virgin Mobile, -30, 1055
2007-02-01, TextDrive, -15, 1040
2007-02-03, Rent, -300, 740



graph

0 Comments:

Post a Comment

<< Home