Ruby Examples
This commit is contained in:
35
level7/70.rb
Executable file
35
level7/70.rb
Executable file
@@ -0,0 +1,35 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
잔액 부족 예외 처리
|
||||
=end
|
||||
|
||||
class BankAccount
|
||||
def initialize(balance)
|
||||
@balance = balance
|
||||
end
|
||||
|
||||
def deposit(amount)
|
||||
@balance += amount
|
||||
end
|
||||
|
||||
def withdraw(amount)
|
||||
begin
|
||||
raise StandardError if @balance < amount
|
||||
@balance -= amount
|
||||
rescue
|
||||
puts "잔액이 부족합니다."
|
||||
@balance
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def get_balance
|
||||
@balance
|
||||
end
|
||||
end
|
||||
|
||||
account = BankAccount.new(100)
|
||||
puts account.withdraw(99)
|
||||
puts account.withdraw(5)
|
||||
Reference in New Issue
Block a user