Решение на Шеста задача от Александър Иванов

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

Към профила на Александър Иванов

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 40 успешни тест(а)
  • 7 неуспешни тест(а)

Код

require "bigdecimal"
require "bigdecimal/util"
class ExchangeRate
class Unknown < RuntimeError
end
def initialize
@rates = {}
end
def set(from, to, rate)
@rates[rate_key_for(from, to)] = rate
end
def get(from, to)
if @rates[rate_key_for from, to]
@rates[rate_key_for(from, to)]
elsif @rates[rate_key_for to, from]
1 / @rates[rate_key_for(to, from)]
else
nil
end
end
def convert(from, to, amount)
if get from, to
get(from, to) * amount
else
raise Unknown
end
end
private
def rate_key_for(from, to)
"#{from}_TO_#{to}"
end
end
class Money
class IncompatibleCurrencies < RuntimeError
end
include Comparable
attr_reader :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
"#{'%.2f' % @amount} #{@currency}"
end
def in(to_currency, exchange_rate)
converted_amount = exchange_rate.convert(@currency, to_currency, @amount)
self.class.new converted_amount, to_currency
end
def *(times)
raise ArgumentError unless times.is_a? Numeric
self.class.new @amount * times, @currency
end
def /(times)
raise ArgumentError unless times.is_a? Numeric
self.class.new @amount / times, @currency
end
def -(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies unless self.currency == other.currency
self.class.new self.amount - other.amount, self.currency
end
def +(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies unless self.currency == other.currency
self.class.new self.amount + other.amount, self.currency
end
def <=>(other)
raise ArgumentError unless other.is_a? Money
raise IncompatibleCurrencies unless self.currency == other.currency
self.amount <=> other.amount
end
end

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

...F...FF...F....F...................FF........

Failures:

  1) ExchangeRate#set sets the exchange rate in both directions
     Failure/Error: rate.get(:EUR, :BGN).should eq '1.25'.to_d
       
       expected: #<BigDecimal:a54ef70,'0.125E1',18(18)>
            got: #<BigDecimal:a781360,'0.2E1',9(36)>
       
       (compared using ==)
     # /tmp/d20130203-23049-1r8hlb6/spec.rb:25:in `block (3 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) ExchangeRate#get always returns 1 as the exchange rate between two identical currencies
     Failure/Error: rate.get(:JPY, :JPY).should eq 1.to_d
       
       expected: #<BigDecimal:9e68d28,'0.1E1',9(36)>
            got: nil
       
       (compared using ==)
     # /tmp/d20130203-23049-1r8hlb6/spec.rb:45:in `block (3 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)>'

  3) ExchangeRate#get does not allow changing the exchange rate between two identical currencies
     Failure/Error: rate.get(:EUR, :EUR).should eq 1.to_d
       
       expected: #<BigDecimal:9e6bc80,'0.1E1',9(36)>
            got: #<BigDecimal:9e6c374,'0.2E1',9(36)>
       
       (compared using ==)
     # /tmp/d20130203-23049-1r8hlb6/spec.rb:50:in `block (3 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)>'

  4) ExchangeRate#convert works for identical currencies without defining any rates
     Failure/Error: rate.convert(:JPY, :JPY, 123.to_d).should eq 123.to_d
     ExchangeRate::Unknown:
       ExchangeRate::Unknown
     # /tmp/d20130203-23049-1r8hlb6/solution.rb:30:in `convert'
     # /tmp/d20130203-23049-1r8hlb6/spec.rb:72:in `block (3 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)>'

  5) Money convertion does not change the amount if the same currency is passed
     Failure/Error: Money.new(5.to_d, :EUR).in(:EUR, ExchangeRate.new).amount.should eq 5.to_d
     ExchangeRate::Unknown:
       ExchangeRate::Unknown
     # /tmp/d20130203-23049-1r8hlb6/solution.rb:30:in `convert'
     # /tmp/d20130203-23049-1r8hlb6/solution.rb:59:in `in'
     # /tmp/d20130203-23049-1r8hlb6/spec.rb:107:in `block (3 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)>'

  6) Money comparison with == raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-1r8hlb6/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)>'

  7) Money comparison with == raises IncompatibleCurrencies when currencies differ
     Failure/Error: expect do
       expected Money::IncompatibleCurrencies but nothing was raised
     # /tmp/d20130203-23049-1r8hlb6/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.05772 seconds
47 examples, 7 failures

Failed examples:

rspec /tmp/d20130203-23049-1r8hlb6/spec.rb:21 # ExchangeRate#set sets the exchange rate in both directions
rspec /tmp/d20130203-23049-1r8hlb6/spec.rb:44 # ExchangeRate#get always returns 1 as the exchange rate between two identical currencies
rspec /tmp/d20130203-23049-1r8hlb6/spec.rb:48 # ExchangeRate#get does not allow changing the exchange rate between two identical currencies
rspec /tmp/d20130203-23049-1r8hlb6/spec.rb:71 # ExchangeRate#convert works for identical currencies without defining any rates
rspec /tmp/d20130203-23049-1r8hlb6/spec.rb:106 # Money convertion does not change the amount if the same currency is passed
rspec /tmp/d20130203-23049-1r8hlb6/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-1r8hlb6/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ

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

Александър обнови решението на 16.01.2013 08:10 (преди над 11 години)

+require "bigdecimal"
+require "bigdecimal/util"
+
+class ExchangeRate
+ class Unknown < RuntimeError
+ end
+
+ def initialize
+ @rates = {}
+ end
+
+ def set(from, to, rate)
+ @rates[rate_key_for(from, to)] = rate
+ end
+
+ def get(from, to)
+ if @rates[rate_key_for from, to]
+ @rates[rate_key_for(from, to)]
+ elsif @rates[rate_key_for to, from]
+ 1 / @rates[rate_key_for(to, from)]
+ else
+ nil
+ end
+ end
+
+ def convert(from, to, amount)
+ if get from, to
+ get(from, to) * amount
+ else
+ raise Unknown
+ end
+ end
+
+ private
+
+ def rate_key_for(from, to)
+ "#{from}_TO_#{to}"
+ end
+end
+
+class Money
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ include Comparable
+
+ attr_reader :amount, :currency
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s
+ "#{'%.2f' % @amount} #{@currency}"
+ end
+
+ def in(to_currency, exchange_rate)
+ converted_amount = exchange_rate.convert(@currency, to_currency, @amount)
+ self.class.new converted_amount, to_currency
+ end
+
+ def *(times)
+ raise ArgumentError unless times.is_a? Numeric
+ self.class.new @amount * times, @currency
+ end
+
+ def /(times)
+ raise ArgumentError unless times.is_a? Numeric
+ self.class.new @amount / times, @currency
+ end
+
+ def -(other)
+ raise ArgumentError unless other.is_a? Money
+ raise IncompatibleCurrencies unless self.currency == other.currency
+
+ self.class.new self.amount - other.amount, self.currency
+ end
+
+ def +(other)
+ raise ArgumentError unless other.is_a? Money
+ raise IncompatibleCurrencies unless self.currency == other.currency
+
+ self.class.new self.amount + other.amount, self.currency
+ end
+
+ def <=>(other)
+ raise ArgumentError unless other.is_a? Money
+ raise IncompatibleCurrencies unless self.currency == other.currency
+
+ self.amount <=> other.amount
+ end
+end