Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ad56ad4
Initial set up of Account class
botrethewey Feb 21, 2017
c1499be
WIP: added owner class and added few tests for it
botrethewey Feb 22, 2017
ea721dc
Reduced the number of attributes in Owner class
botrethewey Feb 22, 2017
f0cfdac
Handles empty hash passed to the construtor
botrethewey Feb 22, 2017
83f1639
Worked out updating account owner data
botrethewey Feb 22, 2017
b1fdd0f
Added tests for the added owner property
botrethewey Feb 22, 2017
acf6fa0
Added test for .self.all class method
botrethewey Feb 23, 2017
9c0b3b7
updated Account class attributes
botrethewey Feb 23, 2017
39554cd
Working baseline
botrethewey Feb 23, 2017
4787765
Working wave2 with completed optional and tests
botrethewey Feb 23, 2017
cd7190d
Created SavingsAccount and CheckingAccount file
botrethewey Feb 23, 2017
6eb336c
Corrected file name
botrethewey Feb 24, 2017
8e13e8d
Withdraw method added to savings account, added working test
botrethewey Feb 24, 2017
5819ab3
Added tests for add_interest method
botrethewey Feb 24, 2017
3d67d3b
Savings account working version for code and tests
botrethewey Feb 24, 2017
e0ea43b
Finished Wave 3 baseline
botrethewey Feb 24, 2017
1c1022d
removed xdescribe
botrethewey Feb 24, 2017
f0ecccf
Added files for optional wave 3
botrethewey Feb 24, 2017
5d02ae6
Added tests for MoneyMarketAccount
botrethewey Feb 24, 2017
7af41f6
Finished tests and cleaned up comments
botrethewey Feb 26, 2017
085dc8a
Edited indentations and spellings
botrethewey Feb 26, 2017
6f15881
Updated Money
botrethewey Feb 27, 2017
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
100 changes: 100 additions & 0 deletions lib/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
require 'csv'
require 'date'

module Bank
class Account
# Allows access to the current balance of an account at any time.
attr_accessor :balance, :owner
# Only allow reader on unique account id, and opendate
attr_reader :id, :open_date

# Constructs a new Account object
# Give a default value, in case the Owner class object is not passed
# Assumes passed parameters are formated in their correct data type.
def initialize id, balance, open_date, owner = nil
# error handling for initial negative balance
if balance >= 0
@balance = balance
else
raise ArgumentError.new "Inital balance cannot be a negetive value"
end

@id = id
@open_date = DateTime.parse(open_date)

# Assumes that required csv file is accesible
CSV.read("support/account_owners.csv").each do |line|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop is problematic. It's reading the file and each find is reading another file each time. The find methods should probably be scanning through the Arrays provided by .all so you don't have to repeat things over and over again.

if line[0].to_i == @id
@owner = Bank::Owner.find(line[1].to_i)
end
end

if owner.class == Bank::Owner
@owner = owner
else
# Default instance of the Owner class initialized with empty hash
@owner = Bank::Owner.new({})
end
end

# Method that returns a collection of Account instances, from data read in CSV
def self.all
all_accounts_array= []

CSV.read("support/accounts.csv").each do |line|
all_accounts_array << Bank::Account.new( line[0].to_i, line[1].to_i, line[2] )
end
return all_accounts_array
end

# Method that returns an instance of an Account class, where the value of the id field in the CSV matches the passed parameter
def self.find(id)
raise ArgumentError.new ("Account id must be a positive integer value") if ( id.class != Integer || id < 1 )

CSV.read("support/accounts.csv").each do |line|
if line[0].to_i == id
account = Bank::Account.new( line[0].to_i, line[1].to_i, line[2])
return account
end
end
raise ArgumentError.new "Account id does not exist in the database"
end


# Method that overwrites existing empty @owner instance variable
def update_owner_data(owner_hash)
# Only overwrite if initially not added to account at the time of initializing account object
if @owner.id == 0
@owner = Bank::Owner.new(owner_hash)
end
end

# Method that handles withdraw
def withdraw(withdraw_amount)
# Error handling for insufficient funds for a withdraw
raise ArgumentError.new ("Withdraw amount must be a positive numericla value") if ( !(withdraw_amount.class == Integer || withdraw_amount.class == Float) || withdraw_amount < 0 )
# Insufficient funds
if @balance < withdraw_amount
puts "You do not have sufficient funds to withdraw the entered amount"
# Negative withdraw amount, invalid
elsif withdraw_amount < 0
raise ArgumentError.new "Withdraw amount cannot be a negetive value"
# Allow withdraw and update the balance
else
@balance -= withdraw_amount
end
return @balance
end

# Method that handles deposits
def deposit(money_amount)
# Negative deposit amount, invalid
if money_amount < 0
raise ArgumentError.new "Deposit amount cannot be a negetive value"
else
@balance += money_amount
return @balance
end
end
end
end
66 changes: 66 additions & 0 deletions lib/checking_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require 'csv'
require_relative 'account'

module Bank
class CheckingAccount < Bank::Account
attr_accessor :new_month
attr_reader :num_checks_used

def initialize(id, balance, open_date)
super(id, balance, open_date)
@num_checks_used = 0
@new_month = false
end

# Method that handles withdraw
def withdraw(withdraw_amount)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you be able to use super in this method to avoid replicating existing functionality.

# raise error for erroneous withdraw value
raise ArgumentError.new ("Withdraw amount must be a positive numericla value") if ( !(withdraw_amount.class == Integer || withdraw_amount.class == Float) || withdraw_amount < 0 )

# Error handling for insufficient funds for a withdraw
transaction_fee = 100

# Balance is insufficient
if @balance <= (withdraw_amount + transaction_fee)
puts "The amount you enetered for the withdraw will cause the balance to go below the $0.00"
# Ok to withdraw, updates the balance
else
@balance -= (withdraw_amount + transaction_fee)
end
return @balance
end

# Method that handles withdraw_using_check
def withdraw_using_check(withdraw_amount)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to use withdraw in this method to save code.

# raise error for non integer withdraw value
raise ArgumentError.new ("Withdraw amount must be a positive numerical value") if ( !(withdraw_amount.class == Integer || withdraw_amount.class == Float) || withdraw_amount < 0 )

# Error handling for insufficient funds for a withdraw
transaction_fee = 100
allotted_negative_balance = -1000
check_fee = 0

if @num_checks_used >= 3
check_fee = 200
end
# Balance is insufficient
if @balance < (withdraw_amount + transaction_fee + check_fee + allotted_negative_balance)
puts "The amount you enetered for the withdraw will cause the balance to go over the allotted_negative_balance of -$10.00"
# Ok to withdraw, update @balance
else
@balance -= (withdraw_amount + transaction_fee + check_fee)
@num_checks_used += 1
end
return @balance
end

# Resets the number of checks used to zero
def reset_checks()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new month functionality wasn't in the requirements and it's making your reset_checks method more cumbersome than it needs to be.

if @new_month
@num_checks_used = 0
@new_month = false
end
end

end
end
64 changes: 64 additions & 0 deletions lib/money_market_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'csv'
require_relative 'savings_account'

module Bank
class MoneyMarketAccount < Bank::SavingsAccount
attr_accessor :new_month, :num_transaction

def initialize(id, balance, open_date)
raise ArgumentError.new("Your initial balance must be at least $10,000.00") if balance < 1000000
super(id, balance, open_date)
@num_transaction = 0
@new_month = false
end

def withdraw(withdraw_amount)
# Raise error for non integer withdraw value
raise ArgumentError.new ("Withdraw amount must be a positive numerical value") if ( !(withdraw_amount.class == Integer || withdraw_amount.class == Float) || withdraw_amount < 0 )
minimum_balance = 1000000
penalty_fee = 0

# Withdraw allowed only if num_transaction is less than 6 AND balance is above minimum_balance
if @num_transaction < 6 && @balance >= minimum_balance
# Withdraw_amount will cause balance to go below minimum_balance
if @balance < withdraw_amount + minimum_balance
penalty_fee = 10000
end
@balance -= (withdraw_amount + penalty_fee)
@num_transaction += 1
end
return @balance
end

# Method that handles interest
def add_interest(rate)
super(rate)
end

# Method that handles deposits
def deposit(money_amount)
# Negative deposit amount, invalid
if money_amount < 0
raise ArgumentError.new "Deposit amount cannot be a negetive value"
elsif @num_transaction < 6
if @balance >= 1000000
@num_transaction += 1
end
@balance += money_amount
end
return @balance
end

# Resets the number of checks used to zero
def reset_transactions()
if @new_month
if @num_transaction == 0
puts "You haven't made any transactions yet."
else
@num_transaction = 0
end
@new_month = false
end
end
end
end
67 changes: 67 additions & 0 deletions lib/owner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module Bank
class Owner
attr_reader :id
attr_accessor :last_name, :first_name, :street_address, :city, :state

def initialize owner_hash = {}
# Assumes that the parameter value is either properly formatted hash or an empty hash
if owner_hash != {}
[:id, :last_name, :first_name, :street_address, :city, :state].each do |expected_key|
if !owner_hash.has_key?(expected_key)
raise ArgumentError.new "Hey you are missing #{expected_key} information for the owner!"
end
end
@id = owner_hash[:id]
@first_name = owner_hash[:first_name]
@last_name = owner_hash[:last_name]
@street_address = owner_hash[:street_address]
@city = owner_hash[:city]
@state = owner_hash[:state]
else
@id = 0
end
end

# Method that returns a collection of Owner instances, from data read in CSV
def self.all
all_owners_array= []

CSV.read("support/owners.csv").each do |line|
all_owners_array << Bank::Owner.new(
{
id: line[0].to_i,
last_name: line[1],
first_name: line[2],
street_address: line[3],
city: line[4],
state: line[5]
}
)
end

return all_owners_array
end

# Method that returns an instance of an Owner where the value of the id field in the CSV matches the passed parameter
def self.find(id)
raise ArgumentError.new ("Account id must be an positive integer value") if ( id.class != Integer || id < 1 )

CSV.read("support/owners.csv").each do |line|
if line[0].to_i == id
owner = Bank::Owner.new(
{
id: line[0].to_i,
last_name: line[1],
first_name: line[2],
street_address: line[3],
city: line[4],
state: line[5]
}
)
return owner
end
end
raise ArgumentError.new "Owner id does not exist in the database"
end
end
end
41 changes: 41 additions & 0 deletions lib/savings_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'csv'
require_relative 'account'

module Bank
class SavingsAccount < Bank::Account

def initialize(id, balance, open_date)
raise ArgumentError.new("Your initial balance must be at least $10.00") if balance < 1000
super(id, balance, open_date)
end

# Method that handles withdraw
def withdraw(withdraw_amount)
# Raise error for non integer withdraw value
raise ArgumentError.new ("Withdraw amount must be a positive numerical value") if ( !(withdraw_amount.class == Integer || withdraw_amount.class == Float) || withdraw_amount < 0 )
# Raises error for negativd withdraw value
raise ArgumentError.new("Withdraw amount cannot be a negetive value") if withdraw_amount < 0

# Error handling for insufficient funds for a withdraw
transaction_fee = 200
minimum_balance = 1000

# Balance is insufficient
if @balance <= (withdraw_amount + minimum_balance + transaction_fee)
puts "The amount you enetered for the withdraw will cause your balance to go below the $10.00 minimum balance required"
# Allows withdraw, update @balance
else
@balance -= (withdraw_amount + transaction_fee)
end
return @balance
end

# Method that handles interest
def add_interest(rate)
raise ArgumentError.new("The interest rate must be a positive numerical value") if rate <= 0
interest = @balance * rate
@balance += interest
return interest
end
end
end
Loading