PIN Error


#1

I’ve tried several variations of code and have consulted the Q&A. I cannot get it to recognize my PIN. (@pin or pin). I’ve removed everything that didn’t seem relevant.

class Account
  attr_reader :name
  attr_reader :balance
def initialize (name, balance=100)
  @name = name
  @balance = balance
end
  
private
  
  def pin
    @pin = 1234
  end
  def pin_error
    "Access denied: incorrect PIN."
  end
 
public
  
def display_balance(pin_number)
        puts pin_number == @pin ? "Balance: $#{@balance}.": pin_error
  end
  
  def withdraw(pin_number, amount)
   if pin_number == pin
     @balance -= amount
     puts "Withdrew #{amount}. New balance: $#{@balance}."
   else puts pin_error
  end
  end 
  
  def deposit(amount)
    @balance += amount
    puts "Ok, your new balance is $#{@balance}. Thanks for stopping by! :-)"
  end
end

checking_account = Account.new("Alex", 1_000)

puts "what would you like to do?"
puts "---Try typing 'deposit' to make a deposit."
puts "---Try typing 'check balanace' to see what ya got!"
puts "---Try typing 'withdraw' to get some out!"

response = gets.chomp
response.downcase!

case response
  when "check balance"
  puts "PIN Please: "
  num = gets.chomp
  checking_account.display_balance(num)
end


There are several other case responses, but I removed them to cut down on clutter. I keep getting an error that says the PINs don’t match. I’ve also tried using “num.to_i” after the gets.chomp line. Please help, I’m so lost


#2

The string must have at least one uppercase letter or this will become, nil.

Remove the bang (!) and add it to your first line…

response = gets.chomp.downcase

It probably does not matter, but I’ve mostly seen the public code above the private code. Will take some checking to see if it really does matter or not.


#3

I moved the public methods above the private and still doesn’t recognize the PIN. I also moved the .downcase to the line above as you recommended. No luck.

I thought maybe it had something to do with “@pin” being an integer and “pin_number” being a string, but no luck there either.


#4

Please post a link to the exercise you are on. Thanks.


#5

It’s those “above and beyond” exercises at the end -

https://www.codecademy.com/courses/learn-ruby/lessons/banking-on-ruby/exercises/well-done


#6

@pin is defined in the pin method so just write as you did in the withdraw method:

pin_number == pin

which you will still probably have to do so an integer is passed in, not a string.


#7

Wow, yeah that did it! Man, I could have sworn I tried that before, but I guess I hadn’t.

Thank you for your help!


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.