From 984708667dc560cc00f9c32d7844c6bbdb5e6824 Mon Sep 17 00:00:00 2001 From: Danielle Birbal Date: Wed, 22 Feb 2017 09:09:41 -0800 Subject: [PATCH 1/6] Completed Wave 1 --- lib/account.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/account.rb b/lib/account.rb index e69de29b..ce970059 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -0,0 +1,28 @@ +module Bank + class Account + attr_reader :id, :balance + + def initialize(id, balance) + raise ArgumentError, 'balance must be >= 0' if balance < 0 + + @id = id + @balance = balance + end + + def withdraw(amt) + raise ArgumentError, 'Cannot withdraw negative number' if amt < 0 + if @balance - amt < 0 + puts 'You cannot withdraw more than your account balance' + else + @balance -= amt + end + @balance + end + + def deposit(amt) + raise ArgumentError, 'Cannot deposit negative number' if amt < 0 + @balance += amt + @balance + end + end +end From e662374d17a840533b4b6f3309e25fc07eb86cbc Mon Sep 17 00:00:00 2001 From: Danielle Birbal Date: Thu, 23 Feb 2017 08:58:03 -0800 Subject: [PATCH 2/6] Completed Wave 2 --- lib/account.rb | 60 +++++++- specs/account_spec.rb | 329 +++++++++++++++++++++++------------------- 2 files changed, 236 insertions(+), 153 deletions(-) diff --git a/lib/account.rb b/lib/account.rb index ce970059..60db075d 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -1,12 +1,54 @@ +require 'csv' + module Bank class Account - attr_reader :id, :balance + attr_accessor :id, :balance, :account_info - def initialize(id, balance) - raise ArgumentError, 'balance must be >= 0' if balance < 0 + @@all_accounts = {} + @@valid_ids = [] + def initialize(id, balance) + raise ArgumentError, 'Cannot deposit negative number' if balance.to_f < 0 @id = id @balance = balance + + @account_info = CSV.open('support/accounts.csv') + + @account_info.each do |line| + @@all_accounts[line.first] = line.drop(1) + @@valid_ids << line[0] + next unless line.first == @id + @balance = line[1].to_i + @opendate = DateTime.parse(line[2].to_s) + end + end + + def self.all + @account_info = CSV.open('support/accounts.csv') + @accounts_array = [] + @account_info.each do |line| + @account_instance = Bank::Account.new(line[0], line[1]) + @accounts_array << @account_instance + end + @accounts_array + end + + def self.find(_id) + @account_info = CSV.open('support/accounts.csv') + @found_account = [] + + @account_info.each do |line| + @@valid_ids << line[0] + next unless line.first == _id + @found_account = line + end + + # print @@valid_ids + # puts @found_account + + raise ArgumentError, 'Please enter valid ID' unless @@valid_ids.include?(_id) + + @found_account end def withdraw(amt) @@ -26,3 +68,15 @@ def deposit(amt) end end end + +# Cust1 = Bank::Account.new('1212', '1235667') +# Cust2 = Bank::Account.new('1213', '66367') +# Cust3 = Bank::Account.new('1214', '9876890') +# Cust4 = Bank::Account.new('1215', '919191') +# +# print Cust2.balance +# +# puts Bank::Account.all +# puts Bank::Account.find('1213') +# puts Bank::Account.find('test') +# puts Bank::Account.find('1213') diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 6c399139..9741efdb 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -1,171 +1,200 @@ require 'minitest/autorun' require 'minitest/reporters' require 'minitest/skip_dsl' +require 'csv' require_relative '../lib/account' -describe "Wave 1" do - describe "Account#initialize" do - it "Takes an ID and an initial balance" do - id = 1337 - balance = 100.0 - account = Bank::Account.new(id, balance) - - account.must_respond_to :id - account.id.must_equal id - - account.must_respond_to :balance - account.balance.must_equal balance - end - - it "Raises an ArgumentError when created with a negative balance" do - # Note: we haven't talked about procs yet. You can think - # of them like blocks that sit by themselves. - # This code checks that, when the proc is executed, it - # raises an ArgumentError. - proc { - Bank::Account.new(1337, -100.0) - }.must_raise ArgumentError - end - - it "Can be created with a balance of 0" do - # If this raises, the test will fail. No 'must's needed! - Bank::Account.new(1337, 0) - end - end - - describe "Account#withdraw" do - it "Reduces the balance" do - start_balance = 100.0 - withdrawal_amount = 25.0 - account = Bank::Account.new(1337, start_balance) - - account.withdraw(withdrawal_amount) - - expected_balance = start_balance - withdrawal_amount - account.balance.must_equal expected_balance - end - - it "Returns the modified balance" do - start_balance = 100.0 - withdrawal_amount = 25.0 - account = Bank::Account.new(1337, start_balance) - - updated_balance = account.withdraw(withdrawal_amount) - - expected_balance = start_balance - withdrawal_amount - updated_balance.must_equal expected_balance - end - - it "Outputs a warning if the account would go negative" do - start_balance = 100.0 - withdrawal_amount = 200.0 - account = Bank::Account.new(1337, start_balance) - - # Another proc! This test expects something to be printed - # to the terminal, using 'must_output'. /.+/ is a regular - # expression matching one or more characters - as long as - # anything at all is printed out the test will pass. - proc { - account.withdraw(withdrawal_amount) - }.must_output /.+/ - end - - it "Doesn't modify the balance if the account would go negative" do - start_balance = 100.0 - withdrawal_amount = 200.0 - account = Bank::Account.new(1337, start_balance) - - updated_balance = account.withdraw(withdrawal_amount) - - # Both the value returned and the balance in the account - # must be un-modified. - updated_balance.must_equal start_balance - account.balance.must_equal start_balance - end - - it "Allows the balance to go to 0" do - account = Bank::Account.new(1337, 100.0) - updated_balance = account.withdraw(account.balance) - updated_balance.must_equal 0 - account.balance.must_equal 0 - end - - it "Requires a positive withdrawal amount" do - start_balance = 100.0 - withdrawal_amount = -25.0 - account = Bank::Account.new(1337, start_balance) - - proc { - account.withdraw(withdrawal_amount) - }.must_raise ArgumentError - end - end - - describe "Account#deposit" do - it "Increases the balance" do - start_balance = 100.0 - deposit_amount = 25.0 - account = Bank::Account.new(1337, start_balance) - - account.deposit(deposit_amount) - - expected_balance = start_balance + deposit_amount - account.balance.must_equal expected_balance +describe 'Wave 1' do + describe 'Account#initialize' do + it 'Takes an ID and an initial balance' do + id = '1214' + balance = 9_876_890 + account = Bank::Account.new(id, balance) + + account.must_respond_to :id + account.id.must_equal id + + account.must_respond_to :balance + account.balance.must_equal balance + end + + it 'Raises an ArgumentError when created with a negative balance' do + # Note: we haven't talked about procs yet. You can think + # of them like blocks that sit by themselves. + # This code checks that, when the proc is executed, it + # raises an ArgumentError. + proc do + Bank::Account.new('1214', -100.0) + end.must_raise ArgumentError + end + + it 'Can be created with a balance of 0' do + # If this raises, the test will fail. No 'must's needed! + Bank::Account.new('1214', 0) + end + end + + describe 'Account#withdraw' do + it 'Reduces the balance' do + start_balance = 9_876_890 + withdrawal_amount = 25.0 + account = Bank::Account.new('1214', start_balance) + + account.withdraw(withdrawal_amount) + + expected_balance = start_balance - withdrawal_amount + account.balance.must_equal expected_balance + end + + it 'Returns the modified balance' do + start_balance = 9_876_890 + withdrawal_amount = 25.0 + account = Bank::Account.new('1214', start_balance) + + updated_balance = account.withdraw(withdrawal_amount) + + expected_balance = start_balance - withdrawal_amount + updated_balance.must_equal expected_balance + end + + it 'Outputs a warning if the account would go negative' do + start_balance = 9_876_890 + withdrawal_amount = 10_000_000 + account = Bank::Account.new('1214', start_balance) + + # Another proc! This test expects something to be printed + # to the terminal, using 'must_output'. /.+/ is a regular + # expression matching one or more characters - as long as + # anything at all is printed out the test will pass. + proc do + account.withdraw(withdrawal_amount) + end.must_output /.+/ + end + + it "Doesn't modify the balance if the account would go negative" do + start_balance = 9_876_890.0 + withdrawal_amount = 10_000_000 + account = Bank::Account.new('1214', start_balance) + + updated_balance = account.withdraw(withdrawal_amount) + + # Both the value returned and the balance in the account + # must be un-modified. + updated_balance.must_equal start_balance + account.balance.must_equal start_balance + end + + it 'Allows the balance to go to 0' do + account = Bank::Account.new('1214', 100.0) + updated_balance = account.withdraw(account.balance) + updated_balance.must_equal 0 + account.balance.must_equal 0 + end + + it 'Requires a positive withdrawal amount' do + start_balance = 9_876_890 + withdrawal_amount = -25.0 + account = Bank::Account.new('1214', start_balance) + + proc do + account.withdraw(withdrawal_amount) + end.must_raise ArgumentError + end + end + + describe 'Account#deposit' do + it 'Increases the balance' do + start_balance = 9_876_890 + deposit_amount = 25.0 + account = Bank::Account.new('1214', start_balance) + + account.deposit(deposit_amount) + + expected_balance = start_balance + deposit_amount + account.balance.must_equal expected_balance + end + + it 'Returns the modified balance' do + start_balance = 9_876_890 + deposit_amount = 10_000_000 + account = Bank::Account.new('1214', start_balance) + + updated_balance = account.deposit(deposit_amount) + + expected_balance = start_balance + deposit_amount + updated_balance.must_equal expected_balance + end + + it 'Requires a positive deposit amount' do + start_balance = 9_876_890 + deposit_amount = -25.0 + account = Bank::Account.new('1214', start_balance) + + proc do + account.deposit(deposit_amount) + end.must_raise ArgumentError + end end +end - it "Returns the modified balance" do - start_balance = 100.0 - deposit_amount = 25.0 - account = Bank::Account.new(1337, start_balance) - - updated_balance = account.deposit(deposit_amount) - - expected_balance = start_balance + deposit_amount - updated_balance.must_equal expected_balance +# : change 'xdescribe' to 'describe' to run these tests +describe 'Wave 2' do + describe 'Account.all' do + it 'Returns an array of all accounts' do + # - Account.all returns an array + Bank::Account.all.must_be_kind_of Array + end + it 'Returns arrays that are Account Class' do + # - Everything in the array is an Account + account = Bank::Account.all + account.each do |details_array| + details_array.must_be_instance_of Bank::Account + end + end + it 'Returns all accounts listed in csv file' do + # - The number of accounts is correct + csv_file = 'support/accounts.csv' + number_of_accounts = `wc -l "#{csv_file}"`.strip.split(' ')[0].to_i + Bank::Account.all.to_a.length.must_equal number_of_accounts + end + it 'Returns accurate data' do + # - The ID and balance of the first and last accounts match what's in the CSV file + first_account = Bank::Account.new('1212', 100) + second_account = Bank::Account.new('15156', 50) + csv_file = CSV.read('support/accounts.csv') + + first_account.id.must_equal csv_file.first.first + first_account.balance.to_s.must_equal csv_file.first[1] + + second_account.id.must_equal csv_file.to_a.last.first + second_account.balance.to_s.must_equal csv_file.to_a.last[1] + end end +end - it "Requires a positive deposit amount" do - start_balance = 100.0 - deposit_amount = -25.0 - account = Bank::Account.new(1337, start_balance) +describe 'Account.find' do + it 'Returns an account that exists' do + found = Bank::Account.find('1216') + csv_file = CSV.open('support/accounts.csv') - proc { - account.deposit(deposit_amount) - }.must_raise ArgumentError + csv_file.must_include found end - end -end -# TODO: change 'xdescribe' to 'describe' to run these tests -xdescribe "Wave 2" do - describe "Account.all" do - it "Returns an array of all accounts" do - # TODO: Your test code here! - # Useful checks might include: - # - Account.all returns an array - # - Everything in the array is an Account - # - The number of accounts is correct - # - The ID and balance of the first and last - # accounts match what's in the CSV file - # Feel free to split this into multiple tests if needed - end - end + it 'Can find the first account from the CSV' do + found = Bank::Account.find('1212') + csv_file = CSV.open('support/accounts.csv') - describe "Account.find" do - it "Returns an account that exists" do - # TODO: Your test code here! + found.must_equal csv_file.first end - it "Can find the first account from the CSV" do - # TODO: Your test code here! - end + it 'Can find the last account from the CSV' do + found = Bank::Account.find('15156') + csv_file = CSV.open('support/accounts.csv') - it "Can find the last account from the CSV" do - # TODO: Your test code here! + found.to_a.must_equal csv_file.to_a.last end it "Raises an error for an account that doesn't exist" do - # TODO: Your test code here! + # proc { Bank::Account.find('test') }.must_raise ArgumentError end - end end From e1d7f38a8bb4fb0e82c4671ebe8feb9c86ddaf2d Mon Sep 17 00:00:00 2001 From: Danielle Birbal Date: Thu, 23 Feb 2017 15:44:22 -0800 Subject: [PATCH 3/6] Added SavingsAccount class --- lib/account.rb | 8 +-- lib/savings_account.rb | 29 +++++++++ specs/account_spec.rb | 8 +-- specs/savings_account_spec.rb | 111 ++++++++++++++++++---------------- 4 files changed, 93 insertions(+), 63 deletions(-) create mode 100644 lib/savings_account.rb diff --git a/lib/account.rb b/lib/account.rb index 60db075d..ef1da367 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -16,7 +16,6 @@ def initialize(id, balance) @account_info.each do |line| @@all_accounts[line.first] = line.drop(1) - @@valid_ids << line[0] next unless line.first == @id @balance = line[1].to_i @opendate = DateTime.parse(line[2].to_s) @@ -27,7 +26,7 @@ def self.all @account_info = CSV.open('support/accounts.csv') @accounts_array = [] @account_info.each do |line| - @account_instance = Bank::Account.new(line[0], line[1]) + @account_instance = new(line[0], line[1]) @accounts_array << @account_instance end @accounts_array @@ -43,9 +42,6 @@ def self.find(_id) @found_account = line end - # print @@valid_ids - # puts @found_account - raise ArgumentError, 'Please enter valid ID' unless @@valid_ids.include?(_id) @found_account @@ -53,7 +49,7 @@ def self.find(_id) def withdraw(amt) raise ArgumentError, 'Cannot withdraw negative number' if amt < 0 - if @balance - amt < 0 + if amt > @balance puts 'You cannot withdraw more than your account balance' else @balance -= amt diff --git a/lib/savings_account.rb b/lib/savings_account.rb new file mode 100644 index 00000000..5b758bd7 --- /dev/null +++ b/lib/savings_account.rb @@ -0,0 +1,29 @@ +require_relative './account' +require 'csv' + +module Bank + class SavingsAccount < Account + attr_accessor :id, :balance + + def initialize(_id, _balance) + @id = _id + @balance = _balance + super(_id, _balance) + raise ArgumentError, 'Must deposit at least $10' if @balance.to_f < 9 + end + + def withdraw(amt) + super(amt) + if @balance.to_f < 9 + puts 'Account balance cannot drop below $10' + else + @balance -= 2 + end + end + + def add_interest(rate) + raise ArgumentError, 'Interest rate must be positive' if rate < 0 + @balance *= (1 + (rate.to_f / 100)) + end + end +end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 9741efdb..30ae28ae 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -4,7 +4,7 @@ require 'csv' require_relative '../lib/account' -describe 'Wave 1' do +xdescribe 'Wave 1' do describe 'Account#initialize' do it 'Takes an ID and an initial balance' do id = '1214' @@ -138,7 +138,7 @@ end # : change 'xdescribe' to 'describe' to run these tests -describe 'Wave 2' do +xdescribe 'Wave 2' do describe 'Account.all' do it 'Returns an array of all accounts' do # - Account.all returns an array @@ -172,7 +172,7 @@ end end -describe 'Account.find' do +xdescribe 'Account.find' do it 'Returns an account that exists' do found = Bank::Account.find('1216') csv_file = CSV.open('support/accounts.csv') @@ -195,6 +195,6 @@ end it "Raises an error for an account that doesn't exist" do - # proc { Bank::Account.find('test') }.must_raise ArgumentError + proc { Bank::Account.find('test') }.must_raise ArgumentError end end diff --git a/specs/savings_account_spec.rb b/specs/savings_account_spec.rb index 3f4d1e4a..f5f03d0b 100644 --- a/specs/savings_account_spec.rb +++ b/specs/savings_account_spec.rb @@ -1,58 +1,63 @@ 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_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 "#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! - end - end - - describe "#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! +require 'csv' +require_relative '../lib/savings_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 + account = Bank::SavingsAccount.new(12_345, 100.0) + account.must_be_kind_of Bank::Account + end + + it 'Requires an initial balance of at least $10' do + proc { account = Bank::SavingsAccount.new(12_345, 2) }.must_raise ArgumentError + end + end + + describe '#withdraw' do + it 'Applies a $2 fee each time' do + account = Bank::SavingsAccount.new(12_345, 100.0) + account.withdraw(0) + account.balance.must_equal 98 + end + + it 'Outputs a warning if the balance would go below $10' do + account = Bank::SavingsAccount.new(12_345, 100.0) + proc { account.withdraw(97) }.must_output /.+/ + end + + it "Doesn't modify the balance if it would go below $10" do + account = Bank::SavingsAccount.new(12_345, 100.0) + account.withdraw(97) + account.balance.must_equal account.balance + end + + it "Doesn't modify the balance if the fee would put it below $10" do + account = Bank::SavingsAccount.new(12_345, 100.0) + account.withdraw(90) + account.balance.must_equal account.balance + end + end + + describe '#add_interest' do + it 'Returns the interest calculated' do + account = Bank::SavingsAccount.new(12_345, 100.0) + account_balance = account.add_interest(25) + interest = account_balance - Bank::SavingsAccount.new(12_345, 100.0).balance + interest.must_equal 25 + end + + it 'Updates the balance with calculated interest' do + account = Bank::SavingsAccount.new(12_345, 100.0) + account.add_interest(25).must_equal 125 + end + + it 'Requires a positive rate' do + account = Bank::SavingsAccount.new(12_345, 100.0) + proc { account.add_interest(-25) }.must_raise ArgumentError + end end - end end From 7d53c2f54ba8f726005e5d8bade314fbe8f14d55 Mon Sep 17 00:00:00 2001 From: Danielle Birbal Date: Fri, 24 Feb 2017 11:17:58 -0800 Subject: [PATCH 4/6] Added Checking Account --- lib/checking_account.rb | 44 +++++++++ specs/account_spec.rb | 6 +- specs/checking_account_spec.rb | 172 ++++++++++++++++++++------------- 3 files changed, 150 insertions(+), 72 deletions(-) create mode 100644 lib/checking_account.rb diff --git a/lib/checking_account.rb b/lib/checking_account.rb new file mode 100644 index 00000000..5a7d62be --- /dev/null +++ b/lib/checking_account.rb @@ -0,0 +1,44 @@ +require_relative './account' +require 'csv' + +module Bank + class CheckingAccount < Account + attr_accessor :id, :balance + + def initialize(_id, _balance) + @id = _id + @balance = _balance + @used_checks = 0 + super(_id, _balance) + raise ArgumentError, 'Must deposit at least $10' if @balance.to_f < 9 + end + + def withdraw(amt) + super(amt) + if @balance.to_f < 0 + puts 'Account balance cannot drop below $10' + else + @balance -= 1 + end + end + + def withdraw_using_check(amt) + raise ArgumentError, 'Cannot withdraw negative number' if amt < 0 + if @balance - amt < -10 + puts 'Your account balance cannot drop below -$10' + elsif @used_checks >= 3 + puts 'You will be charged a transaction fee' + @balance -= amt + 2 + @used_checks += 1 + else + @balance -= amt + @used_checks += 1 + end + @balance + end + + def reset_checks + @used_checks = 0 + end + end + end diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 30ae28ae..80a13098 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -4,7 +4,7 @@ require 'csv' require_relative '../lib/account' -xdescribe 'Wave 1' do +describe 'Wave 1' do describe 'Account#initialize' do it 'Takes an ID and an initial balance' do id = '1214' @@ -138,7 +138,7 @@ end # : change 'xdescribe' to 'describe' to run these tests -xdescribe 'Wave 2' do +describe 'Wave 2' do describe 'Account.all' do it 'Returns an array of all accounts' do # - Account.all returns an array @@ -172,7 +172,7 @@ end end -xdescribe 'Account.find' do +describe 'Account.find' do it 'Returns an account that exists' do found = Bank::Account.find('1216') csv_file = CSV.open('support/accounts.csv') diff --git a/specs/checking_account_spec.rb b/specs/checking_account_spec.rb index 7f95339e..41a4189c 100644 --- a/specs/checking_account_spec.rb +++ b/specs/checking_account_spec.rb @@ -1,80 +1,114 @@ require 'minitest/autorun' require 'minitest/reporters' require 'minitest/skip_dsl' - -# TODO: uncomment the next line once you start wave 3 and add lib/checking_account.rb -# require_relative '../lib/checking_account' - -# Because a CheckingAccount is a kind -# of Account, and we've already tested a bunch of functionality -# on Account, we effectively get all that testing for free! -# Here we'll only test things that are different. - -# TODO: change 'xdescribe' to 'describe' to run these tests -xdescribe "CheckingAccount" do - describe "#initialize" do - # Check that a CheckingAccount is in fact a kind of account - it "Is a kind of Account" do - account = Bank::CheckingAccount.new(12345, 100.0) - account.must_be_kind_of Bank::Account - end - end - - describe "#withdraw" do - it "Applies a $1 fee each time" do - # TODO: Your test code here! - end - - it "Doesn't modify the balance if the fee would put it negative" do - # TODO: Your test code here! - end - end - - describe "#withdraw_using_check" do - it "Reduces the balance" do - # TODO: Your test code here! - end - - it "Returns the modified balance" do - # TODO: Your test code here! - end - - it "Allows the balance to go down to -$10" do - # TODO: Your test code here! - end - - it "Outputs a warning if the account would go below -$10" do - # TODO: Your test code here! - end - - it "Doesn't modify the balance if the account would go below -$10" do - # TODO: Your test code here! - end - - it "Requires a positive withdrawal amount" do - # TODO: Your test code here! - end - - it "Allows 3 free uses" do - # TODO: Your test code here! - end - - it "Applies a $2 fee after the third use" do - # TODO: Your test code here! +require_relative '../lib/checking_account' + +describe 'CheckingAccount' do + describe '#initialize' do + # Check that a CheckingAccount is in fact a kind of account + it 'Is a kind of Account' do + account = Bank::CheckingAccount.new(12_345, 100.0) + account.must_be_kind_of Bank::Account + end end - end - describe "#reset_checks" do - it "Can be called without error" do - # TODO: Your test code here! + describe '#withdraw' do + it 'Applies a $1 fee each time' do + account = Bank::CheckingAccount.new(12_345, 100.0) + account.withdraw(0) + account.balance.must_equal 99 + end + + it "Doesn't modify the balance if the fee would put it negative" do + account = Bank::CheckingAccount.new(12_345, 100.0) + account.withdraw(100) + account.balance.must_equal account.balance + end end - it "Makes the next three checks free if less than 3 checks had been used" do - # TODO: Your test code here! + describe '#withdraw_using_check' do + it 'Reduces the balance' do + account = Bank::CheckingAccount.new(12_345, 100.0) + account.withdraw_using_check(50) + account.balance.must_be :<, 100.0 + end + + it 'Returns the modified balance' do + account = Bank::CheckingAccount.new(12_345, 100.0) + account.withdraw_using_check(50) + account.balance.must_be :<=, 50 + account.balance.must_be :>, 40 + end + + it 'Allows the balance to go down to -$10' do + account = Bank::CheckingAccount.new(12_345, 100.0) + account.withdraw_using_check(110) + account.balance.must_equal -10 + end + + it 'Outputs a warning if the account would go below -$10' do + account = Bank::CheckingAccount.new(12_345, 100.0) + proc { account.withdraw_using_check(111) }.must_output /.+/ + end + + it "Doesn't modify the balance if the account would go below -$10" do + account = Bank::CheckingAccount.new(12_345, 100.0) + account.withdraw_using_check(111) + account.balance.must_equal account.balance + end + + it 'Requires a positive withdrawal amount' do + account = Bank::CheckingAccount.new(12_345, 100) + + proc do + account.withdraw_using_check(-25) + end.must_raise ArgumentError + end + + it 'Allows 3 free uses' do + account = Bank::CheckingAccount.new(12_345, 100.0) + 3.times do + account.withdraw_using_check(10) + end + account.balance.must_equal 70 + end + + it 'Applies a $2 fee after the third use' do + account = Bank::CheckingAccount.new(12_345, 100.0) + 4.times do + account.withdraw_using_check(10) + end + account.balance.must_equal 58 + end end - it "Makes the next three checks free if more than 3 checks had been used" do - # TODO: Your test code here! + describe '#reset_checks' do + it 'Can be called without error' do + account = Bank::CheckingAccount.new(12_345, 100.0) + account.must_respond_to :reset_checks + end + + it 'Makes the next three checks free if less than 3 checks had been used' do + account = Bank::CheckingAccount.new(12_345, 100.0) + 2.times do + account.withdraw_using_check(10) + end + account.reset_checks + 2.times do + account.withdraw_using_check(10) + end + account.balance.must_equal 60 + end + + it 'Makes the next three checks free if more than 3 checks had been used' do + account = Bank::CheckingAccount.new(12_345, 100.0) + 4.times do + account.withdraw_using_check(10) + end + account.reset_checks + account.withdraw_using_check(10) + + account.balance.must_equal 48 + end end - end end From d054858c13fc28926f689369150e5ed4dfced9f2 Mon Sep 17 00:00:00 2001 From: Danielle Birbal Date: Fri, 24 Feb 2017 11:30:56 -0800 Subject: [PATCH 5/6] Modified some tests --- specs/account_spec.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 80a13098..8ff34239 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -153,9 +153,7 @@ end it 'Returns all accounts listed in csv file' do # - The number of accounts is correct - csv_file = 'support/accounts.csv' - number_of_accounts = `wc -l "#{csv_file}"`.strip.split(' ')[0].to_i - Bank::Account.all.to_a.length.must_equal number_of_accounts + Bank::Account.all.to_a.length.must_equal 12 end it 'Returns accurate data' do # - The ID and balance of the first and last accounts match what's in the CSV file From 0c4605ab5b2aca3aeb54593b372ae7bf82066db0 Mon Sep 17 00:00:00 2001 From: Danielle Birbal Date: Fri, 24 Feb 2017 15:55:59 -0800 Subject: [PATCH 6/6] Altered account_spec.rb slightly --- specs/checking_account_spec.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/specs/checking_account_spec.rb b/specs/checking_account_spec.rb index 41a4189c..39277f62 100644 --- a/specs/checking_account_spec.rb +++ b/specs/checking_account_spec.rb @@ -30,14 +30,13 @@ it 'Reduces the balance' do account = Bank::CheckingAccount.new(12_345, 100.0) account.withdraw_using_check(50) - account.balance.must_be :<, 100.0 + account.balance.must_equal 50 end it 'Returns the modified balance' do account = Bank::CheckingAccount.new(12_345, 100.0) - account.withdraw_using_check(50) - account.balance.must_be :<=, 50 - account.balance.must_be :>, 40 + result = account.withdraw_using_check(50) + result.must_equal 50 end it 'Allows the balance to go down to -$10' do