Bank class withdraw


#1

My witdraw method in Account class isnt functioning properly.. In particular im talkin about the case statement. when it equals 0



module Errors
def no_money
@e = "You have no money"
@ee = "You are below 50 please deposit some money!"
end
end

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

def display_balance(pin_number)
puts pin_number == pin ? "Balance: $#{@balance}" : pin_error

end

def withdraw(pin_number, amount)
@balance -= amount
puts "Withdrew #{amount}. New balance: #{@balance}. "
puts "#{no_money[@ee]}" if @balance < 50 #The brackets in the brackets where it says no_monet thats why they neccessary because you got 2 instance variables in no_money method so it targets.
case @balance
when 0
puts "#{no_money[@e]}" #This isn' working findout why.

		if pin_number != pin
		puts pin_error
		end
	end
end

def deposit(pin_number,amount)
if pin_number == pin
@balance += amount
puts " Deposited amount: #{amount} New balance: #{@balance}. "
else
puts pin_error
end
end

#Include error checking that prevents users from overdrawing their accounts

private
def pin
@pin = 1234
end

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

checking_account = Account.new("Al")
checking_account.withdraw(1234,1)
checking_account.display_balance(1234)
checking_account.display_balance(123)
checking_account.deposit(1234,20)
checking_account.withdraw(1234,80)
checking_account.withdraw(123,2)
checking_account.withdraw(1234,37)


Replace this line with your code.


#2

Please include some kind of query in your post. Right now you've said that there is a problem, but not what it is, why you're unable to address it, what you want to know.. It's really hard to give an answer to that. It also helps to make sure that your code's formatting is intact.


#3

I have stated that the case statement within the withdraw method of the class isn't working. Meaning that when my balance hits 0 the case statement doesn't activate. I am wondering why it's not working meaning the case statement.


#4

The word "working" is relative to what you want. I would have to guess. That creates the chance of guessing wrong and spending time on something other than you want.

Making it easy to reply gets you a faster better response at lower effort from the person replying. Not making it easy to reply shows that you don't value other people's time and they'll be less likely to spend any time at all.

If you have a glass jar that is difficult to open and you want help with it. Would you bring it to the person you are asking for help? Or would you have them run up 5 flights of stairs to where you live first? Which one gets you the jar opened faster? Which one will be more likely to succeed in getting help?

If the 0 case doesn't match then perhaps the value wasn't 0. Print it out to to investigate. You can also print out the result of testing if it's equal to 0 so that you know you're not missing anything subtle.


#5

I am sorry but i don't get what your trying to say. I don't know how i didn't make it clear by saying my case statement isn't working. I assumed you would understand what i meant just by looking at the code itself. I specifically pointed out what the problem was by saying its the case statement in the withdraw method.


#6

You explaining it in ten words is faster and more accurate than reading code and guessing how it in your opinion misbehaves.

You posted over 50 lines of code which is badly formatted. Reading it is not convenient easy or pleasant.

Saving yourself from describing the problem also shows how you value your own time compared to that of others.

There's a pretty good article on this here: http://www.catb.org/~esr/faqs/smart-questions.html
The glass jar analogy really should say enough to convince you on this matter. Just the same if you want to improve on this and understand my perspective better than that article may be of value to you.


#7

Ok im sorry about the confusion, is there anyway you Can you teach me how to format it to other's? Because iv'e gotten this complaint before and my code is organized usually and all im doing is copy and pasting it to the post.

And in regarding my problem, let me try...... So the bank class Account has a method withdraw where i can keep taking money out the more i call the withdraw method. The case statement within the method is there to trigger when my instance variable @balance hits 0. But in this program it doesn't. I created the module which is suppose to be accessed when the balance hits 0.


#8

There are formatting tools in the text editor, click the one for preformatted text after having highlighted the code

My suggestion is still to investigate whether it really did hit 0
If it did hit 0, then the when 0 part probably executed and your observation that it didn't is wrong.
If it didn't hit 0, then... whatever makes you say that you did hit 0, wasn't happening


#9

It does land on 0 so i don't know what the case statement wont trigger.


#10

Then it probably does trigger. Whatever is making you say it isn't triggering, is probably not entirely correct. Finding out if it triggers is as easy as adding a print inside the case that says "hello" or something


#11

Bro, i tested it out and runned the code on the command promp and the case statement is activating. IF it did then it would access the module and print the @ee instance variable.


#12

Problem solved then. It activates and it prints the variable. (Or you are contradicting yourself)

If it does activate but doesn't print the intance variable, then the code that is printing the instance variable isn't right - you just determined that it runs, but that it has no effect. So that's what's wrong.


#13

Lol so why does it have no effect?


#14

Why would it?


#15

puts "#{no_money[@e]}"

The expression evaluated is

no_money[@e]

What does that do?


#16

no_money is a method. It's going to return something.
[] is also a method, it too will do something. Which method it is depends on what no_money returns.
@e is an instance variable, you're using that as the argument to the []-method of the value that no_money returns.

If you look at no_money you can see that it returns a string.
So the []-method is therefore String#[] which you can read about here: https://ruby-doc.org/core-2.2.0/String.html#method-i-5B-5D
@e is a string, so the argument given to String#[] is also a string. String#[] has some behaviour for that (which you can read about in its documentation)


#17

But the @@e instance variable worked and ran fine in the command prompt the @ee instance variable didn't run!


#18

Yes they both work. They do something. But what does work mean? (You are using the word work to avoid describing what it should be doing)

What does the method return?
What is the value of @ee?

When you take that value your method returns, and use the []-method on it, with the value of @ee, what does that all evaluate to?

"blah blah blah"["h bl"]
=> "h bl"

I also think that when you wrote that you didn't think through why or what it would be doing. You need to be able to motivate why something will have a particular result.


#19

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