Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
55 changes: 55 additions & 0 deletions lib/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'csv'

module Bank
class Account
attr_reader :id, :balance, :open_date
def initialize(id, balance, open_date = "")
raise ArgumentError.new("balance must be >= 0") if balance < 0

@id = id
@balance = balance
@open_date = open_date
end

def withdraw(amount)
balance_min = 0
withdraw_internal(amount, balance_min)
end

def deposit(amount)
raise ArgumentError.new("deposit must be > 0") if amount <= 0
@balance += amount
end

def self.all
accounts = []
CSV.open("support/accounts.csv").each do |line|
accounts << self.new(line[0].to_f/100, line[1].to_f/100, line[2])
end
return accounts
end

def self.find(id)
self.all.each do |account|
return account if account.id == id
end
raise ArgumentError.new("ID does not exist")
end

private

def withdraw_internal(amount, balance_min)
raise ArgumentError.new("withdrawl must be > 0") if amount <= 0
if update_balance?(amount, balance_min)
@balance -= amount
end
return @balance
end

def update_balance?(amount, balance_min)
return true if @balance - amount >= balance_min
puts "Requested withdrawl amount surpasses allowable account balance."
return false
end
end
end
35 changes: 35 additions & 0 deletions lib/checking_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require_relative 'account'

module Bank
class CheckingAccount < Account
def initialize(id, balance)
super(id, balance)
@number_of_checks = 0 #free_checks = 3
end

def withdraw(amount)
fee = 1.0
balance_min = 0
withdraw_internal(amount + fee, balance_min)
end

def withdraw_using_check(amount)
if @number_of_checks >= 3
fee = 2.0
else
fee = 0
end
balance_min = -10.0

if update_balance?((amount + fee), balance_min)
@number_of_checks += 1
end

withdraw_internal(amount + fee, balance_min)
end

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

module Bank
class SavingsAccount < Account
def initialize(id, balance)
# Account must have a minimum balance of $10
raise ArgumentError.new("balance must be >= 10") if balance < 10
super(id, balance)
end

def withdraw(amount)
fee = 2.0
balance_min = 10.0
withdraw_internal(amount + fee, balance_min)
end

def add_interest(rate) #input rate is assumed a percentage
raise ArgumentError.new("interest rate must be positive") if rate <= 0
interest = @balance * rate / 100
@balance +=interest
return interest
end
end
end
74 changes: 55 additions & 19 deletions specs/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'minitest/reporters'
require 'minitest/skip_dsl'
require_relative '../lib/account'
require 'csv'

describe "Wave 1" do
describe "Account#initialize" do
Expand All @@ -18,10 +19,6 @@
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
Expand Down Expand Up @@ -67,7 +64,7 @@
# anything at all is printed out the test will pass.
proc {
account.withdraw(withdrawal_amount)
}.must_output /.+/
}.must_output (/.+/)
end

it "Doesn't modify the balance if the account would go negative" do
Expand Down Expand Up @@ -137,35 +134,74 @@
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
it "Returns an array" do
accounts = Bank::Account.all
accounts.must_be_instance_of Array
end

it "Contains only Account elements in the returned array" do
accounts = Bank::Account.all
# Check that each element within the array is an instance of the Account class
accounts.each do |account|
account.must_be_instance_of Bank::Account
end
end

it "Includes the correct number of accounts in the returned array" do
accounts = Bank::Account.all
num_of_accounts = CSV.read("support/accounts.csv").size
accounts.length.must_equal num_of_accounts
end

it "Creates a first account with the csv's first listed ID and balance" do
first_id = CSV.read("support/accounts.csv").first[0].to_f/100
first_balance = CSV.read("support/accounts.csv").first[1].to_f/100
accounts = Bank::Account.all

accounts.first.id.must_equal first_id
accounts.first.balance.must_equal first_balance
end

it "Creates a last account with the csv's last listed ID and balance" do
last_id = CSV.read("support/accounts.csv").last[0].to_f/100
last_balance = CSV.read("support/accounts.csv").last[1].to_f/100
accounts = Bank::Account.all

accounts.last.id.must_equal last_id
accounts.last.balance.must_equal last_balance
end
end

describe "Account.find" do
def find_and_verify_account(id)
account = Bank::Account.find(id)
account.must_be_instance_of Bank::Account
account.id.must_equal id
end

it "Returns an account that exists" do
# TODO: Your test code here!
seventh_id = CSV.read("support/accounts.csv")[6][0].to_f/100
find_and_verify_account(seventh_id)
end

it "Can find the first account from the CSV" do
# TODO: Your test code here!
first_id = CSV.read("support/accounts.csv").first[0].to_f/100
find_and_verify_account(first_id)
end

it "Can find the last account from the CSV" do
# TODO: Your test code here!
last_id = CSV.read("support/accounts.csv").last[0].to_f/100
find_and_verify_account(last_id)
end

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

proc {
Bank::Account.find(fake_id)
}.must_raise ArgumentError
end
end
end
Loading