From 26ef2490518cbf6309d47a5b48c1bef17b4b98c9 Mon Sep 17 00:00:00 2001 From: madamecurie Date: Tue, 21 Feb 2017 16:23:06 -0800 Subject: [PATCH 01/23] respond to :id, :balance q --- lib/account.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/account.rb b/lib/account.rb index e69de29b..d5b76d6b 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -0,0 +1,12 @@ +module Bank + + class Account + attr_reader :id, :balance + + def initialize(id, balance) + + @id = id + @balance =balance + end + end +end From a71e5b56c4b58df0eafdbf4f904bca732a8d6c4b Mon Sep 17 00:00:00 2001 From: madamecurie Date: Tue, 21 Feb 2017 23:00:16 -0800 Subject: [PATCH 02/23] balance must not be negative --- lib/account.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index d5b76d6b..8cbd5af2 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -3,10 +3,13 @@ module Bank class Account attr_reader :id, :balance - def initialize(id, balance) + def initialize(id, balance = 0) + raise ArgumentError.new("balance must be >= 0") if balance < 0 @id = id - @balance =balance + @balance = balance + + end end end From 4f659ddb96be403acdad0fe061cd62ca840e7600 Mon Sep 17 00:00:00 2001 From: madamecurie Date: Tue, 21 Feb 2017 23:15:24 -0800 Subject: [PATCH 03/23] Withdrawl test passed --- lib/account.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 8cbd5af2..85bce64e 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -1,5 +1,4 @@ module Bank - class Account attr_reader :id, :balance @@ -8,8 +7,10 @@ def initialize(id, balance = 0) @id = id @balance = balance - - + end + def withdraw(amount) + @balance = @balance - amount + return @balance end end end From 59ce9821c054b7def02969244c858e9629a05d2e Mon Sep 17 00:00:00 2001 From: madamecurie Date: Tue, 21 Feb 2017 23:31:01 -0800 Subject: [PATCH 04/23] insufficient fund --- lib/account.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/account.rb b/lib/account.rb index 85bce64e..aee64f59 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -9,7 +9,11 @@ def initialize(id, balance = 0) @balance = balance end def withdraw(amount) - @balance = @balance - amount + if amount > @balance + puts "Warning: insufficient fund." + else + @balance = @balance - amount + end return @balance end end From 2da5b89b3bc3af4ae713c29e91d22a0d4ef841d8 Mon Sep 17 00:00:00 2001 From: madamecurie Date: Tue, 21 Feb 2017 23:41:46 -0800 Subject: [PATCH 05/23] amount must be >= 0 --- lib/account.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/account.rb b/lib/account.rb index aee64f59..96b6fd6f 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -9,7 +9,8 @@ def initialize(id, balance = 0) @balance = balance end def withdraw(amount) - if amount > @balance + raise ArgumentError.new("amount must be >= 0") if amount < 0 +if amount > @balance puts "Warning: insufficient fund." else @balance = @balance - amount From 9ba6c8d84136186994570628d32e8646ac2b8f0d Mon Sep 17 00:00:00 2001 From: madamecurie Date: Tue, 21 Feb 2017 23:50:30 -0800 Subject: [PATCH 06/23] method deposit increase balance and puts modified balance. --- lib/account.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/account.rb b/lib/account.rb index 96b6fd6f..d9144661 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -10,12 +10,16 @@ def initialize(id, balance = 0) end def withdraw(amount) raise ArgumentError.new("amount must be >= 0") if amount < 0 -if amount > @balance + if amount > @balance puts "Warning: insufficient fund." else @balance = @balance - amount end return @balance end + def deposit(amount) + @balance = @balance + amount + return @balance + end end end From 15351ebca9604ad7b6637e3d4d56f4eea86e7a61 Mon Sep 17 00:00:00 2001 From: madamecurie Date: Tue, 21 Feb 2017 23:53:56 -0800 Subject: [PATCH 07/23] require a positive deposit amount. --- lib/account.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/account.rb b/lib/account.rb index d9144661..032e4305 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -18,6 +18,7 @@ def withdraw(amount) return @balance end def deposit(amount) + raise ArgumentError.new("amount must be >= 0") if amount < 0 @balance = @balance + amount return @balance end From 4c58457313ebbdbffb580db625b72a2bdef65752 Mon Sep 17 00:00:00 2001 From: madamecurie Date: Wed, 22 Feb 2017 00:24:48 -0800 Subject: [PATCH 08/23] owner initialize. --- lib/account.rb | 13 ++++- specs/account_spec.rb | 108 +++++++++++++++++++++++------------------- 2 files changed, 72 insertions(+), 49 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 032e4305..ec0a2d04 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -1,6 +1,6 @@ module Bank class Account - attr_reader :id, :balance + attr_reader :id, :balance, :owner def initialize(id, balance = 0) raise ArgumentError.new("balance must be >= 0") if balance < 0 @@ -22,5 +22,16 @@ def deposit(amount) @balance = @balance + amount return @balance end + def add_owner (owner) + @owner = owner + end + end + + class Owner + attr_reader :name, :address + def initialize(name, address) + @name = name + @address = address + end end end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 6c399139..b366fdc6 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -32,7 +32,6 @@ Bank::Account.new(1337, 0) end end - describe "Account#withdraw" do it "Reduces the balance" do start_balance = 100.0 @@ -45,6 +44,7 @@ account.balance.must_equal expected_balance end + it "Returns the modified balance" do start_balance = 100.0 withdrawal_amount = 25.0 @@ -69,7 +69,6 @@ account.withdraw(withdrawal_amount) }.must_output /.+/ end - it "Doesn't modify the balance if the account would go negative" do start_balance = 100.0 withdrawal_amount = 200.0 @@ -112,60 +111,73 @@ expected_balance = start_balance + deposit_amount account.balance.must_equal expected_balance end + end - 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) + it "Returns the modified balance" do + start_balance = 100.0 + deposit_amount = 25.0 + account = Bank::Account.new(1337, start_balance) - expected_balance = start_balance + deposit_amount - updated_balance.must_equal expected_balance - end - - it "Requires a positive deposit amount" do - start_balance = 100.0 - deposit_amount = -25.0 - account = Bank::Account.new(1337, start_balance) + updated_balance = account.deposit(deposit_amount) - proc { - account.deposit(deposit_amount) - }.must_raise ArgumentError - end + expected_balance = start_balance + deposit_amount + updated_balance.must_equal expected_balance 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 - # - 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 - end - end - describe "Account.find" do - it "Returns an account that exists" do - # TODO: Your test code here! - end + it "Requires a positive deposit amount" do + start_balance = 100.0 + deposit_amount = -25.0 + account = Bank::Account.new(1337, start_balance) - it "Can find the first account from the CSV" do - # TODO: Your test code here! - end + proc { + account.deposit(deposit_amount) + }.must_raise ArgumentError + end +end +describe "Owner#initialize" do + it "Takes a name and an address" do + name = "Rahul" + address = "Renton, WA" + owner = Bank::Owner.new(name, address) - it "Can find the last account from the CSV" do - # TODO: Your test code here! - end + owner.must_respond_to :name + owner.name.must_equal name - it "Raises an error for an account that doesn't exist" do - # TODO: Your test code here! - end + owner.must_respond_to :address + owner.address.must_equal address 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 +# # - 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 +# end +# end +# +# describe "Account.find" do +# it "Returns an account that exists" do +# # TODO: Your test code here! +# end +# +# it "Can find the first account from the CSV" do +# # TODO: Your test code here! +# end +# +# it "Can find the last account from the CSV" do +# # TODO: Your test code here! +# end +# +# it "Raises an error for an account that doesn't exist" do +# # TODO: Your test code here! +# end +# end From 5673f40229d2c77bde8f3dae993f19211267fc3e Mon Sep 17 00:00:00 2001 From: madamecurie Date: Wed, 22 Feb 2017 00:43:17 -0800 Subject: [PATCH 09/23] add owner method --- specs/account_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/specs/account_spec.rb b/specs/account_spec.rb index b366fdc6..82b805ab 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -135,6 +135,14 @@ }.must_raise ArgumentError end end +describe "Account#add_owner" do + it "takes an owner and updates it" do + account = Bank::Account.new(1337, 100) + owner = Bank::Owner.new("Rahul", "Renton,WA") + account.add_owner(owner) + account.owner.must_equal owner + end +end describe "Owner#initialize" do it "Takes a name and an address" do name = "Rahul" From 92ef020a0d129303267a6d0c9271eccc8074f932 Mon Sep 17 00:00:00 2001 From: madamecurie Date: Wed, 22 Feb 2017 14:37:02 -0800 Subject: [PATCH 10/23] Changed 'xdescribe to 'describe'. --- lib/account.rb | 1 + specs/account_spec.rb | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index ec0a2d04..da68da9e 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -21,6 +21,7 @@ def deposit(amount) raise ArgumentError.new("amount must be >= 0") if amount < 0 @balance = @balance + amount return @balance + # @balance += amount end def add_owner (owner) @owner = owner diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 82b805ab..9f1e9fc7 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -157,8 +157,9 @@ end end -# # TODO: change 'xdescribe' to 'describe' to run these tests -# xdescribe "Wave 2" do +# TODO: change 'xdescribe' to 'describe' to run these tests +describe "Wave 2" do +end # describe "Account.all" do # it "Returns an array of all accounts" do # # TODO: Your test code here! From d5220823b36a79a23cadf48bf1a82f0e8628770d Mon Sep 17 00:00:00 2001 From: madamecurie Date: Wed, 22 Feb 2017 22:37:47 -0800 Subject: [PATCH 11/23] Test: What is Account.all --- lib/account.rb | 13 +++++++++++++ specs/account_spec.rb | 34 ++++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index da68da9e..653ac461 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -1,3 +1,6 @@ +require 'csv' +#require_relative '../support' + module Bank class Account attr_reader :id, :balance, :owner @@ -8,6 +11,16 @@ def initialize(id, balance = 0) @id = id @balance = balance end + + def self.all + accounts = [] + CSV.read("support/accounts.csv").each do |line| + accounts << Account.new(line[0].to_i, line[1].to_i) + end + return accounts + end + + def withdraw(amount) raise ArgumentError.new("amount must be >= 0") if amount < 0 if amount > @balance diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 9f1e9fc7..ca59581b 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -160,18 +160,28 @@ # TODO: change 'xdescribe' to 'describe' to run these tests describe "Wave 2" do end -# 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 -# end -# end + 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 + Bank::Account.all.must_be_instance_of Array + # - Everything in the array is an Account + Bank::Account.all.each do |x| + x.must_be_instance_of Bank::Account + end + # - The number of accounts is correct + Bank::Account.all.length.must_equal 12 + # - The ID and balance of the first and last + # accounts match what's in the CSV file + Bank::Account.all[0].id.must_equal 1212 + Bank::Account.all[0].balance.must_equal 1235667 + Bank::Account.all[11].id.must_equal 15156 + Bank::Account.all[11].balance.must_equal 4356772 + + # Feel free to split this into multiple tests if needed + end + end # # describe "Account.find" do # it "Returns an account that exists" do From 3a48e92b1144d9113a8f64570f16450a31592ce2 Mon Sep 17 00:00:00 2001 From: madamecurie Date: Wed, 22 Feb 2017 22:48:51 -0800 Subject: [PATCH 12/23] id test using string --- lib/account.rb | 6 +++++- specs/account_spec.rb | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 653ac461..5f3768ce 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -15,11 +15,15 @@ def initialize(id, balance = 0) def self.all accounts = [] CSV.read("support/accounts.csv").each do |line| - accounts << Account.new(line[0].to_i, line[1].to_i) + accounts << Account.new(line[0], line[1].to_i) end return accounts end + def self.find + @id = Account.new([:id]) + end + def withdraw(amount) raise ArgumentError.new("amount must be >= 0") if amount < 0 diff --git a/specs/account_spec.rb b/specs/account_spec.rb index ca59581b..8e6a7803 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -174,9 +174,9 @@ Bank::Account.all.length.must_equal 12 # - The ID and balance of the first and last # accounts match what's in the CSV file - Bank::Account.all[0].id.must_equal 1212 + Bank::Account.all[0].id.must_equal "1212" Bank::Account.all[0].balance.must_equal 1235667 - Bank::Account.all[11].id.must_equal 15156 + Bank::Account.all[11].id.must_equal "15156" Bank::Account.all[11].balance.must_equal 4356772 # Feel free to split this into multiple tests if needed From d3b2bab5f344a79a4d3edcb6d231e3521b2f2a9e Mon Sep 17 00:00:00 2001 From: madamecurie Date: Wed, 22 Feb 2017 23:10:42 -0800 Subject: [PATCH 13/23] Test describe Account.find --- lib/account.rb | 10 ++++++++-- specs/account_spec.rb | 42 ++++++++++++++++++++++-------------------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 5f3768ce..281d0c04 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -20,8 +20,14 @@ def self.all return accounts end - def self.find - @id = Account.new([:id]) + def self.find(id) + CSV.read("support/accounts.csv").each do |line| + if line[0] == id + return Account.new(line[0], line[1].to_i) + end + end + raise ArgumentError.new("account id doesn't exist") + end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 8e6a7803..8fce28d1 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -162,7 +162,6 @@ end 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 Bank::Account.all.must_be_instance_of Array @@ -179,24 +178,27 @@ Bank::Account.all[11].id.must_equal "15156" Bank::Account.all[11].balance.must_equal 4356772 - # Feel free to split this into multiple tests if needed end end -# -# describe "Account.find" do -# it "Returns an account that exists" do -# # TODO: Your test code here! -# end -# -# it "Can find the first account from the CSV" do -# # TODO: Your test code here! -# end -# -# it "Can find the last account from the CSV" do -# # TODO: Your test code here! -# end -# -# it "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 + Bank::Account.find("15151").must_be_instance_of Bank::Account + end + + it "Can find the first account from the CSV" do + Bank::Account.find("1212").must_be_instance_of Bank::Account + + end + + it "Can find the last account from the CSV" do + Bank::Account.find("15156").must_be_instance_of Bank::Account + + end + + it "Raises an error for an account that doesn't exist" do + proc { + Bank::Account.find("abcd").must_raise ArgumentError + } + end + end From 541de3f6b96995eac545c1365f6fda0a6b4c57d5 Mon Sep 17 00:00:00 2001 From: madamecurie Date: Wed, 22 Feb 2017 23:54:42 -0800 Subject: [PATCH 14/23] Testing for class method self.all return owner instances. --- lib/account.rb | 20 ++++++++++++++++---- specs/account_spec.rb | 34 ++++++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 281d0c04..ad1e9443 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -52,10 +52,22 @@ def add_owner (owner) end class Owner - attr_reader :name, :address - def initialize(name, address) - @name = name - @address = address + attr_reader :id, :last_name, :first_name, :street_address, :city, :state + def initialize(id, last_name, first_name, street_address, city, state) + @id = id + @last_name = last_name + @first_name = first_name + @street_address = street_address + @city = city + @state = state end + def self.all + owners = [] + CSV.read("support/owners.csv").each do |line| + owners << Owner.new(line[0], line[1], line[2], line[3], line[4], line[5]) + end + return owners + end + end end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 8fce28d1..69f87493 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -138,22 +138,21 @@ describe "Account#add_owner" do it "takes an owner and updates it" do account = Bank::Account.new(1337, 100) - owner = Bank::Owner.new("Rahul", "Renton,WA") + owner = Bank::Owner.new("26", "Berrie", "Kevin", "8113 Sutherland Center", "Seattle", "WA") account.add_owner(owner) account.owner.must_equal owner end end describe "Owner#initialize" do it "Takes a name and an address" do - name = "Rahul" - address = "Renton, WA" - owner = Bank::Owner.new(name, address) - - owner.must_respond_to :name - owner.name.must_equal name - - owner.must_respond_to :address - owner.address.must_equal address + first_name = "Kevin" + street_address = "8113 Sutherland Center" + owner = Bank::Owner.new("26", "Berrie", "Kevin", "8113 Sutherland Center", "Seattle", "WA") + owner.must_respond_to :first_name + owner.first_name.must_equal first_name + + owner.must_respond_to :street_address + owner.street_address.must_equal street_address end end @@ -202,3 +201,18 @@ } end end + + describe "Owner.all" do + it "Returns an array of all accounts" do + Bank::Owner.all.must_be_instance_of Array + Bank::Owner.all.each do |x| + x.must_be_instance_of Bank::Owner + end + Bank::Owner.all.length.must_equal 12 + Bank::Owner.all[0].id.must_equal "14" + Bank::Owner.all[0].last_name.must_equal "Morales" + Bank::Owner.all[11].id.must_equal "25" + Bank::Owner.all[11].last_name.must_equal "Clark" + + end + end From 8b9412b3d2af751d8b61f1af5ddf40e215ac93a8 Mon Sep 17 00:00:00 2001 From: madamecurie Date: Thu, 23 Feb 2017 00:14:52 -0800 Subject: [PATCH 15/23] Added self.find class method in owner class and tested it to return an instance of owner. --- lib/account.rb | 10 +++++++++- specs/account_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/account.rb b/lib/account.rb index ad1e9443..288c907d 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -27,7 +27,6 @@ def self.find(id) end end raise ArgumentError.new("account id doesn't exist") - end @@ -61,6 +60,7 @@ def initialize(id, last_name, first_name, street_address, city, state) @city = city @state = state end + def self.all owners = [] CSV.read("support/owners.csv").each do |line| @@ -69,5 +69,13 @@ def self.all return owners end + def self.find(id) + CSV.read("support/owners.csv").each do |line| + if line[0] == id + return Owner.new(line[0], line[1], line[2], line[3], line[4], line[5]) + end + end + raise ArgumentError.new("owner id doesn't exist") + end end end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 69f87493..9793eb9a 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -216,3 +216,25 @@ end end + + describe "Owner.find" do + it "Returns an owners that exists" do + Bank::Owner.find("20").must_be_instance_of Bank::Owner + end + + it "Can find the first owner from the CSV" do + Bank::Owner.find("14").must_be_instance_of Bank::Owner + + end + + it "Can find the last owner from the CSV" do + Bank::Owner.find("25").must_be_instance_of Bank::Owner + + end + + it "Raises an error for an owner that doesn't exist" do + proc { + Bank::Owner.find("ab").must_raise ArgumentError + } + end + end From 03f06472a9e54e00ceab02ec6d421dc9c0496065 Mon Sep 17 00:00:00 2001 From: madamecurie Date: Thu, 23 Feb 2017 21:32:44 -0800 Subject: [PATCH 16/23] Create two new classes CheckingAccount < Account and SavingsAccount < Account under lib/directory. --- lib/checking_account.rb | 4 ++++ lib/savings_account.rb | 4 ++++ 2 files changed, 8 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..f9392ff9 --- /dev/null +++ b/lib/checking_account.rb @@ -0,0 +1,4 @@ +module Bank + class CheckingAccount < Account + end +end diff --git a/lib/savings_account.rb b/lib/savings_account.rb new file mode 100644 index 00000000..ff992a1a --- /dev/null +++ b/lib/savings_account.rb @@ -0,0 +1,4 @@ +module Bank + class SavingsAccount < Account + end +end From 2e5ad61bb698ccb24c1edb26fa3a7fe707e71d3e Mon Sep 17 00:00:00 2001 From: madamecurie Date: Thu, 23 Feb 2017 22:20:01 -0800 Subject: [PATCH 17/23] Convert the values from cents to dollar in self.all (lib/account.rb). --- lib/account.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 288c907d..4c6f3334 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -1,5 +1,4 @@ require 'csv' -#require_relative '../support' module Bank class Account @@ -12,10 +11,10 @@ def initialize(id, balance = 0) @balance = balance end - def self.all + def self.all accounts = [] CSV.read("support/accounts.csv").each do |line| - accounts << Account.new(line[0], line[1].to_i) + accounts << Account.new(line[0], line[1].to_f/100) end return accounts end @@ -23,7 +22,7 @@ def self.all def self.find(id) CSV.read("support/accounts.csv").each do |line| if line[0] == id - return Account.new(line[0], line[1].to_i) + return Account.new(line[0], line[1].to_f/100) end end raise ArgumentError.new("account id doesn't exist") From 3eb43e83e70615bf5efebf620ad4510e5435606a Mon Sep 17 00:00:00 2001 From: madamecurie Date: Thu, 23 Feb 2017 23:41:24 -0800 Subject: [PATCH 18/23] Describe savings account and withdraw method. --- lib/savings_account.rb | 22 ++++++- specs/savings_account_spec.rb | 105 ++++++++++++++++++++++++---------- 2 files changed, 96 insertions(+), 31 deletions(-) diff --git a/lib/savings_account.rb b/lib/savings_account.rb index ff992a1a..6d631246 100644 --- a/lib/savings_account.rb +++ b/lib/savings_account.rb @@ -1,4 +1,22 @@ +require_relative 'account' module Bank - class SavingsAccount < Account - end + class SavingsAccount < Bank::Account + attr_reader :id, :balance + + def initialize(id, balance = 10) + raise ArgumentError.new("balance must be >= 10") if balance < 10 + + @id = id + @balance = balance + end + + def withdraw(amount) + new_balance = @balance - (amount+2) + if new_balance < 10 + puts "Warning: insufficient fund." + return @balance + end + @balance = new_balance + end +end end diff --git a/specs/savings_account_spec.rb b/specs/savings_account_spec.rb index 3f4d1e4a..76b0056c 100644 --- a/specs/savings_account_spec.rb +++ b/specs/savings_account_spec.rb @@ -3,7 +3,7 @@ require 'minitest/skip_dsl' # TODO: uncomment the next line once you start wave 3 and add lib/savings_account.rb -# require_relative '../lib/savings_account' +require_relative '../lib/savings_account' # Because a SavingsAccount is a kind # of Account, and we've already tested a bunch of functionality @@ -11,48 +11,95 @@ # Here we'll only test things that are different. # TODO: change 'xdescribe' to 'describe' to run these tests -xdescribe "SavingsAccount" do +describe "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) + id = 12345 + balance = 100.0 + account = Bank::SavingsAccount.new(id, balance) account.must_be_kind_of Bank::Account - end - it "Requires an initial balance of at least $10" do - # TODO: Your test code here! + account.must_respond_to :id + account.id.must_equal id + + account.must_respond_to :balance + account.balance.must_equal balance end end + # it "Requires an initial balance of at least $10" do + # # TODO: Your test code here! + it "Raises an ArgumentError when initial balance is less than $10" do + proc { + Bank::SavingsAccount.new(13371, 5.0) + }.must_raise ArgumentError + end +end +describe "#withdraw" do + it "Applies a $2 fee each time" do + # TODO: Your test code here! + start_balance = 100.0 + withdrawal_amount = 25.0 + account = Bank::SavingsAccount.new(13371, start_balance) - describe "#withdraw" do - it "Applies a $2 fee each time" do - # TODO: Your test code here! - end + account.withdraw(withdrawal_amount) - it "Outputs a warning if the balance would go below $10" do - # TODO: Your test code here! - end + expected_balance = start_balance - (withdrawal_amount+2) + account.balance.must_equal expected_balance + end - it "Doesn't modify the balance if it would go below $10" do - # TODO: Your test code here! - end + it "Outputs a warning if the balance would go below $10" do + # TODO: Your test code here! + start_balance = 100.0 + withdrawal_amount = 91.0 + account = Bank::SavingsAccount.new(13371, start_balance) + proc { + account.withdraw(withdrawal_amount) + }.must_output /.+/ + end + + it "Doesn't modify the balance if it would go below $10" do + # TODO: Your test code here! + start_balance = 100.0 + withdrawal_amount = 91.0 + account = Bank::SavingsAccount.new(13371, start_balance) + + 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 - it "Doesn't modify the balance if the fee would put it below $10" do - # TODO: Your test code here! - end end - describe "#add_interest" do - it "Returns the interest calculated" do - # TODO: Your test code here! - end + it "Doesn't modify the balance if the fee would put it below $10" do + # TODO: Your test code here! + start_balance = 100.0 + withdrawal_amount = 89.0 + account = Bank::SavingsAccount.new(13371, start_balance) - it "Updates the balance with calculated interest" do - # TODO: Your test code here! - end + updated_balance = account.withdraw(withdrawal_amount) - it "Requires a positive rate" do - # TODO: Your test code here! - 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 end + + +# describe "#add_interest" do +# it "Returns the interest calculated" do +# # TODO: Your test code here! +# end +# +# it "Updates the balance with calculated interest" do +# # TODO: Your test code here! +# end +# +# it "Requires a positive rate" do +# # TODO: Your test code here! +# end +# end +# end From 9154766406ec74ffb6dad56403803b3cd057bc9c Mon Sep 17 00:00:00 2001 From: madamecurie Date: Thu, 23 Feb 2017 23:56:51 -0800 Subject: [PATCH 19/23] Describe add_interest method in savings a/c. --- lib/savings_account.rb | 9 ++++++++- specs/savings_account_spec.rb | 36 +++++++++++++++++++++-------------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/lib/savings_account.rb b/lib/savings_account.rb index 6d631246..c6accbfe 100644 --- a/lib/savings_account.rb +++ b/lib/savings_account.rb @@ -18,5 +18,12 @@ def withdraw(amount) end @balance = new_balance end -end + + def add_interest(rate) + raise ArgumentError.new("rate must be >= 0") if rate < 0 + interest = @balance * rate/100 + @balance = @balance + interest + return interest + end + end end diff --git a/specs/savings_account_spec.rb b/specs/savings_account_spec.rb index 76b0056c..92fb2655 100644 --- a/specs/savings_account_spec.rb +++ b/specs/savings_account_spec.rb @@ -89,17 +89,25 @@ end -# describe "#add_interest" do -# it "Returns the interest calculated" do -# # TODO: Your test code here! -# end -# -# it "Updates the balance with calculated interest" do -# # TODO: Your test code here! -# end -# -# it "Requires a positive rate" do -# # TODO: Your test code here! -# end -# end -# end +describe "#add_interest" do + it "Returns the interest calculated" do + # TODO: Your test code here! + account = Bank::SavingsAccount.new(13371, 10000) + account.add_interest(0.25).must_equal 25 + end + + it "Updates the balance with calculated interest" do + # TODO: Your test code here! + account = Bank::SavingsAccount.new(13371, 10000) + account.add_interest(0.25) + account.balance.must_equal 10025 + end + + it "Requires a positive rate" do + # TODO: Your test code here! + account = Bank::SavingsAccount.new(13371, 10000) + proc { + account.add_interest(-0.25) + }.must_raise ArgumentError + end + end From 9ea4f9d708a8911d11181fc66efd536dbf08e02f Mon Sep 17 00:00:00 2001 From: madamecurie Date: Fri, 24 Feb 2017 00:36:32 -0800 Subject: [PATCH 20/23] Describe methode withdraw in checking a/c. --- lib/checking_account.rb | 19 ++++- specs/checking_account_spec.rb | 123 ++++++++++++++++++++------------- 2 files changed, 93 insertions(+), 49 deletions(-) diff --git a/lib/checking_account.rb b/lib/checking_account.rb index f9392ff9..6189dbb8 100644 --- a/lib/checking_account.rb +++ b/lib/checking_account.rb @@ -1,4 +1,21 @@ +require_relative 'account' module Bank - class CheckingAccount < Account + class CheckingAccount < Bank::Account + attr_reader :id, :balance + + def initialize(id, balance) + @id = id + @balance = balance + end + + def withdraw(amount) + new_balance = @balance - (amount+1) + if new_balance < 0 + puts "Warning: insufficient fund." + return @balance + end + @balance = new_balance + end + end end diff --git a/specs/checking_account_spec.rb b/specs/checking_account_spec.rb index 7f95339e..ab173b9d 100644 --- a/specs/checking_account_spec.rb +++ b/specs/checking_account_spec.rb @@ -3,7 +3,7 @@ require 'minitest/skip_dsl' # TODO: uncomment the next line once you start wave 3 and add lib/checking_account.rb -# require_relative '../lib/checking_account' +require_relative '../lib/checking_account' # Because a CheckingAccount is a kind # of Account, and we've already tested a bunch of functionality @@ -11,70 +11,97 @@ # Here we'll only test things that are different. # TODO: change 'xdescribe' to 'describe' to run these tests -xdescribe "CheckingAccount" do +describe "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) + id = 12345 + balance = 100.0 + account = Bank::CheckingAccount.new(id, balance) account.must_be_kind_of Bank::Account - end - end - describe "#withdraw" do - it "Applies a $1 fee each time" do - # TODO: Your test code here! - end + account.must_respond_to :id + account.id.must_equal id - it "Doesn't modify the balance if the fee would put it negative" do - # TODO: Your test code here! + account.must_respond_to :balance + account.balance.must_equal balance end end +end - describe "#withdraw_using_check" do - it "Reduces the balance" do - # TODO: Your test code here! - end - - it "Returns the modified balance" do - # TODO: Your test code here! - end - - it "Allows the balance to go down to -$10" do + describe "#withdraw" do + it "Applies a $1 fee each time" do # TODO: Your test code here! - end + start_balance = 100.0 + withdrawal_amount = 25.0 + account = Bank::CheckingAccount.new(12345, start_balance) - it "Outputs a warning if the account would go below -$10" do - # TODO: Your test code here! - end + account.withdraw(withdrawal_amount) - it "Doesn't modify the balance if the account would go below -$10" do - # TODO: Your test code here! + expected_balance = start_balance - (withdrawal_amount+1) + account.balance.must_equal expected_balance end - it "Requires a positive withdrawal amount" do + it "Doesn't modify the balance if the fee would put it negative" do # TODO: Your test code here! - end + start_balance = 100.0 + withdrawal_amount = 100.0 + account = Bank::CheckingAccount.new(12345, start_balance) - it "Allows 3 free uses" do - # TODO: Your test code here! - end + updated_balance = account.withdraw(withdrawal_amount) - it "Applies a $2 fee after the third use" do - # TODO: Your test code here! + # 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 end - describe "#reset_checks" do - it "Can be called without error" do - # TODO: Your test code here! - end - - it "Makes the next three checks free if less than 3 checks had been used" do - # TODO: Your test code here! - end - - it "Makes the next three checks free if more than 3 checks had been used" do - # TODO: Your test code here! - end - end -end +# describe "#withdraw_using_check" do +# it "Reduces the balance" do +# # TODO: Your test code here! +# end +# +# it "Returns the modified balance" do +# # TODO: Your test code here! +# end +# +# it "Allows the balance to go down to -$10" do +# # TODO: Your test code here! +# end +# +# it "Outputs a warning if the account would go below -$10" do +# # TODO: Your test code here! +# end +# +# it "Doesn't modify the balance if the account would go below -$10" do +# # TODO: Your test code here! +# end +# +# it "Requires a positive withdrawal amount" do +# # TODO: Your test code here! +# end +# +# it "Allows 3 free uses" do +# # TODO: Your test code here! +# end +# +# it "Applies a $2 fee after the third use" do +# # TODO: Your test code here! +# end +# end +# +# describe "#reset_checks" do +# it "Can be called without error" do +# # TODO: Your test code here! +# end +# +# it "Makes the next three checks free if less than 3 checks had been used" do +# # TODO: Your test code here! +# end +# +# it "Makes the next three checks free if more than 3 checks had been used" do +# # TODO: Your test code here! +# end +# end +# end From 2d0bc304382916f1dede2888a136111c431b3c6d Mon Sep 17 00:00:00 2001 From: madamecurie Date: Sat, 25 Feb 2017 14:33:29 -0800 Subject: [PATCH 21/23] intialized checks in class CheckingAccount --- lib/checking_account.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/checking_account.rb b/lib/checking_account.rb index 6189dbb8..2e03178e 100644 --- a/lib/checking_account.rb +++ b/lib/checking_account.rb @@ -6,6 +6,7 @@ class CheckingAccount < Bank::Account def initialize(id, balance) @id = id @balance = balance + @checks = 0 end def withdraw(amount) From e6da934c6f010a534375f7deb95e20c7c98aef8a Mon Sep 17 00:00:00 2001 From: madamecurie Date: Sun, 26 Feb 2017 18:02:09 -0800 Subject: [PATCH 22/23] Tested withdraw using checks in checking a/c. --- lib/checking_account.rb | 21 ++++ specs/checking_account_spec.rb | 178 +++++++++++++++++++++------------ 2 files changed, 133 insertions(+), 66 deletions(-) diff --git a/lib/checking_account.rb b/lib/checking_account.rb index 2e03178e..d12fdc0a 100644 --- a/lib/checking_account.rb +++ b/lib/checking_account.rb @@ -10,6 +10,7 @@ def initialize(id, balance) end def withdraw(amount) + new_balance = @balance - (amount+1) if new_balance < 0 puts "Warning: insufficient fund." @@ -18,5 +19,25 @@ def withdraw(amount) @balance = new_balance end + def withdraw_using_check(amount) + raise ArgumentError.new "Please enter positive amount" if amount < 0 + @checks += 1 + if @checks > 3 + fee = 2 + else + fee = 0 + end + new_balance = @balance - (amount+fee) + + + if new_balance < -10 + puts "Warning: insufficient fund." + return @balance + end + @balance = new_balance + end + def reset_checks + @checks = 0 + end end end diff --git a/specs/checking_account_spec.rb b/specs/checking_account_spec.rb index ab173b9d..8ba3c919 100644 --- a/specs/checking_account_spec.rb +++ b/specs/checking_account_spec.rb @@ -29,79 +29,125 @@ end end - describe "#withdraw" do - it "Applies a $1 fee each time" do - # TODO: Your test code here! - start_balance = 100.0 - withdrawal_amount = 25.0 - account = Bank::CheckingAccount.new(12345, start_balance) +describe "#withdraw" do + it "Applies a $1 fee each time" do + # TODO: Your test code here! + start_balance = 100.0 + withdrawal_amount = 25.0 + account = Bank::CheckingAccount.new(12345, start_balance) + + account.withdraw(withdrawal_amount) + + expected_balance = start_balance - (withdrawal_amount+1) + account.balance.must_equal expected_balance + end - account.withdraw(withdrawal_amount) + it "Doesn't modify the balance if the fee would put it negative" do + # TODO: Your test code here! + start_balance = 100.0 + withdrawal_amount = 100.0 + account = Bank::CheckingAccount.new(12345, start_balance) + + 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 +end - expected_balance = start_balance - (withdrawal_amount+1) - account.balance.must_equal expected_balance +describe "#withdraw_using_check" do + describe "when withdrawing less than the balance" do + it "Reduces the balance" do + # TODO: Your test code here! + start_balance = 100.0 + checking_account = Bank::CheckingAccount.new(12345, start_balance) + end_balance = checking_account.withdraw_using_check(75) + end_balance.must_equal 25 end - it "Doesn't modify the balance if the fee would put it negative" do + it "Doesn't print anything" do + # TODO: Your test code here! + start_balance = 100.0 + checking_account = Bank::CheckingAccount.new(12345, start_balance) + proc{checking_account.withdraw_using_check(75)}.must_be_silent + end + end + describe "when withdrawing goes into overdraft" do + it "Reduces the balance" do + # TODO: Your test code here! + start_balance = 100.0 + checking_account = Bank::CheckingAccount.new(12345, start_balance) + end_balance = checking_account.withdraw_using_check(105) + end_balance.must_equal -5 + end + it "Doesn't print anything" do + # TODO: Your test code here! + start_balance = 100.0 + checking_account = Bank::CheckingAccount.new(12345, start_balance) + proc{checking_account.withdraw_using_check(105)}.must_be_silent + end + end + describe "when withdrawing goes beyond overdraft limit" do + it "Doesn't reduce the balance" do # TODO: Your test code here! start_balance = 100.0 - withdrawal_amount = 100.0 - account = Bank::CheckingAccount.new(12345, start_balance) + checking_account = Bank::CheckingAccount.new(12345, start_balance) + end_balance = checking_account.withdraw_using_check(111) + end_balance.must_equal 100 + end + it "Prints a warning" do + # TODO: Your test code here! + start_balance = 100.0 + checking_account = Bank::CheckingAccount.new(12345, start_balance) + proc{checking_account.withdraw_using_check(111)}.must_output "Warning: insufficient fund.\n" + end + end + it "Requires a positive withdrawal amount" do + start_balance = 0 + checking_account = Bank::CheckingAccount.new(12345, start_balance) + proc{checking_account.withdraw_using_check(-10)}.must_raise ArgumentError + end - updated_balance = account.withdraw(withdrawal_amount) + it "Allows 3 free uses" do + start_balance = 100 + checking_account = Bank::CheckingAccount.new(12345, start_balance) + 3.times {checking_account.withdraw_using_check(10)} + checking_account.balance.must_equal start_balance - 30 + 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 + it "Applies a $2 fee after the third use" do + start_balance = 100 + checking_account = Bank::CheckingAccount.new(12345, start_balance) + 4.times {checking_account.withdraw_using_check(10)} + checking_account.balance.must_equal start_balance - 42 + end end - end -# describe "#withdraw_using_check" do -# it "Reduces the balance" do -# # TODO: Your test code here! -# end -# -# it "Returns the modified balance" do -# # TODO: Your test code here! -# end -# -# it "Allows the balance to go down to -$10" do -# # TODO: Your test code here! -# end -# -# it "Outputs a warning if the account would go below -$10" do -# # TODO: Your test code here! -# end -# -# it "Doesn't modify the balance if the account would go below -$10" do -# # TODO: Your test code here! -# end -# -# it "Requires a positive withdrawal amount" do -# # TODO: Your test code here! -# end -# -# it "Allows 3 free uses" do -# # TODO: Your test code here! -# end -# -# it "Applies a $2 fee after the third use" do -# # TODO: Your test code here! -# end -# end -# -# describe "#reset_checks" do -# it "Can be called without error" do -# # TODO: Your test code here! -# end -# -# it "Makes the next three checks free if less than 3 checks had been used" do -# # TODO: Your test code here! -# end -# -# it "Makes the next three checks free if more than 3 checks had been used" do -# # TODO: Your test code here! -# end -# end -# end + describe "#reset_checks" do + it "Can be called without error" do + start_balance = 100 + checking_account = Bank::CheckingAccount.new(12345, start_balance) + proc{checking_account.reset_checks}.must_be_silent + end + + + it "Makes the next three checks free if less than 3 checks had been used" do + start_balance = 100 + checking_account = Bank::CheckingAccount.new(12345, start_balance) + 2.times {checking_account.withdraw_using_check(10)} + checking_account.reset_checks + 3.times {checking_account.withdraw_using_check(10)} + checking_account.balance.must_equal start_balance - 50 +end + + it "Makes the next three checks free if more than 3 checks had been used" do + start_balance = 100 + checking_account = Bank::CheckingAccount.new(12345, start_balance) + 4.times {checking_account.withdraw_using_check(10)} + checking_account.reset_checks + 3.times {checking_account.withdraw_using_check(10)} + checking_account.balance.must_equal start_balance - 72 +end + end From 454c45d73a9b9062a84b06838a3191b55766a749 Mon Sep 17 00:00:00 2001 From: madamecurie Date: Sun, 26 Feb 2017 18:18:00 -0800 Subject: [PATCH 23/23] Fixed balance must equal value in dollar. --- specs/account_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 9793eb9a..b507c675 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -173,9 +173,9 @@ # - The ID and balance of the first and last # accounts match what's in the CSV file Bank::Account.all[0].id.must_equal "1212" - Bank::Account.all[0].balance.must_equal 1235667 + Bank::Account.all[0].balance.must_equal 12356.67 Bank::Account.all[11].id.must_equal "15156" - Bank::Account.all[11].balance.must_equal 4356772 + Bank::Account.all[11].balance.must_equal 43567.72 end end