From a16a7c479a26a18dc9ffde19c28e96ef5fe847ae Mon Sep 17 00:00:00 2001 From: Dianne Laguerta Date: Tue, 23 Aug 2016 10:27:41 -0700 Subject: [PATCH 01/18] created Bank module; Account class; initialize, withdraw, and deposit methods --- Account.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Account.rb diff --git a/Account.rb b/Account.rb new file mode 100644 index 00000000..1c76e882 --- /dev/null +++ b/Account.rb @@ -0,0 +1,27 @@ +module Bank + + class Account + attr_accessor :id, :balance + + def initialize(id) #should I put a balance argument here? + @id = id + @balance = 1000 #inital balance of 1000 + end + + def withdraw(withdraw_amount) + + @balance = @balance - withdraw_amount + return @balance +#if balance withdraw goes below 0, raise error + end #withdraw method end + + def deposit(deposit_amount) + + @balance = @balance + deposit_amount + return @balance + + end #deposit method end + + end #Account class end + +end #module name end From 43b62fa7aa22977a3d6e0b207529336c2c0eadad Mon Sep 17 00:00:00 2001 From: Dianne Laguerta Date: Tue, 23 Aug 2016 10:58:01 -0700 Subject: [PATCH 02/18] Working on ArgumentError --- Account.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Account.rb b/Account.rb index 1c76e882..63704ff4 100644 --- a/Account.rb +++ b/Account.rb @@ -8,11 +8,14 @@ def initialize(id) #should I put a balance argument here? @balance = 1000 #inital balance of 1000 end - def withdraw(withdraw_amount) - - @balance = @balance - withdraw_amount + def withdraw(withdraw_amount, message= "Your balance cannot go below 0") + @balance = @balance - withdraw_amount + if @balance < 0 + raise ArgumentError.new(message)#if balance withdraw goes below 0, raise error + @balance = @balance + withdraw_amount + end return @balance -#if balance withdraw goes below 0, raise error + end #withdraw method end def deposit(deposit_amount) From a4ab46404f1aef9bf38fcb7a8f0347ee63e26c6b Mon Sep 17 00:00:00 2001 From: Dianne Laguerta Date: Tue, 23 Aug 2016 11:02:46 -0700 Subject: [PATCH 03/18] fixed ArgumentError for withdraw method --- Account.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Account.rb b/Account.rb index 63704ff4..29aa6a14 100644 --- a/Account.rb +++ b/Account.rb @@ -8,13 +8,14 @@ def initialize(id) #should I put a balance argument here? @balance = 1000 #inital balance of 1000 end - def withdraw(withdraw_amount, message= "Your balance cannot go below 0") - @balance = @balance - withdraw_amount - if @balance < 0 + def withdraw(withdraw_amount, message = "Your balance cannot go below 0") + if withdraw_amount > @balance raise ArgumentError.new(message)#if balance withdraw goes below 0, raise error - @balance = @balance + withdraw_amount + else + @balance = @balance - withdraw_amount + return @balance end - return @balance + end #withdraw method end From 7c1eb0e6061e64bead3d58a7fecb48e89b3c64f8 Mon Sep 17 00:00:00 2001 From: Dianne Laguerta Date: Tue, 23 Aug 2016 15:36:26 -0700 Subject: [PATCH 04/18] added error handling to initial balance --- Account.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Account.rb b/Account.rb index 29aa6a14..374ae0bd 100644 --- a/Account.rb +++ b/Account.rb @@ -3,9 +3,12 @@ module Bank class Account attr_accessor :id, :balance - def initialize(id) #should I put a balance argument here? + def initialize(id, balance, empty_account = "We're sorry, but you cannot open an account without some money!") #should I put a balance argument here? @id = id - @balance = 1000 #inital balance of 1000 + @balance = balance + if @balance <= 0 + raise ArgumentError.new(empty_account) + end end def withdraw(withdraw_amount, message = "Your balance cannot go below 0") From 668c758d8cde0b654b83d196d5b2fd46b2073831 Mon Sep 17 00:00:00 2001 From: Dianne Laguerta Date: Tue, 23 Aug 2016 22:13:10 -0700 Subject: [PATCH 05/18] finished baseline and requirements --- Account.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Account.rb b/Account.rb index 374ae0bd..95ae0a0b 100644 --- a/Account.rb +++ b/Account.rb @@ -3,7 +3,8 @@ module Bank class Account attr_accessor :id, :balance - def initialize(id, balance, empty_account = "We're sorry, but you cannot open an account without some money!") #should I put a balance argument here? + def initialize(id, balance, empty_account = "We're sorry, but you cannot open an account without some money!") + @id = id @balance = balance if @balance <= 0 @@ -12,8 +13,9 @@ def initialize(id, balance, empty_account = "We're sorry, but you cannot open an end def withdraw(withdraw_amount, message = "Your balance cannot go below 0") + if withdraw_amount > @balance - raise ArgumentError.new(message)#if balance withdraw goes below 0, raise error + raise ArgumentError.new(message) #raise error when balance drops below 0 else @balance = @balance - withdraw_amount return @balance From e16f8d052c3b61218618e0af76eb5d90f55d428e Mon Sep 17 00:00:00 2001 From: Dianne Laguerta Date: Wed, 24 Aug 2016 15:38:01 -0700 Subject: [PATCH 06/18] progress on CSV / self.all method --- Account.rb | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/Account.rb b/Account.rb index 95ae0a0b..276481b3 100644 --- a/Account.rb +++ b/Account.rb @@ -1,17 +1,34 @@ +require 'CSV' module Bank - class Account - attr_accessor :id, :balance + attr_accessor :id, :balance, :account_open_date - def initialize(id, balance, empty_account = "We're sorry, but you cannot open an account without some money!") + def initialize(accounts_hash, empty_account = "We're sorry, but you cannot open an account without some money!") + @id = accounts_hash[:id] + @balance = accounts_hash[:balance] + @account_open_date = accounts_hash[:account_open_date] - @id = id - @balance = balance if @balance <= 0 raise ArgumentError.new(empty_account) end + + end #end initialize method + + def self.all + accounts = [] + CSV.read("./support/accounts.csv").each do |line| + accounts_hash = {} + accounts_hash[:id] = line[0] + accounts_hash[:balance] = line[1].to_f/100 + accounts_hash[:account_open_date] = line[2] + accounts << Bank::Account.new(accounts_hash) end + return accounts + end + + +#DON'T TOUCH THIS YET def withdraw(withdraw_amount, message = "Your balance cannot go below 0") if withdraw_amount > @balance @@ -20,17 +37,20 @@ def withdraw(withdraw_amount, message = "Your balance cannot go below 0") @balance = @balance - withdraw_amount return @balance end - - end #withdraw method end def deposit(deposit_amount) - @balance = @balance + deposit_amount return @balance - end #deposit method end end #Account class end +end + + +foots = Bank::Account.all +puts foots -end #module name end +foots.each do |a| + puts a.balance +end From 3a210a75c4df8112ca87cb3da51a0ba9bade62e2 Mon Sep 17 00:00:00 2001 From: Dianne Laguerta Date: Thu, 25 Aug 2016 14:39:16 -0700 Subject: [PATCH 07/18] created working SavingsAccount with withdraw method --- SavingsAccount.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 SavingsAccount.rb diff --git a/SavingsAccount.rb b/SavingsAccount.rb new file mode 100644 index 00000000..6396cefd --- /dev/null +++ b/SavingsAccount.rb @@ -0,0 +1,33 @@ +#SavingsAccount.rb +#module Bank +# create a class called SavingsAccount +class SavingsAccount #< Account +# Inherit the behavior of the Account class (add the < Account ) +# Attributes of the SavingsAccount: +attr_accessor :balance + + def initialize(id, balance, minimum_account= "Your savings account must start with at least $10!") + @id = id + @balance = balance + #super() should allow us in inherit the instance variables of the Account class +# - It cannot go below a balance of $10 + if @balance < 10 + raise ArgumentError.new(minimum_account) + end +# if it does, raise an ArgumentError + end #end initialize + +# Create a method for withdrawal that: + def savings_withdrawal(withdraw_amount, minimum_account= "Your savings account cannot go below $10!") +# - subtracts $2 from the balance each time you withdrawal + if (@balance - 2 - withdraw_amount) < 10 + raise ArgumentError.new(minimum_account) + else + @balance = @balance - 2 - withdraw_amount + return @balance + end + end +end +# - Cannot go below $10, raises an argument error. This includes subtracting the original $2 when calling the method +# -return the original balance +#end From 352460fc16eb386198b31e7325745152137a7b90 Mon Sep 17 00:00:00 2001 From: Dianne Laguerta Date: Thu, 25 Aug 2016 15:56:30 -0700 Subject: [PATCH 08/18] created interest rate method --- SavingsAccount.rb | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/SavingsAccount.rb b/SavingsAccount.rb index 6396cefd..6cb7cd7d 100644 --- a/SavingsAccount.rb +++ b/SavingsAccount.rb @@ -1,10 +1,9 @@ #SavingsAccount.rb -#module Bank +module Bank # create a class called SavingsAccount -class SavingsAccount #< Account -# Inherit the behavior of the Account class (add the < Account ) -# Attributes of the SavingsAccount: -attr_accessor :balance +class SavingsAccount < Account + +attr_accessor :balance #add :id here? def initialize(id, balance, minimum_account= "Your savings account must start with at least $10!") @id = id @@ -27,7 +26,12 @@ def savings_withdrawal(withdraw_amount, minimum_account= "Your savings account c return @balance end end + +#add method for adding the interest rate + def add_interest(rate) + interest_amount = @balance * rate/100 + return @balance = @balance + interest_amount + end +end + end -# - Cannot go below $10, raises an argument error. This includes subtracting the original $2 when calling the method -# -return the original balance -#end From 75b6fa005aa7996da6a03be2e8e8e6b39c0d4a27 Mon Sep 17 00:00:00 2001 From: Dianne Laguerta Date: Thu, 25 Aug 2016 15:58:49 -0700 Subject: [PATCH 09/18] more edits, works with new SavingsAccount class --- Account.rb | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/Account.rb b/Account.rb index 276481b3..44ed8b22 100644 --- a/Account.rb +++ b/Account.rb @@ -1,4 +1,5 @@ require 'CSV' +#require_relative 'Owner.rb' module Bank class Account attr_accessor :id, :balance, :account_open_date @@ -14,21 +15,31 @@ def initialize(accounts_hash, empty_account = "We're sorry, but you cannot open end #end initialize method + #returns a collection of Account instances, representing all of the Accounts described in the CSV. See below for the CSV file specifications def self.all accounts = [] CSV.read("./support/accounts.csv").each do |line| accounts_hash = {} - accounts_hash[:id] = line[0] + accounts_hash[:id] = line[0].to_i accounts_hash[:balance] = line[1].to_f/100 accounts_hash[:account_open_date] = line[2] - accounts << Bank::Account.new(accounts_hash) - end + accounts << Bank::Account.new(accounts_hash) + end return accounts end + #returns an instance of Account where the value of the id field in the CSV matches the passed parameter + def self.find(id) + self.all.each do |account| + if account.id == id + return account + end + end + end -#DON'T TOUCH THIS YET + #DON'T TOUCH THIS YET + #method to withdraw money from an account def withdraw(withdraw_amount, message = "Your balance cannot go below 0") if withdraw_amount > @balance @@ -39,6 +50,7 @@ def withdraw(withdraw_amount, message = "Your balance cannot go below 0") end end #withdraw method end + #method to deposit money into an account def deposit(deposit_amount) @balance = @balance + deposit_amount return @balance @@ -47,10 +59,17 @@ def deposit(deposit_amount) end #Account class end end - -foots = Bank::Account.all -puts foots - -foots.each do |a| - puts a.balance -end +# +# +# # big_foots = Bank::Account.all +# # puts big_foots +# foots = Bank::Account.find(1217) +# # puts foots +# # puts foots.balance +# # puts foots.withdraw(20) +# # +# +# puts "Foots' Account ID: #{foots.id}. Starting balance: $#{foots.balance}" +# puts "Need to buy beer, withdrew $50. Balance after withdrawal: #{foots.withdraw(50)}" +# puts "Got paid, deposited $20! Balance = $#{foots.deposit(20)}" +# puts "Final balance: $#{foots.balance}" From 78d87bced4e09e9ecbe3d8e7097cb439b5affb6f Mon Sep 17 00:00:00 2001 From: Dianne Laguerta Date: Thu, 25 Aug 2016 16:55:06 -0700 Subject: [PATCH 10/18] Started working on CheckingAccount class, pseduocode writeup --- CheckingAccount.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 CheckingAccount.rb diff --git a/CheckingAccount.rb b/CheckingAccount.rb new file mode 100644 index 00000000..18977473 --- /dev/null +++ b/CheckingAccount.rb @@ -0,0 +1,29 @@ +module Bank + class CheckingAccount < Account + #think about addding a constant for minimum balance of -10 +#create CheckingAccount class with inherit Account class +#create withdrawal method + #method subtracts $1 for each withdrawal, plus the withdrawal amount + #if balance goes negative, raise argument error and return original unmodified balance + #returns updated balance + #end withdraw method + + #xcreate a withdraw_using_check(amount) method + #def withdraw_using_check(amount) + #check_count = 0 <--make as an instance variable? + #allow account to overdraft up to $-10 but not any lower + #allow user to withdrawwith penalty the first 3 times + #if > 3, subtract $2 as a transaction fee + # if check_count > 3 + # @balance = balance - amount - 2 + #return @balance + + #check_count += 1 + #end of withdraw_using_check method + + #reset_checks -- resets the checks to zero + #def reset_checks + #check_count = 0 + #end + end CheckingAccount +end #bank From d55da8ea958c19b1377d40e826207879b018704f Mon Sep 17 00:00:00 2001 From: Dianne Laguerta Date: Thu, 25 Aug 2016 20:04:24 -0700 Subject: [PATCH 11/18] fixing bug in withdraw_using_check method --- CheckingAccount.rb | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/CheckingAccount.rb b/CheckingAccount.rb index 18977473..94eb1cff 100644 --- a/CheckingAccount.rb +++ b/CheckingAccount.rb @@ -1,17 +1,39 @@ +require_relative "Account" module Bank class CheckingAccount < Account + attr_accessor :check_count #think about addding a constant for minimum balance of -10 #create CheckingAccount class with inherit Account class #create withdrawal method + def initialize + @check_count = 0 + end + + def withdraw(withdraw_amount) + + #method subtracts $1 for each withdrawal, plus the withdrawal amount #if balance goes negative, raise argument error and return original unmodified balance #returns updated balance #end withdraw method + end #end withdraw method #xcreate a withdraw_using_check(amount) method - #def withdraw_using_check(amount) - #check_count = 0 <--make as an instance variable? + def withdraw_using_check(amount) + #make check_count an instance variable? #allow account to overdraft up to $-10 but not any lower + if @balance != -10 + raise ArgumentError + else + @check_count += 1 + @balance = @balance - amount + return @balance + end + + if @check_count > 3 + @balance = @balance - 2 - amount + return @balance + end #allow user to withdrawwith penalty the first 3 times #if > 3, subtract $2 as a transaction fee # if check_count > 3 @@ -20,10 +42,11 @@ class CheckingAccount < Account #check_count += 1 #end of withdraw_using_check method - + end #reset_checks -- resets the checks to zero - #def reset_checks - #check_count = 0 - #end - end CheckingAccount + def reset_checks + @check_count = 0 + end + + end #CheckingAccount end #bank From dc79071c6212e467472d1a2a4e5ac9ef8f653a3a Mon Sep 17 00:00:00 2001 From: Dianne Laguerta Date: Fri, 26 Aug 2016 10:07:54 -0700 Subject: [PATCH 12/18] fixed withdraw_using_check method, things work! Now on to refactoring --- CheckingAccount.rb | 64 ++++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 39 deletions(-) diff --git a/CheckingAccount.rb b/CheckingAccount.rb index 94eb1cff..4ac610e3 100644 --- a/CheckingAccount.rb +++ b/CheckingAccount.rb @@ -2,51 +2,37 @@ module Bank class CheckingAccount < Account attr_accessor :check_count + #think about addding a constant for minimum balance of -10 -#create CheckingAccount class with inherit Account class -#create withdrawal method - def initialize + #create CheckingAccount class with inherit Account class + #create withdrawal method + def initialize(id, balance) #fix here to use super and inherit id from Account class + @id = id + @balance = balance @check_count = 0 - end - - def withdraw(withdraw_amount) - - - #method subtracts $1 for each withdrawal, plus the withdrawal amount - #if balance goes negative, raise argument error and return original unmodified balance - #returns updated balance - #end withdraw method - end #end withdraw method - #xcreate a withdraw_using_check(amount) method - def withdraw_using_check(amount) - #make check_count an instance variable? - #allow account to overdraft up to $-10 but not any lower - if @balance != -10 - raise ArgumentError + end + + def withdraw_using_check(amount, message = "Don't go below $-10") + #make check_count an instance variable? + if @balance - amount < -10 + raise ArgumentError.new(message) else - @check_count += 1 - @balance = @balance - amount - return @balance + if @check_count > 3 + @check_count += 1 + @balance = @balance - 2 - amount + return @balance + else + @check_count += 1 + @balance = @balance - amount + return @balance + end end - - if @check_count > 3 - @balance = @balance - 2 - amount - return @balance end - #allow user to withdrawwith penalty the first 3 times - #if > 3, subtract $2 as a transaction fee - # if check_count > 3 - # @balance = balance - amount - 2 - #return @balance - - #check_count += 1 - #end of withdraw_using_check method - end - #reset_checks -- resets the checks to zero - def reset_checks - @check_count = 0 - end + #reset_checks -- resets the checks to zero + def reset_checks + @check_count = 0 + end end #CheckingAccount end #bank From c1a320433624d8e2a1c56cd296e7c4b87bcd3cbf Mon Sep 17 00:00:00 2001 From: Dianne Laguerta Date: Fri, 26 Aug 2016 11:26:25 -0700 Subject: [PATCH 13/18] lots of fixes for withdraw methods in Account and CheckingAccount --- Account.rb | 22 ++++------------------ CheckingAccount.rb | 15 ++++++++++++++- SavingsAccount.rb | 2 +- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/Account.rb b/Account.rb index 44ed8b22..8ec915e0 100644 --- a/Account.rb +++ b/Account.rb @@ -42,10 +42,11 @@ def self.find(id) #method to withdraw money from an account def withdraw(withdraw_amount, message = "Your balance cannot go below 0") - if withdraw_amount > @balance - raise ArgumentError.new(message) #raise error when balance drops below 0 + if (withdraw_amount + 1) > @balance + raise Exception.new(message) + return @balance else - @balance = @balance - withdraw_amount + @balance = @balance - withdraw_amount - 1 return @balance end end #withdraw method end @@ -58,18 +59,3 @@ def deposit(deposit_amount) end #Account class end end - -# -# -# # big_foots = Bank::Account.all -# # puts big_foots -# foots = Bank::Account.find(1217) -# # puts foots -# # puts foots.balance -# # puts foots.withdraw(20) -# # -# -# puts "Foots' Account ID: #{foots.id}. Starting balance: $#{foots.balance}" -# puts "Need to buy beer, withdrew $50. Balance after withdrawal: #{foots.withdraw(50)}" -# puts "Got paid, deposited $20! Balance = $#{foots.deposit(20)}" -# puts "Final balance: $#{foots.balance}" diff --git a/CheckingAccount.rb b/CheckingAccount.rb index 4ac610e3..79fc7f93 100644 --- a/CheckingAccount.rb +++ b/CheckingAccount.rb @@ -6,13 +6,26 @@ class CheckingAccount < Account #think about addding a constant for minimum balance of -10 #create CheckingAccount class with inherit Account class #create withdrawal method + + def initialize(id, balance) #fix here to use super and inherit id from Account class @id = id @balance = balance @check_count = 0 + end +#I don't even need this here anymore? + def withdraw(withdraw_amount, message = "Your balance cannot go below 0") + super + # @balance = @balance - 1 - withdraw_amount + # if @balance < 0 + # puts message + # return @balance = @balance + 1 + withdraw_amount + # else + # return @balance + # end end - + def withdraw_using_check(amount, message = "Don't go below $-10") #make check_count an instance variable? if @balance - amount < -10 diff --git a/SavingsAccount.rb b/SavingsAccount.rb index 6cb7cd7d..d480c9e7 100644 --- a/SavingsAccount.rb +++ b/SavingsAccount.rb @@ -1,4 +1,4 @@ -#SavingsAccount.rb +require_relative "Account" module Bank # create a class called SavingsAccount class SavingsAccount < Account From 498013b96e00349b55ae9d9260928623a2eb58c0 Mon Sep 17 00:00:00 2001 From: Dianne Laguerta Date: Fri, 26 Aug 2016 12:14:44 -0700 Subject: [PATCH 14/18] fixed withdraw functionality in CheckingAccount to use super --- Account.rb | 6 +++--- CheckingAccount.rb | 8 +------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/Account.rb b/Account.rb index 8ec915e0..1edb29ec 100644 --- a/Account.rb +++ b/Account.rb @@ -42,11 +42,11 @@ def self.find(id) #method to withdraw money from an account def withdraw(withdraw_amount, message = "Your balance cannot go below 0") - if (withdraw_amount + 1) > @balance - raise Exception.new(message) + if withdraw_amount > @balance + puts message return @balance else - @balance = @balance - withdraw_amount - 1 + @balance = @balance - withdraw_amount return @balance end end #withdraw method end diff --git a/CheckingAccount.rb b/CheckingAccount.rb index 79fc7f93..361934ca 100644 --- a/CheckingAccount.rb +++ b/CheckingAccount.rb @@ -16,14 +16,8 @@ def initialize(id, balance) #fix here to use super and inherit id from Account c #I don't even need this here anymore? def withdraw(withdraw_amount, message = "Your balance cannot go below 0") + withdraw_amount = withdraw_amount + 1 super - # @balance = @balance - 1 - withdraw_amount - # if @balance < 0 - # puts message - # return @balance = @balance + 1 + withdraw_amount - # else - # return @balance - # end end def withdraw_using_check(amount, message = "Don't go below $-10") From 0781f353850711c9e69d7d675e794c559c580bc8 Mon Sep 17 00:00:00 2001 From: Dianne Laguerta Date: Fri, 26 Aug 2016 14:16:39 -0700 Subject: [PATCH 15/18] fixed withdraw method in SavingsAccount to call super, added constants --- CheckingAccount.rb | 4 +--- SavingsAccount.rb | 26 ++++++++++++++++---------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/CheckingAccount.rb b/CheckingAccount.rb index 361934ca..1b7e379f 100644 --- a/CheckingAccount.rb +++ b/CheckingAccount.rb @@ -4,8 +4,6 @@ class CheckingAccount < Account attr_accessor :check_count #think about addding a constant for minimum balance of -10 - #create CheckingAccount class with inherit Account class - #create withdrawal method def initialize(id, balance) #fix here to use super and inherit id from Account class @@ -14,7 +12,7 @@ def initialize(id, balance) #fix here to use super and inherit id from Account c @check_count = 0 end -#I don't even need this here anymore? +#withdraw method that subtracts 1 for withdraw fee def withdraw(withdraw_amount, message = "Your balance cannot go below 0") withdraw_amount = withdraw_amount + 1 super diff --git a/SavingsAccount.rb b/SavingsAccount.rb index d480c9e7..134d9904 100644 --- a/SavingsAccount.rb +++ b/SavingsAccount.rb @@ -2,7 +2,7 @@ module Bank # create a class called SavingsAccount class SavingsAccount < Account - +MINIMUM_BALANCE = 10 attr_accessor :balance #add :id here? def initialize(id, balance, minimum_account= "Your savings account must start with at least $10!") @@ -10,21 +10,27 @@ def initialize(id, balance, minimum_account= "Your savings account must start wi @balance = balance #super() should allow us in inherit the instance variables of the Account class # - It cannot go below a balance of $10 - if @balance < 10 + if @balance < MINIMUM_BALANCE raise ArgumentError.new(minimum_account) end # if it does, raise an ArgumentError end #end initialize -# Create a method for withdrawal that: - def savings_withdrawal(withdraw_amount, minimum_account= "Your savings account cannot go below $10!") -# - subtracts $2 from the balance each time you withdrawal - if (@balance - 2 - withdraw_amount) < 10 - raise ArgumentError.new(minimum_account) - else - @balance = @balance - 2 - withdraw_amount - return @balance +#old savings withdrawal method + def withdraw(withdraw_amount, minimum_account= "Your savings account cannot go below $10!") + withdraw_amount = withdraw_amount + 2 + if (@balance - withdraw_amount) < MINIMUM_BALANCE + puts minimum_account + return @balance end + super + # if (@balance - withdraw_amount) < MINIMUM_BALANCE + # puts minimum_account + # return @balance + # else + # @balance = @balance - 2 - withdraw_amount + # return @balance + # end end #add method for adding the interest rate From e153fef6b1db56140ce723825ca491ea257eb4d9 Mon Sep 17 00:00:00 2001 From: Dianne Laguerta Date: Fri, 26 Aug 2016 15:42:28 -0700 Subject: [PATCH 16/18] current versions of all accounts; all basic requirements should be in working order --- Account.rb | 28 ++++++++++------- CheckingAccount.rb | 12 ++++---- SavingsAccount.rb | 77 ++++++++++++++++++++++++++-------------------- 3 files changed, 66 insertions(+), 51 deletions(-) diff --git a/Account.rb b/Account.rb index 1edb29ec..15cd9cc2 100644 --- a/Account.rb +++ b/Account.rb @@ -4,16 +4,25 @@ module Bank class Account attr_accessor :id, :balance, :account_open_date - def initialize(accounts_hash, empty_account = "We're sorry, but you cannot open an account without some money!") - @id = accounts_hash[:id] - @balance = accounts_hash[:balance] - @account_open_date = accounts_hash[:account_open_date] - - if @balance <= 0 - raise ArgumentError.new(empty_account) + def initialize(id, balance, minimum_account= "Your savings account cannot go below $10!") + @id = id + @balance = balance + if @balance <= self.class::MINIMUM_BALANCE + raise ArgumentError.new(minimum_account) end + end - end #end initialize method +#second wave initialize method to take in CSV file + # def initialize(accounts_hash, empty_account = "We're sorry, but you cannot open an account without some money!") + # @id = accounts_hash[:id] + # @balance = accounts_hash[:balance] + # @account_open_date = accounts_hash[:account_open_date] + # + # if @balance <= self.class::MINIMUM_BALANCE + # raise ArgumentError.new(empty_account) + # end + # + # end #end initialize method #returns a collection of Account instances, representing all of the Accounts described in the CSV. See below for the CSV file specifications def self.all @@ -36,9 +45,6 @@ def self.find(id) end end end - - - #DON'T TOUCH THIS YET #method to withdraw money from an account def withdraw(withdraw_amount, message = "Your balance cannot go below 0") diff --git a/CheckingAccount.rb b/CheckingAccount.rb index 1b7e379f..741a23f7 100644 --- a/CheckingAccount.rb +++ b/CheckingAccount.rb @@ -1,14 +1,13 @@ require_relative "Account" module Bank class CheckingAccount < Account + MINIMUM_BALANCE = 0 attr_accessor :check_count - #think about addding a constant for minimum balance of -10 - - - def initialize(id, balance) #fix here to use super and inherit id from Account class - @id = id - @balance = balance + def initialize(id, balance) + super + # @id = id + # @balance = balance @check_count = 0 end @@ -34,6 +33,7 @@ def withdraw_using_check(amount, message = "Don't go below $-10") end end end + #reset_checks -- resets the checks to zero def reset_checks @check_count = 0 diff --git a/SavingsAccount.rb b/SavingsAccount.rb index 134d9904..d4cb4bc6 100644 --- a/SavingsAccount.rb +++ b/SavingsAccount.rb @@ -1,43 +1,52 @@ require_relative "Account" module Bank -# create a class called SavingsAccount -class SavingsAccount < Account -MINIMUM_BALANCE = 10 -attr_accessor :balance #add :id here? + # create a class called SavingsAccount + class SavingsAccount < Account + MINIMUM_BALANCE = 10 + attr_accessor :balance #add :id here? - def initialize(id, balance, minimum_account= "Your savings account must start with at least $10!") - @id = id - @balance = balance - #super() should allow us in inherit the instance variables of the Account class -# - It cannot go below a balance of $10 - if @balance < MINIMUM_BALANCE - raise ArgumentError.new(minimum_account) - end -# if it does, raise an ArgumentError - end #end initialize + def initialize(id, balance)# minimum_account= "Your savings account must start with at least $10!") + super + # @id = id + # @balance = balance + #super() should allow us in inherit the instance variables of the Account class + # - It cannot go below a balance of $10 + # if @balance < MINIMUM_BALANCE + # raise ArgumentError.new(minimum_account) + # end + # if it does, raise an ArgumentError + end #end initialize -#old savings withdrawal method - def withdraw(withdraw_amount, minimum_account= "Your savings account cannot go below $10!") - withdraw_amount = withdraw_amount + 2 - if (@balance - withdraw_amount) < MINIMUM_BALANCE - puts minimum_account - return @balance + #old savings withdrawal method + def withdraw(withdraw_amount, minimum_account= "Your savings account cannot go below $10!") + withdraw_amount = withdraw_amount + 2 + if (@balance - withdraw_amount) < MINIMUM_BALANCE + puts minimum_account + return @balance + end + super end - super - # if (@balance - withdraw_amount) < MINIMUM_BALANCE - # puts minimum_account - # return @balance - # else - # @balance = @balance - 2 - withdraw_amount - # return @balance - # end - end -#add method for adding the interest rate - def add_interest(rate) - interest_amount = @balance * rate/100 - return @balance = @balance + interest_amount + #add method for adding the interest rate + def add_interest(rate) + interest_amount = @balance * rate/100 + return @balance = @balance + interest_amount + end end end -end + +#Stuff run in terminal +=begin +foot_saves = Bank::SavingsAccount.new("FootsMoney", 600) + => # +2.3.1 :002 > foot_saves.withdraw(100) + => 498 + 2.3.1 :004 > foot_saves.add_interest(20) + => 597 + 2.3.1 :005 > foot_saves.balance + => 597 +2.3.1 :007 > foot_saves.withdraw(590) +Your savings account cannot go below $10! + => 597 +=end From a8cdf3daf35d5dcdd85190b799d39cc4e0ba2383 Mon Sep 17 00:00:00 2001 From: Dianne Laguerta Date: Fri, 26 Aug 2016 17:00:46 -0700 Subject: [PATCH 17/18] adding sample code to run into irb --- CheckingAccount.rb | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/CheckingAccount.rb b/CheckingAccount.rb index 741a23f7..f2025fe6 100644 --- a/CheckingAccount.rb +++ b/CheckingAccount.rb @@ -41,3 +41,42 @@ def reset_checks end #CheckingAccount end #bank + + +#Code I run in Terminal to test +=begin +foots = Bank::CheckingAccount.new(1234, 400) +foots.withdraw(300) + => 99 +foots.check_count + => 0 +foots.withdraw_using_check(5) + => 94 +foots.check_count + => 1 +foots.withdraw_using_check(5) + => 89 +foots.withdraw_using_check(5) + => 84 +foots.withdraw_using_check(5) + => 79 +foots.withdraw_using_check(5) + => 72 +foots.withdraw_using_check(70) + => 0 +foots.withdraw_using_check(5) + => -7 +foots.withdraw_using_check(5) +# ArgumentError: Don't go below $-10 + from /Users/dlaguerta/ada/project-forks/BankAccounts/CheckingAccount.rb:26:in `withdraw_using_check' + from (irb):12 + from /Users/dlaguerta/.rvm/rubies/ruby-2.3.1/bin/irb:11:in `
' +foots.balance + => -7 +foots.deposit(500) + => 493 +foots.reset_checks + => 0 +foots.withdraw(80) + => 412 +=end From 12dbe2eb32103b52012ce050ff67782c431d2422 Mon Sep 17 00:00:00 2001 From: Dianne Laguerta Date: Fri, 26 Aug 2016 23:58:00 -0700 Subject: [PATCH 18/18] added sample code to run in Terminal(irb) --- CheckingAccount.rb | 67 ++++++++++++++++++++++------------------------ SavingsAccount.rb | 23 ++++++++-------- 2 files changed, 43 insertions(+), 47 deletions(-) diff --git a/CheckingAccount.rb b/CheckingAccount.rb index f2025fe6..c258f9f6 100644 --- a/CheckingAccount.rb +++ b/CheckingAccount.rb @@ -44,39 +44,36 @@ def reset_checks #Code I run in Terminal to test -=begin -foots = Bank::CheckingAccount.new(1234, 400) -foots.withdraw(300) - => 99 -foots.check_count - => 0 -foots.withdraw_using_check(5) - => 94 -foots.check_count - => 1 -foots.withdraw_using_check(5) - => 89 -foots.withdraw_using_check(5) - => 84 -foots.withdraw_using_check(5) - => 79 -foots.withdraw_using_check(5) - => 72 -foots.withdraw_using_check(70) - => 0 -foots.withdraw_using_check(5) - => -7 -foots.withdraw_using_check(5) + +puts foots = Bank::CheckingAccount.new(1234, 400) +puts foots.withdraw(300) + #=> 99 +puts foots.check_count +# => 0 +puts foots.withdraw_using_check(5) +# => 94 +puts foots.check_count +# => 1 +puts foots.withdraw_using_check(5) +# => 89 +puts foots.withdraw_using_check(5) +# => 84 +puts foots.withdraw_using_check(5) +# => 79 +puts foots.withdraw_using_check(5) +# => 72 +puts foots.withdraw_using_check(70) +# => 0 +puts foots.withdraw_using_check(5) +# => -7 +puts foots.reset_checks +# => 0 +puts foots.withdraw_using_check(5) # ArgumentError: Don't go below $-10 - from /Users/dlaguerta/ada/project-forks/BankAccounts/CheckingAccount.rb:26:in `withdraw_using_check' - from (irb):12 - from /Users/dlaguerta/.rvm/rubies/ruby-2.3.1/bin/irb:11:in `
' -foots.balance - => -7 -foots.deposit(500) - => 493 -foots.reset_checks - => 0 -foots.withdraw(80) - => 412 -=end +puts foots.balance +# => -7 +puts foots.deposit(500) +# => 493 + +puts foots.withdraw(80) +# => 412 diff --git a/SavingsAccount.rb b/SavingsAccount.rb index d4cb4bc6..d2de438f 100644 --- a/SavingsAccount.rb +++ b/SavingsAccount.rb @@ -37,16 +37,15 @@ def add_interest(rate) #Stuff run in terminal -=begin foot_saves = Bank::SavingsAccount.new("FootsMoney", 600) - => # -2.3.1 :002 > foot_saves.withdraw(100) - => 498 - 2.3.1 :004 > foot_saves.add_interest(20) - => 597 - 2.3.1 :005 > foot_saves.balance - => 597 -2.3.1 :007 > foot_saves.withdraw(590) -Your savings account cannot go below $10! - => 597 -=end + #=> # +foot_saves.withdraw(100) + #=> 498 +foot_saves.add_interest(20) +# => 597 +foot_saves.balance +# => 597 +foot_saves.withdraw(590) +#Your savings account cannot go below $10! +# => 597 +foots.add_interest(20)