Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
a48ccb3
Test 1 passed
kellysouza Feb 21, 2017
f7335b7
Working through spec line 34
kellysouza Feb 21, 2017
01232e2
withdrawal returns new balance
kellysouza Feb 21, 2017
a282d51
withdrawals working
kellysouza Feb 22, 2017
93ad9c6
Wave 1 working through
kellysouza Feb 22, 2017
fa8a55f
updates account methods
kellysouza Feb 22, 2017
2b8cd07
Creates account array
kellysouza Feb 22, 2017
219ee46
account id and balance match
kellysouza Feb 23, 2017
b2790ba
Begin self.find(id) method
kellysouza Feb 23, 2017
70999f7
Find method finds first and last
kellysouza Feb 23, 2017
2b6ea00
Find method: start raise error
kellysouza Feb 23, 2017
8871d02
Add error to .find class method
kellysouza Feb 23, 2017
6542824
add third paramater, timedate, to account initialization
kellysouza Feb 23, 2017
a3a4f68
Cleaning up based on Chris' exapmle
kellysouza Feb 23, 2017
d6c24ec
Cleanup finished
kellysouza Feb 23, 2017
cafbe5c
adds ownerclass #commented out
kellysouza Feb 23, 2017
fd9a233
Set up checking account class and initialize
kellysouza Feb 24, 2017
503a5d0
Add some feature to withraw from checking method
kellysouza Feb 24, 2017
37342d4
Add working withdarawal, begin reset_checks method
kellysouza Feb 24, 2017
cf429e2
reset checks called without error
kellysouza Feb 24, 2017
120e505
reset checks makes three checks free if less than 3 checks
kellysouza Feb 24, 2017
f755ef1
Begin Savings Account
kellysouza Feb 24, 2017
39684b1
Working initialize for savings account
kellysouza Feb 24, 2017
7e3c84a
applies fee with SA withdrawal
kellysouza Feb 24, 2017
af9631d
Adds low balance worning on SA
kellysouza Feb 24, 2017
5d4754d
add @fee to super
kellysouza Feb 25, 2017
2562480
refactoring into super: withdrawal
kellysouza Feb 25, 2017
4bc304a
good through all but last two SA tests
kellysouza Feb 25, 2017
1b474c5
Withdraw complete and passing
kellysouza Feb 25, 2017
d86b66a
Working interest method
kellysouza Feb 25, 2017
dd6ca22
All Workinggit add .
kellysouza Feb 25, 2017
7d255b5
DRYing up code
kellysouza Feb 25, 2017
9393cde
Drying up code
kellysouza Feb 25, 2017
111dcbc
Minor changes
kellysouza Feb 25, 2017
4af2a33
Final(should be)
kellysouza 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
71 changes: 71 additions & 0 deletions lib/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Create a class inside of a module
# Create methods inside the class to perform actions
# Learn how Ruby does error handling
# Verify code correctness by testing
require 'csv'

module Bank
class Account
attr_reader :id, :balance, :timedate

def initialize(id, balance, timedate = nil)
@min_opening_bal = 0
@min_bal = 0
@balance = balance
check_opening_bal
@id = id
@timedate = timedate
@fee = 0
end

def self.all
account_array = []
CSV.read("support/accounts.csv").each do |account|
account_array << (Account.new(account[0], account[1].to_i/100.0, account[2]))
end
account_array
end

# self.find(id) - returns an instance of Account
# where the value of the id field in the CSV matches
# the passed parameter.
def self.find(id)
account_array = Bank::Account.all
account_array.each do |account|
if id == account.id
return account
end
end
raise ArgumentError.new "Account #{id} does not exist"
end

def check_opening_bal
raise ArgumentError.new "Opening balance must be greater than #{@min_opening_bal}" if @balance < @min_opening_bal
end

def withdraw(withdrawal_amount)
check_for_negative(withdrawal_amount)
adjust_if_no_low_balance(withdrawal_amount)
@balance
end

def deposit(deposit_amount)
raise ArgumentError.new "You must deposit an amount" if deposit_amount < 0
@balance += deposit_amount
end

def adjust_if_no_low_balance(withdrawal_amount)
if withdrawal_amount > (@balance - @min_bal)
puts "Warning low balance!"
else
@balance -= (withdrawal_amount + @fee)
end
@balance
end

def check_for_negative(withdrawal_amount)
raise ArgumentError.new "You cannot withdraw a negative amount" if withdrawal_amount < 0
end

end #class account
end # module Bank
39 changes: 39 additions & 0 deletions lib/checking_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require_relative 'account'

module Bank
class CheckingAccount < Account

def initialize(id, balance, timedate = nil)
super
@number_of_checks = 0
@fee = 1
@check_fee = 0
@min_bal = -10
check_opening_bal
end

def withdraw_using_check(withdrawal_amount)
@fee = 0
check_for_negative(withdrawal_amount)
adjust_if_no_low_balance(withdrawal_amount)
count_check
charge_fee_if_appropriate(3, withdrawal_amount)
end

def charge_fee_if_appropriate(check_limit, withdrawal_amount)
if @number_of_checks > check_limit && withdrawal_amount < (@balance - @min_bal)
@check_fee = 2
@balance -= @check_fee
end
return @balance
end

def count_check
@number_of_checks += 1
end

def reset_checks
@number_of_checks = 0
end
end#class CheckingAccount
end#module Bank
26 changes: 26 additions & 0 deletions lib/savings_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require_relative 'account'

module Bank
class SavingsAccount < Account

def initialize(id, balance, timedate = nil)
super
@min_opening_bal = 10
@min_bal = 10
check_opening_bal
end

def withdraw(withdrawal_amount)
@min_bal = 10
@fee = 2
super
end

def add_interest(rate, time_in_months)
raise ArgumentError.new "Please provide a positive rate" if rate <= 0
interest = @balance * rate/100 * time_in_months
@balance += interest
interest
end
end#class SavingsAccount
end#module Bank
112 changes: 84 additions & 28 deletions specs/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
it "Takes an ID and an initial balance" do
id = 1337
balance = 100.0
account = Bank::Account.new(id, balance)
timedate = "1999-03-27 11:30:09 -0800"
account = Bank::Account.new(id, balance, timedate)

account.must_respond_to :id
account.id.must_equal id

account.must_respond_to :balance
account.balance.must_equal balance

account.must_respond_to :timedate
account.timedate.must_equal timedate


end

it "Raises an ArgumentError when created with a negative balance" do
Expand All @@ -23,32 +29,30 @@
# This code checks that, when the proc is executed, it
# raises an ArgumentError.
proc {
Bank::Account.new(1337, -100.0)
Bank::Account.new(1337, -100.0, "1999-03-27 11:30:09 -0800")
}.must_raise ArgumentError
end

it "Can be created with a balance of 0" do
# If this raises, the test will fail. No 'must's needed!
Bank::Account.new(1337, 0)
Bank::Account.new(1337, 0, "1999-03-27 11:30:09 -0800")
end
end

describe "Account#withdraw" do
it "Reduces the balance" do
start_balance = 100.0
withdrawal_amount = 25.0
account = Bank::Account.new(1337, start_balance)

account = Bank::Account.new(1337, start_balance, "1999-03-27 11:30:09 -0800")
account.withdraw(withdrawal_amount)

expected_balance = start_balance - withdrawal_amount
account.balance.must_equal expected_balance
end

it "Returns the modified balance" do
start_balance = 100.0
withdrawal_amount = 25.0
account = Bank::Account.new(1337, start_balance)
account = Bank::Account.new(1337, start_balance, "1999-03-27 11:30:09 -0800")

updated_balance = account.withdraw(withdrawal_amount)

Expand All @@ -59,7 +63,7 @@
it "Outputs a warning if the account would go negative" do
start_balance = 100.0
withdrawal_amount = 200.0
account = Bank::Account.new(1337, start_balance)
account = Bank::Account.new(1337, start_balance, "1999-03-27 11:30:09 -0800")

# Another proc! This test expects something to be printed
# to the terminal, using 'must_output'. /.+/ is a regular
Expand All @@ -73,7 +77,7 @@
it "Doesn't modify the balance if the account would go negative" do
start_balance = 100.0
withdrawal_amount = 200.0
account = Bank::Account.new(1337, start_balance)
account = Bank::Account.new(1337, start_balance, "1999-03-27 11:30:09 -0800")

updated_balance = account.withdraw(withdrawal_amount)

Expand All @@ -84,7 +88,7 @@
end

it "Allows the balance to go to 0" do
account = Bank::Account.new(1337, 100.0)
account = Bank::Account.new(1337, 100.0, "1999-03-27 11:30:09 -0800")
updated_balance = account.withdraw(account.balance)
updated_balance.must_equal 0
account.balance.must_equal 0
Expand All @@ -93,7 +97,7 @@
it "Requires a positive withdrawal amount" do
start_balance = 100.0
withdrawal_amount = -25.0
account = Bank::Account.new(1337, start_balance)
account = Bank::Account.new(1337, start_balance, "1999-03-27 11:30:09 -0800")

proc {
account.withdraw(withdrawal_amount)
Expand All @@ -105,7 +109,7 @@
it "Increases the balance" do
start_balance = 100.0
deposit_amount = 25.0
account = Bank::Account.new(1337, start_balance)
account = Bank::Account.new(1337, start_balance, "1999-03-27 11:30:09 -0800")

account.deposit(deposit_amount)

Expand All @@ -116,7 +120,7 @@
it "Returns the modified balance" do
start_balance = 100.0
deposit_amount = 25.0
account = Bank::Account.new(1337, start_balance)
account = Bank::Account.new(1337, start_balance, "1999-03-27 11:30:09 -0800")

updated_balance = account.deposit(deposit_amount)

Expand All @@ -127,7 +131,7 @@
it "Requires a positive deposit amount" do
start_balance = 100.0
deposit_amount = -25.0
account = Bank::Account.new(1337, start_balance)
account = Bank::Account.new(1337, start_balance, "1999-03-27 11:30:09 -0800")

proc {
account.deposit(deposit_amount)
Expand All @@ -136,36 +140,88 @@
end
end

# TODO: change 'xdescribe' to 'describe' to run these tests
xdescribe "Wave 2" do



describe "Wave 2" do
describe "Account.all" do

before do
@account_array = Bank::Account.all
end


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
@account_array.must_be_instance_of Array

end
# Useful checks might include:

# - The number of accounts is correct
it "The number of accounts is correct" do
@account_array.length.must_equal CSV.read("support/accounts.csv").length
end
# - account is an Array
it "account is an Array" do
@account_array.class.must_equal Array
end

# - Everything in the array is an Account
it "Everything in the array is an Account" do
@account_array.each {|account| account.class.must_equal Bank::Account}
end

# - The ID and balance of the first and last
# accounts match what's in the CSV file
it " accounts match what's in the CSV file" do
index = 0
CSV.read("support/accounts.csv") do |line|
accounts[index].id.must_equal line[0].to_i
accounts[index].id.must_equal line[1].to_i
accounts[index].id.must_equal line[2].to_i
index += 1
end
end

it "The ID and balance of the first and last match csv" do
@account_array.first.id.must_equal "1212"
@account_array.first.balance.must_equal 12356.67
@account_array.last.id.must_equal "15156"
@account_array.last.balance.must_equal 43567.72
end
end


describe "Account.find" do
before do
@test_array = Bank::Account.all
end
# self.find(id) - returns an instance of Account
# where the value of the id field in the CSV matches
# the passed parameter.
it "Returns an account that exists" do
# TODO: Your test code here!
test_variable = Bank::Account.find("1212")
test_variable.must_be_instance_of Bank::Account
test_variable.id.must_equal "1212"
end

it "Can find the first account from the CSV" do
# TODO: Your test code here!
Bank::Account.find(@test_array[0].id).id.must_equal "1212"
end

it "Can find the last account from the CSV" do
# TODO: Your test code here!
Bank::Account.find(@test_array[-1].id).id.must_equal "15156"
end

it "Raises an error for an account that doesn't exist" do
# TODO: Your test code here!
proc {
Bank::Account.find("0000")
}.must_raise ArgumentError
end
end
describe "Account.find" do
before do @test_array = Bank::Account.all
end
end

end
Loading