Skip to content
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5b67ba6
Added module Bank, class Account, and initialize method to create @id…
Lisa-Sano Feb 29, 2016
95db2ef
Added withdraw, deposit methods.
Lisa-Sano Feb 29, 2016
66fdb97
created Owner class with many instance variables like name, business …
Lisa-Sano Feb 29, 2016
2df880d
raise ArgumentError if someone tries to create an account with negati…
Lisa-Sano Feb 29, 2016
8bb28bc
Added owner property to each account.
Lisa-Sano Feb 29, 2016
b627ed2
added the Money gem to be able to use and format currency properly.
Lisa-Sano Mar 1, 2016
b9caef3
added comments to explain the usage of the money gem
Lisa-Sano Mar 1, 2016
5d100ac
Added self.all class method in Bank::Accounts.
Lisa-Sano Mar 2, 2016
95317f0
Added self.find(id) Account class method, changed Money object usage,…
Lisa-Sano Mar 2, 2016
0e34b10
Added self.all and self.find(id) methods to Owner class.
Lisa-Sano Mar 2, 2016
14e83f4
Added an accounts method to the Owner class.
Lisa-Sano Mar 2, 2016
98f08fd
Added a return nil statement to the end of each self.find method in A…
Lisa-Sano Mar 3, 2016
705e51a
Added a new class SavingsAccount that inherits behavior from Account,…
Lisa-Sano Mar 3, 2016
a5b18bc
Updated the functionality of the is_balance_enough method in the Savi…
Lisa-Sano Mar 3, 2016
cc72b27
Added transaction fee and lower initial balance limit constants.
Lisa-Sano Mar 3, 2016
ec9b087
Added add_interest method to SavingsAccount class.
Lisa-Sano Mar 3, 2016
2ab8ddd
Turned id numbers in accounts and owners from CSV files into integers.
Lisa-Sano Mar 3, 2016
60f45de
Added CheckingAccount class and updated withdraw method.
Lisa-Sano Mar 3, 2016
37ff339
Added instance variable @number_of_checks and withraw_using_check met…
Lisa-Sano Mar 4, 2016
36c85fe
Added a reset_checks method to the CheckingAccount class
Lisa-Sano Mar 4, 2016
5708620
Convert id's and balances to numbers during initialization (instead o…
Lisa-Sano Mar 4, 2016
33405ea
Added MoneyMarketAccount class which inherits from Account.
Lisa-Sano Mar 4, 2016
0a375bc
changed to a single BALANCE_LIMIT instead of having a separate initia…
Lisa-Sano Mar 4, 2016
0570278
Added Interest module (used in SavingsAccount and MoneyMarketAccount …
Lisa-Sano Mar 4, 2016
cb34d48
Created a new module BankMethod. It contains a method that is called …
Lisa-Sano Mar 4, 2016
ee05bea
Added a self.find method in the module BankMethod.
Lisa-Sano Mar 4, 2016
62358c2
got rid of unneeded constant and variable
Lisa-Sano Mar 4, 2016
2b3f713
updated find method in the BankMethod module. changed @owner_id in Ow…
Lisa-Sano Mar 5, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
294 changes: 294 additions & 0 deletions bank-account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
# Lisa Rolczynski
# 2016-02-29

require 'csv'
require 'money'
I18n.enforce_available_locales = false # need this not to trip an I18n error!


module Bank

module BankMethod
# accepts 3 arguments: file where particular csv data is located,
# array of keys so that they can be given values from csv file,
# and class_name, which will just be whatever class is calling the method
def self.make_all(file, key_array, class_name)
# make empty array that will be filled with instances
instance_array = []

# iterate over each line of the file, which contains the data for one instance
CSV.open(file, 'r').each do |line|
info_hash = {} # make an empty hash each time it iterates to be filled with new instance data
key_array.each_with_index do |element, index| # iterate over the array of keys to give each a value
info_hash[element] = line[index]
end
instance_array << class_name.new(info_hash) # add new instance to the collection
end

return instance_array # return collection of all instances from the file given
end


def self.find(id, class_name)
#loop through until I find the one I'm looking for
found_instance = nil
class_name.all.each do |line|
if id == line.id
found_instance = line
end
end

return found_instance
end

end


class Account
include BankMethod

attr_reader :id, :owner, :creation_date, :balance
TRANSACTION_FEE = 0
BALANCE_MINIMUM = 0

def initialize(account_info)
@id = account_info[:id].to_i
@owner = account_info[:owner]
@creation_date = account_info[:creation_date]
@balance = account_info[:initial_balance].to_i
is_balance_enough(BALANCE_MINIMUM) # checks if balance meets criteria (is there enough money in it?)
end

def withdraw(money, fee = TRANSACTION_FEE, limit = BALANCE_MINIMUM)
if @balance >= (money + limit + fee) # if balance will stay above the lower limit after money is withdrawn
@balance -= (money + fee)
else
puts "Insufficient funds. Withdrawal canceled."
@balance # return balance without altering it if withdrawal amount is higher than balance
end
end

def deposit(money)
@balance += money
end

def add_owner(owner_obj)
@owner = owner_obj
end

# turn balance into a Money object. Format: $X.XX
def get_balance
Money.new(balance).format
end

def is_balance_enough(limit)
if balance < limit
raise ArgumentError.new("Not enough money!!")
end
end

# return a collection of Account instances, representing all of the
# Accounts described in the CSV.
def self.all
account_keys = [:id, :initial_balance, :creation_date]
BankMethod::make_all("./support/accounts.csv", account_keys, Account)
end

# return an instance of Account, where the value of the id field in
# the CSV matches the passed parameter.
def self.find(id)
BankMethod::find(id, Account)
end
end


class Owner
include BankMethod

attr_reader :id, :first_name, :last_name, :street_address, :city, :state

def initialize(owner_info)
@id = owner_info[:id].to_i
@first_name = owner_info[:first_name]
@last_name = owner_info[:last_name]
@street_address = owner_info[:street_address]
@city = owner_info[:city]
@state = owner_info[:state]
end

# return a collection of account instances that belong to a particular owner
def accounts
owners_accounts = []
CSV.open("./support/account_owners.csv", 'r').each do |line|
if @id == line[1].to_i
account_num = line[0].to_i
owners_accounts << Account.find(account_num)
end
end

# return array filled with owners account instances
if owners_accounts.length == 0
return nil
else
return owners_accounts
end
end

# return a collection of Owner instances, representing all owners described
# in the CSV.
def self.all
owner_keys = [:id, :last_name, :first_name, :street_address, :city, :state]
BankMethod::make_all("./support/owners.csv", owner_keys, Owner)
end

# return an instance of Owner where the value of the id field in the CSV
# matches the passed parameter.
def self.find(id)
BankMethod::find(id, Owner)
end

end


module Interest
def add_interest(rate)
interest = calculate_interest(rate)
@balance += interest
end

def calculate_interest(rate)
interest = balance * rate / 100
end
end


class SavingsAccount < Account
include Interest

TRANSACTION_FEE = 200 # $2.00 transaction fee (200 in cents)
BALANCE_MINIMUM = 1000 # $10.00 lower balance limit

def is_balance_enough(limit)
super(BALANCE_MINIMUM)
end

def withdraw(money)
super(money, TRANSACTION_FEE, BALANCE_MINIMUM)
end
end


class CheckingAccount < Account
attr_reader :number_of_checks
TRANSACTION_FEE = 100

def initialize(account_info)
super
@number_of_checks = 0
end

def withdraw(money, fee = TRANSACTION_FEE, limit = 0)
super(money, fee, limit)
end

def withdraw_using_check(amount)
check_fee = 200
fee = 0 # no fee until the 4th check
@number_of_checks += 1
if number_of_checks > 3
fee = check_fee # if 3 checks have already been used, each additional check has a $2 fee
end
withdraw(amount, fee, -1000) # can have up to a $10 overdraft
end

def reset_checks
@number_of_checks = 0
end

end


class MoneyMarketAccount < Account
include Interest

attr_reader :number_of_transactions
BALANCE_MINIMUM = 1000000 # initial balance can't be less than $10,000

def initialize(account_info)
super
@number_of_transactions = 0
@can_transact = true
@can_withdraw = true
end

def is_balance_enough(limit)
super(BALANCE_MINIMUM)
end

def check_transaction_status
unless @can_withdraw == false # unless withdrawing is suspended due to low account balance
@number_of_transactions += 1
end

if number_of_transactions > 6 # if max number of transactions has been exceeded
@can_transact = false # prevent future transactions from occuring unless reset
end
end

def give_transaction_message
puts "You have exceeded the max number of transactions for this month."
return balance
end

def give_withdrawal_message
puts "You may not withdraw any more money while balance is below #{Money.new(BALANCE_MINIMUM).format}"
return balance
end

def withdraw(money, fee = TRANSACTION_FEE, limit = 0) # balance can go below $10,000 but will cause withdrawals to freeze
check_transaction_status

if @can_transact == false
return give_transaction_message
elsif @can_withdraw == false
return give_withdrawal_message
end

super

if balance < BALANCE_MINIMUM
@can_withdraw = false
@balance -= 10000
return give_withdrawal_message
end

return balance
end

def deposit(money)
check_transaction_status

if @can_transact
super
else
return give_transaction_message
end

if balance > BALANCE_MINIMUM
@can_withdraw = true
end

return balance
end

def reset_transactions
@number_of_transactions = 0
end

end

end