Skip to content

Instantly share code, notes, and snippets.

@dmehrotra
Created August 23, 2013 16:33
Show Gist options
  • Save dmehrotra/6321343 to your computer and use it in GitHub Desktop.
Save dmehrotra/6321343 to your computer and use it in GitHub Desktop.
transactions.rb
#!/usr/bin/env ruby
require 'csv'
balance = 0.00
sum_income = 0.00
sum_deduction = 0.00
overdrafts=[]
deposits = []
deductions = []
CSV.foreach('transactions.csv', headers: true) do |row|
balance += row[1].to_f
if balance < 0
balance -= 20
statement = {}
statement[:current_balance] = balance.round(2)
statement[:description]=row[2]
statement[:amount] = row[1]
statement[:date] = row[0]
overdrafts << statement
end
if row[1].to_f > 0
income = {}
income[:amount] = row[1]
income[:description]=row[2]
income[:date] = row[0]
deposits << income
end
if row[1].to_f < 0
expenses = {}
expenses[:amount] = row[1]
expenses[:description] = row[2]
expenses[:date] = row[0]
deductions << expenses
end
end
overdraft_balance = overdrafts.length * 20
deposits.each do |object|
object.each do |key, value|
if key == :amount
sum_income += value.to_f
end
end
end
deductions.each do |object|
object.each do |key, value|
if key == :amount
sum_deduction += value.to_f
end
end
end
puts "Ending Balance: #{balance}"
puts "Total Income: #{sum_income}"
puts "Total Expenses: #{sum_deduction}"
puts "Total Overdraft Charges: #{overdraft_balance}"
puts "
-------------------OVERDRAFT REPORT--------------------"
overdrafts.each_with_index do |object, index|
puts ''
print index+1
print ' ----'
puts ''
object.each do |key, value|
puts "#{key}: #{value}:"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment