Created
October 7, 2018 02:01
-
-
Save lauslim12/c4a0d389506a3edfc866c7eaf4b1d68a to your computer and use it in GitHub Desktop.
Basic object oriented programming for practice.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Account | |
attr_reader :name | |
attr_reader :balance | |
def initialize(name, balance=100) | |
@name = name | |
@balance = balance | |
end | |
public | |
def display_balance(pin_number) | |
puts pin_number == pin ? "Balance $#{balance}!" : pin_error | |
end | |
def withdraw(pin_number, amount) | |
if pin_number == pin | |
@balance -= amount | |
puts "Withdrew $#{amount}. New balance: $#{balance}." | |
else | |
puts pin_error | |
end | |
end | |
private | |
def pin | |
@pin = 1234 | |
end | |
def pin_error | |
return "Access denied: incorrect PIN." | |
end | |
end | |
x1 = Account.new("Nicholas", 500) | |
x1.withdraw(1234, 300) | |
x1.display_balance(1234) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment