Skip to content

Instantly share code, notes, and snippets.

@jmoon90
Last active December 30, 2015 01:48
Show Gist options
  • Select an option

  • Save jmoon90/7757978 to your computer and use it in GitHub Desktop.

Select an option

Save jmoon90/7757978 to your computer and use it in GitHub Desktop.
#!/user/bin/env ruby
require "csv"
class Tracker
attr_reader :balance
def initialize
@balance = 0
@income = 0
@expenses = 0
@overdraft = 0
@overdraft_summary = []
end
def read_in
array_of_trans = CSV.read('transactions.csv')
array_of_trans.each_with_index do |row, i|
next if i == 0
calculate(row)
end
summary
overdraft_summary
end
def calculate(row)
balance(row)
total_expense(row)
total_income(row)
end
def balance(row)
@balance += row[1].to_i
row << @balance
fee(row) if @balance < 0
end
def fee(row)
@balance -= 20
@overdraft += 20
@overdraft_summary << row
end
def total_income(row)
@income += row[1].to_i if row[1].to_i > 0
end
def total_expense(row)
@expenses += row[1].to_i if row[1].to_i < 0
end
def summary
puts "Ending Balance: #{@balance}"
puts "Total Income: #{@income}"
puts "Total Expenses: #{@expenses}"
puts "Total Overdfrat Charges: #{@overdraft}"
end
def overdraft_summary
puts "\nOverdrafts (balance, expense, description, date)"
@overdraft_summary.each do |transaction|
puts "#{transaction.last}, #{transaction[1]}, #{transaction[2]}, #{transaction.first}"
end
end
end
new = Tracker.new.read_in
date amount description
2012-01-01 1500.0 payday
2012-01-03 -99.18 groceries
2012-01-06 -96.95 electric bill
2012-01-11 -2123.21 mortgage
2012-01-15 1500.0 payday
2012-01-15 -50.18 gas bill
2012-01-15 -48.6 gas
2012-01-16 -44.76 groceries
2012-01-22 -200.0 student loan
2012-01-29 -50.96 groceries
2012-01-29 1500.0 payday
2012-01-29 -40.85 gas
2012-01-29 -150.0 car payment
2012-02-05 -70.24 electric bill
2012-02-08 16.98 gambling
2012-02-10 -2123.21 mortgage
2012-02-11 -48.52 groceries
2012-02-12 -42.68 gas
2012-02-12 1500.0 payday
2012-02-14 -51.88 gas bill
2012-02-21 -200.0 student loan
2012-02-24 -81.42 groceries
2012-02-26 -48.08 gas
2012-02-26 1500.0 payday
2012-02-28 -150.0 car payment
2012-03-06 -66.25 electric bill
2012-03-08 -52.71 groceries
2012-03-11 -48.92 gas
2012-03-11 1500.0 payday
2012-03-11 -2123.21 mortgage
2012-03-15 -47.61 gas bill
2012-03-21 -47.26 groceries
2012-03-22 -200.0 student loan
2012-03-25 -41.7 gas
2012-03-25 1500.0 payday
2012-03-29 -150.0 car payment
2012-04-03 -42.59 groceries
2012-04-05 -68.41 electric bill
2012-04-08 -46.37 gas
2012-04-08 1500.0 payday
2012-04-08 87.76 gambling
2012-04-10 -2123.21 mortgage
2012-04-14 -31.12 gas bill
2012-04-16 -57.33 groceries
2012-04-21 -200.0 student loan
2012-04-22 1500.0 payday
2012-04-22 -48.85 gas
2012-04-28 -150.0 car payment
2012-04-29 -46.62 groceries
2012-05-05 -62.96 electric bill
2012-05-06 -48.44 gas
2012-05-06 1500.0 payday
2012-05-10 -2123.21 mortgage
2012-05-12 -51.24 groceries
2012-05-14 -58.15 gas bill
2012-05-20 -46.11 gas
2012-05-20 1500.0 payday
2012-05-21 -200.0 student loan
2012-05-25 -45.48 groceries
2012-05-28 -150.0 car payment
2012-06-03 1500.0 payday
2012-06-03 -40.92 gas
2012-06-04 -99.62 electric bill
2012-06-07 -90.66 groceries
2012-06-07 -166.28 gambling
2012-06-09 -2123.21 mortgage
2012-06-13 -73.27 gas bill
2012-06-17 1500.0 payday
2012-06-17 -41.69 gas
2012-06-20 -84.4 groceries
2012-06-20 -200.0 student loan
2012-06-27 -150.0 car payment
2012-07-01 -41.97 gas
2012-07-01 1500.0 payday
2012-07-03 -37.9 groceries
2012-07-04 -92.89 electric bill
2012-07-09 -2123.21 mortgage
2012-07-13 -68.94 gas bill
2012-07-15 1500.0 payday
2012-07-15 -48.78 gas
2012-07-16 -79.4 groceries
2012-07-20 -200.0 student loan
2012-07-27 -150.0 car payment
2012-07-29 1500.0 payday
2012-07-29 -46.35 groceries
2012-07-29 -41.37 gas
2012-08-03 -86.7 electric bill
2012-08-06 88.84 gambling
2012-08-08 -2123.21 mortgage
2012-08-11 -97.42 groceries
2012-08-12 -61.53 gas bill
2012-08-12 1500.0 payday
2012-08-12 -44.67 gas
2012-08-19 -200.0 student loan
2012-08-24 -90.62 groceries
2012-08-26 -46.57 gas
2012-08-26 1500.0 payday
2012-08-26 -150.0 car payment
2012-09-02 -98.11 electric bill
2012-09-06 -84.46 groceries
2012-09-07 -2123.21 mortgage
2012-09-09 -43.57 gas
2012-09-09 1500.0 payday
2012-09-11 -46.51 gas bill
2012-09-18 -200.0 student loan
2012-09-19 -71.31 groceries
2012-09-23 -48.23 gas
2012-09-23 1500.0 payday
2012-09-25 -150.0 car payment
2012-10-02 -58.7 groceries
2012-10-02 -99.59 electric bill
2012-10-05 -22.26 gambling
2012-10-07 -44.56 gas
2012-10-07 1500.0 payday
2012-10-07 -2123.21 mortgage
2012-10-11 -37.22 gas bill
2012-10-15 -97.47 groceries
2012-10-18 -200.0 student loan
2012-10-21 -42.23 gas
2012-10-21 1500.0 payday
2012-10-25 -150.0 car payment
2012-10-28 -71.52 groceries
2012-11-01 -62.57 electric bill
2012-11-04 -41.74 gas
2012-11-04 1500.0 payday
2012-11-06 -2123.21 mortgage
2012-11-10 -43.34 groceries
2012-11-10 -57.16 gas bill
2012-11-17 -200.0 student loan
2012-11-18 1500.0 payday
2012-11-18 -40.89 gas
2012-11-23 -60.28 groceries
2012-11-24 -150.0 car payment
2012-12-01 -88.84 electric bill
2012-12-02 1500.0 payday
2012-12-02 -44.09 gas
2012-12-04 90.93 gambling
2012-12-06 -37.58 groceries
2012-12-06 -2123.21 mortgage
2012-12-10 -26.68 gas bill
2012-12-16 1500.0 payday
2012-12-16 -47.73 gas
2012-12-17 -200.0 student loan
2012-12-19 -53.94 groceries
2012-12-24 -150.0 car payment
2012-12-30 -44.25 gas
2012-12-30 1500.0 payday
2012-12-31 -55.4 electric bill
2013-01-01 -71.67 groceries
2013-01-05 -2123.21 mortgage
2013-01-09 -63.75 gas bill
2013-01-13 -46.75 gas
2013-01-13 1500.0 payday
2013-01-14 -56.14 groceries
2013-01-16 -200.0 student loan
2013-01-23 -150.0 car payment
2013-01-27 -98.33 groceries
2013-01-27 -44.47 gas
2013-01-27 1500.0 payday
2013-01-30 -86.83 electric bill
2013-02-02 2.9 gambling
2013-02-04 -2123.21 mortgage
2013-02-08 -70.41 gas bill
2013-02-09 -56.75 groceries
2013-02-10 -49.16 gas
2013-02-10 1500.0 payday
2013-02-15 -200.0 student loan
2013-02-22 -46.94 groceries
2013-02-22 -150.0 car payment
2013-02-24 -41.97 gas
2013-02-24 1500.0 payday
2013-03-01 -68.49 electric bill
2013-03-06 -2123.21 mortgage
2013-03-07 -84.75 groceries
2013-03-10 1500.0 payday
2013-03-10 -43.23 gas
2013-03-10 -38.0 gas bill
2013-03-17 -200.0 student loan
2013-03-20 -52.23 groceries
2013-03-24 1500.0 payday
2013-03-24 -150.0 car payment
2013-03-24 -48.83 gas
2013-03-31 -89.93 electric bill
2013-04-02 -45.53 groceries
2013-04-03 98.09 gambling
2013-04-05 -2123.21 mortgage
2013-04-07 1500.0 payday
2013-04-07 -43.31 gas
2013-04-09 -37.73 gas bill
2013-04-15 -98.7 groceries
2013-04-16 -200.0 student loan
2013-04-21 1500.0 payday
2013-04-21 -46.36 gas
2013-04-23 -150.0 car payment
2013-04-28 -66.11 groceries
2013-04-30 -95.62 electric bill
2013-05-05 -46.88 gas
2013-05-05 -2123.21 mortgage
2013-05-05 1500.0 payday
2013-05-09 -63.67 gas bill
2013-05-11 -34.79 groceries
2013-05-16 -200.0 student loan
2013-05-19 1500.0 payday
2013-05-19 -48.77 gas
2013-05-23 -150.0 car payment
2013-05-24 -73.1 groceries
2013-05-30 -62.57 electric bill
2013-06-02 -41.32 gas
2013-06-02 1500.0 payday
2013-06-02 -6.58 gambling
2013-06-04 -2123.21 mortgage
2013-06-06 -42.58 groceries
2013-06-08 -67.93 gas bill
2013-06-15 -200.0 student loan
2013-06-16 -44.81 gas
2013-06-16 1500.0 payday
2013-06-19 -99.47 groceries
2013-06-22 -150.0 car payment
2013-06-29 -70.8 electric bill
2013-06-30 1500.0 payday
2013-06-30 -42.86 gas
2013-07-02 -85.91 groceries
2013-07-04 -2123.21 mortgage
2013-07-08 -45.69 gas bill
2013-07-14 -48.39 gas
2013-07-14 1500.0 payday
2013-07-15 -200.0 student loan
2013-07-15 -45.05 groceries
2013-07-22 -150.0 car payment
2013-07-28 -45.53 gas
2013-07-28 -64.24 groceries
2013-07-28 1500.0 payday
2013-07-29 -77.7 electric bill
2013-08-01 31.98 gambling
2013-08-03 -2123.21 mortgage
2013-08-07 -62.56 gas bill
2013-08-10 -64.86 groceries
2013-08-11 -40.47 gas
2013-08-11 1500.0 payday
2013-08-14 -200.0 student loan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment