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
60 changes: 60 additions & 0 deletions lib/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'csv'
module Bank
class Account
attr_accessor :id, :balance, :open_date

def initialize(id, balance, open_date = nil) #the equal to nil is so the other wave one tests pass
raise ArgumentError.new("balance must be >= 0") if balance < 0

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

end

def self.all
accounts = []
CSV.read("support/accounts.csv").each do |object|
id = object[0].to_i
balance = object[1].to_f/100
open_date = object[2]
a_account = Bank::Account.new(id, balance, open_date)
accounts << a_account
end
print accounts
return accounts
end

def self.find(id)
a_account = Bank::Account.all
a_account.each do |object|
if object.id == id
return object
end
end
raise ArgumentError.new "Account: #{id} does not exist"
end


def withdraw(amount)
raise ArgumentError.new("Withdraw amount must be a positive integer") if amount < 0
if @balance < amount
puts "Warning: desired withdrawl amount exceeds account balance."
return @balance
else
@balance -= amount
return @balance
end
end

def deposit(amount)
raise ArgumentError.new("Deposit amount must be a positive integer") if amount < 0
@balance += amount
return @balance
end
end
end

# account_1 = Bank::Account.new(1212, 1235667, "1999-03-27 11:30:09 -0800")
# Bank::Account.all
Bank::Account.find(1212)
53 changes: 53 additions & 0 deletions lib/checking_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'csv'
require_relative 'account'

module Bank
class CheckingAccount < Account

def initialize(id, balance)
super(id, balance)
end

def withdraw(amount)
if (@balance - amount) - 1 < 0
puts "You've exceeded the balance"
return @balance
else
@balance = (@balance - amount) - 1
return @balance
end
end

def withdraw_using_check(amount)
check = 0 #need to double check this is right
while check < 3 do
if balance - amount < -10
puts "You are not allowed an overdrft of more than $10."
else
@balance = @balance - amount
return balance
end
while check > 3 do
@balance = @balance - 2
return @balance
end
end
end

def reset_checks(checks)
if check > 3
return check = 0
end
end
end
end





# #withdraw_using_check(amount): The input amount gets taken out of the account as a result of a check withdrawal.
# Returns the updated account balance.
# Allows the account to go into overdraft up to -$10 but not any lower
# 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
48 changes: 48 additions & 0 deletions lib/savings_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'csv'
require_relative 'account'
# attr_accessor :id
# inherit behavior from the Account class.
module Bank
class SavingsAccount < Account

def initialize(id, balance)
super(id, balance)
raise ArgumentError.new("Balance must be at least $10") if balance < 10
end

# # The initial balance cannot be less than $10. If it is, this will raise an ArgumentError
# def starting_balance(balance)
# raise ArgumentError.new("Balance must be at least $10") if balance < 10
# return balance
# end

# Each withdrawal 'transaction' incurs a fee of $2 that is taken out of the balance.
def withdrawal(amount)
if @balance - amount - 2 < 10
puts "Balance cannot go below %10"
return @balance
else
@balance = (@balance - amount) - 2
return @balance
end
end

def add_interest(rate)
interest = @balance * (rate/100)
@balance = @balance + interest
return interest
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.
71 changes: 56 additions & 15 deletions specs/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require 'minitest/reporters'
require 'minitest/skip_dsl'
require_relative '../lib/account'
require 'csv'


describe "Wave 1" do
describe "Account#initialize" do
Expand Down Expand Up @@ -67,7 +69,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 @@ -136,36 +138,75 @@
end
end

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

before do #this before do thing runs things before running any of the tests
@accounts = 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
# accounts = Bank::Account.all
@accounts.must_be_instance_of Array
end

it "Everything in the array is an Account" do
@accounts.each do |object|
object.must_be_instance_of Bank::Account
end
end

it "The number of accounts is correct" do
@accounts.length.must_equal 12
end

it "The ID and balance of the first and last" do
@accounts.first.id.must_equal 1212
@accounts.first.balance.must_equal 12356.67
end

it "The elements match what's in the 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_f
accounts[index].id.must_equal line[2]
index += 1
end
end
end


describe "Account.find" do
it "Returns an account that exists" do
# TODO: Your test code here!
account = Bank::Account.find(1217)
account.must_be_instance_of Bank::Account
account.id.must_equal 1217
account.balance.must_equal 123.23
account.open_date.must_equal "2003-11-07 11:34:56 -0800"

end

it "Can find the first account from the CSV" do
# TODO: Your test code here!
account = Bank::Account.find(1212)
account.must_be_instance_of Bank::Account
account.id.must_equal 1212
account.balance.must_equal 12356.67
account.open_date.must_equal "1999-03-27 11:30:09 -0800"
end

it "Can find the last account from the CSV" do
# TODO: Your test code here!
account = Bank::Account.find(15156)

account.must_be_instance_of Bank::Account
account.id.must_equal 15156
account.balance.must_equal 43567.72
account.open_date.must_equal "1994-11-17 14:04:56 -0800"
end

it "Raises an error for an account that doesn't exist" do
# TODO: Your test code here!
proc { Bank::Account.find(1234567890) }.must_raise ArgumentError
end
end
end
14 changes: 9 additions & 5 deletions specs/checking_account_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'
require 'csv'
# require_relative '../lib/checking_account'

# TODO: uncomment the next line once you start wave 3 and add lib/checking_account.rb
# require_relative '../lib/checking_account'
Expand All @@ -22,29 +24,31 @@

describe "#withdraw" do
it "Applies a $1 fee each time" do
# TODO: Your test code here!
@balane.must_equal (@balance - amount) - 1
end

it "Doesn't modify the balance if the fee would put it negative" do
# TODO: Your test code here!
if (@balance - amount) - 1 < 0
@balace.must_equal @balance
end
end
end

describe "#withdraw_using_check" do
it "Reduces the balance" do
# TODO: Your test code here!
@balance.must_equal @balance - amount
end

it "Returns the modified balance" do
# TODO: Your test code here!
end

it "Allows the balance to go down to -$10" do
# TODO: Your test code here!
@balance.must_be :>=, -10
end

it "Outputs a warning if the account would go below -$10" do
# TODO: Your test code here!

end

it "Doesn't modify the balance if the account would go below -$10" do
Expand Down
Loading