Skip to content

Instantly share code, notes, and snippets.

@waldothedeveloper
Last active July 17, 2017 19:12

Revisions

  1. Waldo Lavaut revised this gist Jul 17, 2017. No changes.
  2. Waldo Lavaut created this gist Jul 16, 2017.
    26 changes: 26 additions & 0 deletions bank_account.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    class BankAccount
    attr_reader :balance
    @@minimum_balance = 200

    def initialize(opening_balance, account_holder)
    raise ArgumentError if opening_balance < @@minimum_balance
    @balance = opening_balance
    end

    def deposit amount
    @balance += amount
    end

    def withdraw amount
    @balance -= amount
    end

    def transfer amount, account
    withdraw amount
    account.deposit amount
    end

    def self.minimum_balance= amount
    @@minimum_balance = amount
    end
    end
    79 changes: 79 additions & 0 deletions bank_account_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    require './bank_account.rb'

    describe BankAccount do
    context "has a balance" do
    let(:account) do
    account = BankAccount.new(500, "Sarah")
    end

    it "is created with an opening balance and the name of the client" do
    expect(account).to be_a(BankAccount)
    end

    it "can report it's balance" do
    expect(account.balance).to eq(500)
    end
    end

    context "making a deposit" do
    let(:account) do
    account = BankAccount.new(500, "Sarah")
    account.deposit(500)
    account
    end

    it "balance is increased" do
    expect(account.balance).to eq(1000)
    end
    end

    context "making a withdrawal" do
    let(:account) do
    account = BankAccount.new(500, "Sarah")
    account.withdraw(200)
    account
    end

    it "balance is decreased" do
    expect(account.balance).to eq(300)
    end
    end

    context "transfering funds" do
    let(:account) do
    account = BankAccount.new(500, "Sarah")
    end

    let(:other_account) do
    other_account = BankAccount.new(1000, "Todd")
    end

    before :each do
    account.transfer(200, other_account)
    end

    it "account balance is decreased" do
    expect(account.balance).to eq(300)
    end

    it "other account balance is increased" do
    expect(other_account.balance).to eq(1200)
    end
    end

    context "minimum balance" do
    it "raises an error if opening balance is too low" do
    expect{ BankAccount.new(199, "Terry") }.to raise_error(ArgumentError)
    end

    it "does NOT raise an error if opening balance is over minimum balance" do
    expect{ BankAccount.new(201, "Terry") }.not_to raise_error
    end

    it "allows the bank owner to change the minimum balance" do
    BankAccount.minimum_balance = 100
    expect(BankAccount.minimum_balance).to eq(100)
    end
    end

    end