Решение на Шеста задача от Йоана Тодорова

Обратно към всички решения

Към профила на Йоана Тодорова

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 29 успешни тест(а)
  • 18 неуспешни тест(а)

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
attr_accessor :exchange_hash
def initialize
@exchange_hash = {}
end
def set(from_currency, to_currency, rate)
unless from_currency==to_currency
@exchange_hash[[from_currency, to_currency]] = rate
@exchange_hash[[to_currency, from_currency]] = 1/rate
end
end
def get(from_currency, to_currency)
return 1 if from_currency==to_currency
return nil unless @exchange_hash[[from_currency, to_currency]]
@exchange_hash[[from_currency, to_currency]]
end
def convert(from_currency, to_currency, amount)
return BigDecimal.new(amount) if from_currency==to_currency
raise ExchangeRate::Unknown unless @exchange_hash[[from_currency, to_currency]]
BigDecimal.new amount*@exchange_hash[[from_currency, to_currency]]
end
end
class Money
include Comparable
class IncompatibleCurrencies < ArgumentError
end
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def amount
@amount
end
def currency
@currency
end
def to_s #oshte ne e qsno kak shte stane
amount_s = @amount.round(2).to_f.to_s
amount_s.gsub!(/(\d+\.)(\d+)/, $1 + $2.ljust(3-$2.length, '0')) if amount_s=~/(\d+\.)(\d+)/
"#{amount_s} #{@currency}"
end
def in(currency, exchange_rate)
Money.new exchange_rate.convert(@currency, currency, @amount), currency
end
def *(numeric)
raise Money::IncompatibleCurrencies unless numeric.kind_of? Numeric
Money.new @amount*numeric.to_d, @currency
end
def /(numeric)
raise Money::IncompatibleCurrencies unless numeric.kind_of? Numeric
Money.new @amount/numeric.to_d, @currency
end
def +(other)
raise ArgumentError unless @currency == other.currency
Money.new @amount+other.amount.to_d, @currency
end
def -(other)
raise ArgumentError unless @currency == other.currency
Money.new @amount-other.amount.to_d, @currency
end
def <=>(other)
@amount <=> other.amount
end
end

Лог от изпълнението

▸ Покажи лога

История (1 версия и 0 коментара)

Йоана обнови решението на 16.01.2013 14:13 (преди около 12 години)

▸ Покажи разликите
+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ class Unknown < RuntimeError
+ end
+
+ attr_accessor :exchange_hash
+ def initialize
+ @exchange_hash = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ unless from_currency==to_currency
+ @exchange_hash[[from_currency, to_currency]] = rate
+ @exchange_hash[[to_currency, from_currency]] = 1/rate
+ end
+ end
+
+ def get(from_currency, to_currency)
+ return 1 if from_currency==to_currency
+ return nil unless @exchange_hash[[from_currency, to_currency]]
+ @exchange_hash[[from_currency, to_currency]]
+ end
+
+ def convert(from_currency, to_currency, amount)
+ return BigDecimal.new(amount) if from_currency==to_currency
+ raise ExchangeRate::Unknown unless @exchange_hash[[from_currency, to_currency]]
+ BigDecimal.new amount*@exchange_hash[[from_currency, to_currency]]
+ end
+end
+
+class Money
+ include Comparable
+
+ class IncompatibleCurrencies < ArgumentError
+ end
+
+ def initialize(amount, currency)
+ @amount, @currency = amount, currency
+ end
+
+ def amount
+ @amount
+ end
+
+ def currency
+ @currency
+ end
+
+ def to_s #oshte ne e qsno kak shte stane
+ amount_s = @amount.round(2).to_f.to_s
+ amount_s.gsub!(/(\d+\.)(\d+)/, $1 + $2.ljust(3-$2.length, '0')) if amount_s=~/(\d+\.)(\d+)/
+ "#{amount_s} #{@currency}"
+ end
+
+ def in(currency, exchange_rate)
+ Money.new exchange_rate.convert(@currency, currency, @amount), currency
+ end
+
+ def *(numeric)
+ raise Money::IncompatibleCurrencies unless numeric.kind_of? Numeric
+ Money.new @amount*numeric.to_d, @currency
+ end
+
+ def /(numeric)
+ raise Money::IncompatibleCurrencies unless numeric.kind_of? Numeric
+ Money.new @amount/numeric.to_d, @currency
+ end
+
+ def +(other)
+ raise ArgumentError unless @currency == other.currency
+ Money.new @amount+other.amount.to_d, @currency
+ end
+
+ def -(other)
+ raise ArgumentError unless @currency == other.currency
+ Money.new @amount-other.amount.to_d, @currency
+ end
+
+ def <=>(other)
+ @amount <=> other.amount
+ end
+end