Created
January 26, 2012 07:45
-
-
Save sgoodwin/1681571 to your computer and use it in GitHub Desktop.
Date-based financial math
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'date' | |
puts "hi, I'm gonna do math!" | |
class CalendarItem | |
attr_accessor :date | |
attr_accessor :amount | |
def initialize(date, amount) | |
@date = date | |
@amount = amount | |
end | |
def self.monthly(start_date, amount, month_count) | |
month = start_date.month | |
values = [] | |
month_count.times.each do | |
date = Date.new(start_date.year, month, 1) | |
values.push(CalendarItem.new(date, amount)) | |
month = month + 1 | |
end | |
return values | |
end | |
end | |
def sum(calendar_items) | |
total = 0 | |
calendar_items.each do |item| | |
total += item.amount | |
end | |
total | |
end | |
def summary(calendar_items, end_date = nil, goal = nil) | |
debits = [] | |
credits = [] | |
total_items = [] | |
calendar_items.sort! { |a,b| a.date <=> b.date } | |
calendar_items.each do |item| | |
if(end_date && (item.date <=> end_date) != -1) | |
break | |
end | |
if(item.amount > 0) | |
credits.push(item) | |
else | |
debits.push(item) | |
end | |
total_items.push(item) | |
end | |
puts "Credits: $#{sum(credits)}" | |
puts "Debits: $#{sum(debits)}" | |
puts "Total: $#{sum(total_items)}" | |
if(goal) | |
money_needed = goal - sum(total_items) | |
bonus_time_needed = ((money_needed)/500)*7 | |
puts "Bonus time needed: #{bonus_time_needed} hours for $#{money_needed}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment