Why do I get "No method Error"


#1

Create an accont class

class Account
attr_reader :name, :balance
def initialize (name, balance=100)
@name = name
@balance = balance
end

 def withdraw (pin_number, amount)
  if pin_number == pin
    if amount > @balance
      puts "Trying to withdraw more than the balance. Enter < $#{@balance}"
    else 
      @balance -= amount
      puts "Withdraw #{amount}. New balance: $#{@balance}"
     end
  else
    puts pin_error
  end
end

def deposit (amount)
  @balance += amount
  puts "Amount: $#{amount} added to the balance. New balance: $#{@balance}"
end

private
def pin
  @pin = 1234
end

def pin_error
  return "Access denied: incorrect PIN."
end

end

checking_account = Account.new("Madhu", 5_000_00)
checking_account.withdraw(345,5_000)
checking_account.withdraw(1234,5_000_01)
checking_account.deposit(1)
checking_account.withdraw(1234,1)

Extended class

class SavingsAccount < Account
def display_balance(pin_number)
puts pin_number == pin ? "Balance: $#{@balance}." : pin_error
end
end

my_savings_account = SavingsAccount.new("Madhu",10_000_00)

SavingsAccount.display_balance(1234)

I'm getting the following error:

Access denied: incorrect PIN.
Trying to withdraw more than the balance. Enter < $500000
Amount: $1 added to the balance. New balance: $500001
Withdraw 1. New balance: $500000
undefined method `display_balance' for Context::SavingsAccount:Class


#2

Please post a link to the exercise in question so we can read the instructions and test your code. Thank you.


#3

It is chapter 10. Banking on Ruby. within that chapter 7 (last one) well done.


#4

Should this be,

my_savings_account.display_balance(1234)

?

I created a Savings_Account class with no attributes and was able to access all the inherited methods.

class SavingsAccount < Account
end
my_savings = SavingsAccount.new("Wee Gillis",100_000)
my_savings.display_balance(1234)
my_savings.withdraw(1234, 1_500)