Created
March 8, 2014 20:15
-
-
Save adeng21/9438210 to your computer and use it in GitHub Desktop.
Bank Balance Challenge
This file contains hidden or 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
| class BankAccount | |
| attr_reader :account_type, :starting_balance, :transactions_array | |
| def initialize(account_type, starting_balance, transactions_array = []) | |
| @account_type = account_type | |
| @starting_balance = starting_balance | |
| @transactions_array = transactions_array | |
| end | |
| def transaction_total | |
| total_transactions = transactions_array.map {|trans| trans.amount} | |
| total_transactions.inject{|sum, n| sum + n} | |
| end | |
| def ending_balance #modify this so it doesnt start from beginning | |
| starting_balance + transaction_total | |
| end | |
| def summary | |
| string = "" | |
| transactions_array.each do |trans| | |
| string += "#{trans.summary}\n" | |
| end | |
| string | |
| end | |
| end |
This file contains hidden or 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
| class BankTransaction | |
| attr_reader :date, :amount, :description, :account | |
| def initialize(date, amount, description, account) | |
| @date = date | |
| @amount = amount | |
| @description = description | |
| @account = account | |
| end | |
| def format_currency(currency) | |
| sprintf('%.2f', currency) | |
| end | |
| def debit_or_credit #rename | |
| if amount<0 | |
| "DEBIT" | |
| elsif amount >0 | |
| "CREDIT" | |
| end | |
| end | |
| def summary | |
| "$#{format_currency(amount.abs)} #{debit_or_credit} #{date} - #{description}" | |
| end | |
| end |
This file contains hidden or 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
| require 'CSV' | |
| require 'pry' | |
| module RunMethods | |
| def load_accounts_from_csv(csv, array) #break into 2 steps, open file, then iterate | |
| CSV.foreach(csv, headers:true) do |row| | |
| array << BankAccount.new(row["Account"], row["Balance"].to_f) | |
| end | |
| array | |
| end | |
| def load_transactions_from_csv(csv, array) #load info from CSV and make into array of hashes | |
| CSV.foreach(csv, headers: true) do |row| | |
| array << BankTransaction.new(row["Date"], row["Amount"].to_f, row["Description"], row["Account"]) | |
| end | |
| array | |
| end | |
| def separate_transactions_by_bank_account(bank_account_array, trans_array) | |
| bank_account_array.each do |bank_account| | |
| trans_array.each do |transaction| | |
| if transaction.account == bank_account.account_type | |
| bank_account.transactions_array << transaction | |
| end | |
| end | |
| end | |
| end | |
| def format_currency(currency) | |
| sprintf('%.2f', currency) | |
| end | |
| def output(array) | |
| array.each do |account| | |
| puts "===#{account.account_type}===" #summary should be in account class, then account summary should be in transaction class | |
| puts "Starting Balance: $#{format_currency(account.starting_balance)}" | |
| puts "Ending Balance: $#{format_currency(account.ending_balance)}" | |
| puts | |
| puts account.summary | |
| puts | |
| end | |
| end | |
| end |
This file contains hidden or 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
| require 'CSV' | |
| require 'pry' | |
| require_relative "bank_transactions" | |
| require_relative "bank_accounts" | |
| require_relative "methods" | |
| include RunMethods | |
| #prep bank transactions | |
| transactions = [] #array of banktransaction items | |
| load_transactions_from_csv('bank_data.csv', transactions) #creates transactions array | |
| #prep bank accounts | |
| bank_accounts = [] #array of bankaccount items | |
| load_accounts_from_csv('balances.csv', bank_accounts) | |
| #separate transactions by bank_account | |
| bank_transactions = separate_transactions_by_bank_account(bank_accounts, transactions) | |
| output(bank_transactions) #call bank account summary |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment