JCD
JCD

Reputation: 69

Accessing and checking a value within a hash

Looked all around and could not find a proper answer to the problem I am facing. I want to access and check for a value within a hash. The code is below...

class Bank

    class AccountMaker
        attr_accessor :account_number, :name, :balance, :pin

        def initialize(account_number, name, balance, pin)
            @account_number = account_number
            @name = name
            @balance = balance
            @pin = pin
        end
    end

    attr_accessor :int_account_number, :int_account_pin

    def initialize
        @accounts = {}
        @int_account_number = int_account_number
        @int_account_pin = int_account_pin
    end

    def add_account(account_number, name, balance, pin)
        @accounts[account_number] = AccountMaker.new(account_number, name, balance, pin)
    end

    def login_screen

        def account_number_login
            puts "Please enter your 7 digit account number."
            account_number = gets.chomp
            int_account_number = account_number.to_i

            if @accounts.has_key?(int_account_number) and (/^\w{7}$/ === account_number)
                thank_you_msg()
                pin_login(int_account_number)
            else
                error_msg()
                account_number_login()
            end
        end

        def pin_login(int_account_number)
            puts "Please enter your 4 digit pin."
            account_pin = gets.chomp
            int_account_pin = account_pin.to_i #May have to use this later

            #puts int_account_number, int_account_pin #Used to check if variables come through

            if (What Should go here?) == int_account_pin #(/^\d{4}$/ === account_pin) 
                thank_you_msg
                main_menu()
            else
                error_msg()
                pin_login(int_account_number)
            end
        end

        account_number_login()
    end

    def main_menu    
    end

end

The question I have is how do I access the hash value of pin for a user inputed account number(key) and check if it matches the user entry? The hash contains the value under :pin but I am having the hardest time trying access and compare it.

Upvotes: 0

Views: 55

Answers (1)

7stud
7stud

Reputation: 48589

The question I have is how do I access the hash value of pin for a user inputed account number(key) and check if it matches the user entry? The hash contains the value under :pin but I am having the hardest time trying access and compare it.

Like this:

@accounts[int_account_number].pin

The fragment:

@accounts[int_account_number]

is an AccountMaker instance. And, an AccountMaker instance has an getter method named pin() (as well as a setter method named pin=()):

                                          Declared right there!
                                                   |
class AccountMaker                                 V
  attr_accessor :account_number, :name, :balance, :pin  

But, your code needs to be reorganized--starting with the indenting: ruby indenting is 2 spaces--not 1 space, not 4 spaces. You can indent ruby code 7 spaces if you want--but don't expect anyone to help you when you post on a public forum. So, your first task is to re-indent all your code to 2 spaces.

Next, get rid of your nested class: move it to the top level and rename it Account. Class names are not verbs--they are nouns. Methods are the verbs.

Then, get rid of all your nested def's--nesting a def most likely doesn't work like you think it does. Rather, all the def's should be defined at the top level of the Bank class.

Upvotes: 1

Related Questions