diff --git a/lib/account.rb b/lib/account.rb index e69de29b..2aeb2e29 100644 --- a/lib/account.rb +++ b/lib/account.rb @@ -0,0 +1,60 @@ +require 'csv' +module Bank + class Account + 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 + + @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 + 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 + +# account_1 = Bank::Account.new(1212, 1235667, "1999-03-27 11:30:09 -0800") +# Bank::Account.all +Bank::Account.find(1212) diff --git a/lib/checking_account.rb b/lib/checking_account.rb new file mode 100644 index 00000000..83f9d010 --- /dev/null +++ b/lib/checking_account.rb @@ -0,0 +1,53 @@ +require 'csv' +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 new file mode 100644 index 00000000..4060018d --- /dev/null +++ b/lib/savings_account.rb @@ -0,0 +1,48 @@ +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) + 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 + + + + + +# 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. 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 diff --git a/specs/checking_account_spec.rb b/specs/checking_account_spec.rb index 7f95339e..252a0d3b 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' @@ -22,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 @@ -40,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 3f4d1e4a..9edea8cb 100644 --- a/specs/savings_account_spec.rb +++ b/specs/savings_account_spec.rb @@ -1,58 +1,66 @@ 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' +# require_relative '..lib/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 + account = Bank::SavingsAccount.new(1213, 3) + proc { if @balance < 10 + end }.must_raise ArgumentError end end 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 - # TODO: Your test code here! + proc { + Bank::Account.new(1337, 5) + }.must_raise ArgumentError end it "Doesn't modify the balance if it would go below $10" do - # TODO: Your test code here! + 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 - # TODO: Your test code here! - end - end - - describe "#add_interest" do - it "Returns the interest calculated" do - # TODO: Your test code here! + 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 - # TODO: Your test code here! - 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 - # TODO: Your test code here! + it "Requires a positive rate" do + rate = @balance * (rate/100) + rate.must_be :>, 0 + end end end -end +end