From 4f85a7aaf4c057e92e637d43b4dc8f2342bbafa0 Mon Sep 17 00:00:00 2001 From: Brenna Date: Tue, 21 Feb 2017 14:47:37 -0800 Subject: [PATCH 01/24] added proj requirements in comments to account.rb --- lib/account.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/account.rb b/lib/account.rb index e69de29b..9fd08131 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -0,0 +1,17 @@ +# Create a Bank module which will contain your Account class and any future bank account logic. +# Create an Account class which should have the following functionality: +# A new account should be created with an ID and an initial balance +# Should have a withdraw method that accepts a single parameter which represents the amount of money that will be withdrawn. This method should return the updated account balance. +# Should have a deposit method that accepts a single parameter which represents the amount of money that will be deposited. This method should return the updated account balance. +# Should be able to access the current balance of an account at any time. + +# Error handling +# A new account cannot be created with initial negative balance - this will raise an ArgumentError (Google this) +# The withdraw method does not allow the account to go negative. Instead it will output a warning message and return the original un-modified balance. + +# Optional: +# Make sure to write tests for any optionals you implement! +# Create an Owner class which will store information about those who own the Accounts. +# This should have info like name and address and any other identifying information that an account owner would have. +# Add an owner property to each Account to track information about who owns the account. +# The Account can be created with an owner, OR you can create a method that will add the owner after the Account has already been created. From b4984e8a0ecc704b2da1fdecc5131dc7ef7ff427 Mon Sep 17 00:00:00 2001 From: Brenna Date: Tue, 21 Feb 2017 15:25:15 -0800 Subject: [PATCH 02/24] created Bank module, Account class, instance vars @id and @balance, reader method for @id, and accessor method for @balance --- lib/account.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/account.rb b/lib/account.rb index 9fd08131..6f9701a6 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -1,5 +1,19 @@ # Create a Bank module which will contain your Account class and any future bank account logic. # Create an Account class which should have the following functionality: +module Bank + + class Account + attr_reader :id + attr_accessor :balance + + def initialize(id, balance) + @id = id + @balance = balance + end + + end + +end # A new account should be created with an ID and an initial balance # Should have a withdraw method that accepts a single parameter which represents the amount of money that will be withdrawn. This method should return the updated account balance. # Should have a deposit method that accepts a single parameter which represents the amount of money that will be deposited. This method should return the updated account balance. From 0ca3728a02d491d45246c9ba048ae9cc15bbca7d Mon Sep 17 00:00:00 2001 From: Brenna Date: Tue, 21 Feb 2017 15:36:01 -0800 Subject: [PATCH 03/24] raises error if negative balance initialized, added withdrawal method --- lib/account.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/account.rb b/lib/account.rb index 6f9701a6..8161d26f 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -8,7 +8,15 @@ class Account def initialize(id, balance) @id = id - @balance = balance + if balance >= 0 + @balance = balance + else + raise ArgumentError.new "You cannot create a bank account with a negative balance." + end + end + + def withdraw(withdrawal_amount) + @balance -= withdrawal_amount end end From eb692d1ba5b7426c195ee279a56e4c1e5eead024 Mon Sep 17 00:00:00 2001 From: Brenna Date: Tue, 21 Feb 2017 15:55:04 -0800 Subject: [PATCH 04/24] prints warning if withdrawal causes balance to go negative --- lib/account.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/account.rb b/lib/account.rb index 8161d26f..8635a53e 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -17,6 +17,11 @@ def initialize(id, balance) def withdraw(withdrawal_amount) @balance -= withdrawal_amount + if @balance < 0 + print "Uh oh! You've overdrawn your account!" + else + return @balance + end end end From b517f973d83076800262b497ff7c22ca1689396f Mon Sep 17 00:00:00 2001 From: Brenna Date: Tue, 21 Feb 2017 16:11:35 -0800 Subject: [PATCH 05/24] prevents user from entering a negative num to withdraw --- lib/account.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 8635a53e..f162e637 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -16,11 +16,15 @@ def initialize(id, balance) end def withdraw(withdrawal_amount) - @balance -= withdrawal_amount - if @balance < 0 - print "Uh oh! You've overdrawn your account!" - else + if withdrawal_amount > 0 + if withdrawal_amount > @balance + print "Uh oh! You've overdrawn your account!" + else + return @balance -= withdrawal_amount + end return @balance + else + raise ArgumentError.new "You cannot withdraw a negative number." end end From 95e2c65310b2d185534ef51299fa8a57ad52ebf0 Mon Sep 17 00:00:00 2001 From: Brenna Date: Tue, 21 Feb 2017 16:13:24 -0800 Subject: [PATCH 06/24] add deposit method --- lib/account.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/account.rb b/lib/account.rb index f162e637..11f83f4b 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -28,6 +28,10 @@ def withdraw(withdrawal_amount) end end + def deposit(deposit_amount) + @balance += deposit_amount + end + end end From 809768a910c62a1a23d3d59509059ba879ca8c0b Mon Sep 17 00:00:00 2001 From: Brenna Date: Tue, 21 Feb 2017 16:26:14 -0800 Subject: [PATCH 07/24] deposit method only accepts positive nums, code meets wave1 requirements --- lib/account.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 11f83f4b..a57dd73b 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -1,5 +1,4 @@ -# Create a Bank module which will contain your Account class and any future bank account logic. -# Create an Account class which should have the following functionality: + module Bank class Account @@ -11,25 +10,29 @@ def initialize(id, balance) if balance >= 0 @balance = balance else - raise ArgumentError.new "You cannot create a bank account with a negative balance." + raise ArgumentError.new "You cannot create a bank account with a negative balance, you goober." end end def withdraw(withdrawal_amount) if withdrawal_amount > 0 if withdrawal_amount > @balance - print "Uh oh! You've overdrawn your account!" + print "Uh oh! You have overdrawn your account, you doof!" else return @balance -= withdrawal_amount end return @balance else - raise ArgumentError.new "You cannot withdraw a negative number." + raise ArgumentError.new "You cannot withdraw a negative amount of money, you silly pants." end end def deposit(deposit_amount) - @balance += deposit_amount + if deposit_amount > 0 + @balance += deposit_amount + else + raise ArgumentError.new "You cannot deposit a negative amount of money, you goofball." + end end end From 97ab4e957dda883e94d93cfb151e4ced873049ea Mon Sep 17 00:00:00 2001 From: Brenna Date: Tue, 21 Feb 2017 16:55:43 -0800 Subject: [PATCH 08/24] option wave1: created owner class, added owner parameter to account_test --- lib/account.rb | 28 +++++++------------ lib/owner.rb | 15 ++++++++++ specs/account_spec.rb | 64 +++++++++++++++++++++++++++++-------------- 3 files changed, 68 insertions(+), 39 deletions(-) create mode 100644 lib/owner.rb diff --git a/lib/account.rb b/lib/account.rb index a57dd73b..b532a6ad 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -2,11 +2,18 @@ module Bank class Account - attr_reader :id - attr_accessor :balance + attr_reader :id, :owner, :balance - def initialize(id, balance) + # Optional: + # Make sure to write tests for any optionals you implement! + # Create an Owner class which will store information about those who own the Accounts. + # This should have info like name and address and any other identifying information that an account owner would have. + # Add an owner property to each Account to track information about who owns the account. + # The Account can be created with an owner, OR you can create a method that will add the owner after the Account has already been created. + + def initialize(id, owner, balance) @id = id + @owner = owner if balance >= 0 @balance = balance else @@ -38,18 +45,3 @@ def deposit(deposit_amount) end end -# A new account should be created with an ID and an initial balance -# Should have a withdraw method that accepts a single parameter which represents the amount of money that will be withdrawn. This method should return the updated account balance. -# Should have a deposit method that accepts a single parameter which represents the amount of money that will be deposited. This method should return the updated account balance. -# Should be able to access the current balance of an account at any time. - -# Error handling -# A new account cannot be created with initial negative balance - this will raise an ArgumentError (Google this) -# The withdraw method does not allow the account to go negative. Instead it will output a warning message and return the original un-modified balance. - -# Optional: -# Make sure to write tests for any optionals you implement! -# Create an Owner class which will store information about those who own the Accounts. -# This should have info like name and address and any other identifying information that an account owner would have. -# Add an owner property to each Account to track information about who owns the account. -# The Account can be created with an owner, OR you can create a method that will add the owner after the Account has already been created. diff --git a/lib/owner.rb b/lib/owner.rb new file mode 100644 index 00000000..73d90fa4 --- /dev/null +++ b/lib/owner.rb @@ -0,0 +1,15 @@ +module Bank + + class Owner + attr_reader :name, :address, :birthday, :favefood + + def initialize(owner_hash) + @name = owner_hash[:name] + @address = owner_hash[:address] + @birthday = owner_hash[:birthday] + @favefood = owner_hash[:favefood] + end + + end + +end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 6c399139..8ecaafc7 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -2,34 +2,42 @@ require 'minitest/reporters' require 'minitest/skip_dsl' require_relative '../lib/account' +require_relative '../lib/owner' + +# before do +# brenna_hash = {name: "Brenna Darroch:", address: "3426 Cotton Top Ct", birthday: "May 22, 1993", favefood: "chocolate"} +# @brenna = Owner.new(brenna_hash) +# end 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) + owner = "Brenna" + account = Bank::Account.new(id, owner, balance) account.must_respond_to :id account.id.must_equal id + account.must_respond_to :owner + account.owner.must_equal owner + account.must_respond_to :balance account.balance.must_equal balance end it "Raises an ArgumentError when created with a negative balance" do - # Note: we haven't talked about procs yet. You can think - # of them like blocks that sit by themselves. - # This code checks that, when the proc is executed, it - # raises an ArgumentError. + # 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) + Bank::Account.new(1337, "Brenna", -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) + Bank::Account.new(1337, "Brenna", 0) end end @@ -37,7 +45,7 @@ it "Reduces the balance" do start_balance = 100.0 withdrawal_amount = 25.0 - account = Bank::Account.new(1337, start_balance) + account = Bank::Account.new(1337, "Brenna", start_balance) account.withdraw(withdrawal_amount) @@ -48,7 +56,7 @@ it "Returns the modified balance" do start_balance = 100.0 withdrawal_amount = 25.0 - account = Bank::Account.new(1337, start_balance) + account = Bank::Account.new(1337, "Brenna", start_balance) updated_balance = account.withdraw(withdrawal_amount) @@ -59,12 +67,8 @@ 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. + account = Bank::Account.new(1337, "Brenna", 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 /.+/ @@ -73,7 +77,7 @@ 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) + account = Bank::Account.new(1337, "Brenna", start_balance) updated_balance = account.withdraw(withdrawal_amount) @@ -84,7 +88,7 @@ end it "Allows the balance to go to 0" do - account = Bank::Account.new(1337, 100.0) + account = Bank::Account.new(1337, "Brenna", 100.0) updated_balance = account.withdraw(account.balance) updated_balance.must_equal 0 account.balance.must_equal 0 @@ -93,7 +97,7 @@ it "Requires a positive withdrawal amount" do start_balance = 100.0 withdrawal_amount = -25.0 - account = Bank::Account.new(1337, start_balance) + account = Bank::Account.new(1337, "Brenna", start_balance) proc { account.withdraw(withdrawal_amount) @@ -105,7 +109,7 @@ it "Increases the balance" do start_balance = 100.0 deposit_amount = 25.0 - account = Bank::Account.new(1337, start_balance) + account = Bank::Account.new(1337, "Brenna", start_balance) account.deposit(deposit_amount) @@ -116,7 +120,7 @@ it "Returns the modified balance" do start_balance = 100.0 deposit_amount = 25.0 - account = Bank::Account.new(1337, start_balance) + account = Bank::Account.new(1337, "Brenna", start_balance) updated_balance = account.deposit(deposit_amount) @@ -127,12 +131,30 @@ it "Requires a positive deposit amount" do start_balance = 100.0 deposit_amount = -25.0 - account = Bank::Account.new(1337, start_balance) + account = Bank::Account.new(1337, "Brenna", start_balance) proc { account.deposit(deposit_amount) }.must_raise ArgumentError end + + describe "Account#initialize" do + it "Takes an ID and an initial balance" do + id = 1337 + balance = 100.0 + owner = "Brenna" + account = Bank::Account.new(id, owner, balance) + + account.must_respond_to :id + account.id.must_equal id + + account.must_respond_to :owner + account.owner.must_equal owner + + account.must_respond_to :balance + account.balance.must_equal balance + end + end end end From add88f2bb137be99708a87f1eee058acc8957468 Mon Sep 17 00:00:00 2001 From: Brenna Date: Tue, 21 Feb 2017 18:18:24 -0800 Subject: [PATCH 09/24] made tests for owner reader methods --- lib/account.rb | 7 ----- lib/owner.rb | 1 + specs/account_spec.rb | 62 +++++++++++++++++-------------------------- 3 files changed, 26 insertions(+), 44 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index b532a6ad..191020d6 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -4,13 +4,6 @@ module Bank class Account attr_reader :id, :owner, :balance - # Optional: - # Make sure to write tests for any optionals you implement! - # Create an Owner class which will store information about those who own the Accounts. - # This should have info like name and address and any other identifying information that an account owner would have. - # Add an owner property to each Account to track information about who owns the account. - # The Account can be created with an owner, OR you can create a method that will add the owner after the Account has already been created. - def initialize(id, owner, balance) @id = id @owner = owner diff --git a/lib/owner.rb b/lib/owner.rb index 73d90fa4..65279dec 100644 --- a/lib/owner.rb +++ b/lib/owner.rb @@ -1,3 +1,4 @@ + module Bank class Owner diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 8ecaafc7..02447a8c 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -4,24 +4,29 @@ require_relative '../lib/account' require_relative '../lib/owner' -# before do -# brenna_hash = {name: "Brenna Darroch:", address: "3426 Cotton Top Ct", birthday: "May 22, 1993", favefood: "chocolate"} -# @brenna = Owner.new(brenna_hash) -# end - describe "Wave 1" do + + before do + brenna_hash = {name: "Brenna Darroch", address: "3426 Cotton Top Ct", birthday: "May 22, 1993", favefood: "chocolate"} + @brenna = Bank::Owner.new(brenna_hash) + end # remember, end is garbage collector! + describe "Account#initialize" do - it "Takes an ID and an initial balance" do + it "Takes an ID, an instance of Owner, and an initial balance" do id = 1337 balance = 100.0 - owner = "Brenna" - account = Bank::Account.new(id, owner, balance) + account = Bank::Account.new(id, @brenna, balance) account.must_respond_to :id account.id.must_equal id account.must_respond_to :owner - account.owner.must_equal owner + account.owner.must_equal @brenna + + account.owner.name.must_equal "Brenna Darroch" + account.owner.address.must_equal "3426 Cotton Top Ct" + account.owner.birthday.must_equal "May 22, 1993" + account.owner.favefood.must_equal "chocolate" account.must_respond_to :balance account.balance.must_equal balance @@ -31,13 +36,13 @@ # 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, "Brenna", -100.0) + Bank::Account.new(1337, @brenna, -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, "Brenna", 0) + Bank::Account.new(1337, @brenna, 0) end end @@ -45,7 +50,7 @@ it "Reduces the balance" do start_balance = 100.0 withdrawal_amount = 25.0 - account = Bank::Account.new(1337, "Brenna", start_balance) + account = Bank::Account.new(1337, @brenna, start_balance) account.withdraw(withdrawal_amount) @@ -56,7 +61,7 @@ it "Returns the modified balance" do start_balance = 100.0 withdrawal_amount = 25.0 - account = Bank::Account.new(1337, "Brenna", start_balance) + account = Bank::Account.new(1337, @brenna, start_balance) updated_balance = account.withdraw(withdrawal_amount) @@ -67,7 +72,7 @@ it "Outputs a warning if the account would go negative" do start_balance = 100.0 withdrawal_amount = 200.0 - account = Bank::Account.new(1337, "Brenna", start_balance) + account = Bank::Account.new(1337, @brenna, 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) @@ -77,7 +82,7 @@ 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, "Brenna", start_balance) + account = Bank::Account.new(1337, @brenna, start_balance) updated_balance = account.withdraw(withdrawal_amount) @@ -88,7 +93,7 @@ end it "Allows the balance to go to 0" do - account = Bank::Account.new(1337, "Brenna", 100.0) + account = Bank::Account.new(1337, @brenna, 100.0) updated_balance = account.withdraw(account.balance) updated_balance.must_equal 0 account.balance.must_equal 0 @@ -97,7 +102,7 @@ it "Requires a positive withdrawal amount" do start_balance = 100.0 withdrawal_amount = -25.0 - account = Bank::Account.new(1337, "Brenna", start_balance) + account = Bank::Account.new(1337, @brenna, start_balance) proc { account.withdraw(withdrawal_amount) @@ -109,7 +114,7 @@ it "Increases the balance" do start_balance = 100.0 deposit_amount = 25.0 - account = Bank::Account.new(1337, "Brenna", start_balance) + account = Bank::Account.new(1337, @brenna, start_balance) account.deposit(deposit_amount) @@ -120,7 +125,7 @@ it "Returns the modified balance" do start_balance = 100.0 deposit_amount = 25.0 - account = Bank::Account.new(1337, "Brenna", start_balance) + account = Bank::Account.new(1337, @brenna, start_balance) updated_balance = account.deposit(deposit_amount) @@ -131,30 +136,13 @@ it "Requires a positive deposit amount" do start_balance = 100.0 deposit_amount = -25.0 - account = Bank::Account.new(1337, "Brenna", start_balance) + account = Bank::Account.new(1337, @brenna, start_balance) proc { account.deposit(deposit_amount) }.must_raise ArgumentError end - describe "Account#initialize" do - it "Takes an ID and an initial balance" do - id = 1337 - balance = 100.0 - owner = "Brenna" - account = Bank::Account.new(id, owner, balance) - - account.must_respond_to :id - account.id.must_equal id - - account.must_respond_to :owner - account.owner.must_equal owner - - account.must_respond_to :balance - account.balance.must_equal balance - end - end end end From 34c20f4db35e52931211cc5899e2ef3cd6e2f7d3 Mon Sep 17 00:00:00 2001 From: Brenna Darroch Date: Wed, 22 Feb 2017 14:02:40 -0800 Subject: [PATCH 10/24] uncomment wave 2 requirements --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6251da8d..1ca1cd2f 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Make sure to write tests for any optionals you implement! - Add an `owner` property to each Account to track information about who owns the account. - The `Account` can be created with an `owner`, OR you can create a method that will add the `owner` after the `Account` has already been created. - + + From a7a4dbce96ca4773a2f5786c53d9c44ec21ae624 Mon Sep 17 00:00:00 2001 From: Brenna Date: Thu, 23 Feb 2017 17:16:57 -0800 Subject: [PATCH 19/24] edited wave 2 code, created savings and checking account stub classes --- lib/account.rb | 25 ++++++++------- lib/checking.rb | 11 +++++++ lib/savings.rb | 11 +++++++ specs/account_spec.rb | 58 ++++++++++++++++++++++------------ specs/checking_account_spec.rb | 3 +- specs/savings_account_spec.rb | 3 +- 6 files changed, 74 insertions(+), 37 deletions(-) create mode 100644 lib/checking.rb create mode 100644 lib/savings.rb diff --git a/lib/account.rb b/lib/account.rb index 3e7e377e..806d3e8a 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -6,6 +6,14 @@ class Account attr_reader :id, :balance, :date attr_accessor :owner + def initialize(id, balance, date, owner = "Customer Name") + raise ArgumentError.new("You cannot create a bank account with a 0 or negative balance, you goober.") if balance < 0 + @id = id + @balance = balance + @date = date + @owner = owner + end + def self.all all_accounts = [] CSV.open("/Users/brenna/ada/week3/BankAccounts/support/accounts.csv").each do | line | @@ -15,21 +23,14 @@ def self.all end def self.find(id) - accounts = Bank::Account.all - raise ArgumentError.new("There's no such account ID, you nincompoop.") if ![1212, 1213, 1214, 1215, 1216, 1217, 15151, 15152, 15153, 15154, 15155, 15156].include?(id) - accounts.each_with_index do |acct, index| + accounts = Bank::Account.all + # raise ArgumentError.new("There's no such account ID, you nincompoop.") if ![1212, 1213, 1214, 1215, 1216, 1217, 15151, 15152, 15153, 15154, 15155, 15156].include?(id) + accounts.each do |acct| if acct.id == id - return accounts[index] + return acct end end - end - - def initialize(id, balance, date, owner = "Customer Name") - raise ArgumentError.new("You cannot create a bank account with a 0 or negative balance, you goober.") if balance < 0 - @id = id - @balance = balance - @date = date - @owner = owner + raise ArgumentError.new("There's no such account ID, you nincompoop.") end def withdraw(withdrawal_amount) diff --git a/lib/checking.rb b/lib/checking.rb new file mode 100644 index 00000000..8efdb953 --- /dev/null +++ b/lib/checking.rb @@ -0,0 +1,11 @@ + +module Bank + + class CheckingAccount + + def initialize + end + + end + +end diff --git a/lib/savings.rb b/lib/savings.rb new file mode 100644 index 00000000..0d843d5d --- /dev/null +++ b/lib/savings.rb @@ -0,0 +1,11 @@ + +module Bank + + class SavingsAccount + + def initialize + end + + end + +end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 6a9a76b5..b521cbb3 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -8,7 +8,7 @@ describe "Wave 1" do before do - brenna_hash = {name: "Brenna Darroch", address: "3426 Cotton Top Ct", birthday: "May 22, 1993", favefood: "chocolate"} + brenna_hash = {id: 0522, last_name: "Darroch", first_name: "Brenna", st_address: "3426 Cotton Top Ct", city: "Fairfax", state: "VA"} @brenna = Bank::Owner.new(brenna_hash) end # remember, end is garbage collector! @@ -31,10 +31,9 @@ account.must_respond_to :owner account.owner.must_equal @brenna - account.owner.name.must_equal "Brenna Darroch" - account.owner.address.must_equal "3426 Cotton Top Ct" - account.owner.birthday.must_equal "May 22, 1993" - account.owner.favefood.must_equal "chocolate" + account.owner.last_name.must_equal "Darroch" + account.owner.first_name.must_equal "Brenna" + account.owner.st_address.must_equal "3426 Cotton Top Ct" end it "Raises an ArgumentError when created with a negative balance" do @@ -153,34 +152,51 @@ end describe "Wave 2" do + # to make test adaptable (eg. calling diff account files), need to read in CSV file to spec file too + + before do + @accounts = Bank::Account.all + end describe "Account.all" do + it "Returns an array of all accounts" do - # - Account.all returns an array - Bank::Account.all.must_be_kind_of Array - # - Everything in the array is an Account - Bank::Account.all.each do |inst| + @accounts.must_be_kind_of Array + end + + it "Everything in the array is an Account" do + # Bank::Account.all.each do |inst| + @accounts.each do |inst| inst.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 + end - Bank::Account.all[-1].id.must_equal 15156 - Bank::Account.all[-1].balance.must_equal 4356772 + it "The number of accounts is correct" do + # Bank::Account.all.length.must_equal 12 + @accounts.length.must_equal 12 + end + + it "The ID and balance of the first and last accounts match what's in the CSV file" do + @accounts[0].id.must_equal 1212 + @accounts[0].balance.must_equal 1235667 + + @accounts[-1].id.must_equal 15156 + @accounts[-1].balance.must_equal 4356772 + end + it "The elements match what's in the file" do + index = 0 + CSV.read("support/accounts.csv") do |line| + accounts[index].id.must_equal line[0].to_i + accounts[index].balance.must_equal line[1].to_f + accounts[index].date.must_equal line[2] + index += 1 + end end end describe "Account.find" do - # before do - # all_accounts = Bank - # end - it "Returns an account that exists" do # doesn't work due to different object IDs of Bank::Account.all ?? # Bank::Account.find(1216).must_be_same_as Bank::Account.all[4] diff --git a/specs/checking_account_spec.rb b/specs/checking_account_spec.rb index 7f95339e..8fb807e5 100644 --- a/specs/checking_account_spec.rb +++ b/specs/checking_account_spec.rb @@ -2,8 +2,7 @@ require 'minitest/reporters' 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' # Because a CheckingAccount is a kind # of Account, and we've already tested a bunch of functionality diff --git a/specs/savings_account_spec.rb b/specs/savings_account_spec.rb index 3f4d1e4a..6beefe05 100644 --- a/specs/savings_account_spec.rb +++ b/specs/savings_account_spec.rb @@ -2,8 +2,7 @@ require 'minitest/reporters' 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' # Because a SavingsAccount is a kind # of Account, and we've already tested a bunch of functionality From 4eb1d95bcb580dbfe6e76782758b241e2af90cdf Mon Sep 17 00:00:00 2001 From: Brenna Date: Thu, 23 Feb 2017 17:55:50 -0800 Subject: [PATCH 20/24] savings acct cannot have a balance below 10 after initialization or withdrawal --- lib/account.rb | 10 +++++----- lib/checking.rb | 6 ++---- lib/savings.rb | 19 ++++++++++++++++-- specs/account_spec.rb | 3 --- specs/checking_account_spec.rb | 3 --- specs/savings_account_spec.rb | 36 +++++++++++++++++++++------------- 6 files changed, 46 insertions(+), 31 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 806d3e8a..d3e15d7e 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -7,7 +7,7 @@ class Account attr_accessor :owner def initialize(id, balance, date, owner = "Customer Name") - raise ArgumentError.new("You cannot create a bank account with a 0 or negative balance, you goober.") if balance < 0 + raise ArgumentError.new("You cannot create a bank account with a 0 or negative balance.") if balance < 0 @id = id @balance = balance @date = date @@ -30,14 +30,14 @@ def self.find(id) return acct end end - raise ArgumentError.new("There's no such account ID, you nincompoop.") + raise ArgumentError.new("There's no such account ID.") end def withdraw(withdrawal_amount) - raise ArgumentError.new("You cannot withdraw a negative amount of money, you silly pants.") if withdrawal_amount < 0 + raise ArgumentError.new("You cannot withdraw a negative amount of money.") if withdrawal_amount < 0 # if withdrawal_amount > 0 if withdrawal_amount > @balance - print "Uh oh! You can't overdraw your account, you doof!" + print "You can't overdraw your account, you doof." else return @balance -= withdrawal_amount end @@ -48,7 +48,7 @@ def withdraw(withdrawal_amount) end def deposit(deposit_amount) - raise ArgumentError.new("You cannot deposit a negative amount of money, you goofball.") if deposit_amount < 0 + raise ArgumentError.new("You cannot deposit a negative amount of money.") if deposit_amount < 0 # if deposit_amount > 0 @balance += deposit_amount # else diff --git a/lib/checking.rb b/lib/checking.rb index 8efdb953..eb281e6a 100644 --- a/lib/checking.rb +++ b/lib/checking.rb @@ -1,10 +1,8 @@ module Bank - class CheckingAccount - - def initialize - end + class CheckingAccount < Account + end diff --git a/lib/savings.rb b/lib/savings.rb index 0d843d5d..95f3aace 100644 --- a/lib/savings.rb +++ b/lib/savings.rb @@ -1,9 +1,24 @@ module Bank - class SavingsAccount + class SavingsAccount < Account + + def initialize(id, balance, date, owner = "Customer Name") + super + raise ArgumentError.new("Balance must be over $10.") if balance < 10 + end + + def withdraw(withdrawal_amount) + if @balance < withdrawal_amount + 12 + print "There must always be at least $10 in your savings." + return @balance + end + super + @balance -= 2 + end + + def add_interest(rate) - def initialize end end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index b521cbb3..ac353073 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -89,9 +89,6 @@ account = Bank::Account.new(1337, start_balance, "Jan 1, 2001", @brenna) 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 diff --git a/specs/checking_account_spec.rb b/specs/checking_account_spec.rb index 8fb807e5..2cb0fb49 100644 --- a/specs/checking_account_spec.rb +++ b/specs/checking_account_spec.rb @@ -4,9 +4,6 @@ require_relative '../lib/checking' -# 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 diff --git a/specs/savings_account_spec.rb b/specs/savings_account_spec.rb index 6beefe05..9fa8c0e4 100644 --- a/specs/savings_account_spec.rb +++ b/specs/savings_account_spec.rb @@ -4,40 +4,48 @@ require_relative '../lib/savings' -# 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 -xdescribe "SavingsAccount" do +describe "SavingsAccount" do + + before do + @account = Bank::SavingsAccount.new(12345, 100.0, "Jan 1, 2017") + end + 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 + @account.must_be_kind_of Bank::Account end - it "Requires an initial balance of at least $10" do - # TODO: Your test code here! + it "Raises an error if initial balance is less than $10" do + proc { + Bank::SavingsAccount.find(12345, 5, "Jan 1, 2017") + }.must_raise ArgumentError end end describe "#withdraw" do it "Applies a $2 fee each time" do - # TODO: Your test code here! + @account.withdraw(50) + @account.balance.must_equal 48.0 end it "Outputs a warning if the balance would go below $10" do - # TODO: Your test code here! + proc { + @account.withdraw(150) + }.must_output /.+/ end it "Doesn't modify the balance if it would go below $10" do - # TODO: Your test code here! + starting_balance = @account.balance + updated_balance = @account.withdraw(150) + updated_balance.must_equal starting_balance end it "Doesn't modify the balance if the fee would put it below $10" do - # TODO: Your test code here! + starting_balance = @account.balance + updated_balance = @account.withdraw(90) + updated_balance.must_equal starting_balance end end From c9f6a10ffc4b8849b797daadb7671abf83739999 Mon Sep 17 00:00:00 2001 From: Brenna Date: Fri, 24 Feb 2017 11:49:17 -0800 Subject: [PATCH 21/24] savings has interest method, passes all tests, checking acct has withdraw method that passed first test --- lib/checking.rb | 7 +++++- lib/owner.rb | 42 ++++++++++++++++++++++++++++++---- lib/savings.rb | 6 ++++- specs/checking_account_spec.rb | 19 +++++++++------ specs/savings_account_spec.rb | 15 ++++++++---- 5 files changed, 70 insertions(+), 19 deletions(-) diff --git a/lib/checking.rb b/lib/checking.rb index eb281e6a..69f4d624 100644 --- a/lib/checking.rb +++ b/lib/checking.rb @@ -2,7 +2,12 @@ module Bank class CheckingAccount < Account - + + def withdraw(withdrawal_amount) + super + @balance -= 1 unless withdrawal_amount > @balance + return @balance + end end diff --git a/lib/owner.rb b/lib/owner.rb index 65279dec..bcab77f6 100644 --- a/lib/owner.rb +++ b/lib/owner.rb @@ -1,14 +1,46 @@ +require 'csv' module Bank class Owner - attr_reader :name, :address, :birthday, :favefood + attr_reader :id, :last_name, :first_name, :st_address, :city, :state #, :name, :address, :birthday, :favefood + + def self.all + CSV.open("/Users/brenna/ada/week3/BankAccounts/support/account_owners.csv").each do | line | + owner_hash = { + id: line[0].to_i, + last_name: line[1], + first_name: line[2], + st_address: line[3], + city: line[4], + state: line[5] + } + ALL_OWNERS << Bank::Owner.new(owner_hash) + end + ALL_OWNERS + end + + def self.find(id) + # accounts = Bank::Account.all + # raise ArgumentError.new("There's no such account ID, you nincompoop.") if ![1212, 1213, 1214, 1215, 1216, 1217, 15151, 15152, 15153, 15154, 15155, 15156].include?(id) + # accounts.each_with_index do |acct, index| + # if acct.id == id + # return accounts[index] + # end + # end + end def initialize(owner_hash) - @name = owner_hash[:name] - @address = owner_hash[:address] - @birthday = owner_hash[:birthday] - @favefood = owner_hash[:favefood] + # @name = owner_hash[:name] + # @address = owner_hash[:address] + # @birthday = owner_hash[:birthday] + # @favefood = owner_hash[:favefood] + @id = owner_hash[:id] + @last_name = owner_hash[:last_name] + @first_name = owner_hash[:first_name] + @st_address = owner_hash[:st_address] + @city = owner_hash[:city] + @state = owner_hash[:state] end end diff --git a/lib/savings.rb b/lib/savings.rb index 95f3aace..cdd40781 100644 --- a/lib/savings.rb +++ b/lib/savings.rb @@ -3,6 +3,8 @@ module Bank class SavingsAccount < Account + attr_accessor :interest + def initialize(id, balance, date, owner = "Customer Name") super raise ArgumentError.new("Balance must be over $10.") if balance < 10 @@ -18,7 +20,9 @@ def withdraw(withdrawal_amount) end def add_interest(rate) - + raise ArgumentError.new("You can't calculate negative interest.") if rate < 0 + @interest = @balance * rate / 100 + @balance += @interest end end diff --git a/specs/checking_account_spec.rb b/specs/checking_account_spec.rb index 2cb0fb49..9f2a991f 100644 --- a/specs/checking_account_spec.rb +++ b/specs/checking_account_spec.rb @@ -6,23 +6,28 @@ # Here we'll only test things that are different. -# TODO: change 'xdescribe' to 'describe' to run these tests -xdescribe "CheckingAccount" do +describe "CheckingAccount" do + + before do + @account = Bank::CheckingAccount.new(12345, 100.0, "Jan 1, 2017") + end + 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 + @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! + @account.withdraw(50) + @account.balance.must_equal 49.0 end it "Doesn't modify the balance if the fee would put it negative" do - # TODO: Your test code here! + starting_balance = @account.balance + updated_balance = @account.withdraw(150) + updated_balance.must_equal starting_balance end end diff --git a/specs/savings_account_spec.rb b/specs/savings_account_spec.rb index 9fa8c0e4..e926b39b 100644 --- a/specs/savings_account_spec.rb +++ b/specs/savings_account_spec.rb @@ -19,7 +19,7 @@ it "Raises an error if initial balance is less than $10" do proc { - Bank::SavingsAccount.find(12345, 5, "Jan 1, 2017") + Bank::SavingsAccount.new(12345, 5, "Jan 1, 2017") }.must_raise ArgumentError end end @@ -51,15 +51,20 @@ describe "#add_interest" do it "Returns the interest calculated" do - # TODO: Your test code here! + rich_account = Bank::SavingsAccount.new(23456, 10000, "Jan 1, 2017") + rich_account.add_interest(0.25) + rich_account.interest.must_equal 25.0 end it "Updates the balance with calculated interest" do - # TODO: Your test code here! + rich_account = Bank::SavingsAccount.new(23456, 10000, "Jan 1, 2017") + rich_account.add_interest(0.25).must_equal 10025.0 end - it "Requires a positive rate" do - # TODO: Your test code here! + it "Raises an error if a negative rate is given" do + proc { + @account.add_interest(-0.5) + }.must_raise ArgumentError end end end From 38bb277bb73bf8164fc0a17fa4e107819e146cd9 Mon Sep 17 00:00:00 2001 From: Brenna Date: Fri, 24 Feb 2017 15:01:36 -0800 Subject: [PATCH 22/24] created withdraw method with fee, withdraw_using_check, passed all tests, wave 3 complete --- lib/account.rb | 12 ++-------- lib/checking.rb | 33 ++++++++++++++++++++++++-- specs/account_spec.rb | 9 -------- specs/checking_account_spec.rb | 42 ++++++++++++++++++++++------------ specs/savings_account_spec.rb | 2 -- 5 files changed, 61 insertions(+), 37 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index d3e15d7e..a490c167 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -24,7 +24,6 @@ def self.all def self.find(id) accounts = Bank::Account.all - # raise ArgumentError.new("There's no such account ID, you nincompoop.") if ![1212, 1213, 1214, 1215, 1216, 1217, 15151, 15152, 15153, 15154, 15155, 15156].include?(id) accounts.each do |acct| if acct.id == id return acct @@ -35,24 +34,17 @@ def self.find(id) def withdraw(withdrawal_amount) raise ArgumentError.new("You cannot withdraw a negative amount of money.") if withdrawal_amount < 0 - # if withdrawal_amount > 0 if withdrawal_amount > @balance - print "You can't overdraw your account, you doof." + print "You can't overdraw your account." else return @balance -= withdrawal_amount end return @balance - # else - # raise ArgumentError.new "You cannot withdraw a negative amount of money, you silly pants." - # end end def deposit(deposit_amount) raise ArgumentError.new("You cannot deposit a negative amount of money.") if deposit_amount < 0 - # if deposit_amount > 0 - @balance += deposit_amount - # else - # raise ArgumentError.new "You cannot deposit a negative amount of money, you goofball." + @balance += deposit_amount end end diff --git a/lib/checking.rb b/lib/checking.rb index 69f4d624..b6828017 100644 --- a/lib/checking.rb +++ b/lib/checking.rb @@ -3,10 +3,39 @@ module Bank class CheckingAccount < Account - def withdraw(withdrawal_amount) + def initialize(id, balance, date, owner = "Customer Name") super - @balance -= 1 unless withdrawal_amount > @balance + @check_count = 0 + end + + def withdraw(withdrawal_amount) + if (withdrawal_amount + 1) > @balance + print "You can't overdraw your account." return @balance + else + @balance -= (withdrawal_amount + 1) + end + return @balance + end + + def withdraw_using_check(withdrawal_amount) + raise ArgumentError.new("You cannot withdraw a negative amount of money using a check.") if withdrawal_amount < 0 + + if @balance < withdrawal_amount - 10 + print "There must always be at least $10 in your savings." + return @balance + end + + @check_count += 1 + if @check_count <= 3 + return @balance -= withdrawal_amount + else + return @balance -= (withdrawal_amount + 2) + end + end + + def reset_checks + @check_count = 0 end end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index ac353073..f8e46b53 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -37,15 +37,12 @@ end it "Raises an ArgumentError when created with a negative balance" do - # Note: we haven't talked about procs yet. You can think of them like blocks that sit by themselves. - # This code checks that, when the proc is executed, it raises an ArgumentError. proc { Bank::Account.new(1337, -100, "Jan 1, 2001", @brenna) }.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, "Jan 1, 2001", @brenna) end end @@ -77,7 +74,6 @@ start_balance = 100.0 withdrawal_amount = 200.0 account = Bank::Account.new(1337, start_balance, "Jan 1, 2001", @brenna) - # 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 /.+/ @@ -162,14 +158,12 @@ end it "Everything in the array is an Account" do - # Bank::Account.all.each do |inst| @accounts.each do |inst| inst.must_be_instance_of Bank::Account end end it "The number of accounts is correct" do - # Bank::Account.all.length.must_equal 12 @accounts.length.must_equal 12 end @@ -195,9 +189,6 @@ describe "Account.find" do it "Returns an account that exists" do - # doesn't work due to different object IDs of Bank::Account.all ?? - # Bank::Account.find(1216).must_be_same_as Bank::Account.all[4] - # Bank::Account.find(1216).id.must_equal Bank::Account.all[4].id accounts = Bank::Account.all id_check = accounts[4].id account = Bank::Account.find(id_check) diff --git a/specs/checking_account_spec.rb b/specs/checking_account_spec.rb index 9f2a991f..fe08d172 100644 --- a/specs/checking_account_spec.rb +++ b/specs/checking_account_spec.rb @@ -4,8 +4,6 @@ require_relative '../lib/checking' -# Here we'll only test things that are different. - describe "CheckingAccount" do before do @@ -26,56 +24,72 @@ it "Doesn't modify the balance if the fee would put it negative" do starting_balance = @account.balance - updated_balance = @account.withdraw(150) + updated_balance = @account.withdraw(100) updated_balance.must_equal starting_balance end end describe "#withdraw_using_check" do it "Reduces the balance" do - # TODO: Your test code here! + @account.withdraw_using_check(40).must_equal 100 - 40 end it "Returns the modified balance" do - # TODO: Your test code here! + @account.withdraw_using_check(40).must_equal 60 end it "Allows the balance to go down to -$10" do - # TODO: Your test code here! + @account.withdraw_using_check(110).must_equal (-10) end it "Outputs a warning if the account would go below -$10" do - # TODO: Your test code here! + proc { + @account.withdraw(150) + }.must_output /.+/ end it "Doesn't modify the balance if the account would go below -$10" do - # TODO: Your test code here! + starting_balance = @account.balance + updated_balance = @account.withdraw_using_check(150) + updated_balance.must_equal starting_balance end it "Requires a positive withdrawal amount" do - # TODO: Your test code here! + proc { + @account.withdraw_using_check(-25) + }.must_raise ArgumentError end it "Allows 3 free uses" do - # TODO: Your test code here! + 3.times { @account.withdraw_using_check(10) } + @account.balance.must_equal 70 end it "Applies a $2 fee after the third use" do - # TODO: Your test code here! + 4.times { @account.withdraw_using_check(10) } + @account.balance.must_equal 58 end end describe "#reset_checks" do it "Can be called without error" do - # TODO: Your test code here! + @account.must_respond_to :reset_checks end it "Makes the next three checks free if less than 3 checks had been used" do - # TODO: Your test code here! + 2.times { @account.withdraw_using_check(10) } + @account.balance.must_equal 80 + @account.reset_checks + 3.times { @account.withdraw_using_check(10) } + @account.balance.must_equal 50 end it "Makes the next three checks free if more than 3 checks had been used" do - # TODO: Your test code here! + 4.times { @account.withdraw_using_check(10) } + @account.balance.must_equal 58 + @account.reset_checks + @account.withdraw_using_check(10) + @account.balance.must_equal 48 end end end diff --git a/specs/savings_account_spec.rb b/specs/savings_account_spec.rb index e926b39b..fc12c45a 100644 --- a/specs/savings_account_spec.rb +++ b/specs/savings_account_spec.rb @@ -4,8 +4,6 @@ require_relative '../lib/savings' -# Here we'll only test things that are different. - describe "SavingsAccount" do before do From 745df104e754ebc369384b26273bdd7438f2f593 Mon Sep 17 00:00:00 2001 From: Brenna Date: Fri, 24 Feb 2017 15:12:19 -0800 Subject: [PATCH 23/24] added owner spec, tested initialize --- lib/owner.rb | 26 +++++++++++++------------- specs/account_spec.rb | 4 ---- specs/owner_spec.rb | 27 +++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 specs/owner_spec.rb diff --git a/lib/owner.rb b/lib/owner.rb index bcab77f6..6bca528d 100644 --- a/lib/owner.rb +++ b/lib/owner.rb @@ -5,6 +5,19 @@ module Bank class Owner attr_reader :id, :last_name, :first_name, :st_address, :city, :state #, :name, :address, :birthday, :favefood + def initialize(owner_hash) + # @name = owner_hash[:name] + # @address = owner_hash[:address] + # @birthday = owner_hash[:birthday] + # @favefood = owner_hash[:favefood] + @id = owner_hash[:id] + @last_name = owner_hash[:last_name] + @first_name = owner_hash[:first_name] + @st_address = owner_hash[:st_address] + @city = owner_hash[:city] + @state = owner_hash[:state] + end + def self.all CSV.open("/Users/brenna/ada/week3/BankAccounts/support/account_owners.csv").each do | line | owner_hash = { @@ -30,19 +43,6 @@ def self.find(id) # end end - def initialize(owner_hash) - # @name = owner_hash[:name] - # @address = owner_hash[:address] - # @birthday = owner_hash[:birthday] - # @favefood = owner_hash[:favefood] - @id = owner_hash[:id] - @last_name = owner_hash[:last_name] - @first_name = owner_hash[:first_name] - @st_address = owner_hash[:st_address] - @city = owner_hash[:city] - @state = owner_hash[:state] - end - end end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index f8e46b53..4201bf3f 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -30,10 +30,6 @@ account.must_respond_to :owner account.owner.must_equal @brenna - - account.owner.last_name.must_equal "Darroch" - account.owner.first_name.must_equal "Brenna" - account.owner.st_address.must_equal "3426 Cotton Top Ct" end it "Raises an ArgumentError when created with a negative balance" do diff --git a/specs/owner_spec.rb b/specs/owner_spec.rb new file mode 100644 index 00000000..df5032c3 --- /dev/null +++ b/specs/owner_spec.rb @@ -0,0 +1,27 @@ +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/skip_dsl' +require 'csv' +require_relative '../lib/account' +require_relative '../lib/owner' + +describe "Wave 1 Optional" do + + before do + brenna_hash = {id: 0522, last_name: "Darroch", first_name: "Brenna", st_address: "3426 Cotton Top Ct", city: "Fairfax", state: "VA"} + @brenna = Bank::Owner.new(brenna_hash) + @account = Bank::Account.new(1234, 333, "May 22, 2011", @brenna) + end + + describe "Owner#initialize" do + it "Takes a hash with data on ID, first and last name, street address, city, and state." do + @account.owner.id.must_equal 0522 + @account.owner.last_name.must_equal "Darroch" + @account.owner.first_name.must_equal "Brenna" + @account.owner.st_address.must_equal "3426 Cotton Top Ct" + @account.owner.city.must_equal "Fairfax" + @account.owner.state.must_equal "VA" + end + end + +end From af4df8e175820e5f211636508eab2e5dcecf548c Mon Sep 17 00:00:00 2001 From: Brenna Date: Fri, 24 Feb 2017 16:07:28 -0800 Subject: [PATCH 24/24] owners all and find method passed tests --- lib/account.rb | 10 ++++---- lib/owner.rb | 28 ++++++++++---------- specs/account_spec.rb | 15 +++++------ specs/owner_spec.rb | 60 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 28 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index a490c167..a402c3f7 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -15,16 +15,16 @@ def initialize(id, balance, date, owner = "Customer Name") end def self.all - all_accounts = [] + return @all_accounts if @all_accounts + @all_accounts = [] CSV.open("/Users/brenna/ada/week3/BankAccounts/support/accounts.csv").each do | line | - all_accounts << Bank::Account.new(line[0].to_i, line[1].to_f, line[2]) + @all_accounts << Bank::Account.new(line[0].to_i, line[1].to_f, line[2]) end - all_accounts + @all_accounts end def self.find(id) - accounts = Bank::Account.all - accounts.each do |acct| + self.all.each do |acct| if acct.id == id return acct end diff --git a/lib/owner.rb b/lib/owner.rb index 6bca528d..30b7830a 100644 --- a/lib/owner.rb +++ b/lib/owner.rb @@ -3,13 +3,9 @@ module Bank class Owner - attr_reader :id, :last_name, :first_name, :st_address, :city, :state #, :name, :address, :birthday, :favefood + attr_reader :id, :last_name, :first_name, :st_address, :city, :state def initialize(owner_hash) - # @name = owner_hash[:name] - # @address = owner_hash[:address] - # @birthday = owner_hash[:birthday] - # @favefood = owner_hash[:favefood] @id = owner_hash[:id] @last_name = owner_hash[:last_name] @first_name = owner_hash[:first_name] @@ -19,6 +15,9 @@ def initialize(owner_hash) end def self.all + # pro tip from Jeremy! memoization: first time, @all_owners is nil, so it reads the CSV, but next time, it just returns @all_owners + return @all_owners if @all_owners + @all_owners = [] CSV.open("/Users/brenna/ada/week3/BankAccounts/support/account_owners.csv").each do | line | owner_hash = { id: line[0].to_i, @@ -28,19 +27,20 @@ def self.all city: line[4], state: line[5] } - ALL_OWNERS << Bank::Owner.new(owner_hash) + @all_owners << Bank::Owner.new(owner_hash) end - ALL_OWNERS + + @all_owners end def self.find(id) - # accounts = Bank::Account.all - # raise ArgumentError.new("There's no such account ID, you nincompoop.") if ![1212, 1213, 1214, 1215, 1216, 1217, 15151, 15152, 15153, 15154, 15155, 15156].include?(id) - # accounts.each_with_index do |acct, index| - # if acct.id == id - # return accounts[index] - # end - # end + self.all.each do |owner| + if owner.id == id + return owner + end + end + + raise ArgumentError.new("There's no such owner ID.") end end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 4201bf3f..976a999f 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -174,9 +174,9 @@ it "The elements match what's in the file" do index = 0 CSV.read("support/accounts.csv") do |line| - accounts[index].id.must_equal line[0].to_i - accounts[index].balance.must_equal line[1].to_f - accounts[index].date.must_equal line[2] + @accounts[index].id.must_equal line[0].to_i + @accounts[index].balance.must_equal line[1].to_f + @accounts[index].date.must_equal line[2] index += 1 end end @@ -185,22 +185,19 @@ describe "Account.find" do it "Returns an account that exists" do - accounts = Bank::Account.all - id_check = accounts[4].id + id_check = @accounts[4].id account = Bank::Account.find(id_check) expect(account.id).must_equal id_check end it "Can find the first account from the CSV" do - accounts = Bank::Account.all - id_check = accounts[0].id + id_check = @accounts[0].id account = Bank::Account.find(id_check) expect(account.id).must_equal id_check end it "Can find the last account from the CSV" do - accounts = Bank::Account.all - id_check = accounts[-1].id + id_check = @accounts[-1].id account = Bank::Account.find(id_check) expect(account.id).must_equal id_check end diff --git a/specs/owner_spec.rb b/specs/owner_spec.rb index df5032c3..6927fa97 100644 --- a/specs/owner_spec.rb +++ b/specs/owner_spec.rb @@ -11,6 +11,7 @@ brenna_hash = {id: 0522, last_name: "Darroch", first_name: "Brenna", st_address: "3426 Cotton Top Ct", city: "Fairfax", state: "VA"} @brenna = Bank::Owner.new(brenna_hash) @account = Bank::Account.new(1234, 333, "May 22, 2011", @brenna) + @owners = Bank::Owner.all end describe "Owner#initialize" do @@ -24,4 +25,63 @@ end end + describe "Owner.all" do + + it "Returns an array of all owners" do + @owners.must_be_kind_of Array + end + + it "Everything in the array is an Owner" do + @owners.each do |inst| + inst.must_be_instance_of Bank::Owner + end + end + + it "The number of owners is correct" do + @owners.length.must_equal 12 + end + + it "The elements match what's in the file" do + index = 0 + CSV.read("support/owners.csv") do |line| + @owners[index].id.must_equal line[0].to_i + @owners[index].last_name.must_equal line[1] + @owners[index].last_name.must_equal line[2] + @owners[index].st_address.must_equal line[3] + @owners[index].city.must_equal line[4] + @owners[index].state.must_equal line[5] + index += 1 + end + end + end + + describe "Owner.find" do + + it "Returns an account that exists" do + id_check = @owners[4].id + owner = Bank::Owner.find(id_check) + expect(owner.id).must_equal id_check + end + + it "Can find the first account from the CSV" do + id_check = @owners[0].id + owner = Bank::Owner.find(id_check) + expect(owner.id).must_equal id_check + end + + it "Can find the last account from the CSV" do + id_check = @owners[-1].id + owner = Bank::Owner.find(id_check) + expect(owner.id).must_equal id_check + end + + it "Raises an error for an account that doesn't exist" do + proc { + Bank::Owner.find(12345) + }.must_raise ArgumentError + end + + end + + end