Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4c6f7db
Started Bank module
habypsow Feb 21, 2017
c7f1bea
Passing withdrawal tests
habypsow Feb 22, 2017
fc87609
need to add withdrawal
habypsow Feb 22, 2017
ff71675
Wave 1 done
habypsow Feb 22, 2017
d7ba438
Added self.all class, starting to write tests
habypsow Feb 23, 2017
5803629
Starting tests, wrote find.all method
habypsow Feb 23, 2017
b28a680
Added more tests in .all method
habypsow Feb 23, 2017
a57d631
more tests added
habypsow Feb 23, 2017
c9eca1b
Finished round 1 tests. Moving on to Find.all tests
habypsow Feb 23, 2017
e210de6
Passed first find test
habypsow Feb 23, 2017
3651023
Wave 2 primary done
habypsow Feb 23, 2017
fbd9277
Fixed errors I was having with Wave 2 and adding inheritance to check…
habypsow Feb 24, 2017
4a88a92
Set crazy warnings in Rakefile to false
habypsow Feb 24, 2017
e6c979c
Working on withdraw_with_check method
habypsow Feb 24, 2017
0881911
Started Savings Account Inheritance, still working on Checking Accoun…
habypsow Feb 24, 2017
897a05e
SavingsAccount withdraw method initialized
habypsow Feb 24, 2017
c9db8c4
Working on Savings Account, getting failure with balance below 0 test.
habypsow Feb 24, 2017
1671c8b
Working on savings_account, finished withdraw method
habypsow Feb 24, 2017
f9af0b6
Updated interest calculation for savings_account
habypsow Feb 24, 2017
5ddf712
savingd_account.rb primary potentially done
habypsow Feb 24, 2017
44d888c
refined withdraw_with_check method for checking_account.rb
habypsow Feb 24, 2017
e6a8ccc
Refactor withdraw_with_check method in checking_account.rb
habypsow Feb 24, 2017
dea67d0
Refactored withdraw_with_check method in checking_account.rb, with no…
habypsow Feb 24, 2017
0c0d33f
Primary Requirements Done
habypsow Feb 25, 2017
6a90016
Bank Accounts submitting
habypsow Feb 27, 2017
3a8e0e8
Removed extraneous interest variable from savings_account
habypsow 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
Binary file added .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = true
t.warning = false
t.test_files = FileList['specs/*_spec.rb']
end

Expand Down
58 changes: 58 additions & 0 deletions lib/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require 'csv'
module Bank


class Account

def self.all
account = []
CSV.open("/Users/habsr/ada/projects/BankAccounts/support/accounts.csv", "r").each do |file|
id = Integer(file[0])

Choose a reason for hiding this comment

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

To make it so it'll work no matter where you clone the repo this should be:

CSV.open("support/accounts.csv", "r").each do |file|

balance = Integer(file[1])
open_date = file[2]
new_account = Account.new(id, balance, open_date)
account << new_account
end
return account
end

def self.find(id)
Account.all.each do |account|
if account.id == id
return account
end
end
puts "That account Does Not Exist"
end

attr_reader :id, :balance
def initialize(id, balance, open_date = nil)
@id = id
@balance = balance
@open_date = open_date
if @balance < 0
raise ArgumentError.new "The starting balance cannot be negative"
end
end

def withdraw(withdrawal_amount)
if withdrawal_amount > @balance
puts "Warning, the balance cannot be negative"
@balance = @balance
elsif withdrawal_amount < 0
raise ArgumentError.new "You cannot withdraw a negative amount"
else
@balance -= withdrawal_amount
end
end

def deposit(deposit_amount)
#raise ArgumentError.new("amount must be >= 0") if amount < 0
if deposit_amount < 0
raise ArgumentError.new "The deposit must be greater than 0"
else
@balance += deposit_amount
end
end
end
end
47 changes: 47 additions & 0 deletions lib/checking_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'csv'
require_relative '../lib/account'

module Bank

class CheckingAccount < Account
attr_reader :id, :balance
def initialize(id, balance)
@id = id

Choose a reason for hiding this comment

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

super would be swell here!

@balance = balance
@check_num = 3

def withdraw(withdrawal_amount)

Choose a reason for hiding this comment

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

I don't see any testing for this method...

check_balance = @balance - (withdrawal_amount + 1)
if check_balance < 0
puts "Warning, this will be below 0 , " + (@balance.to_s)
else
super + (-1)

Choose a reason for hiding this comment

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

Same problem with CheckingAccount earlier.

end
end

def withdraw_with_check(withdrawal_amount)
if withdrawal_amount < 0
raise ArgumentError.new "You cannot withdraw a negative amount"
elsif (@balance - withdrawal_amount) <= -11
puts "Warning, the balance cannot be negative "
return @balance
else
withdrawal_fee = 0
@balance -= withdrawal_amount + withdrawal_fee
@check_num -= 1
if @check_num < 0
withdrawal_fee = 2
@balance -= withdrawal_fee
end
end
end
end

def reset_checks
if @check_num <= 0
@check_num = 3
end
end

end
end
10 changes: 10 additions & 0 deletions lib/owner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Bank
class Owner

def initialize(owner_hash)
@name = owner_hash[:name]
@address = owner_hash[:address]
@account_number = owner_hash[:account_number]
end
end
end
36 changes: 36 additions & 0 deletions lib/savings_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'csv'
require_relative '../lib/account'

module Bank

class SavingsAccount < Account
attr_reader :id, :balance, :interest

def initialize(id, balance)

Choose a reason for hiding this comment

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

You can probably use super to set the instance variables.

raise ArgumentError.new "Balance must be at least $10" unless balance >= 10
@id = id
@balance = balance
end

def withdraw(withdrawal_amount)
temp_balance = @balance - (withdrawal_amount + 2)
if temp_balance < 10
puts "Warning, this will make your balance below your mininum , the current balance is " + (@balance.to_s)
@balance
else
super + (-2)

Choose a reason for hiding this comment

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

This should probably be:

super(withdrawal_amount + 2) because you're taking the return value of the Account class' withdraw method and subtracting two from it. You're not changing @balance in the end.

Let me know if this is confusing.

end
end

def add_interest(rate)
if rate < 0
raise ArgumentError.new "The interest rate cannot be negative"
else
interest = @balance * (rate / 100).to_f
@balance += interest
return interest
end
end
end
end
87 changes: 43 additions & 44 deletions specs/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require_relative '../lib/account'

describe "Wave 1" do

describe "Account#initialize" do
it "Takes an ID and an initial balance" do
id = 1337
Expand All @@ -12,23 +13,18 @@

account.must_respond_to :id
account.id.must_equal id

account.must_respond_to :balance
account.balance.must_equal balance
end

it "Raises an ArgumentError when created with a negative balance" do
# Note: we haven't talked about procs yet. You can think
# of them like blocks that sit by themselves.
# This code checks that, when the proc is executed, it
# raises an ArgumentError.
proc {
Bank::Account.new(1337, -100.0)
}.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)
end
end
Expand All @@ -38,9 +34,7 @@
start_balance = 100.0
withdrawal_amount = 25.0
account = Bank::Account.new(1337, start_balance)

account.withdraw(withdrawal_amount)

expected_balance = start_balance - withdrawal_amount
account.balance.must_equal expected_balance
end
Expand All @@ -51,7 +45,6 @@
account = Bank::Account.new(1337, start_balance)

updated_balance = account.withdraw(withdrawal_amount)

expected_balance = start_balance - withdrawal_amount
updated_balance.must_equal expected_balance
end
Expand All @@ -61,13 +54,8 @@
withdrawal_amount = 200.0
account = Bank::Account.new(1337, start_balance)

# Another proc! This test expects something to be printed
# to the terminal, using 'must_output'. /.+/ is a regular
# expression matching one or more characters - as long as
# anything at all is printed out the test will pass.
proc {
account.withdraw(withdrawal_amount)
}.must_output /.+/
proc { account.withdraw(withdrawal_amount)
}.must_output(/.+/)
end

it "Doesn't modify the balance if the account would go negative" do
Expand All @@ -76,15 +64,13 @@
account = Bank::Account.new(1337, start_balance)

updated_balance = account.withdraw(withdrawal_amount)

# Both the value returned and the balance in the account
# must be un-modified.
updated_balance.must_equal start_balance
account.balance.must_equal start_balance
end

it "Allows the balance to go to 0" do
account = Bank::Account.new(1337, 100.0)

updated_balance = account.withdraw(account.balance)
updated_balance.must_equal 0
account.balance.must_equal 0
Expand All @@ -108,7 +94,6 @@
account = Bank::Account.new(1337, start_balance)

account.deposit(deposit_amount)

expected_balance = start_balance + deposit_amount
account.balance.must_equal expected_balance
end
Expand All @@ -119,7 +104,6 @@
account = Bank::Account.new(1337, start_balance)

updated_balance = account.deposit(deposit_amount)

expected_balance = start_balance + deposit_amount
updated_balance.must_equal expected_balance
end
Expand All @@ -136,36 +120,51 @@
end
end

# TODO: change 'xdescribe' to 'describe' to run these tests
xdescribe "Wave 2" do
describe "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 "Account.find" do
it "Returns an account that exists" do
# TODO: Your test code here!
end
account = Bank::Account.all

it "Can find the first account from the CSV" do
# TODO: Your test code here!
end
account.must_be_instance_of Array

it "Can find the last account from the CSV" do
# TODO: Your test code here!
# - Everything in the array is an Account
account.each do |element|
element.must_be_instance_of Bank::Account
end
# - The number of accounts is correct account.length = 12
account.length.must_equal 12 #CSV.read("support/accounts.csv").length
# - The ID and balance of the first and last ...pull out indexes, id must be equal to
# accounts match what's in the CSV file
account[0].id.must_equal 1212
account[-1].id.must_equal 15156
account[0].balance.must_equal 1235667
account[-1].balance.must_equal 4356772
# Feel free to split this into multiple tests if needed
end

it "Raises an error for an account that doesn't exist" do
# TODO: Your test code here!
describe "Account.find" do
it "Returns an account that exists" do
account = Bank::Account.find(1212)
account.must_be_instance_of Bank::Account
end

it "Can find the first account from the CSV" do
accounts = Bank::Account.all
account = Bank::Account.find(1212)
account.id.must_equal accounts[0].id
end

it "Can find the last account from the CSV" do
accounts = Bank::Account.all
account = Bank::Account.find(15156)
account.id.must_equal accounts[-1].id
end

it "Raises an error for an account that doesn't exist" do
proc { Bank::Account.find(111111)
}.must_output(/.+/)
end
end
end
end
Loading