Явор обнови решението на 15.01.2013 21:46 (преди около 12 години)
+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ def initialize
+ @exchange = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ return if from_currency == to_currency
+ @exchange[[from_currency, to_currency]] = rate
+ @exchange[[to_currency , from_currency]] = (1.to_d) / rate
+ end
+
+ def get(from_currency, to_currency)
+ return 1.to_d if from_currency == to_currency
+ @exchange[[from_currency, to_currency]]
+ end
+
+ def convert(from_currency, to_currency, amount)
+ if get(from_currency, to_currency)
+ amount*get(from_currency, to_currency)
+ else
+ raise Unknown, 'no current exchange rate for these currencies'
+ end
+ end
+
+ class Unknown < RuntimeError
+ def exception(message)
+ RuntimeError.new(message)
+ end
+ end
+end
+
+class Money
+ include Comparable
+
+ attr_reader :amount, :currency
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s
+ @amount.to_digits + " " + @currency.to_s
+ end
+
+ def in(currency, exchange_rate)
+ Money.new(exchange_rate.convert(@currency, currency, @amount), currency)
+ end
+
+ def *(n)
+ if n.is_a? Numeric
+ Money.new(@amount * n, @currency)
+ else
+ raise ArgumentError, 'argument is not of class Numeric'
+ end
+ end
+
+ def /(n)
+ if n.is_a? Numeric
+ Money.new(@amount / n, @currency)
+ else
+ raise ArgumentError, 'argument is not of class Numeric'
+ end
+ end
+
+ def +(other)
+ raise ArgumentError, 'arguments are not of the same class' unless other.is_a? Money
+ if @currency == other.currency
+ Money.new(@amount + other.amount, @currency)
+ else
+ raise IncompatibleCurrencies, 'different currencies'
+ end
+ end
+
+ def -(other)
+ raise ArgumentError, 'arguments are not of the same class' unless other.is_a? Money
+ if @currency == other.currency
+ Money.new((@amount - other.amount).abs, @currency)
+ else
+ raise IncompatibleCurrencies, 'different currencies'
+ end
+ end
+
+ def <=>(other)
+ raise ArgumentError, 'arguments are not of the same class' unless other.is_a? Money
+ if @currency == other.currency
+ @amount <=> other.amount
+ else
+ raise IncompatibleCurrencies, 'different currencies'
+ end
+ end
+
+ def ==(other)
+ raise ArgumentError, 'arguments are not of the same class' unless other.is_a? Money
+ if @currency == other.currency
+ @amount == other.amount
+ else
+ raise IncompatibleCurrencies, 'different currencies'
+ end
+ end
+
+ class IncompatibleCurrencies < RuntimeError
+ def exception(message)
+ RuntimeError.new(message)
+ end
+ end
+end