diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..55f61042 Binary files /dev/null and b/.DS_Store differ diff --git a/lib/account.rb b/lib/account.rb index e69de29b..5ca8c0cf 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -0,0 +1,91 @@ +require 'csv' +require 'pry' + + + +module Bank + + class Account + + attr_accessor :balance + attr_accessor :id, :opendatetime#, :accounts + + @@accounts = nil + def initialize(account_hash) + + @id = account_hash[:id].to_i + @balance = account_hash[:balance].to_i + @opendatetime = account_hash[:opendatetime] + + raise ArgumentError.new "Account balance should not start with a negative number" if @balance < 0 + @balance = @balance + + end #end of initialize + + + def self.all + + if @@accounts == nil + + @@accounts = [] + + CSV.read("support/accounts.csv").each do |row| + account = { + id: row[0], + balance: row[1], + opendatetime: row[2] + } + + @@accounts << Bank::Account.new(account) + + end + end + return @@accounts + end #end self.all + + + # def self.accounts + # @@accounts #= @@accounts + # end + + + def self.find(id) + + @@accounts.find do |account| + + if account.id == id + return account + end + end + + raise ArgumentError.new "#{id} returned no results" + end #end self.find + + + def withdraw(amount) + + if amount < 0 + raise ArgumentError.new "Withdrawal amount cannot be negative number" + else + if @balance < amount + print "Your account is going to be overdrawn" + @balance = @balance + elsif @balance >= amount + return @balance -= amount + end + end + end #end of withdraw method + + def deposit(deposit_amount) + + if deposit_amount < 0 + raise ArgumentError.new "Deposit amount cannot be negative number" + else + @balance += deposit_amount + end + end #end of deposit method + + + end #end of class + +end #end of module diff --git a/lib/checking.rb b/lib/checking.rb new file mode 100644 index 00000000..d3afb61d --- /dev/null +++ b/lib/checking.rb @@ -0,0 +1,61 @@ + +require 'csv' +require 'pry' +require_relative 'account.rb' + + +module Bank + class CheckingAccount < Account + + attr_accessor :checks + + def initialize(account) + super + @checks = 3 + end + + + def withdraw(amount) + + if @balance - (amount + 1) < 1 + print "Insufficient Funds" + return @balnace = @balance + end + super + return @balance -= 1 + + end + + + def check_withdraw(amount) + + if amount < 0 + print "Cannot enter negative amount" + return @balance + end + + check_fee = 0 + + if @checks < 1 + check_fee = 2 + end + + if @balance - (amount + check_fee) < (-10) + print "Insufficient Funds" + @balnace = @balance + else + @balance = @balance - (amount + check_fee) + @checks -= 1 + return @balance + end + + end + + + def reset_checks + @checks = 3 + end + + + end +end diff --git a/lib/savings.rb b/lib/savings.rb new file mode 100644 index 00000000..20de3a95 --- /dev/null +++ b/lib/savings.rb @@ -0,0 +1,46 @@ +require 'csv' +require 'pry' +require_relative 'account.rb' + + +module Bank + class SavingsAccount < Account + + def initialize(account) + + raise ArgumentError.new "Savings Accounts must have an Initial Balance of $10" if account[:balance] < 10 + super + + end + + def withdraw(amount) + + if (@balance - (amount + 2) ) < 10 + puts "Insufficient Funds" + return @balance + end + super + return @balance - 2 + + end + + + def add_interest(rate) + + if rate < 0 + print "We do not accept negative rates" + return @balance + else + interest = @balance * (rate/100) + @balance += interest + return interest + end + + end + + + end +end + +new_account = Bank::SavingsAccount.new({balance: 10}) +puts new_account.balance diff --git a/specs/account_spec.rb b/specs/account_spec.rb index 6c399139..4ee44fe7 100644 --- a/specs/account_spec.rb +++ b/specs/account_spec.rb @@ -1,35 +1,37 @@ -require 'minitest/autorun' -require 'minitest/reporters' -require 'minitest/skip_dsl' +require_relative "spec_helper" 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) + it "Takes an ID and an initial balance" do #this is one test case with 4 assertions + + account = Bank::Account.new({id: 1337, balance: 100.00}) account.must_respond_to :id - account.id.must_equal id + account.id.must_equal 1337 account.must_respond_to :balance - account.balance.must_equal balance + account.balance.must_equal 100.00 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) + Bank::Account.new({id: 1337, balance: -100.0}) }.must_raise ArgumentError end it "Can be created with a balance of 0" do + # If this raises, the test will fail. No 'must's needed! - Bank::Account.new(1337, 0) + Bank::Account.new({id: 1337, balance: 0}) end end @@ -37,7 +39,7 @@ it "Reduces the balance" do start_balance = 100.0 withdrawal_amount = 25.0 - account = Bank::Account.new(1337, start_balance) + account = Bank::Account.new({id: 1337, balance: start_balance}) account.withdraw(withdrawal_amount) @@ -46,9 +48,10 @@ end it "Returns the modified balance" do + start_balance = 100.0 withdrawal_amount = 25.0 - account = Bank::Account.new(1337, start_balance) + account = Bank::Account.new({id: 1337, balance: start_balance}) updated_balance = account.withdraw(withdrawal_amount) @@ -57,9 +60,10 @@ 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) + account = Bank::Account.new({id: 1337, balance: start_balance}) # Another proc! This test expects something to be printed # to the terminal, using 'must_output'. /.+/ is a regular @@ -67,14 +71,15 @@ # 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 + start_balance = 100.0 withdrawal_amount = 200.0 - account = Bank::Account.new(1337, start_balance) - + account = Bank::Account.new({id: 1337, balance: start_balance}) updated_balance = account.withdraw(withdrawal_amount) # Both the value returned and the balance in the account @@ -84,16 +89,18 @@ end it "Allows the balance to go to 0" do - account = Bank::Account.new(1337, 100.0) + + account = Bank::Account.new({id: 1337, balance: 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) + account = Bank::Account.new({id: 1337, balance: start_balance}) proc { account.withdraw(withdrawal_amount) @@ -103,9 +110,10 @@ 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 = Bank::Account.new({id: 1337, balance: start_balance}) account.deposit(deposit_amount) @@ -114,9 +122,10 @@ end it "Returns the modified balance" do + start_balance = 100.0 deposit_amount = 25.0 - account = Bank::Account.new(1337, start_balance) + account = Bank::Account.new({id: 1337, balance: start_balance}) updated_balance = account.deposit(deposit_amount) @@ -125,9 +134,10 @@ end it "Requires a positive deposit amount" do + start_balance = 100.0 deposit_amount = -25.0 - account = Bank::Account.new(1337, start_balance) + account = Bank::Account.new({id: 1337, balance: start_balance}) proc { account.deposit(deposit_amount) @@ -137,35 +147,80 @@ end # TODO: 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 - # 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 + + before do + @accounts = Bank::Account.all + end + + it "Account.all returns an array" do + @accounts.class.must_equal Array + end + + # it "Everything in the array is an Account" do + # + # @accounts.each do |account| + # account.must_be_instance_of Bank::Account + # + # #@accounts.must_be_instance_of Account + # #@accounts.new.must_be_instance_of Account.class + # end + # end + + it "The number of accounts is correct" do + #Bank::Account.all.length.must_equal 12 + @accounts.length.must_equal CSV.read("support/accounts.csv").count + end + + it "the ID and balance of the first and last accounts match what's in the CSV file" do + @accounts[0].id.must_equal 1212 + @accounts[0].balance.must_equal 1235667 + @accounts[11].id.must_equal 15156 + @accounts[11].balance.must_equal 4356772 + # Feel free to split this into multiple tests if needed end end describe "Account.find" do + + before do + @accounts = Bank::Account.all + end + it "Returns an account that exists" do - # TODO: Your test code here! + + account = Bank::Account.find(15155) + + account.must_be_instance_of Bank::Account + account.id.must_equal 15155 + account.balance.must_equal 999999 + account.opendatetime.must_equal "1990-06-10 13:13:13 -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 1235667 + account.opendatetime.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 4356772 + account.opendatetime.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(15115) }.must_raise ArgumentError end end end diff --git a/specs/checking_account_spec.rb b/specs/checking_account_spec.rb index 7f95339e..7118c7da 100644 --- a/specs/checking_account_spec.rb +++ b/specs/checking_account_spec.rb @@ -1,9 +1,11 @@ -require 'minitest/autorun' -require 'minitest/reporters' +require_relative "spec_helper" require 'minitest/skip_dsl' + + + # TODO: uncomment the next line once you start wave 3 and add lib/checking_account.rb -# require_relative '../lib/checking_account' +require_relative '../lib/checking' # Because a CheckingAccount is a kind # of Account, and we've already tested a bunch of functionality @@ -11,70 +13,111 @@ # Here we'll only test things that are different. # TODO: change 'xdescribe' to 'describe' to run these tests -xdescribe "CheckingAccount" do +describe "CheckingAccount" do + before do + @account = Bank::CheckingAccount.new({id: 12345, balance: 100.0}) + end + describe "#initialize" do # Check that a CheckingAccount is in fact a kind of account it "Is a kind of Account" do - account = Bank::CheckingAccount.new(12345, 100.0) - account.must_be_kind_of Bank::Account + + @account.must_be_kind_of Bank::Account end end describe "#withdraw" do it "Applies a $1 fee each time" do - # TODO: Your test code here! + @account.withdraw(20).must_equal 79 end it "Doesn't modify the balance if the fee would put it negative" do - # TODO: Your test code here! + @account.withdraw(110).must_equal 100 end end - describe "#withdraw_using_check" do + describe "#check_withdraw" do + before do + @account = Bank::CheckingAccount.new({balance: 100}) + end + it "Reduces the balance" do - # TODO: Your test code here! + @account.check_withdraw(20).must_be :<, 100 end it "Returns the modified balance" do - # TODO: Your test code here! + @account.check_withdraw(50).must_equal 50 end it "Allows the balance to go down to -$10" do - # TODO: Your test code here! + @account.check_withdraw(110).must_equal (-10) end it "Outputs a warning if the account would go below -$10" do - # TODO: Your test code here! + proc { @account.check_withdraw(200) }.must_output(/.+/) end it "Doesn't modify the balance if the account would go below -$10" do - # TODO: Your test code here! + @account.check_withdraw(200).must_equal 100 end it "Requires a positive withdrawal amount" do - # TODO: Your test code here! + @account.check_withdraw(-100).must_equal 100 end it "Allows 3 free uses" do - # TODO: Your test code here! + @account.check_withdraw(20).must_equal 80 + @account.check_withdraw(20).must_equal 60 + @account.check_withdraw(20).must_equal 40 + @account.check_withdraw(20).must_equal 18 + end it "Applies a $2 fee after the third use" do - # TODO: Your test code here! + @account.check_withdraw(20).must_equal 80 + @account.check_withdraw(20).must_equal 60 + @account.check_withdraw(20).must_equal 40 + @account.check_withdraw(20).must_equal 18 + @account.check_withdraw(20).must_equal (-4) end end describe "#reset_checks" do + + before do + @account = Bank::CheckingAccount.new({balance: 100}) + end + it "Can be called without error" do - # TODO: Your test code here! + @account.reset_checks.must_equal 3 end it "Makes the next three checks free if less than 3 checks had been used" do - # TODO: Your test code here! + @account.check_withdraw(20).must_equal 80 + @account.check_withdraw(20).must_equal 60 + @account.checks.must_equal (1) + @account.reset_checks + @account.checks.must_equal 3 + @account.check_withdraw(20).must_equal 40 + @account.check_withdraw(20).must_equal 20 + @account.check_withdraw(20).must_equal 0 + @account.check_withdraw(8).must_equal (-10) end it "Makes the next three checks free if more than 3 checks had been used" do - # TODO: Your test code here! + + @account.check_withdraw(20).must_equal 80 + @account.check_withdraw(20).must_equal 60 + @account.check_withdraw(20).must_equal 40 + @account.check_withdraw(20).must_equal 18 + @account.checks.must_equal (-1) + @account.reset_checks + @account.checks.must_equal 3 + @account.deposit(102) + @account.check_withdraw(20).must_equal 100 + @account.check_withdraw(20).must_equal 80 + @account.check_withdraw(20).must_equal 60 + @account.check_withdraw(20).must_equal 38 end end end diff --git a/specs/savings_account_spec.rb b/specs/savings_account_spec.rb index 3f4d1e4a..86013261 100644 --- a/specs/savings_account_spec.rb +++ b/specs/savings_account_spec.rb @@ -1,9 +1,5 @@ -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' +require_relative "spec_helper" +require_relative '../lib/savings' # Because a SavingsAccount is a kind # of Account, and we've already tested a bunch of functionality @@ -11,48 +7,61 @@ # 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 = Bank::SavingsAccount.new({id: 12345, balance: 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! + proc { Bank::SavingsAccount.new({id: 555555, balance: 5}) }.must_raise ArgumentError end end describe "#withdraw" do + + before do + @account = Bank::SavingsAccount.new({balance: 100}) + end + it "Applies a $2 fee each time" do - # TODO: Your test code here! + + @account.withdraw(20).must_equal 78 end it "Outputs a warning if the balance would go below $10" do - # TODO: Your test code here! + + proc { @account.withdraw(110) }.must_output(/.+/) end it "Doesn't modify the balance if it would go below $10" do - # TODO: Your test code here! + @account.withdraw(200).must_equal 100 end it "Doesn't modify the balance if the fee would put it below $10" do - # TODO: Your test code here! + @account.withdraw(100).must_equal 100 end end describe "#add_interest" do + + before do + @account = Bank::SavingsAccount.new({balance: 10000}) + end + it "Returns the interest calculated" do - # TODO: Your test code here! + @account.add_interest(0.25).must_equal 25 end it "Updates the balance with calculated interest" do - # TODO: Your test code here! + @account.add_interest(0.25) + @account.balance.must_equal 10025 end it "Requires a positive rate" do - # TODO: Your test code here! + @account.add_interest(-1).must_equal 10000 end end end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 00000000..4e4a2ba8 --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,10 @@ +require 'simplecov' +SimpleCov.start + + +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/skip_dsl' +require_relative '../lib/account' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new