-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.rb
43 lines (40 loc) · 1.08 KB
/
card.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
39
40
41
42
43
require 'luhn'
class Card
attr_accessor :name, :card_number, :balance, :limit, :orig_limit
def initialize(name, card_number, limit)
@name = name
@card_number = card_number
if Luhn.valid? @card_number
@balance = "$0"
else
@balance = "ERROR"
end
@limit = limit
@orig_limit = limit
end
def charge(amount)
unless @balance == "ERROR"
amount = amount.gsub(/\D/,'').to_i
balance = @balance.gsub(/\D/,'').to_i
limit = @limit.gsub(/\D/,'').to_i
if amount < limit
@balance = "$" + (balance + amount).to_s
@limit = "$" + (limit - amount).to_s
end
end
end
def credit(amount)
unless @balance == "ERROR"
amount = amount.gsub(/\D/,'').to_i
balance = @balance.gsub(/\D/,'').to_i
limit = @limit.gsub(/\D/,'').to_i
@balance = "$" + (balance - amount).to_s
@limit = "$" + (limit + amount).to_s
# this makes sure that you never go over your original limit, because
# that would be absurd
if @limit > @orig_limit
@limit = @orig_limit
end
end
end
end