2015-10-07 161 views
-3

我正在使用Ruby创建银行帐户。我得到一个未定义的方法或局部变量错误,即使该方法已经定义,有人可以告诉我这个代码有什么问题吗?我曾尝试重写main_menu方法,但仍然收到相同的错误。创建银行帐户

class Account 
    attr_reader :name, :checking_account, :savings_account 

    def initialize(name, checking_account, savings_account) 
    @name = name 
    @checking_account = checking_account 
    @savings_account = savings_account 
    end 
end 

def display 
    puts "Enter your PIN:" 
    input = gets.chomp 

    if input = pin 
    main_menu 
    else 
    bad_pin 
    end 
end 

def main_menu 
    puts """ 
    Welcome back #{name}! 
    Would you like to: 
    Display Balance press '1' 
    Make Withdrawl press '2' 
    Make Deposit press '3' 
    Exit press '4' 
    """ 

    input = gets.chomp 

    case option 
    when 1 
     balance 
    when 2 
     withdrawl 
    when 3 
     deposit 
    else 
     exit 
    end 
end 

def balance 
    puts "Which balance? Checking or Savings?" 
    input = gets.chomp 

    if input =~ /checking/i 
    puts "Your balance for your Checking Account is: $#{checking_account}." 
    elsif input =~ /savings/i 
    puts "Your balance for your Savings Account is: $#{savings_account}." 
    else 
    main_menu 
    end 
end 

def withdrawl(pin_number, amount) 
    puts "Enter PIN to make a withdrawl:" 
    input = gets.chomp 

    case withdrawl 
    when checking_account 
     @checking_account -= amount 
     puts "You have withdrawn $#{amount}; you now have ${checking_account} in your checking." 
    when savings_account 
     @savings_account -= amount 
     puts "You have withdrawn ${amount}; you now have $#{savings_account} in your savings." 
    else 
     bad_pin 
    end 
end 

def deposit 
    puts "Which account would you like to deposit into: Checkings, or Savings?" 
    input = gets.chomp 

    if input =~ /checking/i 
    @checking_account += amount 
    puts "You have made a deposit of $#{amount} leaving you with $#{checking_account}." 
    elsif input =~ /savings/i 
    @savings_account += amount 
    puts "You have made a deposit of $#{amount} leaving you with $#{savings_account}." 
    else 
    main_menu 
    end 
end 

def pin 
    @pin = 1234 
end 

def bad_pin 
    puts "Access Denied: incorrect PIN" 
    exit 
end 

my_account = Account.new("Thomas", 500_000, 750_000) 
display 

我recieving的错误是这样的:

bank.rb:25:in `main_menu': undefined local variable or method `name' for main:Object (NameError) 
     from bank.rb:16:in `display' 
     from bank.rb:101:in `<main>' 
+2

请正确缩进您的代码。 –

+0

我使用的是geddit,它不会缩进你。 – 13aal

+0

'if input = pin':比较相等是否用'=='完成。 '='用于赋值。 ''“”''也是错误的,一个单引号(''')就足够了。 – cremno

回答

1

移动内部

所有的方法和呼吁类即如你的显示方法,你的情况my_account的

这应该做的伎俩! :)

+0

谢谢!有效。 – 13aal

+0

只是upvote并接受答案,如果你不介意:) –

1

你必须把所有这些方法为Account类体。否则,他们将不会看到实例变量或attr_reader方法。

class Account 
    attr_reader :name, :checking_account, :savings_account 

    def initialize(name, checking_account, savings_account) 
    @name = name 
    @checking_account = checking_account 
    @savings_account = savings_account 
    end 
end # <---- This closes your class, it has to be moved past the last method 

此错误通过适当的缩进即时显示。

和结束时,你叫my_accountdisplay

my_account.display 
+0

他似乎希望*这些方法在课外。即他只是最后调用'display'。 – Mischa

+0

我认为这是由第一个错误引起的另一个错误。他一路访问实例变量。 –

+0

噢,对不起;-) – Mischa