7/7 Improving the class



It is suggested that one was to improve the class is to; "Include error checking that prevents users from overdrawing their accounts" Just wondering how I would go about do this?



A few thoughts:

  1. In the real world, one would have to enter a PIN before even having the option to deposit, withdraw or view a balance; so before we break our brains thinking about how to include PIN and withdrawal in the same step, maybe this wouldn't be the case in the real world.
  2. For the sake of the exercise, so far I have this:

The result I get is:

Balance: $50000.
Insufficient funds; desired amount cannot exceed account balance.
Withdrew $51000. New balance: $50000.
Access denied: incorrect PIN.

So, while it isn't perfect, (Because it still reads "Withdrew $51000...") it clearly shows that the balance didn't change. What I would like to achieve is that the withdraw method not allow a withdrawal happen in the first place. So...

I'm going to research using && as an initial step. If:

pin == pin_number && @balance > amount is true

Then the program can continue running; if not, it should puts some string that says "either your PIN is wrong or your amount is greater than account balance.

Extra note, sometimes when a username or password is wrong (on any given site/login), the programmer has written code to say "either your pw or un are wrong" which doesn't help the user; a more detailed script tells the user which one is wrong; I'll try to write that.

Anyone else have tips?


I'm testing my code that passed the course and I've decided i definitely would not use this bank.
`class Account
attr_reader :name
attr_reader :balance

def initialize(name, balance=100)
    @name = name
    @balance = balance

def pin
    @pin = 1234

def pin_error
    return "Access denied: incorrect PIN."

def display_balance(pin_number)
    puts pin_number == pin ? "Balance: $#{balance}." : pin_error

def withdraw(pin_number, amount)
    puts @balance < amount ? "Insufficient funds. Withdrawal amount cannot exceed current account balance." : " "
    puts pin_number == pin ? "Withdrew #{amount}. New balance: $#{balance}." : pin_error
    @balance -= amount

def deposit(pin_number, amount)
    puts pin_number == pin ? "Deposited #{amount}. New balance: $#{balance}." : pin_error
   @balance += amount



checking_account = Account.new("Jesse", 10_000_000)

checking_account.withdraw(4321, 1_000)
checking_account.withdraw(1234, 1_000)
checking_account.deposit(4321, 1_000)
checking_account.deposit(1234, 1_000)`

this yields

`Balance: $10000000.

Access denied: incorrect PIN.
Balance: $9999000.

Withdrew 1000. New balance: $9999000.
Balance: $9998000.
Access denied: incorrect PIN.
Balance: $9999000.
Deposited 1000. New balance: $9999000.

which shows that even with an incorrect pin my code still allows a user to make withdrawals and deposits. I'm thinking we need a bit more code that'll either cancel out the user's input (an if statement that re-adds the amount) or something that actually denies access when the "Access Denied" message is triggered.


I added just one line to withdraw method:
pin_number == pin ? @balance -= amount : pin_error
It prevents changing balance if PIN is incorrect.

I did simmilar operation in deposit method:
pin == pin_number ? @balance += amount : pin_error