Skip to content
Open
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
28f73f8
Create Bank module, Account class, and Owner class;
sophiabaldonado Feb 29, 2016
8f941aa
Updated comments for clarity;
sophiabaldonado Feb 29, 2016
9bae1db
Took out capitalize method that errors if an owner name isn't include…
sophiabaldonado Feb 29, 2016
42f2f0f
Created balance_inquiry method to return balance as a formatted string;
sophiabaldonado Mar 1, 2016
8cb3bea
Added dollar sign to formatting on balance_inquiry;
sophiabaldonado Mar 1, 2016
d9b5104
Reads a csv file and creates a hash with the account id as the key an…
sophiabaldonado Mar 2, 2016
cb839c5
Added an all method that does too much;
sophiabaldonado Mar 2, 2016
f95d63a
Created csv_info, accounts, and all methods;
sophiabaldonado Mar 2, 2016
9f16323
Accounts method uses methods instead of instance variables; csv_data …
sophiabaldonado Mar 2, 2016
22fce94
self.find checks if the value passed in is equal to the id of the fir…
sophiabaldonado Mar 2, 2016
2175a2a
Store account instances created in account method in a hash instead o…
sophiabaldonado Mar 2, 2016
908e10c
Just kidding accounts is an array again not a hash; Fixed find method…
sophiabaldonado Mar 2, 2016
f85673a
Convert balance from csv file to float so deposti and withdraw method…
sophiabaldonado Mar 2, 2016
8d068cd
Update Owner class initialize variables; Owner class method csv_data …
sophiabaldonado Mar 2, 2016
8452bc0
Owners class method makes new instances of Owner from the data in csv…
sophiabaldonado Mar 2, 2016
d3e8fad
Owners class takes a hash again; owners class method makes a hash for…
sophiabaldonado Mar 2, 2016
6edc946
Create self.find method for Owner class; Fix bug for find method in A…
sophiabaldonado Mar 2, 2016
65677ed
Import account owner data from csv file; self.find_owner Account clas…
sophiabaldonado Mar 3, 2016
329dff2
self.find.owner Account class method now finds associated owner for a…
sophiabaldonado Mar 3, 2016
9a541b5
Added comments for clarity;
sophiabaldonado Mar 3, 2016
fe59eb1
Account withdraw method now uses constants; Created SavingsAccount cl…
sophiabaldonado Mar 3, 2016
16628c7
Created add_interest method in SavingsAccount that returns amount of …
sophiabaldonado Mar 3, 2016
af4e87c
Created CheckingAccount class with unique withdraw_fee constant;
sophiabaldonado Mar 3, 2016
053257b
Created a method to withdraw from CheckingAccount with a check, uses …
sophiabaldonado Mar 4, 2016
2b78c31
Created find_accounts method to search for accounts owned by an owner…
sophiabaldonado Mar 4, 2016
14a09de
New MoneyMarketAccount; Added minimum initial balance constant for Ac…
sophiabaldonado Mar 4, 2016
cfc5cba
Added transaction count to base Account class, deposits and withdrawa…
sophiabaldonado Mar 4, 2016
a4ed65f
Changed some error and comment wording for clarity;
sophiabaldonado Mar 4, 2016
b4c4057
Renamed create accounts and create owners methods to .all because .al…
sophiabaldonado Mar 5, 2016
a9d50aa
Fix a namespace in a couple methods;
sophiabaldonado Mar 5, 2016
c812ca0
Fix some comparisons and use constant for minimum balance instead of …
sophiabaldonado 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
267 changes: 267 additions & 0 deletions bank_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
require 'CSV'

module Bank


class Account
MINIMUM_BALANCE = 0
WITHDRAW_FEE = 0
MIN_INITIAL_BALANCE = 0
attr_reader :id, :balance, :transaction_count

def initialize(id, initial_balance, opendate, owner=nil)
@id = id
@balance = initial_balance
check_initial_balance
@opendate = opendate
@owner = owner
@minimum_balance = MINIMUM_BALANCE
@withdraw_fee = WITHDRAW_FEE
@transaction_count = 0
end

def check_initial_balance
raise ArgumentError, "Starting balance must be a number." unless balance.is_a? Numeric
raise ArgumentError, "Starting balance is too low." unless balance >= self.class::MIN_INITIAL_BALANCE
end

# Creates an array containing data from "./support/accounts.csv" or another file
def self.csv_data(file_path="./support/accounts.csv")
CSV.read(file_path)
end

# Created an array containing account instances from the data in csv_data csv file
def self.all
all_accounts = []
csv_data.each_index do |i|
id = csv_data[i][0]
initial_balance = csv_data[i][1].to_f
opendate = csv_data[i][2]
all_accounts << self.new(id, initial_balance, opendate)
end
return all_accounts
end

# Find and return an Account instance when you pass in its account ID number
def self.find(find_id)
self.all.each_index do |i|
if self.all[i].id == find_id.to_s
return self.all[i]
end
end
end

# Create and array from the data in "./support/account_owners.csv" or other file
def self.csv_owner_data(file_path="./support/account_owners.csv")
CSV.read(file_path)
end

# Call on Bank::Account and pass in an account ID to get the associated Owner
def self.find_owner(account_id)
csv_owner_data.each_index do |i|
if csv_owner_data[i][0] == account_id.to_s
return Owner.find(csv_owner_data[i][1])
end
end
end

def withdraw_error(amount)
puts "Not enough money in the account."
end

def one_transaction
@transaction_count += 1
end

def reset_transaction_count
@transaction_count = 0
end

# Accepts a single parameter for the amount of money to be withdrawn.
# Absolute value to input for negative numbers.
# Returns the account balance.
def withdraw(amount)
amount = amount.abs
if (@balance - (amount + @withdraw_fee)) < @minimum_balance
withdraw_error(amount)
else
@balance = @balance - (amount + @withdraw_fee)
one_transaction
end
return balance
end

# Accepts a single parameter for the amount of money to be deposited.
# Absolute value to input for negative numbers.
# Returns the account balance.
def deposit(amount)
amount = amount.abs
@balance = @balance + amount
one_transaction
return @balance
end

# Call this method for a people friendly view of account balance
def balance_inquiry
"$#{'%.2f' % @balance}"
end

def add_interest(rate)
interest = balance * rate/100
@balance = @balance + interest
return interest
end
end


class SavingsAccount < Account
MINIMUM_BALANCE = 10
WITHDRAW_FEE = 2
MIN_INITIAL_BALANCE = 10
attr_reader :balance, :minimum_initial_balance
def initialize(id, initial_balance, opendate, owner=nil)
super
@minimum_balance = MINIMUM_BALANCE
@withdraw_fee = WITHDRAW_FEE
end

end


class CheckingAccount < Account
WITHDRAW_FEE = 1

attr_reader :used_check, :reset_checks
def initialize(id, initial_balance, opendate, owner=nil)
super
@withdraw_fee = WITHDRAW_FEE
@used_check = 0
end

def reset_checks
@used_check = 0
end

def withdraw_using_check(amount)
@minimum_balance = -10
if used_check >= 3
@withdraw_fee = 2
withdraw(amount)
else
@withdraw_fee = 0
withdraw(amount)
@used_check += 1
end
return balance
end

def add_interest(rate)
raise ArgumentError, "Cannot add interest on checking account."
end
end


class MoneyMarketAccount < Account
MINIMUM_BALANCE = 10000
MIN_INITIAL_BALANCE = 10000

def initialize(id, initial_balance, opendate, owner=nil)
super
@minimum_balance = MINIMUM_BALANCE
end

def withdraw_error(amount)
@balance = @balance - (amount + @withdraw_fee + 100)
one_transaction
return balance
end

def balance_too_low
raise ArgumentError, "Balance is under $10,000" unless balance >= @minimum_balance
end

def maximum_transaction_check
if transaction_count >= 6
raise ArgumentError, "Too many transactions this month."
end
end

def withdraw(amount)
balance_too_low
maximum_transaction_check
super
end

def deposit(amount)
if balance < @minimum_balance
super
@transaction_count -= 1
return balance
else
maximum_transaction_check
super
end
end

end


class Owner
attr_reader :id, :first_name, :last_name, :address
def initialize(owner_hash)
@id = owner_hash[:id]
@last_name = owner_hash[:last_name]
@first_name = owner_hash[:first_name]
@address = [owner_hash[:street], owner_hash[:city], owner_hash[:state]]
end

# Create an array from the data in "./support/owners.csv" or another file
def self.csv_data(file_path="./support/owners.csv")
CSV.read(file_path)
end

# Creates an array of Owner instances, each of which was created from the data
# in csv_data after it was placed in owner hash
def self.all
all_owners = []
csv_data.each_index do |i|
owner = {
id: csv_data[i][0],
last_name: csv_data[i][1],
first_name: csv_data[i][2],
street: csv_data[i][3],
city: csv_data[i][4],
state: csv_data[i][5]
}
all_owners << self.new(owner)
end
return all_owners
end

# Finds and returns an Owner instance if you pass in its ID number
def self.find(find_id)
self.all.each_index do |i|
if self.all[i].id == find_id.to_s
return self.all[i]
end
end
end

# Create and array from the data in account_owners.csv
def self.csv_owner_data(file_path="./support/account_owners.csv")
CSV.read(file_path)
end

# Use accounts_owner.csv array to find an owner id that matches input then
# returns the correlating accounts
def self.find_accounts(owner_id)
csv_owner_data.each_index do |i|
if csv_owner_data[i][1] == owner_id.to_s
return Account.find(csv_owner_data[i][0])
end
end
end

end

end