Решение на Шеста задача от Явор Лилянов

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

Към профила на Явор Лилянов

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 46 успешни тест(а)
  • 1 неуспешни тест(а)

Код

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

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

...............F...............................

Failures:

  1) Money has a custom to_s representation
     Failure/Error: Money.new('12.1'.to_d, :USD).to_s.should eq '12.10 USD'
       
       expected: "12.10 USD"
            got: "12.1 USD"
       
       (compared using ==)
     # /tmp/d20130203-23049-cndei2/spec.rb:90:in `block (2 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.06425 seconds
47 examples, 1 failure

Failed examples:

rspec /tmp/d20130203-23049-cndei2/spec.rb:88 # Money has a custom to_s representation

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

Явор обнови решението на 15.01.2013 21:46 (преди над 11 години)

+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