Here is my visionary of deposit,withdraw and get pin


#1



class Account
    attr_reader :name, :balance
    def initialize(name,balance=100)
        @name=name
        @balance=balance
    end
    private
    def pin
        @pin=1234
    end
    def pin_error
        return "Access denied: incorrect PIN."
    end
    public
    def deposit(pin_number,amount)
        if pin_number==pin
            @balance+=amount
            puts"Deposit #{amount}.New balance:$#{@balance}."
        else pin_error
        end
    end
    
    def display_balance(pin_number)
        if pin_number==pin 
            puts "Balance:$#{@balance}."
       else pin_error
       end
       
   end
   def withdraw(pin_number,amount)
        @balance-=amount
       if  pin_number==pin
           if @balance>0
            puts"Withdrew #{amount}.New balance:$#{@balance}."
           else
            puts"You art trying withdrew #{amount},not sufficient fund.New balance:$#{@balance}."
           end
       else puts pin_error
       end  
   end
   def get_pin
       puts @pin
   end
end
checking_account=Account.new("Armen",1000)
checking_account.withdraw(1234,900)
checking_account.display_balance(1234)
checking_account.get_pin
checking_account.deposit(1234,188)
checking_account.withdraw(1234,38)
checking_account.withdraw(1234,500)
checking_account.withdraw(1244,38)
please any suggestion,correction,optimization are welcomed. I just wondering is it possible in withdraw method instead if ..else use other kind conditional structures. Share with me your thoughts.


#2

Please are you having any issue or just sharing?

The code looks awesome to me.


#3

just sharing,
it works great.only thing If there is some optimization especially in withdraw if ..else statement tell me,for me it looks little bit clumsy .thanks


#4

Awesome. I should double like this :smile:


#5

But it gives an error when not sufficient funds, but still deducts the money and makes a new balance?
Shouldnt it either let it be a negative number and not give an error
OR
not let withdraw when amount > balance