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 - Kayla Kubicke - Bank Accounts #36

Open
wants to merge 12 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
56 changes: 56 additions & 0 deletions lib/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'csv'

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

def withdraw(amount)
raise ArgumentError.new("withdrawal amount must be >= 0") if amount < 0
if amount > @balance
raise ArgumentError.new("withdrawal amount must be > 0") if @balance < 0
puts "Account would go negative."
elsif amount <= @balance
@balance = @balance - amount
end
return @balance
end

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

def self.all
accounts_array = []

read_file = CSV.read('support/accounts.csv')

read_file.each do |line|
id = line[0].to_i
balance = line[1].to_i
account = Bank::Account.new(id, balance)
accounts_array << account
end

return accounts_array
end

def self.find(id)
accounts = Bank::Account.all

accounts.each do |account|
if account.id == id
return account
end
end
raise ArgumentError.new "Account does not exist."
end

end
end
75 changes: 75 additions & 0 deletions lib/checking_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require_relative './account'
require 'csv'

module Bank
class MoneyMarketAccount < Bank::Account

Choose a reason for hiding this comment

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

Why is this MoneyMarketAccount - I would have expected to see CheckingAccount in this file (and that's what you appear to have implemented).

def initialize(id, balance)
super(id, balance)
@counter = 0
end

def withdraw(amount)

Choose a reason for hiding this comment

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

It might be cleaner to use super here to call Account#withdraw, and centralize your logic all in one place. In this case Account#withdraw would probably require more parameters, like a fee and a minimum balance.

amount += 1

if (@balance - amount) < 0
return @balance
raise ArgumentError.new("account must be > 0") if (@balance - amount) < 0
else
@balance = @balance - amount
return @balance
end
end

#Note: In the video for Wave 3 it was mentioned that we could
#forgo counting by month. I did not account for the month.
def withdraw_using_check(amount)
# @counter = 0
fee = 2
test_balance = @balance - amount
raise ArgumentError.new("needs a positive withdrawal amount") if amount < 0

if @counter <= 3 && test_balance < -10
return @balance
raise ArgumentError.new("account will go below -10") if (@balance - amount) < -10
elsif @counter <= 3 && test_balance >= -10
@balance = @balance - amount
return @balance
@counter += 1
elsif @counter > 3 && test_balance >= -10
@balance = @balance - amount - fee
@counter += 1
elsif @counter > 3 && test_balance < -10
raise ArgumentError.new("account will go below -10") if (@balance - amount) < -10
return @balance
end
end

end
end

test_1 = Bank::MoneyMarketAccount.new(666, 100)
# puts test_1.withdraw_using_check(2)


4.times do
# test_1.withdraw_using_check(2)
puts test_1.withdraw_using_check(2)
puts @counter.class
end



# if @counter <= 3 && test_balance < -10
# return @balance
# raise ArgumentError.new("account will go below -10") if (@balance - amount) < -10
# elsif @counter <= 3 && test_balance >= -10
# @balance = @balance - amount
# return @balance
# @counter += 1
# elsif @counter > 3 && test_balance >= -10
# @balance = @balance - amount - fee
# @counter += 1
# elsif @counter > 3 && test_balance < -10
# raise ArgumentError.new("account will go below -10") if (@balance - amount) < -10
# return @balance
# end
38 changes: 38 additions & 0 deletions lib/savings_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require_relative './account'
require 'csv'

module Bank
class SavingsAccount < Bank::Account
def initialize(id, balance)
super(id, balance)
raise ArgumentError.new("balance must be > 10") if balance < 10
@interest = 0
end

def withdraw(amount)
amount += 2

if (@balance - amount) < 10
return @balance
raise ArgumentError.new("account must be > 10") if (@balance - amount) < 10
else
@balance = @balance - amount
return @balance
end
end

def add_interest(rate)
raise ArgumentError.new("interest must be > 0") if rate < 0

@interest = @balance * (rate / 100)

@balance = @balance + @interest
return @interest
end

end
end

# test_1 = Bank::SavingsAccount.new(666, 10_000)
# puts test_1.withdraw(2)
# puts test_1.add_interest(0.25)
80 changes: 55 additions & 25 deletions specs/account_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
require 'csv'
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'
require_relative '../lib/account'

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

describe "Wave 1" do
describe "Account#initialize" do
it "Takes an ID and an initial balance" do
Expand All @@ -18,10 +21,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 @@ -61,10 +60,6 @@
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 /.+/
Expand All @@ -77,8 +72,6 @@

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
Expand Down Expand Up @@ -136,36 +129,73 @@
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

Choose a reason for hiding this comment

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

Nice use of before!

@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.must_be_instance_of Array
end
it "Everything in the array is an Account" do
@accounts.each do |account|
account.must_be_instance_of Bank::Account
end
end
it "Number of accounts is correct" do
@accounts.length.must_equal 12
end

it "Match elements in file" do
CSV.read('support/accounts.csv') do |line|
counter = 0
@accounts[counter].id.must_equal line[0].to_i
@accounts[counter].balance.must_equal line[1].to_i
counter += 1
end
end
end

it "ID and balance match the first and last element" do
@accounts.first.id.must_equal 1212
@accounts.first.balance.must_equal 1235667

@accounts.last.id.must_equal 15156
@accounts.last.balance.must_equal 4356772
end
end


describe "Account.find" do
it "Returns an account that exists" do
# TODO: Your test code here!
account = Bank::Account.find(1216)

account.must_be_instance_of Bank::Account
account.id.must_equal 1216
account.balance.must_equal 100022
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 1235667
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 4356772
end

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