Решение на Шеста задача от Пламен Тотев

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

Към профила на Пламен Тотев

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize
@rates = {}
end
def set(from_currency, to_currency, rate)
if from_currency == to_currency then return end
@rates[from_currency] ||= {}
@rates[from_currency][to_currency] = rate
@rates[to_currency] ||= {}
@rates[to_currency][from_currency] = 1 / rate
end
def get(from_currency, to_currency)
if from_currency == to_currency then return 1.to_d end
from_rates = @rates[from_currency]
return nil if !from_rates
from_rates[to_currency]
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
raise Unknown if !rate
rate * amount
end
end
class Money
include Comparable
attr_reader :amount, :currency
class IncompatibleCurrencies < RuntimeError
end
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def in(currency, exchange_rate)
new_amount = exchange_rate.convert @currency, currency, @amount
Money.new new_amount, currency
end
def *(number)
if !number.is_a? Numeric then raise ArgumentError end
Money.new @amount * number, @currency
end
def /(number)
if !number.is_a? Numeric then raise ArgumentError end
Money.new @amount / number, @currency
end
def +(other)
if !other.is_a? Money then raise ArgumentError end
if @currency != other.currency then raise IncompatibleCurrencies end
Money.new @amount + other.amount, @currency
end
def -(other)
if !other.is_a? Money then raise ArgumentError end
if @currency != other.currency then raise IncompatibleCurrencies end
Money.new @amount - other.amount, @currency
end
def <=>(other)
if !other.is_a? Money then raise ArgumentError end
if @currency != other.currency then raise IncompatibleCurrencies end
@amount <=> other.amount
end
def to_s
"%.2f %s" % [@amount, @currency]
end
end

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

.....................................FF........

Failures:

  1) Money comparison with == raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-jbv4ym/spec.rb:195:in `block (4 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)>'

  2) Money comparison with == raises IncompatibleCurrencies when currencies differ
     Failure/Error: expect do
       expected Money::IncompatibleCurrencies but nothing was raised
     # /tmp/d20130203-23049-jbv4ym/spec.rb:201:in `block (4 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.05756 seconds
47 examples, 2 failures

Failed examples:

rspec /tmp/d20130203-23049-jbv4ym/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-jbv4ym/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ

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

Пламен обнови решението на 16.01.2013 00:34 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ class Unknown < RuntimeError
+ end
+
+ def initialize
+ @rates = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ if from_currency == to_currency then return end
+
+ @rates[from_currency] ||= {}
+ @rates[from_currency][to_currency] = rate
+ @rates[to_currency] ||= {}
+ @rates[to_currency][from_currency] = 1 / rate
+ end
+
+ def get(from_currency, to_currency)
+ if from_currency == to_currency then return 1.to_d end
+
+ from_rates = @rates[from_currency]
+ return nil if !from_rates
+
+ from_rates[to_currency]
+ end
+
+ def convert(from_currency, to_currency, amount)
+ rate = get(from_currency, to_currency)
+ raise Unknown if !rate
+
+ rate * amount
+ end
+end
+
+class Money
+ include Comparable
+ attr_reader :amount, :currency
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def in(currency, exchange_rate)
+ new_amount = exchange_rate.convert @currency, currency, @amount
+
+ Money.new new_amount, currency
+ end
+
+ def *(number)
+ if !number.is_a? Numeric then raise ArgumentError end
+
+ Money.new @amount * number, @currency
+ end
+
+ def /(number)
+ if !number.is_a? Numeric then raise ArgumentError end
+
+ Money.new @amount / number, @currency
+ end
+
+ def +(other)
+ if !other.is_a? Money then raise ArgumentError end
+ if @currency != other.currency then raise IncompatibleCurrencies end
+
+ Money.new @amount + other.amount, @currency
+ end
+
+ def -(other)
+ if !other.is_a? Money then raise ArgumentError end
+ if @currency != other.currency then raise IncompatibleCurrencies end
+
+ Money.new @amount - other.amount, @currency
+ end
+
+ def <=>(other)
+ if !other.is_a? Money then raise ArgumentError end
+ if @currency != other.currency then raise IncompatibleCurrencies end
+
+ @amount <=> other.amount
+ end
+
+ def to_s
+ "%.2f %s" % [@amount, @currency]
+ end
+end