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

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

Към профила на Йордан Стефанов

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
def initialize
@rates = {}
end
def set from_currency, to_currency, rate
@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 not @rates[from_currency]
return nil
end
@rates[from_currency][to_currency]
end
def convert from_currency, to_currency, amount
if not get( from_currency, to_currency )
return nil
end
amount * get( from_currency, to_currency )
end
end
class Money
include Comparable
attr_reader :amount, :currency
def initialize amount, currency
@amount = amount
@currency = currency
end
def to_s
"#{ amount.round( 2 ).to_s('F') } #{ currency.to_s }"
end
def in new_currency, exchange_rate
if not exchange_rate.get currency, new_currency
raise Unknown, "\"#{new_currency}\" is unknown currency"
end
self.class.new exchange_rate.convert( currency, new_currency, amount ), new_currency
end
def * amount
self.class.new self.amount * amount, currency
end
def / amount
self.class.new self.amount / amount, currency
end
def + money
if money.class != self.class
raise ArgumentError
end
if money.currency != self.currency
raise IncompatibleCurrencies
end
self.class.new self.amount + money.amount, currency
end
def - money
if money.class != self.class
raise ArgumentError
end
if money.currency != self.currency
raise IncompatibleCurrencies
end
self.class.new self.amount - money.amount, currency
end
def <=> money
if money.class != self.class
raise ArgumentError
end
if money.currency != self.currency
raise IncompatibleCurrencies
end
amount <=> money.amount
end
class Unknown < RuntimeError
end
class IncompatibleCurrencies < RuntimeError
end
end

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

.......FFF..F..F.FF.F.F........FF....FF........

Failures:

  1) 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:94c8bb0,'0.1E1',9(36)>
            got: nil
       
       (compared using ==)
     # /tmp/d20130203-23049-17auynz/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)>'

  2) 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:94cc79c,'0.1E1',9(36)>
            got: #<BigDecimal:94cc9f4,'0.5E0',9(36)>
       
       (compared using ==)
     # /tmp/d20130203-23049-17auynz/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)>'

  3) ExchangeRate#convert raises an ExchangeRate::Unknown exception when the rate is not defined
     Failure/Error: end.to raise_error(ExchangeRate::Unknown)
     NameError:
       uninitialized constant ExchangeRate::Unknown
     # /tmp/d20130203-23049-17auynz/spec.rb:58: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
       
       expected: #<BigDecimal:9558968,'0.123E3',9(36)>
            got: nil
       
       (compared using ==)
     # /tmp/d20130203-23049-17auynz/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 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-17auynz/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)>'

  6) 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
     Money::Unknown:
       "EUR" is unknown currency
     # /tmp/d20130203-23049-17auynz/solution.rb:47:in `in'
     # /tmp/d20130203-23049-17auynz/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)>'

  7) Money convertion raises an ExchangeRate::Unknown exception for unknown rates
     Failure/Error: end.to raise_error(ExchangeRate::Unknown)
     NameError:
       uninitialized constant ExchangeRate::Unknown
     # /tmp/d20130203-23049-17auynz/spec.rb:113: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)>'

  8) Money arithmetic * with money objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<TypeError: Money can't be coerced into BigDecimal>
     # /tmp/d20130203-23049-17auynz/spec.rb:129: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)>'

  9) Money arithmetic / with money objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<TypeError: Money can't be coerced into BigDecimal>
     # /tmp/d20130203-23049-17auynz/spec.rb:129: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)>'

  10) Money arithmetic * with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<TypeError: String can't be coerced into BigDecimal>
     # /tmp/d20130203-23049-17auynz/spec.rb:160: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)>'

  11) Money arithmetic / with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<TypeError: String can't be coerced into BigDecimal>
     # /tmp/d20130203-23049-17auynz/spec.rb:160: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)>'

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

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

Failed examples:

rspec /tmp/d20130203-23049-17auynz/spec.rb:44 # ExchangeRate#get always returns 1 as the exchange rate between two identical currencies
rspec /tmp/d20130203-23049-17auynz/spec.rb:48 # ExchangeRate#get does not allow changing the exchange rate between two identical currencies
rspec /tmp/d20130203-23049-17auynz/spec.rb:55 # ExchangeRate#convert raises an ExchangeRate::Unknown exception when the rate is not defined
rspec /tmp/d20130203-23049-17auynz/spec.rb:71 # ExchangeRate#convert works for identical currencies without defining any rates
rspec /tmp/d20130203-23049-17auynz/spec.rb:88 # Money has a custom to_s representation
rspec /tmp/d20130203-23049-17auynz/spec.rb:106 # Money convertion does not change the amount if the same currency is passed
rspec /tmp/d20130203-23049-17auynz/spec.rb:110 # Money convertion raises an ExchangeRate::Unknown exception for unknown rates
rspec /tmp/d20130203-23049-17auynz/spec.rb:128 # Money arithmetic * with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-17auynz/spec.rb:128 # Money arithmetic / with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-17auynz/spec.rb:159 # Money arithmetic * with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-17auynz/spec.rb:159 # Money arithmetic / with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-17auynz/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-17auynz/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ

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

Йордан обнови решението на 16.01.2013 16:19 (преди около 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ def initialize
+ @rates = {}
+ end
+
+ def set from_currency, to_currency, rate
+ @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 not @rates[from_currency]
+ return nil
+ end
+ @rates[from_currency][to_currency]
+ end
+
+ def convert from_currency, to_currency, amount
+ if not get( from_currency, to_currency )
+ return nil
+ end
+ amount * get( from_currency, to_currency )
+ end
+end
+
+class Money
+ include Comparable
+
+ attr_reader :amount, :currency
+
+ def initialize amount, currency
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s
+ "#{ amount.round( 2 ).to_s('F') } #{ currency.to_s }"
+ end
+
+ def in new_currency, exchange_rate
+ if not exchange_rate.get currency, new_currency
+ raise Unknown, "\"#{new_currency}\" is unknown currency"
+ end
+ self.class.new exchange_rate.convert( currency, new_currency, amount ), new_currency
+ end
+
+ def * amount
+ self.class.new self.amount * amount, currency
+ end
+
+ def / amount
+ self.class.new self.amount / amount, currency
+ end
+
+ def + money
+ if money.class != self.class
+ raise ArgumentError
+ end
+ if money.currency != self.currency
+ raise IncompatibleCurrencies
+ end
+ self.class.new self.amount + money.amount, currency
+ end
+
+ def - money
+ if money.class != self.class
+ raise ArgumentError
+ end
+ if money.currency != self.currency
+ raise IncompatibleCurrencies
+ end
+ self.class.new self.amount - money.amount, currency
+ end
+
+ def <=> money
+ if money.class != self.class
+ raise ArgumentError
+ end
+ if money.currency != self.currency
+ raise IncompatibleCurrencies
+ end
+ amount <=> money.amount
+ end
+
+ class Unknown < RuntimeError
+ end
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+end