From e295a92fd9a4af16f3e82ea28518ff120a2ce124 Mon Sep 17 00:00:00 2001 From: Allison Northrop Date: Wed, 22 Feb 2017 09:15:00 -0800 Subject: [PATCH 1/6] wrote wave 1 code to address account tests --- lib/account.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/lib/account.rb b/lib/account.rb index e69de29b..67bb33c9 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -0,0 +1,33 @@ +#all ruby code should be in the lib folder. it should be lib/class_name.rb +#the specs will be in that directory specs/class_name_spec.rb +#this is where I do the first wave. only in one class + +module Bank + class Account + attr_reader :id, :balance + + def initialize(id, balance) + raise ArgumentError.new("balance must be >= 0") if balance < 0 + + @id = id + @balance = balance + end + + def withdraw(amount) + raise ArgumentError.new("Withdraw amount must be a positive integer") if amount < 0 + if @balance < amount + puts "Warning: desired withdrawl amount exceeds account balance." + return @balance + else + @balance -= amount + return @balance + end + end + + def deposit(amount) + raise ArgumentError.new("Deposit amount must be a positive integer") if amount < 0 + @balance += amount + return @balance + end + end +end From 962b37984e9bdb21e8ba93c24b959a994a7b7262 Mon Sep 17 00:00:00 2001 From: Allison Northrop Date: Fri, 24 Feb 2017 14:28:58 -0800 Subject: [PATCH 2/6] wrote a all.self method and a find.selffind and add method --- lib/account.rb | 39 ++++++++++++++++++++---- specs/account_spec.rb | 71 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 89 insertions(+), 21 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 67bb33c9..8255082c 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -1,18 +1,41 @@ -#all ruby code should be in the lib folder. it should be lib/class_name.rb -#the specs will be in that directory specs/class_name_spec.rb -#this is where I do the first wave. only in one class - +require 'csv' module Bank class Account - attr_reader :id, :balance + attr_reader :id, :balance, :open_date - def initialize(id, balance) + def initialize(id, balance, open_date = nil) #the equal to nil is so the other wave one tests pass raise ArgumentError.new("balance must be >= 0") if balance < 0 @id = id @balance = balance + @open_date = open_date + end + def self.all + accounts = [] + CSV.read("support/accounts.csv").each do |object| + id = object[0].to_i + balance = object[1].to_f/100 + open_date = object[2] + a_account = Bank::Account.new(id, balance, open_date) + accounts << a_account + end + print accounts + return accounts + end + + def self.find(id) + a_account = Bank::Account.all + a_account.each do |object| + if object.id == id + return object + end + end + raise ArgumentError.new "Account: #{id} does not exist" + end + + def withdraw(amount) raise ArgumentError.new("Withdraw amount must be a positive integer") if amount < 0 if @balance < amount @@ -31,3 +54,7 @@ def deposit(amount) end end end + +# account_1 = Bank::Account.new(1212, 1235667, "1999-03-27 11:30:09 -0800") +# Bank::Account.all +Bank::Account.find(1212) diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 6c399139..dac81350 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -2,6 +2,8 @@ require 'minitest/reporters' require 'minitest/skip_dsl' require_relative '../lib/account' +require 'csv' + describe "Wave 1" do describe "Account#initialize" do @@ -67,7 +69,7 @@ # anything at all is printed out the test will pass. proc { account.withdraw(withdrawal_amount) - }.must_output /.+/ + }.must_output(/.+/) end it "Doesn't modify the balance if the account would go negative" do @@ -136,36 +138,75 @@ end end -# TODO: change 'xdescribe' to 'describe' to run these tests -xdescribe "Wave 2" do +# change 'xdescribe' to 'describe' to run these tests +describe "Wave 2" do describe "Account.all" do + + before do #this before do thing runs things before running any of the tests + @accounts = Bank::Account.all + end + it "Returns an array of all accounts" do - # TODO: Your test code here! - # Useful checks might include: - # - Account.all returns an array - # - Everything in the array is an Account - # - The number of accounts is correct - # - The ID and balance of the first and last - # accounts match what's in the CSV file - # Feel free to split this into multiple tests if needed + # accounts = Bank::Account.all + @accounts.must_be_instance_of Array + end + + it "Everything in the array is an Account" do + @accounts.each do |object| + object.must_be_instance_of Bank::Account + end + end + + it "The number of accounts is correct" do + @accounts.length.must_equal 12 + end + + it "The ID and balance of the first and last" do + @accounts.first.id.must_equal 1212 + @accounts.first.balance.must_equal 12356.67 + 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].id.must_equal line[1].to_f + accounts[index].id.must_equal line[2] + index += 1 + end end end + describe "Account.find" do it "Returns an account that exists" do - # TODO: Your test code here! + account = Bank::Account.find(1217) + account.must_be_instance_of Bank::Account + account.id.must_equal 1217 + account.balance.must_equal 123.23 + account.open_date.must_equal "2003-11-07 11:34:56 -0800" + end it "Can find the first account from the CSV" do - # TODO: Your test code here! + account = Bank::Account.find(1212) + account.must_be_instance_of Bank::Account + account.id.must_equal 1212 + account.balance.must_equal 12356.67 + account.open_date.must_equal "1999-03-27 11:30:09 -0800" end it "Can find the last account from the CSV" do - # TODO: Your test code here! + account = Bank::Account.find(15156) + + account.must_be_instance_of Bank::Account + account.id.must_equal 15156 + account.balance.must_equal 43567.72 + account.open_date.must_equal "1994-11-17 14:04:56 -0800" end it "Raises an error for an account that doesn't exist" do - # TODO: Your test code here! + proc { Bank::Account.find(1234567890) }.must_raise ArgumentError end end end From 1b27bf43674a46bd07921f83ab65d13791ac9241 Mon Sep 17 00:00:00 2001 From: Allison Northrop Date: Fri, 24 Feb 2017 15:48:04 -0800 Subject: [PATCH 3/6] created a sub class SavingsAccount --- lib/checking_account.rb | 2 ++ lib/savings_account.rb | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 lib/checking_account.rb create mode 100644 lib/savings_account.rb diff --git a/lib/checking_account.rb b/lib/checking_account.rb new file mode 100644 index 00000000..93cf5338 --- /dev/null +++ b/lib/checking_account.rb @@ -0,0 +1,2 @@ +require 'csv' +require_relative '../lib/account' diff --git a/lib/savings_account.rb b/lib/savings_account.rb new file mode 100644 index 00000000..0fa88483 --- /dev/null +++ b/lib/savings_account.rb @@ -0,0 +1,33 @@ +require 'csv' +require_relative 'account' +# attr_accessor :id +# inherit behavior from the Account class. +module Bank + class SavingsAccount < Account + + def initialize(id, balance) + super(id, balance) + end + + + + end +end + + + + +# SavingsAccount +# +# Create a SavingsAccount class which should inherit behavior from the Account class. It should include the following updated functionality: +# +# The initial balance cannot be less than $10. If it is, this will raise an ArgumentError +# Updated withdrawal functionality: +# Each withdrawal 'transaction' incurs a fee of $2 that is taken out of the balance. +# Does not allow the account to go below the $10 minimum balance - Will output a warning message and return the original un-modified balance +# It should include the following new method: +# +# #add_interest(rate): Calculate the interest on the balance and add the interest to the balance. Return the interest that was calculated and added to the balance (not the updated balance). +# Input rate is assumed to be a percentage (i.e. 0.25). +# The formula for calculating interest is balance * rate/100 +# Example: If the interest rate is 0.25 and the balance is $10,000, then the interest that is returned is $25 and the new balance becomes $10,025. From b0336d8c7d6a42bb84aa58d38fe45071a202d2b7 Mon Sep 17 00:00:00 2001 From: Allison Northrop Date: Fri, 24 Feb 2017 15:53:19 -0800 Subject: [PATCH 4/6] wrote a test for returning balance arg error --- specs/checking_account_spec.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/specs/checking_account_spec.rb b/specs/checking_account_spec.rb index 7f95339e..44e37488 100644 --- a/specs/checking_account_spec.rb +++ b/specs/checking_account_spec.rb @@ -1,6 +1,8 @@ require 'minitest/autorun' require 'minitest/reporters' require 'minitest/skip_dsl' +require 'csv' +# require_relative '../lib/checking_account' # TODO: uncomment the next line once you start wave 3 and add lib/checking_account.rb # require_relative '../lib/checking_account' From 648e382070b4f0486fc4fd02e88e539d1d0eecd0 Mon Sep 17 00:00:00 2001 From: Allison Northrop Date: Fri, 24 Feb 2017 15:53:56 -0800 Subject: [PATCH 5/6] wrote a test for returning balance arg error --- specs/savings_account_spec.rb | 71 ++++++++++++++++------------------- 1 file changed, 33 insertions(+), 38 deletions(-) diff --git a/specs/savings_account_spec.rb b/specs/savings_account_spec.rb index 3f4d1e4a..d0e4ed95 100644 --- a/specs/savings_account_spec.rb +++ b/specs/savings_account_spec.rb @@ -1,58 +1,53 @@ require 'minitest/autorun' require 'minitest/reporters' require 'minitest/skip_dsl' - -# TODO: uncomment the next line once you start wave 3 and add lib/savings_account.rb +require 'csv' # require_relative '../lib/savings_account' +require_relative '../lib/savings_account' -# Because a SavingsAccount is a kind -# of Account, and we've already tested a bunch of functionality -# 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 describe "#initialize" do it "Is a kind of Account" do + # Check that a SavingsAccount is in fact a kind of account account = Bank::SavingsAccount.new(12345, 100.0) account.must_be_kind_of Bank::Account end - it "Requires an initial balance of at least $10" do - # TODO: Your test code here! + it "Returns a ArgumentError if initial balance is less than 10" do + proc { balance < 10 }.must_raise ArgumentError end end - describe "#withdraw" do + xdescribe "#withdraw" do it "Applies a $2 fee each time" do # TODO: Your test code here! end - - it "Outputs a warning if the balance would go below $10" do - # TODO: Your test code here! - end - - it "Doesn't modify the balance if it would go below $10" do - # TODO: Your test code here! - end - - it "Doesn't modify the balance if the fee would put it below $10" do - # TODO: Your test code here! - end - end - - describe "#add_interest" do - it "Returns the interest calculated" do - # TODO: Your test code here! - end - - it "Updates the balance with calculated interest" do - # TODO: Your test code here! - end - - it "Requires a positive rate" do - # TODO: Your test code here! - end + # + # it "Outputs a warning if the balance would go below $10" do + # # TODO: Your test code here! + # end + # + # it "Doesn't modify the balance if it would go below $10" do + # # TODO: Your test code here! + # end + # + # it "Doesn't modify the balance if the fee would put it below $10" do + # # TODO: Your test code here! + # end + # end + # + # describe "#add_interest" do + # it "Returns the interest calculated" do + # # TODO: Your test code here! + # end + # + # it "Updates the balance with calculated interest" do + # # TODO: Your test code here! + # end + # + # it "Requires a positive rate" do + # # TODO: Your test code here! + # end end end From dfc120c81cc4ce0c2ef21e2447f0f41e8500ad4e Mon Sep 17 00:00:00 2001 From: Allison Northrop Date: Mon, 27 Feb 2017 08:35:40 -0800 Subject: [PATCH 6/6] completed bank account --- lib/account.rb | 2 +- lib/checking_account.rb | 53 +++++++++++++++++++++++- lib/savings_account.rb | 35 +++++++++++----- specs/checking_account_spec.rb | 12 +++--- specs/savings_account_spec.rb | 73 ++++++++++++++++++++-------------- 5 files changed, 128 insertions(+), 47 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index 8255082c..2aeb2e29 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -1,7 +1,7 @@ require 'csv' module Bank class Account - attr_reader :id, :balance, :open_date + attr_accessor :id, :balance, :open_date def initialize(id, balance, open_date = nil) #the equal to nil is so the other wave one tests pass raise ArgumentError.new("balance must be >= 0") if balance < 0 diff --git a/lib/checking_account.rb b/lib/checking_account.rb index 93cf5338..83f9d010 100644 --- a/lib/checking_account.rb +++ b/lib/checking_account.rb @@ -1,2 +1,53 @@ require 'csv' -require_relative '../lib/account' +require_relative 'account' + +module Bank + class CheckingAccount < Account + + def initialize(id, balance) + super(id, balance) + end + + def withdraw(amount) + if (@balance - amount) - 1 < 0 + puts "You've exceeded the balance" + return @balance + else + @balance = (@balance - amount) - 1 + return @balance + end + end + + def withdraw_using_check(amount) + check = 0 #need to double check this is right + while check < 3 do + if balance - amount < -10 + puts "You are not allowed an overdrft of more than $10." + else + @balance = @balance - amount + return balance + end + while check > 3 do + @balance = @balance - 2 + return @balance + end + end + end + + def reset_checks(checks) + if check > 3 + return check = 0 + end + end + end +end + + + + + +# #withdraw_using_check(amount): The input amount gets taken out of the account as a result of a check withdrawal. +# Returns the updated account balance. +# Allows the account to go into overdraft up to -$10 but not any lower +# The user is allowed three free check uses in one month, but any subsequent use adds a $2 transaction fee +# #reset_checks: Resets the number of checks used to zero diff --git a/lib/savings_account.rb b/lib/savings_account.rb index 0fa88483..4060018d 100644 --- a/lib/savings_account.rb +++ b/lib/savings_account.rb @@ -7,27 +7,42 @@ class SavingsAccount < Account def initialize(id, balance) super(id, balance) + raise ArgumentError.new("Balance must be at least $10") if balance < 10 end + # # The initial balance cannot be less than $10. If it is, this will raise an ArgumentError + # def starting_balance(balance) + # raise ArgumentError.new("Balance must be at least $10") if balance < 10 + # return balance + # end + + # Each withdrawal 'transaction' incurs a fee of $2 that is taken out of the balance. + def withdrawal(amount) + if @balance - amount - 2 < 10 + puts "Balance cannot go below %10" + return @balance + else + @balance = (@balance - amount) - 2 + return @balance + end + end - + def add_interest(rate) + interest = @balance * (rate/100) + @balance = @balance + interest + return interest + end end end -# SavingsAccount -# -# Create a SavingsAccount class which should inherit behavior from the Account class. It should include the following updated functionality: -# -# The initial balance cannot be less than $10. If it is, this will raise an ArgumentError -# Updated withdrawal functionality: -# Each withdrawal 'transaction' incurs a fee of $2 that is taken out of the balance. -# Does not allow the account to go below the $10 minimum balance - Will output a warning message and return the original un-modified balance + # It should include the following new method: # -# #add_interest(rate): Calculate the interest on the balance and add the interest to the balance. Return the interest that was calculated and added to the balance (not the updated balance). +# #add_interest(rate): Calculate the interest on the balance and add the interest +#to the balance. Return the interest that was calculated and added to the balance (not the updated balance). # Input rate is assumed to be a percentage (i.e. 0.25). # The formula for calculating interest is balance * rate/100 # Example: If the interest rate is 0.25 and the balance is $10,000, then the interest that is returned is $25 and the new balance becomes $10,025. diff --git a/specs/checking_account_spec.rb b/specs/checking_account_spec.rb index 44e37488..252a0d3b 100644 --- a/specs/checking_account_spec.rb +++ b/specs/checking_account_spec.rb @@ -24,17 +24,19 @@ describe "#withdraw" do it "Applies a $1 fee each time" do - # TODO: Your test code here! + @balane.must_equal (@balance - amount) - 1 end it "Doesn't modify the balance if the fee would put it negative" do - # TODO: Your test code here! + if (@balance - amount) - 1 < 0 + @balace.must_equal @balance + end end end describe "#withdraw_using_check" do it "Reduces the balance" do - # TODO: Your test code here! + @balance.must_equal @balance - amount end it "Returns the modified balance" do @@ -42,11 +44,11 @@ end it "Allows the balance to go down to -$10" do - # TODO: Your test code here! + @balance.must_be :>=, -10 end it "Outputs a warning if the account would go below -$10" do - # TODO: Your test code here! + end it "Doesn't modify the balance if the account would go below -$10" do diff --git a/specs/savings_account_spec.rb b/specs/savings_account_spec.rb index d0e4ed95..9edea8cb 100644 --- a/specs/savings_account_spec.rb +++ b/specs/savings_account_spec.rb @@ -4,9 +4,11 @@ require 'csv' # require_relative '../lib/savings_account' require_relative '../lib/savings_account' +# require_relative '..lib/account' describe "SavingsAccount" do describe "#initialize" do + it "Is a kind of Account" do # Check that a SavingsAccount is in fact a kind of account @@ -15,39 +17,50 @@ end it "Returns a ArgumentError if initial balance is less than 10" do - proc { balance < 10 }.must_raise ArgumentError + account = Bank::SavingsAccount.new(1213, 3) + proc { if @balance < 10 + end }.must_raise ArgumentError end end - xdescribe "#withdraw" do + describe "#withdraw" do it "Applies a $2 fee each time" do - # TODO: Your test code here! + amount.must_equal @balance - amount - 2 + end + # + it "Outputs a warning if the balance would go below $10" do + proc { + Bank::Account.new(1337, 5) + }.must_raise ArgumentError + end + + it "Doesn't modify the balance if it would go below $10" do + Bank::SavingsAccount.new(id, balance) + if @balance < 10 + return @balance + end + end + + it "Doesn't modify the balance if the fee would put it below $10" do + if @balance - amount -2 < 10 + return @balancest + end + end + # + describe "#add_interest" do + it "Returns the interest calculated" do + interest.must_equal @balance * (rate/100) + end + + it "Updates the balance with calculated interest" do + rate = @balance * (rate/100) + @balance.must_equal @balance + rate + end + + it "Requires a positive rate" do + rate = @balance * (rate/100) + rate.must_be :>, 0 + end end - # - # it "Outputs a warning if the balance would go below $10" do - # # TODO: Your test code here! - # end - # - # it "Doesn't modify the balance if it would go below $10" do - # # TODO: Your test code here! - # end - # - # it "Doesn't modify the balance if the fee would put it below $10" do - # # TODO: Your test code here! - # end - # end - # - # describe "#add_interest" do - # it "Returns the interest calculated" do - # # TODO: Your test code here! - # end - # - # it "Updates the balance with calculated interest" do - # # TODO: Your test code here! - # end - # - # it "Requires a positive rate" do - # # TODO: Your test code here! - # end end -end +end