-
Notifications
You must be signed in to change notification settings - Fork 254
/
bank_account.rb
38 lines (32 loc) · 1.11 KB
/
bank_account.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Problem: https://exercism.org/tracks/ruby/exercises/bank-account
# Solution
class BankAccount
def initialize
@opened = false
@balance = 0
end
def open
raise ArgumentError.new("You can't open an already open account") if @opened
@opened = true
end
def balance
raise ArgumentError.new("You can't check the balance of a closed account") unless @opened
@balance
end
def deposit(amount)
raise ArgumentError.new("You can't deposit a negative amount") if amount<0
raise ArgumentError.new("You can't deposit money into a closed account") unless @opened
@balance+= amount
end
def withdraw(amount)
raise ArgumentError.new("You can't withdraw money into a closed account") unless @opened
raise ArgumentError.new("You can't withdraw a negative amount") if amount<=0
raise ArgumentError.new("You can't withdraw more than you have") if amount>@balance
@balance-=amount
end
def close
raise ArgumentError.new("You can't close an already closed account") unless @opened
@opened = false
@balance = 0
end
end