`<top(required)=“”>':未初始化的常量Object :: ATM(NameError)

时间:2015-11-23 12:53:55

标签: ruby

我已经创建了一个自动柜员机几天了,并且最近在尝试让课程运行时遇到了这个错误,我明白<top (required)>'意味着有一些事情发生在顶部文件,但我似乎无法找到任何问题,有人可以向我解释这里的问题是什么,以及我应该如何修复它?

main.rb来源:

#!/usr/bin/env ruby

################
#ATM Rewrite 
#                                
#Creator Lost Bam      
#                        
#11/19/15                
##################

require_relative 'initialize.rb'
require_relative 'checking.rb'
require_relative 'savings.rb'
require_relative 'transfer.rb'
require_relative 'loan.rb'
require_relative './ready/exit.rb'#if you're gonna run the file take the './ready' off of this one`
require_relative './ready/redirect.rb'# and this one

class ATM
    attr_accessor :name, :checking_account, :savings_account, :pin_number, :transfer, :loan
        def initialize( name, checking_account, savings_account, pin_number )
            @name = name
            @checking_account = checking_account
            @savings_account = savings_account
            @pin_number = pin_number
        end
    end

    def pin #add regex =~ /^\d{4}/ to compare against input of user for a 4 digit combination
        x = 3
        while (x > 0) do
            puts "Enter PIN(#{x} attempts left):"
            pin_num = gets.chomp
            case pin_num.to_i
            when @pin_number
                menu
            else
                puts "Invalid PIN"
                x -= 1
                bad_pin if x == 0
            end
        end
    end

    def menu 
        puts <<-END.gsub(/^\s*>/, ' ')
            >
            >Welcome #{name} thank you for choosing Bank of Bam.
            >You may choose from the list below of what you would like to do
            >For checking inquiries press '1'
            >For savings account information press '2'
            >To transfer funds press '3'
            >To either apply for a loan, or get information on a loan press '4'
            >To exit the system press '5'
            >
        END
        input = gets.chomp
        case input.to_i
        when 1
            checking_information
        when 2
            savings_information
        when 3
            transfer_funds
        when 4
            loan_info
        when 5
            exit_screen
        else
            puts "Error Invalid option please try again"
            menu
        end
    end

    def bad_pin
        abort('Exiting system, all attempts failed...')
        exit
    end

initialize.rb source(我正在测试运行的地方)

my_acc = ATM.new("Thomas", 700, 700, 1234)
my_acc.pin #also attempted using other methods no success

checking.rb来源:

private

def checking_information
    puts <<-END.gsub(/^\s*>/, ' ')
        >
        >Your current balance is: #{@checking_account}
        >Deposit funds '1'
        >Make a withdrawl '2'
        >Go back '3'
        >
    END
    input = gets.chomp
    case input.to_i
    when 1
        deposit
    when 2
        withdrawl
    when 3
        redirect
    else
        puts "Invalid option"
        checking_information
    end
end

def deposit
    puts "Enter amount to deposit:"
    amount = gets.chomp.to_i
    @checking_account += amount.to_f
    puts "Your new balance is #{@checking_account}"
    puts "Would you like make another deposit?"
    input = gets.chomp
    if input == /yes/i
        deposit
    else
        redirect
    end
end

def withdrawl
    puts "Your current balance is #{@checking_account}. Please enter amount to withdrawl:"
    input = gets.chomp.to_i
    @checking_account -= input.to_f
    puts "Your new balance is #{@checking_account}"
    puts "Would you like to make another withdrawl?"
    if input == /yes/i
        withdrawl
    else
        redirect
    end
end

Savings.rb和transfer.rb不完整(甚至还没有开始),所以没有必要展示它们。

loan.rb来源:

private

class Loan 
    attr_accessor :credit
        def initialize( score )
            @score = 0
        end
    end

    def loan_info
        puts <<-END.gsub(/^\s*>/, ' ')
            >
            >Hello and welcome to the credit station
            >Please choose from the list below
            >Would you like to Apply for a loan '1'
            >Check credit score '2'
            >Go back '3'
            >
        END
        input = gets.chomp
        case input.to_i
        when 1
            apply_credit
        when 2
            check_score
        else
            redirect
        end
    end

    def apply_credit
        if @score >= 640
            accepted
        else
            denied_loan
        end
    end

    def accepted
        loan = %w(100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600 1700 1800 1900 2000 2100 2200 2300 2400 2500 2600 2700 2800 2900 3000 3100 3200 3300 3400 3500 3600 3700 3800 3900 4000 4100 4200 4300 4400 4500 4600 4700 4800 4900 5000)
        loan_amount = loan.sample
        puts "You have been accepted for a #{loan_amount} loan which will be added to your bank account"
        puts <<-END.gsub(/^\s*>/, ' ')
            >
            >Which account would you like to add that to?
            >Checking Account '1'
            >Savings Account '2'
            >
        END
        input = gets.chomp
        case input.to_i
        when 1
            @checking_account += loan_amount
            puts "#{loan_amount} has been added to your checking account, your new balance is #{@checking_account}"
            puts "Your card will now be returned for security purposes."
            exit_screen
        when 2
            @savings_account += loan_amount #Not completed yet..
            puts "#{loan_amount} has been added to your savings account, your new balance is #{@savings_account}"
            puts "Your card will now be returned for security purposes"
            exit_screen
        end
    end

redirect.rb和exit.rb source:

    def redirect
        puts <<-END.gsub(/^\s*>/, ' ')
                >
                >Please choose from the list below
                >Main menu '1'
                >Checking inquiries  '2'
                >Savings account information '3'
                >Transfer funds '4'
                >Apply or view a loan '5'
                >Return card and exit system '6'
                >
            END
        input = gets.chomp
        case input.to_i
        when 1
            menu
        when 2
            checking_information
        when 3
            savings_information
        when 4
            transfer_funds
        when 5
            loan_info
        when 6
            exit_screen
        else
            puts "Invalid input"
            redirect
        end
    end


----------

def exit_screen
    puts "Returning card.."
    puts "Thank you for choosing Bank of Bam, have a nice day!"
    exit
end

更新

嗯我被建议更改名称并从初始化文件中要求main,所以我做了和BOOM相同的错误,但这次它添加了一些东西:

`<top (required)>': private method `pin' 
called for #<ATM:0x00000016482f18> (NoMethodError) 

嗯,这不仅仅是花哨..似乎工作除了pin不是私人方法,非常好..( sarcasm ..

0 个答案:

没有答案