7/7 Customizing Banking on Ruby


#1



Hey all,

I recently finished the Ruby course and I decided to customize the last project.
As per the instructions, each method was to accept a pin number and amount, however, I wanted to create a more realistic program that asks for a pin number prior to presenting the user with banking options.

I was playing around for some time and I finally ended up with the attached code.
Every time I try to run the code through codecademy it gets up to "What would you like to do today?" (so after pin verification) but then the program quits out saying it took too long.
However, I copied the code into Text Wrangler and ran it through terminal and it works just fine (i.e. all methods work flawlessly and quickly).

So my questions are:
1. Why does this code not run successfully through codecademy?
2. Is there a more succinct way of writing this code?

P.S. All input is appreciated (even unrelated to my questions)
Thanks in advance!

https://www.codecademy.com/en/courses/ruby-beginner-en-32cN3/0/7?curriculum_id=5059f8619189a5000201fbcb


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

    public

    def display_balance
	puts "Balance: $#{balance}."
    end

    def withdraw(amount)
        @balance -= amount
        puts "Withdrew #{amount}. New balance: $#{@balance}."
    end   

    def deposit(amount)
        @balance += amount
        puts "Deposited #{amount}. New balance: $#{@balance}."
    end

    def pincheck(pinnum)
    
        if pinnum == pin
            puts "What would you like to do today? (Deposit, Withdraw, Check Balance)"
            user_input = gets.chomp
            user_input.upcase!
        
            case user_input

            when "DEPOSIT"
                puts "How much would you like to deposit today?"
                amount = Integer(gets.chomp)
                deposit(amount)

            when "WITHDRAW"
                puts "How much would you like to withdraw today?"
                amount = Integer(gets.chomp)
                withdraw(amount)

            when "CHECK BALANCE"
                display_balance
            
            else
                puts "Sorry, that option is not supported"
            end
    else
        puts pin_error
    end
end

private

    def pin
        @pin = 1234
    end
    
    def pin_error
        puts "Access denied: incorrect PIN."
    end


end

checkings_account = Account.new("User", 1_000)

#Pin Verification
puts "Welcome, please enter your PIN:"
pinnum = Integer(gets.chomp)
checkings_account.pincheck(pinnum)


#2

Usually a good idea to create a smaller example that reproduces the problem so that it's easier to tell if there's something else affecting the outcome.

class Test
  def take_input
    gets.chomp
  end
end

Test.new.take_input

That still times out. So I'd say there's something fishy with codecademy's ruby environment, perhaps something to do with how they inspect your program, some dirty hack.

Aside from that, I wouldn't mix the logic of the account and the user interface because god classes that try to do everything are difficult to maintain, they're a mess.

All the public methods of Account that make any changes or expose private information need to check authorisation, that's not the case right now (the class should be sanity-checking all operations)

I suggest running a style checker on your code. especially since you have easy access to it as you have your own ruby environment set up.

$ gem install rubocop
$ rubocop my_code.rb

You might need to add the option --user-install on a mac, I think it defaults to system-wide gems otherwise

$ gem install --help | grep -A1 user-install
        --[no-]user-install          Install in user's home directory instead
                                     of GEM_HOME.

Fix up most of what it points out (might want to skip documentation for example, or you might disagree with something) and use the corrections as a guide to writing code with better formatting.


#3

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