5. Making a Withdrawal


#1

Hello !

Someone can help me please !!
I have to do :
Add a public withdraw method to your class that takes two parameters, pin_number and amount. If pin_number matches pin, your method should subtract the amount from the balance and puts "Withdrew #{amount}. New balance: $#{@balance}." Otherwise, it should puts pin_error.

Again, I don't understand where is my mistake in my code ....

class Account
attr_reader :name
attr_reader :balance
def initialize(name, balance=100)
@name = name
@balance = balance
end
public
def display_balance(pin_number)
if pin_number == pin
puts "Balance: $#{@balance}."
else pin_error
end
public
def withdraw(pin_number, amount)
if pin_number == pin
@balance -=amount
puts "Withdrew #{amount}. New balance: $#{@balance}."
else
puts "pin_error"
end
end
private
def pin
@pin = 1234
end

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

If someone can explain me :slightly_smiling: !!!
Thank you !!!


#2

I have changed some things....

I have written

1 class Account
2 attr_reader :name
3 attr_reader :balance
4 def initialize(name, balance=100)
5 @name = name
6 @balance = balance
7 end
8
9 public
10 def display_balance(pin_number)
11 if pin_number == pin
12 puts "Balance: $#{@balance}."
13 else pin_error
14 end

15 public
16 def withdraw(pin_number, amount)
17 if pin_number == pin
18 @balance -=amount
19 puts "Withdrew #{amount}. New balance: $#{@balance}."
20 else
21 puts "pin_error"
22 end
23 end
24 end
25 private
26 def pin
27 @pin = 1234
28 end
29
30 private
31 def pin_error
32 return "Access denied: incorrect PIN."
33 end
34 end
35

I have the result : "nil"
But there is a message :
"Oops, try again. Did you add a public withdraw method to your Account class?"

But I think, i have declared my public withdraw method, no ??

Thanks for your help!!!


#4