From 6c66f0f5a3a79a6d7ee77c09cff199b14d2b205c Mon Sep 17 00:00:00 2001 From: janicewilson Date: Tue, 21 Feb 2017 15:12:08 -0800 Subject: [PATCH 01/20] Account#initialize test passed. --- lib/account.rb | 15 +++++++++++++++ specs/account_spec.rb | 10 ++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index e69de29b..28750666 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -0,0 +1,15 @@ +module Bank + + class Account + + attr_accessor :id, :balance + + def initialize(id, balance) + + @id = id + @balance = balance + + end + end + +end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 6c399139..0f2ede2a 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -3,6 +3,8 @@ require 'minitest/skip_dsl' require_relative '../lib/account' +Minitest::Reporters.use! + describe "Wave 1" do describe "Account#initialize" do it "Takes an ID and an initial balance" do @@ -17,7 +19,7 @@ account.balance.must_equal balance end - it "Raises an ArgumentError when created with a negative balance" do + xit "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 @@ -27,13 +29,13 @@ }.must_raise ArgumentError end - it "Can be created with a balance of 0" do + xit "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 - describe "Account#withdraw" do + xdescribe "Account#withdraw" do it "Reduces the balance" do start_balance = 100.0 withdrawal_amount = 25.0 @@ -101,7 +103,7 @@ end end - describe "Account#deposit" do + xdescribe "Account#deposit" do it "Increases the balance" do start_balance = 100.0 deposit_amount = 25.0 From d352739b67497b97dbfaf65c759138b6fd8fb33a Mon Sep 17 00:00:00 2001 From: janicewilson Date: Tue, 21 Feb 2017 15:17:01 -0800 Subject: [PATCH 02/20] Raises an ArgumentError when created with a negative balance test passed. --- lib/account.rb | 9 ++++++++- specs/account_spec.rb | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 28750666..aa002dec 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -7,7 +7,14 @@ class Account def initialize(id, balance) @id = id - @balance = balance + + if balance > 0 + @balance = balance + + else + raise ArgumentError.new "Can't create an account with a negative balance." + + end end end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 0f2ede2a..62f3b92c 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -19,7 +19,7 @@ account.balance.must_equal balance end - xit "Raises an ArgumentError when created with a negative balance" do + 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 @@ -29,7 +29,7 @@ }.must_raise ArgumentError end - xit "Can be created with a balance of 0" do + 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 From 51641623d23e78301df6a6b812a0cb2fb5df2fc3 Mon Sep 17 00:00:00 2001 From: janicewilson Date: Tue, 21 Feb 2017 15:19:24 -0800 Subject: [PATCH 03/20] Can be created with a balance of 0 test passed. --- lib/account.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/account.rb b/lib/account.rb index aa002dec..0c7c252a 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -8,7 +8,7 @@ def initialize(id, balance) @id = id - if balance > 0 + if balance > -1 @balance = balance else From aa0734e019aabb7818bc392a5ed579ce69a58d49 Mon Sep 17 00:00:00 2001 From: janicewilson Date: Tue, 21 Feb 2017 15:45:21 -0800 Subject: [PATCH 04/20] Account#withdraw test passed. --- lib/account.rb | 22 +++++++++++++++++++++- specs/account_spec.rb | 12 ++++++------ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 0c7c252a..201e118c 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -2,7 +2,7 @@ module Bank class Account - attr_accessor :id, :balance + attr_accessor :id, :balance, :start_balance def initialize(id, balance) @@ -16,7 +16,27 @@ def initialize(id, balance) end + @start_balance + end + + def withdraw(withdrawal_amount) + + start_balance = @balance + + @balance = start_balance - withdrawal_amount + + end + end end + + +# new_account = Bank::Account.new(133, 100) +# +# puts new_account.balance +# +# expected_balance = new_account.withdraw(25) +# +# puts expected_balance diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 62f3b92c..3d4fd337 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -35,7 +35,7 @@ end end - xdescribe "Account#withdraw" do + describe "Account#withdraw" do it "Reduces the balance" do start_balance = 100.0 withdrawal_amount = 25.0 @@ -47,7 +47,7 @@ account.balance.must_equal expected_balance end - it "Returns the modified balance" do + xit "Returns the modified balance" do start_balance = 100.0 withdrawal_amount = 25.0 account = Bank::Account.new(1337, start_balance) @@ -58,7 +58,7 @@ updated_balance.must_equal expected_balance end - it "Outputs a warning if the account would go negative" do + xit "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) @@ -72,7 +72,7 @@ }.must_output /.+/ end - it "Doesn't modify the balance if the account would go negative" do + xit "Doesn't modify the balance if the account would go negative" do start_balance = 100.0 withdrawal_amount = 200.0 account = Bank::Account.new(1337, start_balance) @@ -85,14 +85,14 @@ account.balance.must_equal start_balance end - it "Allows the balance to go to 0" do + xit "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 end - it "Requires a positive withdrawal amount" do + xit "Requires a positive withdrawal amount" do start_balance = 100.0 withdrawal_amount = -25.0 account = Bank::Account.new(1337, start_balance) From 6c6aa1cf1cd563bfc1091c2fa4da6cbca853c02f Mon Sep 17 00:00:00 2001 From: janicewilson Date: Tue, 21 Feb 2017 15:57:05 -0800 Subject: [PATCH 05/20] "Returns the modified balance" and "Outputs a warning if the account would go negative" tests passed. --- lib/account.rb | 10 ++++++++-- specs/account_spec.rb | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 201e118c..3344305c 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -8,7 +8,7 @@ def initialize(id, balance) @id = id - if balance > -1 + if balance >= - @balance = balance else @@ -23,9 +23,15 @@ def initialize(id, balance) def withdraw(withdrawal_amount) start_balance = @balance - @balance = start_balance - withdrawal_amount + if @balance >= 0 + @balance + else + puts "Your balance is now negative." + + end + end end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 3d4fd337..abe04747 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -47,7 +47,7 @@ account.balance.must_equal expected_balance end - xit "Returns the modified balance" do + it "Returns the modified balance" do start_balance = 100.0 withdrawal_amount = 25.0 account = Bank::Account.new(1337, start_balance) @@ -58,7 +58,7 @@ updated_balance.must_equal expected_balance end - xit "Outputs a warning if the account would go negative" do + 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) From 8b63bb68b7b525e72d9c2e0c44a833c939018b27 Mon Sep 17 00:00:00 2001 From: janicewilson Date: Tue, 21 Feb 2017 16:14:43 -0800 Subject: [PATCH 06/20] "Allows the balance to go to 0" and "Requires a positive withdrawal amount" tests passed. --- lib/account.rb | 9 +++++---- specs/account_spec.rb | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 3344305c..bd35a342 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -8,7 +8,7 @@ def initialize(id, balance) @id = id - if balance >= - + if balance >= 0 @balance = balance else @@ -23,12 +23,13 @@ def initialize(id, balance) def withdraw(withdrawal_amount) start_balance = @balance - @balance = start_balance - withdrawal_amount - if @balance >= 0 - @balance + if start_balance >= withdrawal_amount + @balance = start_balance - withdrawal_amount + else puts "Your balance is now negative." + @balance end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index abe04747..94cd1b33 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -72,7 +72,7 @@ }.must_output /.+/ end - xit "Doesn't modify the balance if the account would go negative" do + it "Doesn't modify the balance if the account would go negative" do start_balance = 100.0 withdrawal_amount = 200.0 account = Bank::Account.new(1337, start_balance) @@ -85,7 +85,7 @@ account.balance.must_equal start_balance end - xit "Allows the balance to go to 0" do + 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 From b80a9c3f7378553472c7fc2708be1bdd0ab47c42 Mon Sep 17 00:00:00 2001 From: janicewilson Date: Tue, 21 Feb 2017 16:27:17 -0800 Subject: [PATCH 07/20] "Requires a positive withdrawal amount" test passes. --- lib/account.rb | 19 ++++++++++++++----- specs/account_spec.rb | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index bd35a342..f22e39dd 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -22,19 +22,28 @@ def initialize(id, balance) def withdraw(withdrawal_amount) - start_balance = @balance + if withdrawal_amount >= 0 - if start_balance >= withdrawal_amount - @balance = start_balance - withdrawal_amount + start_balance = @balance + + if start_balance >= withdrawal_amount + @balance = start_balance - withdrawal_amount + + else + puts "Your balance is now negative." + @balance + + end else - puts "Your balance is now negative." - @balance + raise ArgumentError.new "Withdrawal amounts must be positive." end + end + end end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 94cd1b33..646326e7 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -92,7 +92,7 @@ account.balance.must_equal 0 end - xit "Requires a positive withdrawal amount" do + it "Requires a positive withdrawal amount" do start_balance = 100.0 withdrawal_amount = -25.0 account = Bank::Account.new(1337, start_balance) From 78f2f946f132f93135a63a66d62585058c6f071b Mon Sep 17 00:00:00 2001 From: janicewilson Date: Tue, 21 Feb 2017 16:39:45 -0800 Subject: [PATCH 08/20] "Increases the balance", "Returns the modified balance" and "Requires a positive deposit amount" tests passed. --- lib/account.rb | 16 ++++++++++++++++ specs/account_spec.rb | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/account.rb b/lib/account.rb index f22e39dd..71b1189e 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -43,6 +43,22 @@ def withdraw(withdrawal_amount) end + def deposit(deposit_amount) + + if deposit_amount >= 0 + + start_balance = @balance + + @balance = start_balance + deposit_amount + + else + raise ArgumentError.new "Withdrawal amounts must be positive." + + end + + + end + end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 646326e7..17b72690 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -103,7 +103,7 @@ end end - xdescribe "Account#deposit" do + describe "Account#deposit" do it "Increases the balance" do start_balance = 100.0 deposit_amount = 25.0 From 44535b00df59370228d2f21f72c30cb22b54bfcf Mon Sep 17 00:00:00 2001 From: janicewilson Date: Wed, 22 Feb 2017 14:43:08 -0800 Subject: [PATCH 09/20] Added the optional. Attempted to add a "before" statement, but too time-consuming. I have CSV'ing to do. --- lib/account.rb | 34 +++++++++++++++++++++---- specs/account_spec.rb | 59 +++++++++++++++++++++++++++++-------------- 2 files changed, 69 insertions(+), 24 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 71b1189e..4eab15b3 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -1,10 +1,23 @@ module Bank + class Owner + + attr_accessor :name, :phone + + def initialize(hash) + + @name = hash[:name] + @phone = hash[:phone] + + end + + end + class Account - attr_accessor :id, :balance, :start_balance + attr_accessor :id, :balance, :owner - def initialize(id, balance) + def initialize(id, balance, hash) @id = id @@ -16,7 +29,7 @@ def initialize(id, balance) end - @start_balance + @owner = Bank::Owner.new(hash) end @@ -64,11 +77,22 @@ def deposit(deposit_amount) end - -# new_account = Bank::Account.new(133, 100) +# hash = {name: "Janice", phone: "303-349-1433"} +# +# owner_1 = Bank::Owner.new(hash) +# +# puts owner_1.name +# +# puts owner_1.phone +# +# new_account = Bank::Account.new(133, 100, hash) # # puts new_account.balance # +# puts new_account.owner.name +# +# puts new_account.owner.phone +# # expected_balance = new_account.withdraw(25) # # puts expected_balance diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 17b72690..23638f10 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -6,17 +6,25 @@ Minitest::Reporters.use! describe "Wave 1" do + before do + @hash = {name: "Janice", phone: "303-349-1433"} + @id = 1337 + @balance = 100.0 + @account = Bank::Account.new(@id, @balance, @hash) + end describe "Account#initialize" do - it "Takes an ID and an initial balance" do - id = 1337 - balance = 100.0 - account = Bank::Account.new(id, balance) + it "Takes an ID, an initial balance and account owner" do - account.must_respond_to :id - account.id.must_equal id + @account.must_respond_to :id + @account.id.must_equal @id + + @account.must_respond_to :balance + @account.balance.must_equal @balance + + @account.must_respond_to :owner + @account.owner.name.must_equal @hash[:name] + @account.owner.phone.must_equal @hash[:phone] - account.must_respond_to :balance - account.balance.must_equal balance end it "Raises an ArgumentError when created with a negative balance" do @@ -25,21 +33,26 @@ # This code checks that, when the proc is executed, it # raises an ArgumentError. proc { - Bank::Account.new(1337, -100.0) + @hash + Bank::Account.new(@id, -100.0, @hash) }.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) + @hash + Bank::Account.new(@id, 0, @hash) end end describe "Account#withdraw" do it "Reduces the balance" do + hash = {name: "Janice", phone: "303-349-1433"} start_balance = 100.0 withdrawal_amount = 25.0 - account = Bank::Account.new(1337, start_balance) + account = Bank::Account.new(1337, start_balance, hash) account.withdraw(withdrawal_amount) @@ -48,9 +61,10 @@ end it "Returns the modified balance" do + hash = {name: "Janice", phone: "303-349-1433"} start_balance = 100.0 withdrawal_amount = 25.0 - account = Bank::Account.new(1337, start_balance) + account = Bank::Account.new(1337, start_balance, hash) updated_balance = account.withdraw(withdrawal_amount) @@ -59,9 +73,10 @@ end it "Outputs a warning if the account would go negative" do + hash = {name: "Janice", phone: "303-349-1433"} start_balance = 100.0 withdrawal_amount = 200.0 - account = Bank::Account.new(1337, start_balance) + account = Bank::Account.new(1337, start_balance, hash) # Another proc! This test expects something to be printed # to the terminal, using 'must_output'. /.+/ is a regular @@ -73,9 +88,10 @@ end it "Doesn't modify the balance if the account would go negative" do + hash = {name: "Janice", phone: "303-349-1433"} start_balance = 100.0 withdrawal_amount = 200.0 - account = Bank::Account.new(1337, start_balance) + account = Bank::Account.new(1337, start_balance, hash) updated_balance = account.withdraw(withdrawal_amount) @@ -86,16 +102,18 @@ end it "Allows the balance to go to 0" do - account = Bank::Account.new(1337, 100.0) + hash = {name: "Janice", phone: "303-349-1433"} + account = Bank::Account.new(1337, 100.0, hash) updated_balance = account.withdraw(account.balance) updated_balance.must_equal 0 account.balance.must_equal 0 end it "Requires a positive withdrawal amount" do + hash = {name: "Janice", phone: "303-349-1433"} start_balance = 100.0 withdrawal_amount = -25.0 - account = Bank::Account.new(1337, start_balance) + account = Bank::Account.new(1337, start_balance, hash) proc { account.withdraw(withdrawal_amount) @@ -105,9 +123,10 @@ describe "Account#deposit" do it "Increases the balance" do + hash = {name: "Janice", phone: "303-349-1433"} start_balance = 100.0 deposit_amount = 25.0 - account = Bank::Account.new(1337, start_balance) + account = Bank::Account.new(1337, start_balance, hash) account.deposit(deposit_amount) @@ -116,9 +135,10 @@ end it "Returns the modified balance" do + hash = {name: "Janice", phone: "303-349-1433"} start_balance = 100.0 deposit_amount = 25.0 - account = Bank::Account.new(1337, start_balance) + account = Bank::Account.new(1337, start_balance, hash) updated_balance = account.deposit(deposit_amount) @@ -127,9 +147,10 @@ end it "Requires a positive deposit amount" do + hash = {name: "Janice", phone: "303-349-1433"} start_balance = 100.0 deposit_amount = -25.0 - account = Bank::Account.new(1337, start_balance) + account = Bank::Account.new(1337, start_balance, hash) proc { account.deposit(deposit_amount) From 75faf680d4aad0bbc248af3c71f4edfc79f0a7a9 Mon Sep 17 00:00:00 2001 From: janicewilson Date: Thu, 23 Feb 2017 06:13:42 -0800 Subject: [PATCH 10/20] Reboot re Wave 1. Committing without regrets. --- lib/account.rb | 63 ++++++-- specs/account_spec.rb | 323 ++++++++++++++++++++++++------------------ 2 files changed, 231 insertions(+), 155 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 4eab15b3..1c28bae8 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -1,3 +1,5 @@ +require 'csv' + module Bank class Owner @@ -15,19 +17,43 @@ def initialize(hash) class Account + def self.all(file, hash) + + all_accounts = [] + + CSV.open(file).each do | line | + + account = Account.new(line[0].to_i, line[1].to_i, hash) + + all_accounts << account + end + + all_accounts + end + + def self.find_account(file, id) + + CSV.open(file).each do | account | + + account.id.include?(id) + + end + + end + attr_accessor :id, :balance, :owner def initialize(id, balance, hash) @id = id - if balance >= 0 + # if balance >= 0 @balance = balance - else - raise ArgumentError.new "Can't create an account with a negative balance." - - end + # else + # raise ArgumentError.new "Can't create an account with a negative balance." + # + # end @owner = Bank::Owner.new(hash) @@ -73,26 +99,33 @@ def deposit(deposit_amount) end + end end -# hash = {name: "Janice", phone: "303-349-1433"} -# +hash = {name: "Janice", phone: "303-349-1433"} + # owner_1 = Bank::Owner.new(hash) -# + # puts owner_1.name -# + # puts owner_1.phone -# + # new_account = Bank::Account.new(133, 100, hash) -# + # puts new_account.balance -# + # puts new_account.owner.name -# + # puts new_account.owner.phone -# + # expected_balance = new_account.withdraw(25) -# + # puts expected_balance + +# all_accounts = Bank::Account.all("../support/accounts.csv", hash) +# +# find_accounts = Bank::Account.find_account("../support/accounts.csv", 1213) +# +# puts find_accounts diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 23638f10..d3373176 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -5,176 +5,219 @@ Minitest::Reporters.use! -describe "Wave 1" do - before do - @hash = {name: "Janice", phone: "303-349-1433"} - @id = 1337 - @balance = 100.0 - @account = Bank::Account.new(@id, @balance, @hash) - end - describe "Account#initialize" do - it "Takes an ID, an initial balance and account owner" do - - @account.must_respond_to :id - @account.id.must_equal @id - - @account.must_respond_to :balance - @account.balance.must_equal @balance - - @account.must_respond_to :owner - @account.owner.name.must_equal @hash[:name] - @account.owner.phone.must_equal @hash[:phone] - - 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 { - @hash - Bank::Account.new(@id, -100.0, @hash) - }.must_raise ArgumentError +# #describe "Wave 1" do +# before do +# @hash = {name: "Janice", phone: "303-349-1433"} +# @id = 1337 +# @balance = 100.0 +# @account = Bank::Account.new(@id, @balance, @hash) +# end +# describe "Account#initialize" do +# it "Takes an ID, an initial balance and account owner" do +# +# @account.must_respond_to :id +# @account.id.must_equal @id +# +# @account.must_respond_to :balance +# @account.balance.must_equal @balance +# +# @account.must_respond_to :owner +# @account.owner.name.must_equal @hash[:name] +# @account.owner.phone.must_equal @hash[:phone] +# +# 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 { +# @hash +# Bank::Account.new(@id, -100.0, @hash) +# }.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! +# @hash +# Bank::Account.new(@id, 0, @hash) +# end +# end +# +# # describe "Account#withdraw" do +# it "Reduces the balance" do +# hash = {name: "Janice", phone: "303-349-1433"} +# start_balance = 100.0 +# withdrawal_amount = 25.0 +# account = Bank::Account.new(1337, start_balance, hash) +# +# account.withdraw(withdrawal_amount) +# +# expected_balance = start_balance - withdrawal_amount +# account.balance.must_equal expected_balance +# end +# +# it "Returns the modified balance" do +# hash = {name: "Janice", phone: "303-349-1433"} +# start_balance = 100.0 +# withdrawal_amount = 25.0 +# account = Bank::Account.new(1337, start_balance, hash) +# +# updated_balance = account.withdraw(withdrawal_amount) +# +# expected_balance = start_balance - withdrawal_amount +# updated_balance.must_equal expected_balance +# end +# +# #it "Outputs a warning if the account would go negative" do +# hash = {name: "Janice", phone: "303-349-1433"} +# start_balance = 100.0 +# withdrawal_amount = 200.0 +# account = Bank::Account.new(1337, start_balance, hash) +# +# # 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 /.+/ +# end +# +# #it "Doesn't modify the balance if the account would go negative" do +# hash = {name: "Janice", phone: "303-349-1433"} +# start_balance = 100.0 +# withdrawal_amount = 200.0 +# account = Bank::Account.new(1337, start_balance, hash) +# +# 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 +# hash = {name: "Janice", phone: "303-349-1433"} +# account = Bank::Account.new(1337, 100.0, hash) +# updated_balance = account.withdraw(account.balance) +# updated_balance.must_equal 0 +# account.balance.must_equal 0 +# end +# +# it "Requires a positive withdrawal amount" do +# hash = {name: "Janice", phone: "303-349-1433"} +# start_balance = 100.0 +# withdrawal_amount = -25.0 +# account = Bank::Account.new(1337, start_balance, hash) +# +# proc { +# account.withdraw(withdrawal_amount) +# }.must_raise ArgumentError +# end +# end +# +# describe "Account#deposit" do +# it "Increases the balance" do +# hash = {name: "Janice", phone: "303-349-1433"} +# start_balance = 100.0 +# deposit_amount = 25.0 +# account = Bank::Account.new(1337, start_balance, hash) +# +# account.deposit(deposit_amount) +# +# expected_balance = start_balance + deposit_amount +# account.balance.must_equal expected_balance +# end +# +# it "Returns the modified balance" do +# hash = {name: "Janice", phone: "303-349-1433"} +# start_balance = 100.0 +# deposit_amount = 25.0 +# account = Bank::Account.new(1337, start_balance, hash) +# +# updated_balance = account.deposit(deposit_amount) +# +# expected_balance = start_balance + deposit_amount +# updated_balance.must_equal expected_balance +# end +# +# it "Requires a positive deposit amount" do +# hash = {name: "Janice", phone: "303-349-1433"} +# start_balance = 100.0 +# deposit_amount = -25.0 +# account = Bank::Account.new(1337, start_balance, hash) +# +# proc { +# account.deposit(deposit_amount) +# }.must_raise ArgumentError +# end +# end +# end +# TODO: change 'xdescribe' to 'describe' to run these tests +describe "Wave 2" do + describe "Account.all" do + it "Returns an array of all accounts" do - end + hash = {name: "Janice", phone: "303-349-1433"} + all_accounts = Bank::Account.all("../support/accounts.csv", hash) + all_accounts.must_be_kind_of Array - it "Can be created with a balance of 0" do - # If this raises, the test will fail. No 'must's needed! - @hash - Bank::Account.new(@id, 0, @hash) end - end - describe "Account#withdraw" do - it "Reduces the balance" do - hash = {name: "Janice", phone: "303-349-1433"} - start_balance = 100.0 - withdrawal_amount = 25.0 - account = Bank::Account.new(1337, start_balance, hash) + it "The number of accounts is correct" do - account.withdraw(withdrawal_amount) + hash = {name: "Janice", phone: "303-349-1433"} + all_accounts = Bank::Account.all("../support/accounts.csv", hash) + all_accounts.length.must_equal 12 - expected_balance = start_balance - withdrawal_amount - account.balance.must_equal expected_balance end - it "Returns the modified balance" do - hash = {name: "Janice", phone: "303-349-1433"} - start_balance = 100.0 - withdrawal_amount = 25.0 - account = Bank::Account.new(1337, start_balance, hash) + it "Everything in the array is an Account" do - updated_balance = account.withdraw(withdrawal_amount) + hash = {name: "Janice", phone: "303-349-1433"} + all_accounts = Bank::Account.all("../support/accounts.csv", hash) - expected_balance = start_balance - withdrawal_amount - updated_balance.must_equal expected_balance - end + all_accounts.each do | account | - it "Outputs a warning if the account would go negative" do - hash = {name: "Janice", phone: "303-349-1433"} - start_balance = 100.0 - withdrawal_amount = 200.0 - account = Bank::Account.new(1337, start_balance, hash) - - # 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 /.+/ - end + account.must_be_kind_of Bank::Account - it "Doesn't modify the balance if the account would go negative" do - hash = {name: "Janice", phone: "303-349-1433"} - start_balance = 100.0 - withdrawal_amount = 200.0 - account = Bank::Account.new(1337, start_balance, hash) + end - 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 - hash = {name: "Janice", phone: "303-349-1433"} - account = Bank::Account.new(1337, 100.0, hash) - updated_balance = account.withdraw(account.balance) - updated_balance.must_equal 0 - account.balance.must_equal 0 - end + it "The ID and balance of the first and last accounts match what's in the CSV file" do - it "Requires a positive withdrawal amount" do - hash = {name: "Janice", phone: "303-349-1433"} - start_balance = 100.0 - withdrawal_amount = -25.0 - account = Bank::Account.new(1337, start_balance, hash) + hash = {name: "Janice", phone: "303-349-1433"} + all_accounts = Bank::Account.all("../support/accounts.csv", hash) - proc { - account.withdraw(withdrawal_amount) - }.must_raise ArgumentError - end - end + all_accounts[0].id.must_equal 1212 + all_accounts[-1].id.must_equal 15156 + all_accounts[0].balance.must_equal 1235667 + all_accounts[-1].balance.must_equal 4356772 - describe "Account#deposit" do - it "Increases the balance" do - hash = {name: "Janice", phone: "303-349-1433"} - start_balance = 100.0 - deposit_amount = 25.0 - account = Bank::Account.new(1337, start_balance, hash) - account.deposit(deposit_amount) - - expected_balance = start_balance + deposit_amount - account.balance.must_equal expected_balance - end - - it "Returns the modified balance" do - hash = {name: "Janice", phone: "303-349-1433"} - start_balance = 100.0 - deposit_amount = 25.0 - account = Bank::Account.new(1337, start_balance, hash) - - updated_balance = account.deposit(deposit_amount) + end - expected_balance = start_balance + deposit_amount - updated_balance.must_equal expected_balance end - it "Requires a positive deposit amount" do - hash = {name: "Janice", phone: "303-349-1433"} - start_balance = 100.0 - deposit_amount = -25.0 - account = Bank::Account.new(1337, start_balance, hash) - - proc { - account.deposit(deposit_amount) - }.must_raise ArgumentError - end - end -end - -# TODO: change 'xdescribe' to 'describe' to run these tests -xdescribe "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 + # ✓ Account.all returns an array # - Everything in the array is an Account - # - The number of accounts is correct + # ✓ 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 + + + xdescribe "Account.find" do it "Returns an account that exists" do # TODO: Your test code here! end From e3ffbb89a723e0fa45ce6182e45bfceca2941924 Mon Sep 17 00:00:00 2001 From: janicewilson Date: Thu, 23 Feb 2017 15:13:30 -0800 Subject: [PATCH 11/20] YAR (Yet Another Reboot.) --- lib/account.rb | 54 +++--- specs/account_spec.rb | 378 ++++++++++++++++++++---------------------- 2 files changed, 201 insertions(+), 231 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 1c28bae8..1bb7f192 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -2,28 +2,15 @@ module Bank - class Owner - - attr_accessor :name, :phone - - def initialize(hash) - - @name = hash[:name] - @phone = hash[:phone] - - end - - end - class Account - def self.all(file, hash) + def self.all(file) all_accounts = [] CSV.open(file).each do | line | - account = Account.new(line[0].to_i, line[1].to_i, hash) + account = Account.new(line[0].to_i, line[1].to_i) all_accounts << account end @@ -31,31 +18,35 @@ def self.all(file, hash) all_accounts end - def self.find_account(file, id) + def self.find_account(file, inquiry) + + all_accounts = Bank::Account.all(file) + + all_accounts.each do | account | + + if account.id == inquiry - CSV.open(file).each do | account | + puts account - account.id.include?(id) + end - end + end end - attr_accessor :id, :balance, :owner + attr_accessor :id, :balance - def initialize(id, balance, hash) + def initialize(id,balance) @id = id - # if balance >= 0 + if balance >= 0 @balance = balance - # else - # raise ArgumentError.new "Can't create an account with a negative balance." - # - # end + else + raise ArgumentError.new "Can't create an account with a negative balance." - @owner = Bank::Owner.new(hash) + end end @@ -99,11 +90,12 @@ def deposit(deposit_amount) end - end end + + hash = {name: "Janice", phone: "303-349-1433"} # owner_1 = Bank::Owner.new(hash) @@ -123,9 +115,5 @@ def deposit(deposit_amount) # expected_balance = new_account.withdraw(25) # puts expected_balance - -# all_accounts = Bank::Account.all("../support/accounts.csv", hash) # -# find_accounts = Bank::Account.find_account("../support/accounts.csv", 1213) -# -# puts find_accounts +# all_accounts = Bank::Account.all("../support/accounts.csv", hash) diff --git a/specs/account_spec.rb b/specs/account_spec.rb index d3373176..8fc5aea2 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -5,233 +5,215 @@ Minitest::Reporters.use! -# #describe "Wave 1" do -# before do -# @hash = {name: "Janice", phone: "303-349-1433"} -# @id = 1337 -# @balance = 100.0 -# @account = Bank::Account.new(@id, @balance, @hash) -# end -# describe "Account#initialize" do -# it "Takes an ID, an initial balance and account owner" do -# -# @account.must_respond_to :id -# @account.id.must_equal @id -# -# @account.must_respond_to :balance -# @account.balance.must_equal @balance -# -# @account.must_respond_to :owner -# @account.owner.name.must_equal @hash[:name] -# @account.owner.phone.must_equal @hash[:phone] -# -# 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 { -# @hash -# Bank::Account.new(@id, -100.0, @hash) -# }.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! -# @hash -# Bank::Account.new(@id, 0, @hash) -# end -# end -# -# # describe "Account#withdraw" do -# it "Reduces the balance" do -# hash = {name: "Janice", phone: "303-349-1433"} -# start_balance = 100.0 -# withdrawal_amount = 25.0 -# account = Bank::Account.new(1337, start_balance, hash) -# -# account.withdraw(withdrawal_amount) -# -# expected_balance = start_balance - withdrawal_amount -# account.balance.must_equal expected_balance -# end -# -# it "Returns the modified balance" do -# hash = {name: "Janice", phone: "303-349-1433"} -# start_balance = 100.0 -# withdrawal_amount = 25.0 -# account = Bank::Account.new(1337, start_balance, hash) -# -# updated_balance = account.withdraw(withdrawal_amount) -# -# expected_balance = start_balance - withdrawal_amount -# updated_balance.must_equal expected_balance -# end -# -# #it "Outputs a warning if the account would go negative" do -# hash = {name: "Janice", phone: "303-349-1433"} -# start_balance = 100.0 -# withdrawal_amount = 200.0 -# account = Bank::Account.new(1337, start_balance, hash) -# -# # 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 /.+/ -# end -# -# #it "Doesn't modify the balance if the account would go negative" do -# hash = {name: "Janice", phone: "303-349-1433"} -# start_balance = 100.0 -# withdrawal_amount = 200.0 -# account = Bank::Account.new(1337, start_balance, hash) -# -# 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 -# hash = {name: "Janice", phone: "303-349-1433"} -# account = Bank::Account.new(1337, 100.0, hash) -# updated_balance = account.withdraw(account.balance) -# updated_balance.must_equal 0 -# account.balance.must_equal 0 -# end -# -# it "Requires a positive withdrawal amount" do -# hash = {name: "Janice", phone: "303-349-1433"} -# start_balance = 100.0 -# withdrawal_amount = -25.0 -# account = Bank::Account.new(1337, start_balance, hash) -# -# proc { -# account.withdraw(withdrawal_amount) -# }.must_raise ArgumentError -# end -# end -# -# describe "Account#deposit" do -# it "Increases the balance" do -# hash = {name: "Janice", phone: "303-349-1433"} -# start_balance = 100.0 -# deposit_amount = 25.0 -# account = Bank::Account.new(1337, start_balance, hash) -# -# account.deposit(deposit_amount) -# -# expected_balance = start_balance + deposit_amount -# account.balance.must_equal expected_balance -# end -# -# it "Returns the modified balance" do -# hash = {name: "Janice", phone: "303-349-1433"} -# start_balance = 100.0 -# deposit_amount = 25.0 -# account = Bank::Account.new(1337, start_balance, hash) -# -# updated_balance = account.deposit(deposit_amount) -# -# expected_balance = start_balance + deposit_amount -# updated_balance.must_equal expected_balance -# end -# -# it "Requires a positive deposit amount" do -# hash = {name: "Janice", phone: "303-349-1433"} -# start_balance = 100.0 -# deposit_amount = -25.0 -# account = Bank::Account.new(1337, start_balance, hash) -# -# proc { -# account.deposit(deposit_amount) -# }.must_raise ArgumentError -# end -# end -# end +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/skip_dsl' +require_relative '../lib/account' -# TODO: change 'xdescribe' to 'describe' to run these tests -describe "Wave 2" do - describe "Account.all" do - it "Returns an array of all accounts" do +Minitest::Reporters.use! - hash = {name: "Janice", phone: "303-349-1433"} - all_accounts = Bank::Account.all("../support/accounts.csv", hash) - all_accounts.must_be_kind_of Array +describe "Wave 1" do + describe "Account#initialize" do + it "Takes an ID and an initial balance" do + id = 1337 + balance = 100.0 + account = Bank::Account.new(id, balance) - end + account.must_respond_to :id + account.id.must_equal id - it "The number of accounts is correct" do + account.must_respond_to :balance + account.balance.must_equal balance + end - hash = {name: "Janice", phone: "303-349-1433"} - all_accounts = Bank::Account.all("../support/accounts.csv", hash) - all_accounts.length.must_equal 12 + 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 - it "Everything in the array is an Account" do + describe "Account#withdraw" do + it "Reduces the balance" do + start_balance = 100.0 + withdrawal_amount = 25.0 + account = Bank::Account.new(1337, start_balance) - hash = {name: "Janice", phone: "303-349-1433"} - all_accounts = Bank::Account.all("../support/accounts.csv", hash) + account.withdraw(withdrawal_amount) - all_accounts.each do | account | + expected_balance = start_balance - withdrawal_amount + account.balance.must_equal expected_balance + end - account.must_be_kind_of Bank::Account + it "Returns the modified balance" do + start_balance = 100.0 + withdrawal_amount = 25.0 + account = Bank::Account.new(1337, start_balance) - end + updated_balance = account.withdraw(withdrawal_amount) + expected_balance = start_balance - withdrawal_amount + updated_balance.must_equal expected_balance end - it "The ID and balance of the first and last accounts match what's in the CSV file" do - - hash = {name: "Janice", phone: "303-349-1433"} - all_accounts = Bank::Account.all("../support/accounts.csv", hash) + 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) + + # 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 /.+/ + end - all_accounts[0].id.must_equal 1212 - all_accounts[-1].id.must_equal 15156 - all_accounts[0].balance.must_equal 1235667 - all_accounts[-1].balance.must_equal 4356772 + it "Doesn't modify the balance if the account would go negative" do + start_balance = 100.0 + withdrawal_amount = 200.0 + account = Bank::Account.new(1337, start_balance) + updated_balance = account.withdraw(withdrawal_amount) - end + # 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 end - # 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 "Requires a positive withdrawal amount" do + start_balance = 100.0 + withdrawal_amount = -25.0 + account = Bank::Account.new(1337, start_balance) + proc { + account.withdraw(withdrawal_amount) + }.must_raise ArgumentError + end + end + describe "Account#deposit" do + it "Increases the balance" do + start_balance = 100.0 + deposit_amount = 25.0 + account = Bank::Account.new(1337, start_balance) - xdescribe "Account.find" do - it "Returns an account that exists" do - # TODO: Your test code here! - end + account.deposit(deposit_amount) - it "Can find the first account from the CSV" do - # TODO: Your test code here! + expected_balance = start_balance + deposit_amount + account.balance.must_equal expected_balance end - it "Can find the last account from the CSV" do - # TODO: Your test code here! + it "Returns the modified balance" do + start_balance = 100.0 + deposit_amount = 25.0 + 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 - it "Raises an error for an account that doesn't exist" do - # TODO: Your test code here! + it "Requires a positive deposit amount" do + start_balance = 100.0 + deposit_amount = -25.0 + account = Bank::Account.new(1337, start_balance) + + proc { + account.deposit(deposit_amount) + }.must_raise ArgumentError end end end + +# TODO: change 'xdescribe' to 'describe' to run these tests +describe "Wave 2" do + + before do + + @all_accounts = Bank::Account.all("../support/accounts.csv") + + end + + describe "Account.all" do + + it "Returns an array of all accounts" do + + @all_accounts.must_be_kind_of Array + + end + + it "The number of accounts is correct" do + + @all_accounts.length.must_equal 12 + + end + + + it "Everything in the array is an Account" do + + + + @all_accounts.each do | account | + + account.must_be_kind_of Bank::Account + + end + + end + + + it "The ID and balance of the first and last accounts match what's in the CSV file" do + + @all_accounts[0].id.must_equal 1212 + @all_accounts[-1].id.must_equal 15156 + @all_accounts[0].balance.must_equal 1235667 + @all_accounts[-1].balance.must_equal 4356772 + + end + + end + + # xdescribe "Account.find" do + # it "Returns an account that exists" do + # + # hash = {name: "Janice", phone: "303-349-1433"} + # find_account = Bank::Account.all("../support/accounts.csv", hash, 1213) + # + # find_account.must_be_kind_of Bank::Account + # # find_account.id.must_equal 1213 + # + # end + # + # xit "Can find the first account from the CSV" do + # # TODO: Your test code here! + # end + # + # xit "Can find the last account from the CSV" do + # # TODO: Your test code here! + # end + # + # xit "Raises an error for an account that doesn't exist" do + # # TODO: Your test code here! + # end + # end +end From 3cc028d9a03e3102e521b5a5db641c261ae3050b Mon Sep 17 00:00:00 2001 From: janicewilson Date: Thu, 23 Feb 2017 16:06:46 -0800 Subject: [PATCH 12/20] All tests but the last have passed. --- lib/account.rb | 37 +++++++++++++++--------- specs/account_spec.rb | 67 ++++++++++++++++++++++++------------------- 2 files changed, 61 insertions(+), 43 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 1bb7f192..d5c59dee 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -18,20 +18,37 @@ def self.all(file) all_accounts end - def self.find_account(file, inquiry) + def self.find_with_id(file, inquiry) # using ID all_accounts = Bank::Account.all(file) + found = "" all_accounts.each do | account | - if account.id == inquiry - puts account + if account.id == inquiry + found = account end + end + found + + + end + + def self.find_with_index(file, inquiry) # using index in array + + all_accounts = Bank::Account.all(file) + + found = all_accounts[inquiry] + + raise ArgumentError.new "Can't create an account with a negative balance." if found == nil + + found + end attr_accessor :id, :balance @@ -94,16 +111,6 @@ def deposit(deposit_amount) end - - -hash = {name: "Janice", phone: "303-349-1433"} - -# owner_1 = Bank::Owner.new(hash) - -# puts owner_1.name - -# puts owner_1.phone - # new_account = Bank::Account.new(133, 100, hash) # puts new_account.balance @@ -116,4 +123,6 @@ def deposit(deposit_amount) # puts expected_balance # -# all_accounts = Bank::Account.all("../support/accounts.csv", hash) +# all_accounts = Bank::Account.all("../support/accounts.csv") + +#find_account = Bank::Account.find("../support/accounts.csv", 1213) diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 8fc5aea2..d9f94e6c 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -152,7 +152,7 @@ @all_accounts = Bank::Account.all("../support/accounts.csv") - end + end describe "Account.all" do @@ -160,24 +160,22 @@ @all_accounts.must_be_kind_of Array - end + end it "The number of accounts is correct" do @all_accounts.length.must_equal 12 - end + end it "Everything in the array is an Account" do - - @all_accounts.each do | account | account.must_be_kind_of Bank::Account - end + end end @@ -193,27 +191,38 @@ end - # xdescribe "Account.find" do - # it "Returns an account that exists" do - # - # hash = {name: "Janice", phone: "303-349-1433"} - # find_account = Bank::Account.all("../support/accounts.csv", hash, 1213) - # - # find_account.must_be_kind_of Bank::Account - # # find_account.id.must_equal 1213 - # - # end - # - # xit "Can find the first account from the CSV" do - # # TODO: Your test code here! - # end - # - # xit "Can find the last account from the CSV" do - # # TODO: Your test code here! - # end - # - # xit "Raises an error for an account that doesn't exist" do - # # TODO: Your test code here! - # end - # end + describe "Account.find" do + + it "Returns an account that exists" do + + find_account = Bank::Account.find_with_id("../support/accounts.csv", 1213) + find_account.must_be_kind_of Bank::Account + find_account.id.must_equal 1213 + + end + + it "Can find the first account from the CSV" do + + find_account = Bank::Account.find_with_index("../support/accounts.csv", 0) + find_account.must_be_kind_of Bank::Account + find_account.id.must_equal 1212 + + end + + it "Can find the last account from the CSV" do + + find_account = Bank::Account.find_with_index("../support/accounts.csv", -1) + find_account.must_be_kind_of Bank::Account + find_account.id.must_equal 15156 + + end + + it "Raises an error for an account that doesn't exist" do + + proc {find_account = Bank::Account.find_with_index("../support/accounts.csv", 13)}.must_raise ArgumentError + + end + + end + end From 85e624e17505afbb4666b03793159b8fd053bdbd Mon Sep 17 00:00:00 2001 From: janicewilson Date: Thu, 23 Feb 2017 16:57:53 -0800 Subject: [PATCH 13/20] All tests but the last have passed. --- lib/account.rb | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index d5c59dee..c86103be 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -21,20 +21,22 @@ def self.all(file) def self.find_with_id(file, inquiry) # using ID all_accounts = Bank::Account.all(file) - found = "" - - all_accounts.each do | account | - - - if account.id == inquiry - - found = account - end - - - end - - found + # found = "" + # + # all_accounts.each do | account | + # + # + # if account.id == inquiry + # + # found = account + # end + # + # + # end + # + # found + + all_accounts[] end @@ -43,11 +45,13 @@ def self.find_with_index(file, inquiry) # using index in array all_accounts = Bank::Account.all(file) - found = all_accounts[inquiry] - - raise ArgumentError.new "Can't create an account with a negative balance." if found == nil + # found = all_accounts[inquiry] + # + # raise ArgumentError.new "Can't create an account with a negative balance." if found == nil + # + # found - found + all_accounts.fetch(inquiry, "Count doesn't exist.") end From f69fa40dd89b350245127a5703c30f0e55124ed3 Mon Sep 17 00:00:00 2001 From: janicewilson Date: Fri, 24 Feb 2017 11:59:17 -0800 Subject: [PATCH 14/20] A fully sanitized Wave 2. --- lib/account.rb | 108 ++++++++++++++---------------------------- specs/account_spec.rb | 16 ++++--- 2 files changed, 44 insertions(+), 80 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index c86103be..107c963f 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -10,7 +10,11 @@ def self.all(file) CSV.open(file).each do | line | - account = Account.new(line[0].to_i, line[1].to_i) + id = line[0].to_i + balance = line[1].to_i + date_opened = line[2] + + account = Account.new(id, balance, date_opened) all_accounts << account end @@ -18,95 +22,65 @@ def self.all(file) all_accounts end - def self.find_with_id(file, inquiry) # using ID + def self.find(file, id) all_accounts = Bank::Account.all(file) - # found = "" - # - # all_accounts.each do | account | - # - # - # if account.id == inquiry - # - # found = account - # end - # - # - # end - # - # found - - all_accounts[] + all_accounts.each do | account | - end + if account.id == id - def self.find_with_index(file, inquiry) # using index in array + return account - all_accounts = Bank::Account.all(file) + end - # found = all_accounts[inquiry] - # - # raise ArgumentError.new "Can't create an account with a negative balance." if found == nil - # - # found + end - all_accounts.fetch(inquiry, "Count doesn't exist.") + raise ArgumentError.new "Account #{id} does not exist." end - attr_accessor :id, :balance - - def initialize(id,balance) + def self.find_with_index(file, index) - @id = id + all_accounts = Bank::Account.all(file) - if balance >= 0 - @balance = balance + all_accounts.fetch(index) + + end - else - raise ArgumentError.new "Can't create an account with a negative balance." - end + attr_accessor :id, :balance, :date_opened - end + def initialize(id, balance, date_opened = nil) - def withdraw(withdrawal_amount) + raise ArgumentError.new "Can't create an account with a negative balance." unless balance >= 0 - if withdrawal_amount >= 0 + @id = id + @balance = balance + @date_opened = date_opened - start_balance = @balance + end - if start_balance >= withdrawal_amount - @balance = start_balance - withdrawal_amount + def withdraw(withdrawal_amount) - else - puts "Your balance is now negative." - @balance + raise ArgumentError.new "Please enter a withdrawal amount greater than 0." unless withdrawal_amount > 0 - end + if @balance >= withdrawal_amount + @balance -= withdrawal_amount else - raise ArgumentError.new "Withdrawal amounts must be positive." + puts "You haven't sufficient funds for withdrawal." + @balance end - end def deposit(deposit_amount) - if deposit_amount >= 0 - - start_balance = @balance - - @balance = start_balance + deposit_amount - - else - raise ArgumentError.new "Withdrawal amounts must be positive." - - end + raise ArgumentError.new "Please enter a deposit amount greater than 0." unless deposit_amount > 0 + @balance += deposit_amount end @@ -115,18 +89,6 @@ def deposit(deposit_amount) end -# new_account = Bank::Account.new(133, 100, hash) - -# puts new_account.balance - -# puts new_account.owner.name - -# puts new_account.owner.phone - -# expected_balance = new_account.withdraw(25) - -# puts expected_balance -# -# all_accounts = Bank::Account.all("../support/accounts.csv") - -#find_account = Bank::Account.find("../support/accounts.csv", 1213) +find_account = Bank::Account.find("../support/accounts.csv", 1213) +puts find_account +puts find_account.class diff --git a/specs/account_spec.rb b/specs/account_spec.rb index d9f94e6c..339dda62 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -191,23 +191,23 @@ end - describe "Account.find" do + describe "Account.find" do - it "Returns an account that exists" do + it "Returns an account that exists" do - find_account = Bank::Account.find_with_id("../support/accounts.csv", 1213) + find_account = Bank::Account.find("../support/accounts.csv", 1213) find_account.must_be_kind_of Bank::Account find_account.id.must_equal 1213 - end + end - it "Can find the first account from the CSV" do + it "Can find the first account from the CSV" do find_account = Bank::Account.find_with_index("../support/accounts.csv", 0) find_account.must_be_kind_of Bank::Account find_account.id.must_equal 1212 - end + end it "Can find the last account from the CSV" do @@ -219,7 +219,9 @@ it "Raises an error for an account that doesn't exist" do - proc {find_account = Bank::Account.find_with_index("../support/accounts.csv", 13)}.must_raise ArgumentError + proc {find_account = Bank::Account.find("../support/accounts.csv", 3333)}.must_raise ArgumentError + + proc {find_account = Bank::Account.find_with_index("../support/accounts.csv", 13)}.must_raise IndexError end From f530f7bd0c2157a49e3152306d109fdcbe437d62 Mon Sep 17 00:00:00 2001 From: janicewilson Date: Fri, 24 Feb 2017 15:00:20 -0800 Subject: [PATCH 15/20] Minitests for Savings Account \'initialize\' and \'withdraw\' successfully completed. --- specs/account_spec.rb | 7 --- specs/savings_account_spec.rb | 109 +++++++++++++++++++++++++--------- 2 files changed, 82 insertions(+), 34 deletions(-) diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 339dda62..e726d1be 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -5,13 +5,6 @@ Minitest::Reporters.use! -require 'minitest/autorun' -require 'minitest/reporters' -require 'minitest/skip_dsl' -require_relative '../lib/account' - -Minitest::Reporters.use! - describe "Wave 1" do describe "Account#initialize" do it "Takes an ID and an initial balance" do diff --git a/specs/savings_account_spec.rb b/specs/savings_account_spec.rb index 3f4d1e4a..54eba6f0 100644 --- a/specs/savings_account_spec.rb +++ b/specs/savings_account_spec.rb @@ -1,6 +1,9 @@ require 'minitest/autorun' require 'minitest/reporters' require 'minitest/skip_dsl' +require_relative '../lib/savings_account' + +Minitest::Reporters.use! # TODO: uncomment the next line once you start wave 3 and add lib/savings_account.rb # require_relative '../lib/savings_account' @@ -11,38 +14,90 @@ # Here we'll only test things that are different. # TODO: change 'xdescribe' to 'describe' to run these tests -xdescribe "SavingsAccount" do - describe "#initialize" do - it "Is a kind of Account" do - # Check that a SavingsAccount is in fact a kind of account - account = Bank::SavingsAccount.new(12345, 100.0) - account.must_be_kind_of Bank::Account - end +describe "SavingsAccount" do - it "Requires an initial balance of at least $10" do - # TODO: Your test code here! - end - end + describe "#initialize" do - describe "#withdraw" do - it "Applies a $2 fee each time" do - # TODO: Your test code here! - end + it "Is a kind of Account" do - it "Outputs a warning if the balance would go below $10" do - # TODO: Your test code here! - end + account = Bank::SavingsAccount.new(12345, 100.0) + account.must_be_kind_of Bank::Account - it "Doesn't modify the balance if it would go below $10" do - # TODO: Your test code here! - end + end - it "Doesn't modify the balance if the fee would put it below $10" do - # TODO: Your test code here! - end - end + it "Requires an initial balance of at least $10" do + + proc { + Bank::SavingsAccount.new(1337, -100.0) + }.must_raise ArgumentError + + proc { + Bank::SavingsAccount.new(1337, -0.0) + }.must_raise ArgumentError + + proc { + Bank::SavingsAccount.new(1337, 9.0) + }.must_raise ArgumentError + + end + end + + describe "#withdraw" do + + # it "Applies a $2 fee each time" do + # # TODO: Your test code here! + # end - describe "#add_interest" do + it "Outputs a warning if the balance would go below $10" do + + start_balance = 50.0 + withdrawal_amount = 48.0 + account = Bank::SavingsAccount.new(1337, start_balance) + + proc { + account.withdraw(withdrawal_amount) + }.must_output /.+/ + + end + + it "Doesn't modify the balance if it would go below $10" do + + start_balance = 50.0 + withdrawal_amount = 41.0 + account = Bank::SavingsAccount.new(1337, start_balance) + + updated_balance = account.withdraw(withdrawal_amount) + updated_balance.must_equal start_balance + account.balance.must_equal start_balance + end + + it "Doesn't modify the balance if it would go below $10" do + + start_balance = 50.0 + withdrawal_amount = 41.0 + account = Bank::SavingsAccount.new(1337, start_balance) + + updated_balance = account.withdraw(withdrawal_amount) + updated_balance.must_equal start_balance + account.balance.must_equal start_balance + end + + + it "Doesn't modify the balance if it would go below $10 due to the $2 fee" do + + start_balance = 11 + withdrawal_amount = 1 + account = Bank::SavingsAccount.new(1337, start_balance) + + updated_balance = account.withdraw(withdrawal_amount) + updated_balance.must_equal start_balance + account.balance.must_equal start_balance + end + + + end + + #describe "#add_interest" do it "Returns the interest calculated" do # TODO: Your test code here! end @@ -54,5 +109,5 @@ it "Requires a positive rate" do # TODO: Your test code here! end - end + end From 16d46394b897b8708ba4ed30ef5be943ace31782 Mon Sep 17 00:00:00 2001 From: janicewilson Date: Fri, 24 Feb 2017 15:01:28 -0800 Subject: [PATCH 16/20] Added RB files for savings and checking account.. --- lib/checking_account.rb | 0 lib/savings_account.rb | 49 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 lib/checking_account.rb create mode 100644 lib/savings_account.rb diff --git a/lib/checking_account.rb b/lib/checking_account.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/savings_account.rb b/lib/savings_account.rb new file mode 100644 index 00000000..5c8f7ab4 --- /dev/null +++ b/lib/savings_account.rb @@ -0,0 +1,49 @@ +require 'csv' +require_relative 'account' + +module Bank + + class SavingsAccount < Account + + def initialize(id, balance) + + raise ArgumentError.new "Starting balance must be $10 or more." until balance >= 10 + super + + + end + + def withdraw(withdrawal_amount) + + raise ArgumentError.new "Please enter a withdrawal amount greater than 0." unless withdrawal_amount > 0 + + fee = 2 + + if @balance - (withdrawal_amount + fee) >= 12 + @balance -= (withdrawal_amount + fee) + + else + puts "You haven't sufficient funds for withdrawal." + return @balance + + end + + end + + + end + +end + + +#Updated withdrawal functionality: +#Each withdrawal 'transaction' incurs a fee of $2 that is taken out of the balance. +#Does not allow the account to go below the $10 minimum balance - Will output a warning #message and return the original un-modified balance +#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. + +# janice = Bank::SavingsAccount.new(333, 60) From fe911d4b8d47067959e27c3297b7387e14ab806f Mon Sep 17 00:00:00 2001 From: janicewilson Date: Fri, 24 Feb 2017 15:45:56 -0800 Subject: [PATCH 17/20] Minitest /'add_interest/' passed for Saving Account. --- lib/savings_account.rb | 18 +++++++-- specs/savings_account_spec.rb | 69 +++++++++++++++++++++-------------- 2 files changed, 55 insertions(+), 32 deletions(-) diff --git a/lib/savings_account.rb b/lib/savings_account.rb index 5c8f7ab4..df40b182 100644 --- a/lib/savings_account.rb +++ b/lib/savings_account.rb @@ -30,15 +30,22 @@ def withdraw(withdrawal_amount) end + def add_interest(rate) + + raise ArgumentError.new "Please enter an interest rate greater than 0.00." unless rate > 0.00 + + start_balance = @balance + @balance *= 1 + (rate/100) + interest_earned = start_balance * (rate/100) + + end + end end -#Updated withdrawal functionality: -#Each withdrawal 'transaction' incurs a fee of $2 that is taken out of the balance. -#Does not allow the account to go below the $10 minimum balance - Will output a warning #message and return the original un-modified balance #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). @@ -46,4 +53,7 @@ def withdraw(withdrawal_amount) #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. -# janice = Bank::SavingsAccount.new(333, 60) +# janice = Bank::SavingsAccount.new(333, 10) +# +# puts janice.add_interest(0.25) +# puts janice.balance diff --git a/specs/savings_account_spec.rb b/specs/savings_account_spec.rb index 54eba6f0..f2a183a7 100644 --- a/specs/savings_account_spec.rb +++ b/specs/savings_account_spec.rb @@ -27,36 +27,24 @@ it "Requires an initial balance of at least $10" do - proc { - Bank::SavingsAccount.new(1337, -100.0) - }.must_raise ArgumentError + proc {Bank::SavingsAccount.new(1337, -100.0)}.must_raise ArgumentError - proc { - Bank::SavingsAccount.new(1337, -0.0) - }.must_raise ArgumentError + proc {Bank::SavingsAccount.new(1337, -0.0)}.must_raise ArgumentError - proc { - Bank::SavingsAccount.new(1337, 9.0) - }.must_raise ArgumentError + proc {Bank::SavingsAccount.new(1337, 9.0)}.must_raise ArgumentError end end describe "#withdraw" do - # it "Applies a $2 fee each time" do - # # TODO: Your test code here! - # end - it "Outputs a warning if the balance would go below $10" do start_balance = 50.0 withdrawal_amount = 48.0 account = Bank::SavingsAccount.new(1337, start_balance) - proc { - account.withdraw(withdrawal_amount) - }.must_output /.+/ + proc {account.withdraw(withdrawal_amount)}.must_output /.+/ end @@ -83,7 +71,7 @@ end - it "Doesn't modify the balance if it would go below $10 due to the $2 fee" do + it "Doesn't modify the balance if it would go below $10 due to the $2 fee" do start_balance = 11 withdrawal_amount = 1 @@ -92,22 +80,47 @@ updated_balance = account.withdraw(withdrawal_amount) updated_balance.must_equal start_balance account.balance.must_equal start_balance - end + + end end - #describe "#add_interest" do - it "Returns the interest calculated" do - # TODO: Your test code here! - end + describe "#add_interest" do + + it "Returns the interest calculated" do + + start_balance = 10000 + rate = 0.25 + account = Bank::SavingsAccount.new(1337, start_balance) + calculated_interest = start_balance * (rate/100) + + calculated_interest.must_equal account.add_interest(rate) + + end + + it "Updates the balance with calculated interest" do - it "Updates the balance with calculated interest" do - # TODO: Your test code here! - end + start_balance = 12000 + rate = 0.13 + account = Bank::SavingsAccount.new(1337, start_balance) + account.add_interest(rate) + new_balance = start_balance * (1 + (rate/100)) - it "Requires a positive rate" do - # TODO: Your test code here! - end + new_balance.must_equal account.balance + + end + + it "Requires a positive rate" do + + start_balance = 12000 + rate = -0.1 + account = Bank::SavingsAccount.new(1337, start_balance) + + proc {account.add_interest(rate)}.must_raise ArgumentError + + end + + end end From 396ed5485ba75c75b10936a42e818a9995262479 Mon Sep 17 00:00:00 2001 From: janicewilson Date: Sat, 25 Feb 2017 11:52:55 -0800 Subject: [PATCH 18/20] Minitests re \'initialize\' and \'withdraw'\ updated. --- lib/checking_account.rb | 42 ++++++++++++++++++ specs/checking_account_spec.rb | 80 ++++++++++++++++++++-------------- specs/savings_account_spec.rb | 9 ---- 3 files changed, 89 insertions(+), 42 deletions(-) diff --git a/lib/checking_account.rb b/lib/checking_account.rb index e69de29b..0fec65f9 100644 --- a/lib/checking_account.rb +++ b/lib/checking_account.rb @@ -0,0 +1,42 @@ +require 'csv' +require_relative 'account' + +module Bank + + class CheckingAccount < Account + + def withdraw(withdrawal_amount) + + raise ArgumentError.new "Please enter a withdrawal amount greater than 0." unless withdrawal_amount > 0 + + withdrawal_fee = 1 + + if @balance >= withdrawal_amount + withdrawal_fee + @balance -= (withdrawal_amount + withdrawal_fee) + + else + puts "You haven't sufficient funds for withdrawal." + @balance + + 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 +# +# janice = Bank::CheckingAccount.new(333, 300) +# +# puts janice.withdraw(1) +# puts janice.balance diff --git a/specs/checking_account_spec.rb b/specs/checking_account_spec.rb index 7f95339e..97092919 100644 --- a/specs/checking_account_spec.rb +++ b/specs/checking_account_spec.rb @@ -1,70 +1,84 @@ require 'minitest/autorun' require 'minitest/reporters' require 'minitest/skip_dsl' +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' - -# Because a CheckingAccount is a kind -# of Account, and we've already tested a bunch of functionality -# on Account, we effectively get all that testing for free! -# Here we'll only test things that are different. - -# TODO: change 'xdescribe' to 'describe' to run these tests -xdescribe "CheckingAccount" do - describe "#initialize" do - # Check that a CheckingAccount is in fact a kind of account - it "Is a kind of Account" do - account = Bank::CheckingAccount.new(12345, 100.0) - account.must_be_kind_of Bank::Account - end - end +Minitest::Reporters.use! - describe "#withdraw" do - it "Applies a $1 fee each time" do - # TODO: Your test code here! - end +describe "CheckingAccount" do - it "Doesn't modify the balance if the fee would put it negative" do - # TODO: Your test code here! - end - end + describe "#initialize" do + + it "Is a kind of Account" do + + account = Bank::CheckingAccount.new(12345, 100.0) + account.must_be_kind_of Bank::Account + + end + end + + describe "#withdraw" do + + it "Applies a $1 fee each time" do + + start_balance = 300 + withdrawal_amount = 298 + withdrawal_fee = 1 + account = Bank::CheckingAccount.new(12345, start_balance) + + account.withdraw(withdrawal_amount).must_equal start_balance - (withdrawal_amount +withdrawal_fee) + + end + + it "Doesn't modify the balance if the fee would put it negative" do + + start_balance = 300 + withdrawal_amount = 300 + withdrawal_fee = 1 + account = Bank::CheckingAccount.new(12345, start_balance) + account.withdraw(withdrawal_amount) + + account.withdraw(withdrawal_amount).must_equal start_balance + + end + + end describe "#withdraw_using_check" do it "Reduces the balance" do # TODO: Your test code here! end - it "Returns the modified balance" do + xit "Returns the modified balance" do # TODO: Your test code here! end - it "Allows the balance to go down to -$10" do + xit "Allows the balance to go down to -$10" do # TODO: Your test code here! end - it "Outputs a warning if the account would go below -$10" do + xit "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 + xit "Doesn't modify the balance if the account would go below -$10" do # TODO: Your test code here! end - it "Requires a positive withdrawal amount" do + xit "Requires a positive withdrawal amount" do # TODO: Your test code here! end - it "Allows 3 free uses" do + xit "Allows 3 free uses" do # TODO: Your test code here! end - it "Applies a $2 fee after the third use" do + xit "Applies a $2 fee after the third use" do # TODO: Your test code here! end end - describe "#reset_checks" do + xdescribe "#reset_checks" do it "Can be called without error" do # TODO: Your test code here! end diff --git a/specs/savings_account_spec.rb b/specs/savings_account_spec.rb index f2a183a7..dc3b5a3d 100644 --- a/specs/savings_account_spec.rb +++ b/specs/savings_account_spec.rb @@ -5,15 +5,6 @@ Minitest::Reporters.use! -# TODO: uncomment the next line once you start wave 3 and add lib/savings_account.rb -# require_relative '../lib/savings_account' - -# Because a SavingsAccount is a kind -# of Account, and we've already tested a bunch of functionality -# on Account, we effectively get all that testing for free! -# Here we'll only test things that are different. - -# TODO: change 'xdescribe' to 'describe' to run these tests describe "SavingsAccount" do describe "#initialize" do From 502cc28629d808cf0b8429c2118b7f49b8c716f2 Mon Sep 17 00:00:00 2001 From: janicewilson Date: Sat, 25 Feb 2017 12:02:24 -0800 Subject: [PATCH 19/20] Minitests re \'initialize\' and \'withdraw'\ updated. --- lib/checking_account.rb | 8 ++++++-- specs/checking_account_spec.rb | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/checking_account.rb b/lib/checking_account.rb index 0fec65f9..862621d9 100644 --- a/lib/checking_account.rb +++ b/lib/checking_account.rb @@ -8,8 +8,8 @@ class CheckingAccount < Account def withdraw(withdrawal_amount) raise ArgumentError.new "Please enter a withdrawal amount greater than 0." unless withdrawal_amount > 0 - - withdrawal_fee = 1 + + withdrawal_fee = 0 if @balance >= withdrawal_amount + withdrawal_fee @balance -= (withdrawal_amount + withdrawal_fee) @@ -22,6 +22,10 @@ def withdraw(withdrawal_amount) end + def withdraw(withdrawal_amount) + + end + end diff --git a/specs/checking_account_spec.rb b/specs/checking_account_spec.rb index 97092919..3126ab36 100644 --- a/specs/checking_account_spec.rb +++ b/specs/checking_account_spec.rb @@ -46,11 +46,21 @@ describe "#withdraw_using_check" do it "Reduces the balance" do - # TODO: Your test code here! + + start_balance = 300 + withdrawal_amount = 100 + withdrawal_fee = 0 + account = Bank::CheckingAccount.new(12345, start_balance) + account.withdraw_using_check(withdrawal_amount) + + account.withdraw(withdrawal_amount).must_equal start_balance - (withdrawal_amount + withdrawal_fee) + end xit "Returns the modified balance" do - # TODO: Your test code here! + + account.withdraw(withdrawal_amount).must_equal + end xit "Allows the balance to go down to -$10" do From c26a9edb89f56e4be64f59a7faaea79572f72de9 Mon Sep 17 00:00:00 2001 From: janicewilson Date: Sat, 25 Feb 2017 14:58:50 -0800 Subject: [PATCH 20/20] All systems go. All mini-tests passed. --- lib/checking_account.rb | 66 +++++++++-- specs/checking_account_spec.rb | 193 ++++++++++++++++++++++++++------- 2 files changed, 211 insertions(+), 48 deletions(-) diff --git a/lib/checking_account.rb b/lib/checking_account.rb index 862621d9..0c4492ee 100644 --- a/lib/checking_account.rb +++ b/lib/checking_account.rb @@ -5,11 +5,21 @@ module Bank class CheckingAccount < Account + attr_reader :counter, :check_withdrawal_fee + + def initialize(id, balance, date_opened = nil) + + super + @counter = 0 + @check_withdrawal_fee + + end + def withdraw(withdrawal_amount) raise ArgumentError.new "Please enter a withdrawal amount greater than 0." unless withdrawal_amount > 0 - withdrawal_fee = 0 + withdrawal_fee = 1 if @balance >= withdrawal_amount + withdrawal_fee @balance -= (withdrawal_amount + withdrawal_fee) @@ -22,25 +32,67 @@ def withdraw(withdrawal_amount) end - def withdraw(withdrawal_amount) + def withdraw_with_check(withdrawal_amount) + + raise ArgumentError.new "Please enter a withdrawal amount greater than 0." unless withdrawal_amount > 0 + + @counter += 1 + deficit_limit = 10 + + if (1..3) === @counter then @check_withdrawal_fee = 0; end + if (4..10000) === @counter then @check_withdrawal_fee = 2; end + + if @balance - (withdrawal_amount + @check_withdrawal_fee) >= -deficit_limit + @balance -= (withdrawal_amount + @check_withdrawal_fee) + + else + puts "You haven't sufficient funds for withdrawal." + @balance + + end end + def reset_checks + @counter = 0 + + 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 # -# janice = Bank::CheckingAccount.new(333, 300) +janice = Bank::CheckingAccount.new(333, 300) + +withdrawal_amount = 3 + +# 2.times do +# +# janice.withdraw_with_check(withdrawal_amount) +# puts janice.counter +# puts janice.balance +# puts janice.check_withdrawal_fee +# +# end +# +# janice.reset_checks +# +# puts janice.reset_checks.class +# +# puts "Reset: #{janice.reset_checks}" +# +# 2.times do +# +# janice.withdraw_with_check(withdrawal_amount) +# puts janice.counter +# puts janice.balance +# puts janice.check_withdrawal_fee # -# puts janice.withdraw(1) -# puts janice.balance +# end diff --git a/specs/checking_account_spec.rb b/specs/checking_account_spec.rb index 3126ab36..f8a7817e 100644 --- a/specs/checking_account_spec.rb +++ b/specs/checking_account_spec.rb @@ -44,61 +44,172 @@ end - describe "#withdraw_using_check" do - it "Reduces the balance" do + describe "#withdraw_with_check" do - start_balance = 300 - withdrawal_amount = 100 - withdrawal_fee = 0 - account = Bank::CheckingAccount.new(12345, start_balance) - account.withdraw_using_check(withdrawal_amount) + it "Reduces the balance" do - account.withdraw(withdrawal_amount).must_equal start_balance - (withdrawal_amount + withdrawal_fee) + start_balance = 300 + withdrawal_amount = 100 + account = Bank::CheckingAccount.new(12345, start_balance) + account.withdraw_with_check(withdrawal_amount) - end + account.balance.must_equal start_balance - withdrawal_amount - xit "Returns the modified balance" do + end - account.withdraw(withdrawal_amount).must_equal + it "Returns the modified balance" do - end + start_balance = 300 + withdrawal_amount = 100 + account = Bank::CheckingAccount.new(12345, start_balance) - xit "Allows the balance to go down to -$10" do - # TODO: Your test code here! - end + account.withdraw_with_check(withdrawal_amount).must_equal start_balance - withdrawal_amount - xit "Outputs a warning if the account would go below -$10" do - # TODO: Your test code here! - end + end - xit "Doesn't modify the balance if the account would go below -$10" do - # TODO: Your test code here! - end + it "Allows the balance to go down to -$10" do - xit "Requires a positive withdrawal amount" do - # TODO: Your test code here! - end + start_balance = 300 + withdrawal_amount = 310 + deficit_limit = -10 + account = Bank::CheckingAccount.new(12345, start_balance) - xit "Allows 3 free uses" do - # TODO: Your test code here! - end + account.withdraw_with_check(withdrawal_amount).must_equal deficit_limit - xit "Applies a $2 fee after the third use" do - # TODO: Your test code here! - end - end + end + + it "Outputs a warning if the account would go below -$10" do + + start_balance = 300 + withdrawal_amount = 311 + account = Bank::CheckingAccount.new(12345, start_balance) + + proc {account.withdraw_with_check(withdrawal_amount)}.must_output /.+/ + + end + + it "Doesn't modify the balance if the account would go below -$10" do + + start_balance = 300 + withdrawal_amount = 311 + account = Bank::CheckingAccount.new(12345, start_balance) + + account.withdraw_with_check(withdrawal_amount).must_equal start_balance + + end + + it "Requires a positive withdrawal amount" do + + start_balance = 300 + withdrawal_amount = -1 + account = Bank::CheckingAccount.new(12345, start_balance) + + proc {account.withdraw_with_check(withdrawal_amount)}.must_raise ArgumentError + + end + + it "Allows 3 free uses" do + + start_balance = 300 + withdrawal_amount = 1 + account = Bank::CheckingAccount.new(12345, start_balance) + + 3.times do + + account.withdraw_with_check(withdrawal_amount) + + end + + account.check_withdrawal_fee.must_equal 0 + + + end + + it "Applies a $2 fee after the third use" do + + start_balance = 300 + withdrawal_amount = 1 + account = Bank::CheckingAccount.new(12345, start_balance) + + 4.times do - xdescribe "#reset_checks" do - it "Can be called without error" do - # TODO: Your test code here! - end + account.withdraw_with_check(withdrawal_amount) - it "Makes the next three checks free if less than 3 checks had been used" do - # TODO: Your test code here! - end + end - it "Makes the next three checks free if more than 3 checks had been used" do - # TODO: Your test code here! - end + account.check_withdrawal_fee.must_equal 2 + + + + end end + + describe "#reset_checks" do + + it "Can be called without error" do + + start_balance = 300 + withdrawal_amount = 1 + account = Bank::CheckingAccount.new(12345, start_balance) + + 4.times do + + account.withdraw_with_check(withdrawal_amount) + + end + + account.reset_checks.must_equal 0 + end + + it "Makes the next three checks free if less than 3 checks had been used" do + + start_balance = 300 + withdrawal_amount = 1 + account = Bank::CheckingAccount.new(12345, start_balance) + + 2.times do + + account.withdraw_with_check(withdrawal_amount) + + end + + account.reset_checks + + 3.times do + + account.withdraw_with_check(withdrawal_amount) + + end + + account.check_withdrawal_fee.must_equal 0 + + end + + it "Makes the next three checks free if more than 3 checks had been used" do + + + start_balance = 300 + withdrawal_amount = 1 + account = Bank::CheckingAccount.new(12345, start_balance) + + 5.times do + + account.withdraw_with_check(withdrawal_amount) + + end + + account.reset_checks + + 3.times do + + account.withdraw_with_check(withdrawal_amount) + + end + + account.check_withdrawal_fee.must_equal 0 + + end + + end + end