Петър обнови решението на 16.01.2013 00:27 (преди около 12 години)
+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRateRow
+ attr_accessor :from, :to, :rate
+
+ def initialize(from_currency, to_currency, rate)
+ @from = from_currency
+ @to = to_currency
+ @rate = rate
+ end
+
+ def match?(from_currency, to_currency)
+ (from_currency == @from and to_currency == @to) or
+ (from_currency == @to and to_currency == @from)
+ end
+
+ def rate(from_currency, to_currency)
+ if from_currency == @from and to_currency == @to
+ @rate
+ else
+ '1'.to_d / @rate
+ end
+ end
+end
+
+class ExchangeRate
+ def initialize
+ @rate_rows = []
+ end
+
+ def set(from_currency, to_currency, rate)
+ rate_row = find_rate from_currency, to_currency
+
+ if rate_row == nil
+ @rate_rows << ExchangeRateRow.new(from_currency, to_currency, rate)
+ else
+ rate_row.from, rate_row.to, rate_row.rate = from_currency, to_currency, rate
+ end
+ end
+
+ def get(from_currency, to_currency)
+ return 1 if from_currency == to_currency
+
+ rate_row = find_rate from_currency, to_currency
+
+ if rate_row == nil
+ nil
+ else
+ rate_row.rate from_currency, to_currency
+ end
+ end
+
+ def convert(from_currency, to_currency, amount)
+ rate = get from_currency, to_currency
+
+ if rate == nil
+ raise Unknown
+ else
+ amount * rate
+ end
+ end
+
+ class Unknown < RuntimeError
+ end
+
+ private
+
+ def find_rate(from_currency, to_currency)
+ @rate_rows.find { |row| row.match? from_currency, to_currency }
+ end
+end
+
+class Money
+ include Comparable
+ attr_reader :amount, :currency
+
+ def initialize(amount, currency)
+ @amount, @currency = amount, currency
+ end
+
+ def to_s
+ @amount.to_s('2F') + " " + @currency.to_s
+ end
+
+ def in(currency, exchange_rate)
+ new_currency_amount = exchange_rate.convert @currency, currency, @amount
+ Money.new new_currency_amount, currency
+ end
+
+ def *(number)
+ check_for_numeric number
+ Money.new @amount * number.to_d, @currency
+ end
+
+ def /(number)
+ check_for_numeric number
+ Money.new @amount / number.to_d, @currency
+ end
+
+ def -(money)
+ check_for_money money
+ Money.new @amount - money.amount, @currency
+ end
+
+ def +(money)
+ check_for_money money
+ Money.new @amount + money.amount, @currency
+ end
+
+ def <=>(other)
+ check_for_money other
+ @currency <=> other.currency
+ end
+
+ def ==(other)
+ check_for_money other
+ @currency == other.currency
+ end
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ private
+ def check_for_numeric(argumet)
+ raise ArgumentError unless argumet.is_a? Numeric
+ end
+
+ def check_for_money(argumet)
+ raise ArgumentError unless argumet.is_a? Money
+ raise IncompatibleCurrencies unless @currency == argumet.currency
+ end
+end