forked from tiyd-rails-2015-01/currency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency.rb
42 lines (34 loc) · 1021 Bytes
/
currency.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
require './different_currency_code_error'
class Currency
attr_reader :amount, :currency_code
def initialize(amount, currency_code)
@amount = amount
@currency_code = currency_code
end
def == (other_money)
self.amount == other_money.amount && self.currency_code == other_money.currency_code
end
def + (other_money)
if self.currency_code == other_money.currency_code
Currency.new(amount + other_money.amount, currency_code)
else
raise DifferentCurrencyCodeError, "You cannot add two different kinds of currency"
end
end
def - (other_money)
if self.currency_code == other_money.currency_code
Currency.new(amount - other_money.amount, currency_code)
else
raise DifferentCurrencyCodeError, "You cannot subtract two different kinds of currency."
end
end
def * (number)
Currency.new(amount * number, currency_code)
end
end
# @currency_symbols = {"$" => :USD,
# EUR: "€",
# GBP: "£",
# AUD: "A$",
# CAD: "C$",
# JPY: "¥"}