Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queues - Erica Case - BankAccounts #22

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
Binary file added .DS_Store
Binary file not shown.
57 changes: 57 additions & 0 deletions lib/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'CSV'
module Bank
class Account
attr_reader :id, :balance, :date

def initialize(id, balance, date)
raise ArgumentError.new("balance must be >= 0") if balance < 0
@id = id.to_i
@balance = balance.to_i
@date = date
end

def withdraw(amount, fee = 0, min_balance = 0)
raise ArgumentError.new ("Nice try! You can't withdraw negative money, and withdrawing $0 would be a waste of our time.") if amount <= 0

balance_would_be = @balance - amount - fee

if balance_would_be < min_balance
puts "You don't have enough money to withdraw #{amount} cents."

puts "There would be a #{fee} convenience fees."

puts "You must maintain a minimum balance of #{min_balance} cents."
else
@balance = balance_would_be
end
return @balance
end

def deposit(amount)
raise ArgumentError.new ("We don't want your debt (negative money) here!") if amount < 0

@balance += amount
return @balance
end

# returns a collection of Account instances, representing all of the Accounts described in CSV
def self.all(csv_filename)
CSV.read(csv_filename).map do |row|
id = row[0].to_i
balance = row[1].to_i
date = row[2]
self.new(id, balance, date)
end
#return accounts
end

def self.find(csv_filename, id_to_find)
fail = proc {
raise ArgumentError.new("There is no client with the id you entered.")
}
self.all(csv_filename).find(fail) do |acct|
acct.id.to_i == id_to_find.to_i
end
end
end
end
32 changes: 32 additions & 0 deletions lib/checking_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Bank
class CheckingAccount < Account
def initialize(id, balance, date)
super(id, balance, date)
@withdrawals_by_check = 0
end

def withdraw(amount, fee = 100, min_balance = 0)
super(amount, fee, min_balance)
return @balance
end

def withdraw_using_check(amount)
balance_before_withdrawal = @balance

if @withdrawals_by_check > 2
self.withdraw(amount, fee = 200, min_balance = -1000 )
else
self.withdraw(amount, fee = 0, min_balance = -1000 )
end

if @balance < balance_before_withdrawal
@withdrawals_by_check += 1
end
@balance = balance
end

def reset_checks
@withdrawals_by_check = 0
end
end
end
25 changes: 25 additions & 0 deletions lib/savings_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

module Bank
class SavingsAccount < Account
def initialize(id, balance, date)

raise ArgumentError.new("balance must be at least $10") if balance < 1000

super(id, balance, date)
end

def withdraw(amount)
fee = 200
min_balance = 1000
super(amount, fee, min_balance)
end

def add_interest(rate)
raise ArgumentError.new("Negative interest rates are not only bad for your finances; they are illegal") if rate < 0

interest = @balance * rate/100
@balance += interest
return interest
end
end
end
94 changes: 77 additions & 17 deletions specs/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
require 'minitest/reporters'
require 'minitest/skip_dsl'
require_relative '../lib/account'
require 'CSV'
require 'skip'

describe "Wave 1" do
xdescribe "Wave 1" do
describe "Account#initialize" do
it "Takes an ID and an initial balance" do
id = 1337
Expand All @@ -17,6 +19,7 @@
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.
Expand All @@ -27,6 +30,7 @@
}.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)
Expand Down Expand Up @@ -56,11 +60,12 @@
updated_balance.must_equal expected_balance
end



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,)
# 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
Expand Down Expand Up @@ -101,6 +106,7 @@
end
end


describe "Account#deposit" do
it "Increases the balance" do
start_balance = 100.0
Expand Down Expand Up @@ -136,36 +142,90 @@
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

# - Account.all returns an array
it "Returns an array" do
accounts = Bank::Account.all("support/accounts.csv")
accounts.must_be_kind_of (Array)
end

it "contains only accounts" do
accounts = Bank::Account.all("support/accounts.csv")
accounts.each do |acct|
acct.must_be_kind_of Bank::Account
end
end

it "has the right number of elements" do
accounts = Bank::Account.all("support/accounts.csv")

accounts.length == CSV.read("support/accounts.csv").length
end

it "matches the first and last row" do
accounts = Bank::Account.all("support/accounts.csv")
compare_to = CSV.read("support/accounts.csv")

accounts.first.id.must_equal compare_to.first.first.to_i

accounts.first.balance.must_equal compare_to.first[1].to_i

accounts.last.balance.must_equal compare_to.last[1].to_i

accounts.last.balance.must_equal compare_to.last[1].to_i
end
end


describe "Account.find" do
it "Returns an account that exists" do
# TODO: Your test code here!
csv_filename = "support/accounts.csv"

account = Bank::Account.find(csv_filename, 1212)

acct_arr = [account.id.to_s, account.balance.to_s, account.date]

exists = false
CSV.open(csv_filename).each do |row|
if acct_arr == row
exists = true
end
end
exists.must_equal true
end


it "Can find the first account from the CSV" do
# TODO: Your test code here!
csv_filename = "support/accounts.csv"
first_row = CSV.read(csv_filename).first

account = Bank::Account.find(csv_filename, first_row.first)

acct_arr = [account.id.to_s, account.balance.to_s, account.date]

acct_arr.must_equal first_row

end

it "Can find the last account from the CSV" do
# TODO: Your test code here!
csv_filename = "support/accounts.csv"
last_row = CSV.read(csv_filename).last

account = Bank::Account.find(csv_filename, last_row.first)

acct_arr = [account.id.to_s, account.balance.to_s, account.date]

acct_arr.must_equal last_row
end

it "Raises an error for an account that doesn't exist" do
# TODO: Your test code here!
proc {
Bank::Account.find("support/accounts.csv", "5892")
}.must_raise ArgumentError
end
end
end
Loading