Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6c66f0f
Account#initialize test passed.
janicewilson Feb 21, 2017
d352739
Raises an ArgumentError when created with a negative balance test pas…
janicewilson Feb 21, 2017
5164162
Can be created with a balance of 0 test passed.
janicewilson Feb 21, 2017
aa0734e
Account#withdraw test passed.
janicewilson Feb 21, 2017
6c6aa1c
"Returns the modified balance" and "Outputs a warning if the account …
janicewilson Feb 21, 2017
8b63bb6
"Allows the balance to go to 0" and "Requires a positive withdrawal a…
janicewilson Feb 22, 2017
b80a9c3
"Requires a positive withdrawal amount" test passes.
janicewilson Feb 22, 2017
78f2f94
"Increases the balance", "Returns the modified balance" and "Requires…
janicewilson Feb 22, 2017
44535b0
Added the optional. Attempted to add a "before" statement, but too ti…
janicewilson Feb 22, 2017
75faf68
Reboot re Wave 1. Committing without regrets.
janicewilson Feb 23, 2017
e3ffbb8
YAR (Yet Another Reboot.)
janicewilson Feb 23, 2017
3cc028d
All tests but the last have passed.
janicewilson Feb 24, 2017
85e624e
All tests but the last have passed.
janicewilson Feb 24, 2017
f69fa40
A fully sanitized Wave 2.
janicewilson Feb 24, 2017
f530f7b
Minitests for Savings Account \'initialize\' and \'withdraw\' success…
janicewilson Feb 24, 2017
16d4639
Added RB files for savings and checking account..
janicewilson Feb 24, 2017
fe911d4
Minitest /'add_interest/' passed for Saving Account.
janicewilson Feb 24, 2017
396ed54
Minitests re \'initialize\' and \'withdraw'\ updated.
janicewilson Feb 25, 2017
502cc28
Minitests re \'initialize\' and \'withdraw'\ updated.
janicewilson Feb 25, 2017
c26a9ed
All systems go. All mini-tests passed.
janicewilson Feb 25, 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
94 changes: 94 additions & 0 deletions lib/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
require 'csv'

module Bank

class Account

def self.all(file)

all_accounts = []

CSV.open(file).each do | line |

id = line[0].to_i
balance = line[1].to_i
date_opened = line[2]

account = Account.new(id, balance, date_opened)

all_accounts << account
end

all_accounts
end

def self.find(file, id)

all_accounts = Bank::Account.all(file)

all_accounts.each do | account |

if account.id == id

return account

end

end

raise ArgumentError.new "Account #{id} does not exist."

end

def self.find_with_index(file, index)

all_accounts = Bank::Account.all(file)

all_accounts.fetch(index)

end


attr_accessor :id, :balance, :date_opened

def initialize(id, balance, date_opened = nil)

raise ArgumentError.new "Can't create an account with a negative balance." unless balance >= 0

@id = id
@balance = balance
@date_opened = date_opened

end

def withdraw(withdrawal_amount)

raise ArgumentError.new "Please enter a withdrawal amount greater than 0." unless withdrawal_amount > 0

if @balance >= withdrawal_amount
@balance -= withdrawal_amount

else
puts "You haven't sufficient funds for withdrawal."
@balance

end

end

def deposit(deposit_amount)

raise ArgumentError.new "Please enter a deposit amount greater than 0." unless deposit_amount > 0

@balance += deposit_amount

end


end

end

find_account = Bank::Account.find("../support/accounts.csv", 1213)
puts find_account
puts find_account.class
98 changes: 98 additions & 0 deletions lib/checking_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
require 'csv'
require_relative 'account'

module Bank

class CheckingAccount < Account

attr_reader :counter, :check_withdrawal_fee

def initialize(id, balance, date_opened = nil)

super
@counter = 0
@check_withdrawal_fee

end

def withdraw(withdrawal_amount)

raise ArgumentError.new "Please enter a withdrawal amount greater than 0." unless withdrawal_amount > 0

withdrawal_fee = 1

if @balance >= withdrawal_amount + withdrawal_fee
@balance -= (withdrawal_amount + withdrawal_fee)

else
puts "You haven't sufficient funds for withdrawal."
@balance

end

end

def withdraw_with_check(withdrawal_amount)

raise ArgumentError.new "Please enter a withdrawal amount greater than 0." unless withdrawal_amount > 0

@counter += 1
deficit_limit = 10

if (1..3) === @counter then @check_withdrawal_fee = 0; end
if (4..10000) === @counter then @check_withdrawal_fee = 2; end

if @balance - (withdrawal_amount + @check_withdrawal_fee) >= -deficit_limit
@balance -= (withdrawal_amount + @check_withdrawal_fee)

else
puts "You haven't sufficient funds for withdrawal."
@balance

end

end

def reset_checks

@counter = 0

end

end

end



#The user is allowed three free check uses in one month, but any subsequent use adds a $2 transaction fee

#reset_checks: Resets the number of checks used to zero
#
janice = Bank::CheckingAccount.new(333, 300)

withdrawal_amount = 3

# 2.times do
#
# janice.withdraw_with_check(withdrawal_amount)
# puts janice.counter
# puts janice.balance
# puts janice.check_withdrawal_fee
#
# end
#
# janice.reset_checks
#
# puts janice.reset_checks.class
#
# puts "Reset: #{janice.reset_checks}"
#
# 2.times do
#
# janice.withdraw_with_check(withdrawal_amount)
# puts janice.counter
# puts janice.balance
# puts janice.check_withdrawal_fee
#
# end
59 changes: 59 additions & 0 deletions lib/savings_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'csv'
require_relative 'account'

module Bank

class SavingsAccount < Account

def initialize(id, balance)

raise ArgumentError.new "Starting balance must be $10 or more." until balance >= 10
super


end

def withdraw(withdrawal_amount)

raise ArgumentError.new "Please enter a withdrawal amount greater than 0." unless withdrawal_amount > 0

fee = 2

if @balance - (withdrawal_amount + fee) >= 12
@balance -= (withdrawal_amount + fee)

else
puts "You haven't sufficient funds for withdrawal."
return @balance

end

end

def add_interest(rate)

raise ArgumentError.new "Please enter an interest rate greater than 0.00." unless rate > 0.00

start_balance = @balance
@balance *= 1 + (rate/100)
interest_earned = start_balance * (rate/100)

end


end

end


#It should include the following new method:

#add_interest(rate): Calculate the interest on the balance and add the interest to the balance. Return the interest that was calculated and added to the balance (not the updated balance).
#Input rate is assumed to be a percentage (i.e. 0.25).
#The formula for calculating interest is balance * rate/100
#Example: If the interest rate is 0.25 and the balance is $10,000, then the interest that is returned is $25 and the new balance becomes $10,025.

# janice = Bank::SavingsAccount.new(333, 10)
#
# puts janice.add_interest(0.25)
# puts janice.balance
106 changes: 79 additions & 27 deletions specs/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'minitest/skip_dsl'
require_relative '../lib/account'

Minitest::Reporters.use!

describe "Wave 1" do
describe "Account#initialize" do
it "Takes an ID and an initial balance" do
Expand Down Expand Up @@ -137,35 +139,85 @@
end

# TODO: change 'xdescribe' to 'describe' to run these tests
xdescribe "Wave 2" do
describe "Account.all" do
it "Returns an array of all accounts" do
# TODO: Your test code here!
# Useful checks might include:
# - Account.all returns an array
# - Everything in the array is an Account
# - The number of accounts is correct
# - The ID and balance of the first and last
# accounts match what's in the CSV file
# Feel free to split this into multiple tests if needed
end
end
describe "Wave 2" do

describe "Account.find" do
it "Returns an account that exists" do
# TODO: Your test code here!
end
before do

it "Can find the first account from the CSV" do
# TODO: Your test code here!
end
@all_accounts = Bank::Account.all("../support/accounts.csv")

it "Can find the last account from the CSV" do
# TODO: Your test code here!
end
end

describe "Account.all" do

it "Returns an array of all accounts" do

@all_accounts.must_be_kind_of Array

end

it "The number of accounts is correct" do

@all_accounts.length.must_equal 12

end


it "Everything in the array is an Account" do

@all_accounts.each do | account |

account.must_be_kind_of Bank::Account

end

end


it "The ID and balance of the first and last accounts match what's in the CSV file" do

@all_accounts[0].id.must_equal 1212
@all_accounts[-1].id.must_equal 15156
@all_accounts[0].balance.must_equal 1235667
@all_accounts[-1].balance.must_equal 4356772

end

end

describe "Account.find" do

it "Returns an account that exists" do

find_account = Bank::Account.find("../support/accounts.csv", 1213)
find_account.must_be_kind_of Bank::Account
find_account.id.must_equal 1213

end

it "Can find the first account from the CSV" do

find_account = Bank::Account.find_with_index("../support/accounts.csv", 0)
find_account.must_be_kind_of Bank::Account
find_account.id.must_equal 1212

end

it "Can find the last account from the CSV" do

find_account = Bank::Account.find_with_index("../support/accounts.csv", -1)
find_account.must_be_kind_of Bank::Account
find_account.id.must_equal 15156

end

it "Raises an error for an account that doesn't exist" do

proc {find_account = Bank::Account.find("../support/accounts.csv", 3333)}.must_raise ArgumentError

proc {find_account = Bank::Account.find_with_index("../support/accounts.csv", 13)}.must_raise IndexError

end

end

it "Raises an error for an account that doesn't exist" do
# TODO: Your test code here!
end
end
end
Loading