From 3b01b6c1f857fa22eebd7c1612f8e20d18a8559b Mon Sep 17 00:00:00 2001 From: Valerie Date: Mon, 29 Feb 2016 14:23:46 -0800 Subject: [PATCH 01/28] Added Bank module, Account class, and the initialize method containing @id and @balance instance variables. --- bank_accounts.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 bank_accounts.rb diff --git a/bank_accounts.rb b/bank_accounts.rb new file mode 100644 index 00000000..11b14800 --- /dev/null +++ b/bank_accounts.rb @@ -0,0 +1,13 @@ +# 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. + +module Bank + class Account + def initialize(id, initial_balance) + @id = id + @balance = initial_balance + end +end From 59a1eda04141622e7aa9037dcf268f97f655e2d4 Mon Sep 17 00:00:00 2001 From: Valerie Date: Mon, 29 Feb 2016 14:28:02 -0800 Subject: [PATCH 02/28] Oops, last commit was missing an 'end'. Fixed. --- bank_accounts.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bank_accounts.rb b/bank_accounts.rb index 11b14800..4045fac2 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -10,4 +10,6 @@ def initialize(id, initial_balance) @id = id @balance = initial_balance end + + end end From 8e0eead0999981c4a79c067b790978eb38d663bf Mon Sep 17 00:00:00 2001 From: Valerie Date: Mon, 29 Feb 2016 14:49:57 -0800 Subject: [PATCH 03/28] Added withdraw method and show_balance method. --- bank_accounts.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bank_accounts.rb b/bank_accounts.rb index 4045fac2..0ea9a952 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -4,6 +4,9 @@ # 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. +# use this later when you're ready to deal with converting 1000 into $10.00, etc. +# require "money" + module Bank class Account def initialize(id, initial_balance) @@ -11,5 +14,13 @@ def initialize(id, initial_balance) @balance = initial_balance end + def withdraw(amount_to_withdraw) + @balance = @balance - amount_to_withdraw + show_balance + end + + def show_balance + puts "The balance for this account is currently #{@balance}." + end end end From b47194a4a12ffcee5e623bc4a5783ed214557971 Mon Sep 17 00:00:00 2001 From: Valerie Date: Mon, 29 Feb 2016 14:55:50 -0800 Subject: [PATCH 04/28] Made it so show_balance method actually returns the account's balance, instead of just outputting it for the user. --- bank_accounts.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index 0ea9a952..1297166a 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -19,8 +19,14 @@ def withdraw(amount_to_withdraw) show_balance end + def deposit(amount_to_deposit) + @balance = @balance + amount_to_deposit + show_balance + end + def show_balance - puts "The balance for this account is currently #{@balance}." + puts "The balance for this account is currently $#{@balance}." + return @balance end end end From c9e0ca979cafedef74e06e68c48b201052b20d8a Mon Sep 17 00:00:00 2001 From: Valerie Date: Mon, 29 Feb 2016 15:12:17 -0800 Subject: [PATCH 05/28] When initializing an account, user must now provide a number greater than 1 for their initial balance, otherwise an ArgumentError will be thrown. --- bank_accounts.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bank_accounts.rb b/bank_accounts.rb index 1297166a..0e2cdd19 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -9,7 +9,11 @@ module Bank class Account + attr_reader :balance def initialize(id, initial_balance) + if initial_balance < 1 + raise ArgumentError.new("You must have at least $1 to open an account.") + end @id = id @balance = initial_balance end From 09f9dbcb95f36ebbc760a0074d57b2600ce40f8b Mon Sep 17 00:00:00 2001 From: Valerie Date: Mon, 29 Feb 2016 15:26:00 -0800 Subject: [PATCH 06/28] Added ArgumentError when user tries to withdraw more money than is already in their account. --- bank_accounts.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bank_accounts.rb b/bank_accounts.rb index 0e2cdd19..03780b51 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -19,6 +19,9 @@ def initialize(id, initial_balance) end def withdraw(amount_to_withdraw) + if amount_to_withdraw > @balance + raise ArgumentError.new("This withdrawal would cause a negative balance. Do not attempt.") + end @balance = @balance - amount_to_withdraw show_balance end From a9c7424dc0ba3428922aaa17a2c639e06cea3dee Mon Sep 17 00:00:00 2001 From: Valerie Date: Mon, 29 Feb 2016 15:45:20 -0800 Subject: [PATCH 07/28] Added Owner class with initialize method and attr_reader for name, phone, email, and address attributes. --- bank_accounts.rb | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index 03780b51..38587e76 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -1,15 +1,14 @@ -# 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. - -# use this later when you're ready to deal with converting 1000 into $10.00, etc. +# use money gem later when users get involved +# for example, if a user enters 10, it will convert to 1000 for the money gem, which will then convert it to $10.00 # require "money" module Bank + class Account - attr_reader :balance + # For bank employee eyes only! I would remove the attr_reader if I was going + # to expose this code anywhere public. + attr_reader :id, :balance + def initialize(id, initial_balance) if initial_balance < 1 raise ArgumentError.new("You must have at least $1 to open an account.") @@ -20,7 +19,8 @@ def initialize(id, initial_balance) def withdraw(amount_to_withdraw) if amount_to_withdraw > @balance - raise ArgumentError.new("This withdrawal would cause a negative balance. Do not attempt.") + raise ArgumentError.new("This withdrawal would cause a negative balance. + Do not attempt.") end @balance = @balance - amount_to_withdraw show_balance @@ -31,9 +31,23 @@ def deposit(amount_to_deposit) show_balance end + # displays the balance in a nice user-friendly way, but also returns it to the other methods def show_balance puts "The balance for this account is currently $#{@balance}." return @balance end end + + class Owner + # For bank employee eyes only! I would remove the attr_reader if I was going + # to expose this code anywhere public. + attr_reader :name, :phone, :email, :address + + def initialize(name, phone_number, email_address, street_address) + @name = name + @phone = phone_number + @email = email_address + @address = street_address + end + end end From c1a90b76e8ee5d42892dc5daad19351f1eaa9826 Mon Sep 17 00:00:00 2001 From: Valerie Date: Mon, 29 Feb 2016 16:41:07 -0800 Subject: [PATCH 08/28] Created add_owner method and @owner instance variable. --- bank_accounts.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index 38587e76..172423f6 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -7,7 +7,7 @@ module Bank class Account # For bank employee eyes only! I would remove the attr_reader if I was going # to expose this code anywhere public. - attr_reader :id, :balance + attr_reader :id, :balance, :owner def initialize(id, initial_balance) if initial_balance < 1 @@ -15,6 +15,16 @@ def initialize(id, initial_balance) end @id = id @balance = initial_balance + @owner = "No Owner Assigned" # temporary value until Owner is created + end + + # If the owner has already been created in the Owner class, the method should be called like so: + # account_instance.add_owner(owner_instance.name) + # If owner does not exist, the method should be called like so: + # account_instance = Bank::Owner.new("Barbara Thompson", "545-665-5535", "looploop@loo.org", "5545 Apple Drive Issaquah, WA 98645") + def add_owner(owner_name) + @owner = owner_name + #Bank::Owner.name end def withdraw(amount_to_withdraw) @@ -36,12 +46,13 @@ def show_balance puts "The balance for this account is currently $#{@balance}." return @balance end + end class Owner # For bank employee eyes only! I would remove the attr_reader if I was going # to expose this code anywhere public. - attr_reader :name, :phone, :email, :address + attr_reader :name, :phone, :email, :address, :owner def initialize(name, phone_number, email_address, street_address) @name = name From 859e5f2b4d679c482fc710c12161e46ef6fc02cb Mon Sep 17 00:00:00 2001 From: Valerie Date: Tue, 1 Mar 2016 15:56:31 -0800 Subject: [PATCH 09/28] Refactored initializer by making it so you pass in an account_info hash to create the instance variables --- bank_accounts.rb | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index 172423f6..d5e447ef 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -2,20 +2,31 @@ # for example, if a user enters 10, it will convert to 1000 for the money gem, which will then convert it to $10.00 # require "money" +require "CSV" + module Bank class Account # For bank employee eyes only! I would remove the attr_reader if I was going # to expose this code anywhere public. - attr_reader :id, :balance, :owner + attr_reader :id, :balance, :owner, :account_info + + def initialize(account_info) + # manually choose the data from the first line of the CSV file and ensure + # you can create a new instance of your Account using that data + + # @all_account_info = CSV.read("accounts.csv") + + # create a hash with keys id, balance, date_created and get those values + # from reading accounts.csv (which turns it into an array), and use indexes + # of given array to shovel into account_info hash? - def initialize(id, initial_balance) - if initial_balance < 1 + if account_info[:balance] < 1 raise ArgumentError.new("You must have at least $1 to open an account.") end - @id = id - @balance = initial_balance - @owner = "No Owner Assigned" # temporary value until Owner is created + @id = account_info[:id] + @balance = account_info[:balance] + @owner = account_info[:owner] # temporary value until Owner is created end # If the owner has already been created in the Owner class, the method should be called like so: From c3f6e1edc6b36b949b210b6fbd1840d6e732dd1b Mon Sep 17 00:00:00 2001 From: Valerie Date: Tue, 1 Mar 2016 20:16:03 -0800 Subject: [PATCH 10/28] Added self.make_account method, which pulls in the data from the CSV file as an array of arrays, then iterates through and creates hashes from that, then puts those hashes into a new array which will be returned whenever the method is accessed. --- bank_accounts.rb | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index d5e447ef..fbebc3f9 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -2,31 +2,42 @@ # for example, if a user enters 10, it will convert to 1000 for the money gem, which will then convert it to $10.00 # require "money" -require "CSV" - +require 'csv' module Bank class Account # For bank employee eyes only! I would remove the attr_reader if I was going # to expose this code anywhere public. - attr_reader :id, :balance, :owner, :account_info + attr_reader :id, :balance, :date_created def initialize(account_info) # manually choose the data from the first line of the CSV file and ensure # you can create a new instance of your Account using that data - # @all_account_info = CSV.read("accounts.csv") - # create a hash with keys id, balance, date_created and get those values # from reading accounts.csv (which turns it into an array), and use indexes # of given array to shovel into account_info hash? - if account_info[:balance] < 1 - raise ArgumentError.new("You must have at least $1 to open an account.") - end + #if account_info[:balance] < 1 + # raise ArgumentError.new("You must have at least $1 to open an account.") + #end @id = account_info[:id] @balance = account_info[:balance] - @owner = account_info[:owner] # temporary value until Owner is created + @date_created = account_info[:date_created] + # ADD THIS BACK IN LATER + # @owner = account_info[:owner] # temporary value until Owner is created + end + + def self.make_account + array_of_accounts = CSV.read("support/accounts.csv") + account_info_array = [] + # this method should return an array of hashes, where each hash represents one row of data + # array_of_accounts.each do.... get a hash somehow {id => element[0], balance => element[1], } + # put those hashes into an array, and return that array to whoever calls this method + array_of_accounts.each do |element| + account_info_array << {id: element[0], balance: element[1], date_created: element[2]} + end + return account_info_array end # If the owner has already been created in the Owner class, the method should be called like so: @@ -73,3 +84,7 @@ def initialize(name, phone_number, email_address, street_address) end end end + +new_account = Bank::Account.new(id: "", balance: "", date_created: "") + +#@array_of_accounts = CSV.open("accounts.csv", "r") From 825f819bdafea7e84e5d8593b511499563807eda Mon Sep 17 00:00:00 2001 From: Valerie Date: Wed, 2 Mar 2016 12:45:40 -0800 Subject: [PATCH 11/28] Renamed self.make_account method to self.all because I realized that's what the requirements were asking for --- bank_accounts.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index fbebc3f9..b350f49c 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -28,7 +28,8 @@ def initialize(account_info) # @owner = account_info[:owner] # temporary value until Owner is created end - def self.make_account + def self.all + # pulls in the data from the CSV file as an array of arrays array_of_accounts = CSV.read("support/accounts.csv") account_info_array = [] # this method should return an array of hashes, where each hash represents one row of data From 4969449df9b68d46b0c260135f3655e41fed2687 Mon Sep 17 00:00:00 2001 From: Valerie Date: Wed, 2 Mar 2016 13:04:14 -0800 Subject: [PATCH 12/28] Fixed self.all so that each of the hashes it was generating gets stored as an Account instance, THEN shovels into the account_info_array --- bank_accounts.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index b350f49c..8d35bf21 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -33,10 +33,11 @@ def self.all array_of_accounts = CSV.read("support/accounts.csv") account_info_array = [] # this method should return an array of hashes, where each hash represents one row of data - # array_of_accounts.each do.... get a hash somehow {id => element[0], balance => element[1], } - # put those hashes into an array, and return that array to whoever calls this method + # array_of_accounts.each do.... make a hash {id => element[0], balance => element[1], } + # instantiate a new Account based on that hash, after each one shovel into an array + # return that array to whoever calls this method array_of_accounts.each do |element| - account_info_array << {id: element[0], balance: element[1], date_created: element[2]} + account_info_array << Account.new({id: element[0], balance: element[1], date_created: element[2]}) end return account_info_array end From 3a9942f1db295e4b67ab3e9acb837fb6a6817eaf Mon Sep 17 00:00:00 2001 From: Valerie Date: Wed, 2 Mar 2016 13:44:59 -0800 Subject: [PATCH 13/28] Added self.find method --- bank_accounts.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/bank_accounts.rb b/bank_accounts.rb index 8d35bf21..289382d3 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -42,6 +42,21 @@ def self.all return account_info_array end + def self.find(given_id) + #returns an instance of Account where the value of the id field in the CSV + #matches the passed parameter + + # iterate over the Account instances until you find the instance with the matching id + # all the account instances are listed in account_info_array + Bank::Account.all.each do |account| + if account.id == given_id + return account + else + puts "Not found." + end + end + end + # If the owner has already been created in the Owner class, the method should be called like so: # account_instance.add_owner(owner_instance.name) # If owner does not exist, the method should be called like so: From f7d27f2ec65a07841dfabfaf7ae5825fafd3a5cc Mon Sep 17 00:00:00 2001 From: Valerie Date: Wed, 2 Mar 2016 14:22:16 -0800 Subject: [PATCH 14/28] Deleted pointless account instantiation at end of code and added some comments explaining my dilemma with self.find in plain old English --- bank_accounts.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index 289382d3..d58aac11 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -3,6 +3,7 @@ # require "money" require 'csv' +require 'money' module Bank class Account @@ -48,6 +49,10 @@ def self.find(given_id) # iterate over the Account instances until you find the instance with the matching id # all the account instances are listed in account_info_array + + # the problem with this is it generates the accounts from the CSV every time. + # what if you've withdrawn or deposited from the account? the original CSV, which + # self.all reads from, doesn't reflect that! Bank::Account.all.each do |account| if account.id == given_id return account @@ -57,6 +62,10 @@ def self.find(given_id) end end + # def convert_cents(money) + # money = Money.new() + # end + # If the owner has already been created in the Owner class, the method should be called like so: # account_instance.add_owner(owner_instance.name) # If owner does not exist, the method should be called like so: @@ -102,6 +111,4 @@ def initialize(name, phone_number, email_address, street_address) end end -new_account = Bank::Account.new(id: "", balance: "", date_created: "") - #@array_of_accounts = CSV.open("accounts.csv", "r") From e19584b5535e6af7e391fef32cb937aeec346b5b Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 3 Mar 2016 09:06:18 -0800 Subject: [PATCH 15/28] Added money conversion method to convert cents into dollars using Money gem --- bank_accounts.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index d58aac11..368f4ab8 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -4,6 +4,7 @@ require 'csv' require 'money' +I18n.enforce_available_locales = false module Bank class Account @@ -38,7 +39,7 @@ def self.all # instantiate a new Account based on that hash, after each one shovel into an array # return that array to whoever calls this method array_of_accounts.each do |element| - account_info_array << Account.new({id: element[0], balance: element[1], date_created: element[2]}) + account_info_array << Account.new({id: element[0], balance: element[1].to_i, date_created: element[2]}) end return account_info_array end @@ -62,9 +63,9 @@ def self.find(given_id) end end - # def convert_cents(money) - # money = Money.new() - # end + def convert_cents(money) + @balance = Money.new(money, "USD") + end # If the owner has already been created in the Owner class, the method should be called like so: # account_instance.add_owner(owner_instance.name) @@ -80,7 +81,8 @@ def withdraw(amount_to_withdraw) raise ArgumentError.new("This withdrawal would cause a negative balance. Do not attempt.") end - @balance = @balance - amount_to_withdraw + #@balance = @balance - amount_to_withdraw + convert_cents(@balance - amount_to_withdraw) show_balance end From 237254da3652fca4ef6f9fa04ef3946dbcd0aa81 Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 3 Mar 2016 12:55:14 -0800 Subject: [PATCH 16/28] Added empty CheckingAccount and SavingsAccount methods, and deleted a bunch of old useless comments --- bank_accounts.rb | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index 368f4ab8..307f0f3b 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -1,15 +1,9 @@ -# use money gem later when users get involved -# for example, if a user enters 10, it will convert to 1000 for the money gem, which will then convert it to $10.00 -# require "money" - require 'csv' require 'money' I18n.enforce_available_locales = false module Bank class Account - # For bank employee eyes only! I would remove the attr_reader if I was going - # to expose this code anywhere public. attr_reader :id, :balance, :date_created def initialize(account_info) @@ -82,7 +76,7 @@ def withdraw(amount_to_withdraw) Do not attempt.") end #@balance = @balance - amount_to_withdraw - convert_cents(@balance - amount_to_withdraw) + @balance = @balance - amount_to_withdraw show_balance end @@ -93,15 +87,21 @@ def deposit(amount_to_deposit) # displays the balance in a nice user-friendly way, but also returns it to the other methods def show_balance + convert_cents(@balance) puts "The balance for this account is currently $#{@balance}." return @balance end end + class CheckingAccount + end + + class SavingsAccount + end + + class Owner - # For bank employee eyes only! I would remove the attr_reader if I was going - # to expose this code anywhere public. attr_reader :name, :phone, :email, :address, :owner def initialize(name, phone_number, email_address, street_address) @@ -112,5 +112,3 @@ def initialize(name, phone_number, email_address, street_address) end end end - -#@array_of_accounts = CSV.open("accounts.csv", "r") From 5a6f9d4cb91623a076a761822cf2ac4cb1f20dec Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 3 Mar 2016 13:25:17 -0800 Subject: [PATCH 17/28] Last commit should have said 'classes', not methods. This time I updated the SavingsAccount class with a super'd initializer to account for the different minimum balance requirements. --- bank_accounts.rb | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index 307f0f3b..26224cac 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -25,6 +25,7 @@ def initialize(account_info) end def self.all + # this is what you'd do if working from a CSV file... not related to the instances we've initialized for testing # pulls in the data from the CSV file as an array of arrays array_of_accounts = CSV.read("support/accounts.csv") account_info_array = [] @@ -63,8 +64,6 @@ def convert_cents(money) # If the owner has already been created in the Owner class, the method should be called like so: # account_instance.add_owner(owner_instance.name) - # If owner does not exist, the method should be called like so: - # account_instance = Bank::Owner.new("Barbara Thompson", "545-665-5535", "looploop@loo.org", "5545 Apple Drive Issaquah, WA 98645") def add_owner(owner_name) @owner = owner_name #Bank::Owner.name @@ -94,10 +93,22 @@ def show_balance end - class CheckingAccount + class CheckingAccount < Account end - class SavingsAccount + class SavingsAccount < Account + # The initial balance cannot be less than $10. If it is, this will raise an ArgumentError + def initialize(account_info) + if account_info[:balance] < 10 + raise ArgumentError.new("You must have at least $10 to open a savings account.") + end + super + end + + # Updated withdrawal functionality: + # Each withdrawal 'transaction' incurs a fee of $2 that is taken out of the balance. + # Does not allow the account to go below the $10 minimum balance - Will output a warning message and return the original un-modified balance + end @@ -112,3 +123,9 @@ def initialize(name, phone_number, email_address, street_address) end end end + +# for testing the different methods in IRB? +@checking_instance = Bank::CheckingAccount.new({id: "1001", balance: 120045, date_created: "March 5, 2016"}) +@savings_instance = Bank::SavingsAccount.new({id: "1000", balance: 503030, date_created: "March 5, 2016"}) + +@owner_instance = Bank::Owner.new("Barbara Thompson", "545-665-5535", "looploop@loo.org", "5545 Apple Drive Issaquah, WA 98645") From a83ff6bb397864ffb490dbf4415e44bd32bfe597 Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 3 Mar 2016 14:19:22 -0800 Subject: [PATCH 18/28] Adjusted show_balance method so it no longer operates using money fractionals. --- bank_accounts.rb | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index 26224cac..64b9bd93 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -59,7 +59,7 @@ def self.find(given_id) end def convert_cents(money) - @balance = Money.new(money, "USD") + Money.new(money, "USD") end # If the owner has already been created in the Owner class, the method should be called like so: @@ -74,21 +74,21 @@ def withdraw(amount_to_withdraw) raise ArgumentError.new("This withdrawal would cause a negative balance. Do not attempt.") end - #@balance = @balance - amount_to_withdraw @balance = @balance - amount_to_withdraw show_balance + return @balance end def deposit(amount_to_deposit) @balance = @balance + amount_to_deposit show_balance + return @balance end # displays the balance in a nice user-friendly way, but also returns it to the other methods def show_balance - convert_cents(@balance) - puts "The balance for this account is currently $#{@balance}." - return @balance + pretty_money = convert_cents(@balance) + puts "The balance for this account is currently $#{pretty_money}." end end @@ -97,7 +97,9 @@ class CheckingAccount < Account end class SavingsAccount < Account - # The initial balance cannot be less than $10. If it is, this will raise an ArgumentError + TRANSACTION_FEE = 200 + + # The initial balance cannot be less than $10. def initialize(account_info) if account_info[:balance] < 10 raise ArgumentError.new("You must have at least $10 to open a savings account.") @@ -107,6 +109,12 @@ def initialize(account_info) # Updated withdrawal functionality: # Each withdrawal 'transaction' incurs a fee of $2 that is taken out of the balance. + def withdraw(amount_to_withdraw) + @balance = super - TRANSACTION_FEE + show_balance + return @balance + end + # Does not allow the account to go below the $10 minimum balance - Will output a warning message and return the original un-modified balance end From 6d2d66f2aa39cc89cad323c0faac978e147f321e Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 3 Mar 2016 16:09:23 -0800 Subject: [PATCH 19/28] Got rid of magic numbers and replaced with constants for minimum balance --- bank_accounts.rb | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index 64b9bd93..645d3255 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -4,7 +4,8 @@ module Bank class Account - attr_reader :id, :balance, :date_created + attr_reader :id, :balance, :date_created, :min_bal + MINIMUM_BALANCE = 100 def initialize(account_info) # manually choose the data from the first line of the CSV file and ensure @@ -14,9 +15,9 @@ def initialize(account_info) # from reading accounts.csv (which turns it into an array), and use indexes # of given array to shovel into account_info hash? - #if account_info[:balance] < 1 - # raise ArgumentError.new("You must have at least $1 to open an account.") - #end + if account_info[:balance] < MINIMUM_BALANCE + raise ArgumentError.new("You must have at least $1 to open an account.") + end @id = account_info[:id] @balance = account_info[:balance] @date_created = account_info[:date_created] @@ -87,6 +88,9 @@ def deposit(amount_to_deposit) # displays the balance in a nice user-friendly way, but also returns it to the other methods def show_balance + # temporarily converts @balance to dollar format for easier readability, + # but doesn't affect original numbers. This way all math can still be done + # in cents. pretty_money = convert_cents(@balance) puts "The balance for this account is currently $#{pretty_money}." end @@ -98,20 +102,25 @@ class CheckingAccount < Account class SavingsAccount < Account TRANSACTION_FEE = 200 + MINIMUM_BALANCE = 1000 # The initial balance cannot be less than $10. def initialize(account_info) - if account_info[:balance] < 10 + if account_info[:balance] < MINIMUM_BALANCE raise ArgumentError.new("You must have at least $10 to open a savings account.") end super end - # Updated withdrawal functionality: # Each withdrawal 'transaction' incurs a fee of $2 that is taken out of the balance. + # But how do I make it so show_balance doesn't show twice? def withdraw(amount_to_withdraw) @balance = super - TRANSACTION_FEE - show_balance + if @balance < MINIMUM_BALANCE + # nope you can't do that + else + show_balance + end return @balance end From 3b0bdebfb55b0fd729c8c74031181d13cd3fe617 Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 3 Mar 2016 16:31:45 -0800 Subject: [PATCH 20/28] Made it so SavingsAccount.withdraw raises an ArgumentError if you try to withdraw past the minimum balance --- bank_accounts.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index 645d3255..8fcab7c7 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -4,7 +4,7 @@ module Bank class Account - attr_reader :id, :balance, :date_created, :min_bal + attr_reader :id, :balance, :date_created MINIMUM_BALANCE = 100 def initialize(account_info) @@ -115,13 +115,14 @@ def initialize(account_info) # Each withdrawal 'transaction' incurs a fee of $2 that is taken out of the balance. # But how do I make it so show_balance doesn't show twice? def withdraw(amount_to_withdraw) - @balance = super - TRANSACTION_FEE - if @balance < MINIMUM_BALANCE - # nope you can't do that + if (@balance - amount_to_withdraw) < MINIMUM_BALANCE + raise ArgumentError.new("Savings accounts must maintain at least + $#{convert_cents(MINIMUM_BALANCE)}. Do not attempt.") else + @balance = super - TRANSACTION_FEE show_balance + return @balance end - return @balance end # Does not allow the account to go below the $10 minimum balance - Will output a warning message and return the original un-modified balance From 2db32f5fe8b2c63267e7b4405f413312aeb3865b Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 3 Mar 2016 16:55:32 -0800 Subject: [PATCH 21/28] Created add_interest(rate) and calculate_interest(rate) methods --- bank_accounts.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index 8fcab7c7..7cda8df9 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -115,6 +115,7 @@ def initialize(account_info) # Each withdrawal 'transaction' incurs a fee of $2 that is taken out of the balance. # But how do I make it so show_balance doesn't show twice? def withdraw(amount_to_withdraw) + # Does not allow the account to go below the $10 minimum balance - Will output a warning message if (@balance - amount_to_withdraw) < MINIMUM_BALANCE raise ArgumentError.new("Savings accounts must maintain at least $#{convert_cents(MINIMUM_BALANCE)}. Do not attempt.") @@ -125,8 +126,14 @@ def withdraw(amount_to_withdraw) end end - # Does not allow the account to go below the $10 minimum balance - Will output a warning message and return the original un-modified balance + def add_interest(rate) + interest = calculate_interest(rate) + @balance = @balance + interest + end + def calculate_interest(rate) + @balance * rate / 100 + end end From 653cb1fe81d3319a319ada051dd7a8da0cfed910 Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 4 Mar 2016 09:48:01 -0800 Subject: [PATCH 22/28] Added argument instance_var to withdraw method, so i could prevent the SavingsAccount and CheckingAccount withdrawal methods from doing show_balance twice thanks to super --- bank_accounts.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index 7cda8df9..ab99b5c9 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -70,13 +70,18 @@ def add_owner(owner_name) #Bank::Owner.name end - def withdraw(amount_to_withdraw) + def withdraw(amount_to_withdraw, instance_var) if amount_to_withdraw > @balance raise ArgumentError.new("This withdrawal would cause a negative balance. Do not attempt.") end @balance = @balance - amount_to_withdraw - show_balance + # included the following because otherwise "super" in the SavingsAccount + # and CheckingAccount withdrawal methods will do show_balance twice, once + # before the withdrawal has even occurred... and that's annoying :| + unless instance_var.is_a?(SavingsAccount) || instance_var.is_a?(CheckingAccount) + show_balance + end return @balance end @@ -114,7 +119,7 @@ def initialize(account_info) # Each withdrawal 'transaction' incurs a fee of $2 that is taken out of the balance. # But how do I make it so show_balance doesn't show twice? - def withdraw(amount_to_withdraw) + def withdraw(amount_to_withdraw, instance_var) # Does not allow the account to go below the $10 minimum balance - Will output a warning message if (@balance - amount_to_withdraw) < MINIMUM_BALANCE raise ArgumentError.new("Savings accounts must maintain at least From e9ddd907f26f3d7b681f194f2c35dd54a45dbb45 Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 4 Mar 2016 10:11:58 -0800 Subject: [PATCH 23/28] Added withdraw method for checking accounts --- bank_accounts.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/bank_accounts.rb b/bank_accounts.rb index ab99b5c9..96929bf2 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -103,6 +103,20 @@ def show_balance end class CheckingAccount < Account + TRANSACTION_FEE = 100 + # instance_var is the name of the instance variable you're calling the method on + def withdraw(amount_to_withdraw, instance_var) + @balance = super - TRANSACTION_FEE + show_balance + return @balance + end + + def withdraw_using_check(amount) + #The input amount gets taken out of the account as a result of a check withdrawal. Returns the updated account balance. + #Allows the account to go into overdraft up to -$10 but not any lower + #The user is allowed three free check uses in one month, but any subsequent use adds a $2 transaction fee + end + end class SavingsAccount < Account From 8b485935a23bca0d6a13db8360c0edb601ecc12a Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 4 Mar 2016 11:21:05 -0800 Subject: [PATCH 24/28] Commenting out my withdraw methods so I can refactor them after lunch...withdraw_using_checks gets all messed up so I'm reorganizing what functionality needs to be in each withdraw method --- bank_accounts.rb | 79 +++++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index 96929bf2..84f39aa8 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -70,20 +70,21 @@ def add_owner(owner_name) #Bank::Owner.name end - def withdraw(amount_to_withdraw, instance_var) - if amount_to_withdraw > @balance - raise ArgumentError.new("This withdrawal would cause a negative balance. - Do not attempt.") - end - @balance = @balance - amount_to_withdraw - # included the following because otherwise "super" in the SavingsAccount - # and CheckingAccount withdrawal methods will do show_balance twice, once - # before the withdrawal has even occurred... and that's annoying :| - unless instance_var.is_a?(SavingsAccount) || instance_var.is_a?(CheckingAccount) - show_balance - end - return @balance - end + ### BRB REFACTORING + # def withdraw(amount_to_withdraw, instance_var) + # if amount_to_withdraw > @balance + # raise ArgumentError.new("This withdrawal would cause a negative balance. + # Do not attempt.") + # end + # @balance = @balance - amount_to_withdraw + # # included the following because otherwise "super" in the SavingsAccount + # # and CheckingAccount withdrawal methods will do show_balance twice, once + # # before the withdrawal has even occurred... and that's annoying :| + # unless instance_var.is_a?(SavingsAccount) || instance_var.is_a?(CheckingAccount) + # show_balance + # end + # return @balance + # end def deposit(amount_to_deposit) @balance = @balance + amount_to_deposit @@ -105,17 +106,22 @@ def show_balance class CheckingAccount < Account TRANSACTION_FEE = 100 # instance_var is the name of the instance variable you're calling the method on - def withdraw(amount_to_withdraw, instance_var) - @balance = super - TRANSACTION_FEE - show_balance - return @balance - end - def withdraw_using_check(amount) - #The input amount gets taken out of the account as a result of a check withdrawal. Returns the updated account balance. - #Allows the account to go into overdraft up to -$10 but not any lower - #The user is allowed three free check uses in one month, but any subsequent use adds a $2 transaction fee - end + ### BRB REFACTORING + # def withdraw(amount_to_withdraw, instance_var) + # @balance = super - TRANSACTION_FEE + # show_balance + # return @balance + # end + + ### BRB REFACTORING + # def withdraw_using_check(amount_to_withdraw, instance_var) + # minimum_balance = -1000 + # if (@balance - amount_to_withdraw) < minimum_balance + # raise ArgumentError.new("Withdrawal by check only allows accounts to go + # into overdraft up to -$10. Do not attempt.") + # end + end @@ -131,19 +137,18 @@ def initialize(account_info) super end - # Each withdrawal 'transaction' incurs a fee of $2 that is taken out of the balance. - # But how do I make it so show_balance doesn't show twice? - def withdraw(amount_to_withdraw, instance_var) - # Does not allow the account to go below the $10 minimum balance - Will output a warning message - if (@balance - amount_to_withdraw) < MINIMUM_BALANCE - raise ArgumentError.new("Savings accounts must maintain at least - $#{convert_cents(MINIMUM_BALANCE)}. Do not attempt.") - else - @balance = super - TRANSACTION_FEE - show_balance - return @balance - end - end + ### BRB REFACTORING + # def withdraw(amount_to_withdraw, instance_var) + # # Does not allow the account to go below the $10 minimum balance - Will output a warning message + # if (@balance - amount_to_withdraw) < MINIMUM_BALANCE + # raise ArgumentError.new("Savings accounts must maintain at least + # $#{convert_cents(MINIMUM_BALANCE)}. Do not attempt.") + # else + # @balance = super - TRANSACTION_FEE + # show_balance + # return @balance + # end + # end def add_interest(rate) interest = calculate_interest(rate) From 225da932f9f1fe8a3506a0958e1a1b7facca1719 Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 4 Mar 2016 14:09:38 -0800 Subject: [PATCH 25/28] Recreated withdraw and withdraw_using_check methods for Account and CheckingAccount --- bank_accounts.rb | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index 84f39aa8..7ed92dae 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -5,7 +5,7 @@ module Bank class Account attr_reader :id, :balance, :date_created - MINIMUM_BALANCE = 100 + MINIMUM_STARTING_BALANCE = 100 def initialize(account_info) # manually choose the data from the first line of the CSV file and ensure @@ -15,7 +15,7 @@ def initialize(account_info) # from reading accounts.csv (which turns it into an array), and use indexes # of given array to shovel into account_info hash? - if account_info[:balance] < MINIMUM_BALANCE + if account_info[:balance] < MINIMUM_STARTING_BALANCE raise ArgumentError.new("You must have at least $1 to open an account.") end @id = account_info[:id] @@ -70,6 +70,14 @@ def add_owner(owner_name) #Bank::Owner.name end + def withdraw(amount_to_withdraw, minimum_balance=0, transaction_fee=0) + if @balance - amount_to_withdraw < minimum_balance + raise ArgumentError.new("This withdrawal would cause a negative balance.") + end + @balance = @balance - amount_to_withdraw + # show_balance + end + ### BRB REFACTORING # def withdraw(amount_to_withdraw, instance_var) # if amount_to_withdraw > @balance @@ -104,8 +112,14 @@ def show_balance end class CheckingAccount < Account - TRANSACTION_FEE = 100 - # instance_var is the name of the instance variable you're calling the method on + + def withdraw(amount_to_withdraw, minimum_balance=0, transaction_fee=100) + @balance = super - transaction_fee + end + + def withdraw_using_check(amount_to_withdraw, minimum_balance=-1000, transaction_fee=0) + withdraw(amount_to_withdraw, minimum_balance, transaction_fee) + end ### BRB REFACTORING # def withdraw(amount_to_withdraw, instance_var) @@ -114,6 +128,8 @@ class CheckingAccount < Account # return @balance # end + + ### BRB REFACTORING # def withdraw_using_check(amount_to_withdraw, instance_var) # minimum_balance = -1000 @@ -126,12 +142,11 @@ class CheckingAccount < Account end class SavingsAccount < Account - TRANSACTION_FEE = 200 - MINIMUM_BALANCE = 1000 + MINIMUM_STARTING_BALANCE = 1000 # The initial balance cannot be less than $10. def initialize(account_info) - if account_info[:balance] < MINIMUM_BALANCE + if account_info[:balance] < MINIMUM_STARTING_BALANCE raise ArgumentError.new("You must have at least $10 to open a savings account.") end super @@ -174,7 +189,7 @@ def initialize(name, phone_number, email_address, street_address) end # for testing the different methods in IRB? -@checking_instance = Bank::CheckingAccount.new({id: "1001", balance: 120045, date_created: "March 5, 2016"}) -@savings_instance = Bank::SavingsAccount.new({id: "1000", balance: 503030, date_created: "March 5, 2016"}) - +@checking_instance = Bank::CheckingAccount.new({id: "1001", balance: 20045, date_created: "March 5, 2016"}) +@savings_instance = Bank::SavingsAccount.new({id: "1000", balance: 55000, date_created: "March 5, 2016"}) +@account_instance = Bank::Account.new({id: "0999", balance: 20000, date_created: "March 3, 2016"}) @owner_instance = Bank::Owner.new("Barbara Thompson", "545-665-5535", "looploop@loo.org", "5545 Apple Drive Issaquah, WA 98645") From ddde179760dccdd0f57da574432846ec7a1314b8 Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 4 Mar 2016 14:22:22 -0800 Subject: [PATCH 26/28] Updated withdraw_using_check method to include check_count which goes up each time you call the method, and takes away a transaction fee when you try to use more than 3 checks --- bank_accounts.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index 7ed92dae..1bc00925 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -112,13 +112,25 @@ def show_balance end class CheckingAccount < Account + attr_reader :check_count + def initialize(account_info) + @check_count = 0 + super + end def withdraw(amount_to_withdraw, minimum_balance=0, transaction_fee=100) @balance = super - transaction_fee end def withdraw_using_check(amount_to_withdraw, minimum_balance=-1000, transaction_fee=0) - withdraw(amount_to_withdraw, minimum_balance, transaction_fee) + # does this first so the final value returned is @balance + @check_count += 1 + @balance = withdraw(amount_to_withdraw, minimum_balance, transaction_fee) + if @check_count > 3 + transaction_fee = 200 + @balance = @balance - transaction_fee + end + return @balance end ### BRB REFACTORING From d0e0adea562e26a49d2ab116471b5aa76a2bdb27 Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 4 Mar 2016 14:58:34 -0800 Subject: [PATCH 27/28] Made it so argument error still pops up for withdraw_using_check method when only the transaction fee sends it over --- bank_accounts.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index 1bc00925..f235c9e5 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -5,7 +5,7 @@ module Bank class Account attr_reader :id, :balance, :date_created - MINIMUM_STARTING_BALANCE = 100 + MINIMUM_STARTING_BALANCE = 0 def initialize(account_info) # manually choose the data from the first line of the CSV file and ensure @@ -16,7 +16,7 @@ def initialize(account_info) # of given array to shovel into account_info hash? if account_info[:balance] < MINIMUM_STARTING_BALANCE - raise ArgumentError.new("You must have at least $1 to open an account.") + raise ArgumentError.new("You must have at least $#{MINIMUM_STARTING_BALANCE} to open an account.") end @id = account_info[:id] @balance = account_info[:balance] @@ -72,7 +72,7 @@ def add_owner(owner_name) def withdraw(amount_to_withdraw, minimum_balance=0, transaction_fee=0) if @balance - amount_to_withdraw < minimum_balance - raise ArgumentError.new("This withdrawal would cause a negative balance.") + raise ArgumentError.new("This withdrawal would go below the minimum balance.") end @balance = @balance - amount_to_withdraw # show_balance @@ -119,6 +119,9 @@ def initialize(account_info) end def withdraw(amount_to_withdraw, minimum_balance=0, transaction_fee=100) + if super - transaction_fee + raise ArgumentError.new("This withdrawal would go below the minimum balance.") + end @balance = super - transaction_fee end @@ -130,6 +133,7 @@ def withdraw_using_check(amount_to_withdraw, minimum_balance=-1000, transaction_ transaction_fee = 200 @balance = @balance - transaction_fee end + # why do I need return @balance here? why does it return nil otherwise? return @balance end From 0dd8c9c149ddc2498287d58b3a3b393d23c4de24 Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 4 Mar 2016 23:37:09 -0800 Subject: [PATCH 28/28] Added check_withdraw_type and check_against_minimum methods to Account class, then linked child classes to that. Cleaned up code and deleted all superfluous comments and methods. --- bank_accounts.rb | 149 ++++++++++++++++------------------------------- 1 file changed, 49 insertions(+), 100 deletions(-) diff --git a/bank_accounts.rb b/bank_accounts.rb index f235c9e5..d0a08acf 100644 --- a/bank_accounts.rb +++ b/bank_accounts.rb @@ -1,6 +1,4 @@ require 'csv' -require 'money' -I18n.enforce_available_locales = false module Bank class Account @@ -8,32 +6,22 @@ class Account MINIMUM_STARTING_BALANCE = 0 def initialize(account_info) - # manually choose the data from the first line of the CSV file and ensure - # you can create a new instance of your Account using that data - - # create a hash with keys id, balance, date_created and get those values - # from reading accounts.csv (which turns it into an array), and use indexes - # of given array to shovel into account_info hash? - if account_info[:balance] < MINIMUM_STARTING_BALANCE raise ArgumentError.new("You must have at least $#{MINIMUM_STARTING_BALANCE} to open an account.") end @id = account_info[:id] @balance = account_info[:balance] @date_created = account_info[:date_created] - # ADD THIS BACK IN LATER - # @owner = account_info[:owner] # temporary value until Owner is created end def self.all - # this is what you'd do if working from a CSV file... not related to the instances we've initialized for testing + # this is what you'd do if working from a CSV file... not related to the instances we've initialized for testing. # pulls in the data from the CSV file as an array of arrays array_of_accounts = CSV.read("support/accounts.csv") account_info_array = [] - # this method should return an array of hashes, where each hash represents one row of data - # array_of_accounts.each do.... make a hash {id => element[0], balance => element[1], } - # instantiate a new Account based on that hash, after each one shovel into an array - # return that array to whoever calls this method + # this method should return an array of hashes, where each hash represents one row of data. + # instantiates a new Account based on each hash, after each one it shovels into an array and + # returns that array to whoever calls this method array_of_accounts.each do |element| account_info_array << Account.new({id: element[0], balance: element[1].to_i, date_created: element[2]}) end @@ -41,10 +29,10 @@ def self.all end def self.find(given_id) - #returns an instance of Account where the value of the id field in the CSV - #matches the passed parameter + # returns an instance of Account where the value of the id field in the CSV + # matches the passed parameter - # iterate over the Account instances until you find the instance with the matching id + # iterates over the Account instances until you find the instance with the matching id # all the account instances are listed in account_info_array # the problem with this is it generates the accounts from the CSV every time. @@ -59,54 +47,43 @@ def self.find(given_id) end end - def convert_cents(money) - Money.new(money, "USD") - end - # If the owner has already been created in the Owner class, the method should be called like so: - # account_instance.add_owner(owner_instance.name) + # @account_instance.add_owner(owner_instance.name) def add_owner(owner_name) @owner = owner_name #Bank::Owner.name end def withdraw(amount_to_withdraw, minimum_balance=0, transaction_fee=0) - if @balance - amount_to_withdraw < minimum_balance - raise ArgumentError.new("This withdrawal would go below the minimum balance.") - end + check_withdraw_type("account", amount_to_withdraw, minimum_balance, transaction_fee) @balance = @balance - amount_to_withdraw - # show_balance end - ### BRB REFACTORING - # def withdraw(amount_to_withdraw, instance_var) - # if amount_to_withdraw > @balance - # raise ArgumentError.new("This withdrawal would cause a negative balance. - # Do not attempt.") - # end - # @balance = @balance - amount_to_withdraw - # # included the following because otherwise "super" in the SavingsAccount - # # and CheckingAccount withdrawal methods will do show_balance twice, once - # # before the withdrawal has even occurred... and that's annoying :| - # unless instance_var.is_a?(SavingsAccount) || instance_var.is_a?(CheckingAccount) - # show_balance - # end - # return @balance - # end +# uses the type of withdraw ("from savings", "using a check", etc.) to determine if the user is overdrafting + def check_withdraw_type(withdraw_type, amount_to_withdraw, minimum_balance, transaction_fee) + case withdraw_type + when "account", "checking", "savings" + check_against_minimum(amount_to_withdraw, minimum_balance, transaction_fee) + when "check" + check_against_minimum(amount_to_withdraw, minimum_balance, transaction_fee) + if @check_count >= 3 + transaction_fee = 200 + check_against_minimum(amount_to_withdraw, minimum_balance, transaction_fee) + @balance = @balance - transaction_fee + end + end + end + +# compares balance with minimum balance and doesn't let the user continue if they try +# to go over + def check_against_minimum(amount_to_withdraw, minimum_balance, transaction_fee) + if (@balance - amount_to_withdraw - transaction_fee) < minimum_balance + raise ArgumentError.new("This withdrawal would go below the minimum balance.") + end + end def deposit(amount_to_deposit) @balance = @balance + amount_to_deposit - show_balance - return @balance - end - - # displays the balance in a nice user-friendly way, but also returns it to the other methods - def show_balance - # temporarily converts @balance to dollar format for easier readability, - # but doesn't affect original numbers. This way all math can still be done - # in cents. - pretty_money = convert_cents(@balance) - puts "The balance for this account is currently $#{pretty_money}." end end @@ -119,41 +96,20 @@ def initialize(account_info) end def withdraw(amount_to_withdraw, minimum_balance=0, transaction_fee=100) - if super - transaction_fee - raise ArgumentError.new("This withdrawal would go below the minimum balance.") - end + check_withdraw_type("checking", amount_to_withdraw, minimum_balance, transaction_fee) @balance = super - transaction_fee end def withdraw_using_check(amount_to_withdraw, minimum_balance=-1000, transaction_fee=0) - # does this first so the final value returned is @balance - @check_count += 1 + check_withdraw_type("check", amount_to_withdraw, minimum_balance, transaction_fee) @balance = withdraw(amount_to_withdraw, minimum_balance, transaction_fee) - if @check_count > 3 - transaction_fee = 200 - @balance = @balance - transaction_fee - end - # why do I need return @balance here? why does it return nil otherwise? + @check_count += 1 return @balance end - ### BRB REFACTORING - # def withdraw(amount_to_withdraw, instance_var) - # @balance = super - TRANSACTION_FEE - # show_balance - # return @balance - # end - - - - ### BRB REFACTORING - # def withdraw_using_check(amount_to_withdraw, instance_var) - # minimum_balance = -1000 - # if (@balance - amount_to_withdraw) < minimum_balance - # raise ArgumentError.new("Withdrawal by check only allows accounts to go - # into overdraft up to -$10. Do not attempt.") - # end - + def reset_checks + @check_count = 0 + end end @@ -161,25 +117,17 @@ class SavingsAccount < Account MINIMUM_STARTING_BALANCE = 1000 # The initial balance cannot be less than $10. - def initialize(account_info) - if account_info[:balance] < MINIMUM_STARTING_BALANCE - raise ArgumentError.new("You must have at least $10 to open a savings account.") - end + def initialize(account_info) + if account_info[:balance] < MINIMUM_STARTING_BALANCE + raise ArgumentError.new("You must have at least $#{MINIMUM_STARTING_BALANCE} to open a savings account.") + end super - end + end - ### BRB REFACTORING - # def withdraw(amount_to_withdraw, instance_var) - # # Does not allow the account to go below the $10 minimum balance - Will output a warning message - # if (@balance - amount_to_withdraw) < MINIMUM_BALANCE - # raise ArgumentError.new("Savings accounts must maintain at least - # $#{convert_cents(MINIMUM_BALANCE)}. Do not attempt.") - # else - # @balance = super - TRANSACTION_FEE - # show_balance - # return @balance - # end - # end + def withdraw(amount_to_withdraw, minimum_balance=1000, transaction_fee=200) + check_withdraw_type("savings", amount_to_withdraw, minimum_balance, transaction_fee) + @balance = super - transaction_fee + end def add_interest(rate) interest = calculate_interest(rate) @@ -204,8 +152,9 @@ def initialize(name, phone_number, email_address, street_address) end end -# for testing the different methods in IRB? +# for testing the different methods in IRB, play with them! +# when using in IRB, these instance names must have @ sign before them, but not when run in ruby @checking_instance = Bank::CheckingAccount.new({id: "1001", balance: 20045, date_created: "March 5, 2016"}) -@savings_instance = Bank::SavingsAccount.new({id: "1000", balance: 55000, date_created: "March 5, 2016"}) +@savings_instance = Bank::SavingsAccount.new({id: "1000", balance: 130000, date_created: "March 5, 2016"}) @account_instance = Bank::Account.new({id: "0999", balance: 20000, date_created: "March 3, 2016"}) @owner_instance = Bank::Owner.new("Barbara Thompson", "545-665-5535", "looploop@loo.org", "5545 Apple Drive Issaquah, WA 98645")