2015-10-07 257 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