Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- =begin
- Define a class called CheckingAccount that inherits from BankAccount.
- Write it so that if more than 3 withdrawals are made, it charges a $5
- fee for each transaction afterwards. Use a constant called
- MAX_FREE_WITHDRAWALS to set the maximum number of withdrawals before the
- fee kicks in. Define a method called get_free_withdrawal_limit to expose
- the value of the constant. If there aren't enough funds to cover the fee
- and the limit has been reached, it should not make a successful
- withdrawal. The class should also have a transfer method that uses an
- account to move money from, and an amount to transfer as arguments and
- allows you to withdraw amount from account and deposit amount into the
- account the method was called on. Don't implement a method to reset the
- number of transactions.
- =end
- class BankAccount
- attr_reader :balance
- def initialize(balance) #constructor
- @balance = balance
- end
- def display_balance
- @balance
- end
- def deposit(amount)
- if(amount > 0)
- @balance += amount
- else
- 'Cannot deposit negative amounts or 0'
- end
- end
- def withdraw(amount) #setter
- if (amount <= @balance)
- @balance -= amount
- @number_of_withdrawals += 1
- else
- 'Insufficient funds'
- end
- end
- end
- class CheckingAccount < BankAccount
- attr_reader :number_of_withdrawals
- MAX_FREE_WITHDRAWALS = 3
- def get_free_withdrawal_limit
- MAX_FREE_WITHDRAWALS
- end
- def initialize(balance)
- super(balance) # calls the parent initialize constructor and variable
- @number_of_withdrawals = 0
- end
- def deposit
- super
- end
- def withdraw
- super
- end
- def transfer(account, amount)
- end
- end
- #------------------------------------------TEST SPECS--------------------------------------
- describe CheckingAccount do
- it "has BankAccount as its parent class" do
- expect(CheckingAccount.superclass).to eq(BankAccount)
- end
- before do
- @account = CheckingAccount.new(100)
- end
- describe "attributes" do
- it "responds to 'balance'" do
- expect(@account).to respond_to(:balance)
- end
- it "returns the correct value for 'balance'" do
- expect(@account.balance).to eq(100)
- end
- it "responds to 'number_of_withdrawals'" do
- expect(@account).to respond_to(:number_of_withdrawals)
- end
- it "returns the correct value for 'number_of_withdrawals'" do
- expect(@account.number_of_withdrawals).to eq(0)
- end
- end
- describe "methods" do
- describe "#get_free_withdrawal_limit" do
- it "responds to 'get_free_withdrawal_limit'" do
- expect(@account).to respond_to(:get_free_withdrawal_limit)
- end
- it "responds to 'get_free_withdrawal_limit'" do
- expect(@account.get_free_withdrawal_limit).to eq(3)
- end
- end
- describe "#transfer" do
- before do
- #create account to transfer from
- @other_account = CheckingAccount.new(50)
- end
- it "increases the value of the balance attribute of the account called on while reducing the one for the argument account" do
- #capture current balance on both accounts
- current_balance = @account.balance
- other_balance = @other_account.balance
- #perform transaction
- @account.transfer(@other_account, 50)
- #check that account was credited $50
- expect(@account.balance).to eq(current_balance + 50)
- #check that argument account was reduced by $50
- expect(@other_account.balance).to eq(other_balance - 50)
- end
- it "does not transfer money when funds are not enough" do
- #capture current balance on both accounts
- current_balance = @account.balance
- other_balance = @other_account.balance
- #perform transaction
- @account.transfer(@other_account, 100)
- #check that account balance remained the same
- expect(@account.balance).to eq(current_balance)
- #check that argument account balance remained the same
- expect(@other_account.balance).to eq(other_balance)
- end
- end
- describe "#withdraw" do
- it "increments 'number_of_withdrawals' by one after a successful withdrawal" do
- current_withdrawals = @account.number_of_withdrawals
- @account.withdraw(50)
- expect(@account.number_of_withdrawals).to eq(current_withdrawals + 1)
- end
- it "reduces 'balance' by 'amount' and charges $5 fee if 'number_of_withdrawals' > 3" do
- @account.withdraw(10)
- expect(@account.balance).to eq(90)
- @account.withdraw(10)
- expect(@account.balance).to eq(80)
- @account.withdraw(10)
- expect(@account.balance).to eq(70)
- #transaction limit passed. Charge fee on this withdrawal
- @account.withdraw(10)
- expect(@account.balance).to eq(55)
- end
- it "does not 'withdraw' if the fee would make 'balance' negative" do
- @account.withdraw(25)
- @account.withdraw(25)
- @account.withdraw(25)
- @account.withdraw(25)
- expect(@account.balance).to eq(25)
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment