Решение на Шеста задача от Светлин Николов

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

Към профила на Светлин Николов

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 24 успешни тест(а)
  • 23 неуспешни тест(а)

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
attr_accessor :currencies_to_rate_hash
class Unknown < RuntimeError
end
def initialize
@currencies_to_rate_hash = {}
end
def set(from_currency, to_currency, rate)
@currencies_to_rate_hash["#{from_currency}->#{to_currency}"] = rate
@currencies_to_rate_hash["#{to_currency}->#{from_currency}"] = 1 / rate
end
def get(from_currency, to_currency)
@currencies_to_rate_hash["#{from_currency}->#{to_currency}"]
end
def convert(from_currency, to_currency, amount)
key_exist = @currencies_to_rate_hash.has_key? "#{from_currency}->#{to_currency}"
raise Unknown unless key_exist
get(from_currency, to_currency) * amount.floor(2)
end
end
class Money
include Comparable
attr_accessor :amount, :currency
class IncompatibleCurrencies < RuntimeError
end
def initialize(amount, currency)
@amount = amount.floor(2)
@currency = currency
end
def amount
@amount
end
def currency
@currency
end
def to_s
self.amount.to_s("F") + ' ' + self.currency.to_s
end
def in(currency, exchange_rate)
Money.new exchange_rate.convert(self.currency, currency, self.amount), currency
end
def *(other)
Money.new(self.amount * other, self.currency)
end
def /(other)
Money.new(self.amount / other, self.currency)
end
def +(other)
raise IncompatibleCurrencies unless self.currency.eql? other.currency
raise ArgumentError unless other.is_a? Money
Money.new(self.amount + other.amount, self.currency)
end
def -(other)
raise IncompatibleCurrencies unless self.currency.eql? other.currency
raise ArgumentError unless other.is_a? Money
Money.new(self.amount - other.amount, self.currency)
end
def <=>(other)
raise IncompatibleCurrencies unless self.currency.eql? other.currency
raise ArgumentError unless other.is_a? Money
self.amount <=> other.amount
end
end

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

.......FF...F..F.F..FFFF..F..FFFFFFF.FFF.F.F.F.

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:999b534,'0.1E1',9(36)>
            got: nil
       
       (compared using ==)
     # /tmp/d20130203-23049-f3fcwk/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:999e2c0,'0.1E1',9(36)>
            got: #<BigDecimal:999e4b4,'0.5E0',9(36)>
       
       (compared using ==)
     # /tmp/d20130203-23049-f3fcwk/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 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-f3fcwk/solution.rb:24:in `convert'
     # /tmp/d20130203-23049-f3fcwk/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)>'

  4) 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-f3fcwk/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)>'

  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-f3fcwk/solution.rb:24:in `convert'
     # /tmp/d20130203-23049-f3fcwk/solution.rb:54:in `in'
     # /tmp/d20130203-23049-f3fcwk/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 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-f3fcwk/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)>'

  7) Money arithmetic allows / with a numeric
     Failure/Error: result.amount.should eq bucks.amount.public_send(operation, numeric)
       
       expected: #<BigDecimal:9d8fe60,'0.1190476190 47619048E0',18(36)>
            got: #<BigDecimal:9d8ad98,'0.11E0',9(36)>
       
       (compared using ==)
     # /tmp/d20130203-23049-f3fcwk/spec.rb:125: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)>'

  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-f3fcwk/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 numeric objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for 42:Fixnum>
     # /tmp/d20130203-23049-f3fcwk/spec.rb:137: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 numeric objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for 42:Fixnum>
     # /tmp/d20130203-23049-f3fcwk/spec.rb:137: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 #<NoMethodError: undefined method `currency' for "foobar":String>
     # /tmp/d20130203-23049-f3fcwk/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 arithmetic - with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for "foobar":String>
     # /tmp/d20130203-23049-f3fcwk/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)>'

  13) 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-f3fcwk/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)>'

  14) 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-f3fcwk/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)>'

  15) Money comparison works when currencies are the same
     Failure/Error: (a < b).should be_true
       expected: true value
            got: false
     # /tmp/d20130203-23049-f3fcwk/spec.rb:181: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)>'

  16) Money comparison works for equality when currencies are the same
     Failure/Error: (Money.new('12.45'.to_d, :BGN) == Money.new('12.451'.to_d, :BGN)).should be_false
       expected: false value
            got: true
     # /tmp/d20130203-23049-f3fcwk/spec.rb:190: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)>'

  17) Money comparison with <=> raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for :larodi:Symbol>
     # /tmp/d20130203-23049-f3fcwk/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)>'

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

  19) Money comparison with == raises IncompatibleCurrencies when currencies differ
     Failure/Error: expect do
       expected Money::IncompatibleCurrencies but nothing was raised
     # /tmp/d20130203-23049-f3fcwk/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)>'

  20) Money comparison with < raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for :larodi:Symbol>
     # /tmp/d20130203-23049-f3fcwk/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)>'

  21) Money comparison with <= raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for :larodi:Symbol>
     # /tmp/d20130203-23049-f3fcwk/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)>'

  22) Money comparison with > raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for :larodi:Symbol>
     # /tmp/d20130203-23049-f3fcwk/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)>'

  23) Money comparison with >= raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for :larodi:Symbol>
     # /tmp/d20130203-23049-f3fcwk/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)>'

Finished in 0.06407 seconds
47 examples, 23 failures

Failed examples:

rspec /tmp/d20130203-23049-f3fcwk/spec.rb:44 # ExchangeRate#get always returns 1 as the exchange rate between two identical currencies
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:48 # ExchangeRate#get does not allow changing the exchange rate between two identical currencies
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:71 # ExchangeRate#convert works for identical currencies without defining any rates
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:88 # Money has a custom to_s representation
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:106 # Money convertion does not change the amount if the same currency is passed
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:128 # Money arithmetic * with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:119 # Money arithmetic allows / with a numeric
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:128 # Money arithmetic / with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:136 # Money arithmetic + with numeric objects raises an ArgumentError
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:136 # Money arithmetic - with numeric objects raises an ArgumentError
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:159 # Money arithmetic + with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:159 # Money arithmetic - with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:159 # Money arithmetic * with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:159 # Money arithmetic / with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:168 # Money comparison works when currencies are the same
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:188 # Money comparison works for equality when currencies are the same
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:194 # Money comparison with <=> raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:194 # Money comparison with < raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:194 # Money comparison with <= raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:194 # Money comparison with > raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-f3fcwk/spec.rb:194 # Money comparison with >= raises ArgumentError when comparing with other objects

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

Светлин обнови решението на 14.01.2013 01:15 (преди около 12 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+class ExchangeRate
+ attr_accessor :currencies_to_rate_hash
+
+ class Unknown < RuntimeError
+ end
+
+ def initialize
+ @currencies_to_rate_hash = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ @currencies_to_rate_hash["#{from_currency}->#{to_currency}"] = rate
+ @currencies_to_rate_hash["#{to_currency}->#{from_currency}"] = 1 / rate
+ end
+
+ def get(from_currency, to_currency)
+ @currencies_to_rate_hash["#{from_currency}->#{to_currency}"]
+ end
+
+ def convert(from_currency, to_currency, amount)
+ key_exist = @currencies_to_rate_hash.has_key? "#{from_currency}->#{to_currency}"
+ raise Unknown unless key_exist
+ get(from_currency, to_currency) * amount
+ end
+end
+
+class Money
+ include Comparable
+ attr_accessor :amount, :currency
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def amount
+ @amount
+ end
+
+ def currency
+ @currency
+ end
+
+ def to_s
+ self.amount.to_s("F") + ' ' + self.currency.to_s
+ end
+
+ def in(currency, exchange_rate)
+ Money.new exchange_rate.convert(self.currency, currency, self.amount), currency
+ end
+
+ def *(other)
+ Money.new(self.amount * other, self.currency)
+ end
+
+ def /(other)
+ Money.new(self.amount / other, self.currency)
+ end
+
+ def +(other)
+ raise IncompatibleCurrencies unless self.currency.eql? other.currency
+ raise ArgumentError unless other.is_a? Money
+ Money.new(self.amount + other.amount, self.currency)
+ end
+
+ def -(other)
+ raise IncompatibleCurrencies unless self.currency.eql? other.currency
+ raise ArgumentError unless other.is_a? Money
+ Money.new(self.amount - other.amount, self.currency)
+ end
+
+ def <=>(other)
+ raise IncompatibleCurrencies unless self.currency.eql? other.currency
+ raise ArgumentError unless other.is_a? Money
+ self.amount <=> other.amount
+ end
+
+end

Светлин обнови решението на 15.01.2013 00:29 (преди около 12 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
attr_accessor :currencies_to_rate_hash
class Unknown < RuntimeError
end
def initialize
@currencies_to_rate_hash = {}
end
def set(from_currency, to_currency, rate)
@currencies_to_rate_hash["#{from_currency}->#{to_currency}"] = rate
@currencies_to_rate_hash["#{to_currency}->#{from_currency}"] = 1 / rate
end
def get(from_currency, to_currency)
@currencies_to_rate_hash["#{from_currency}->#{to_currency}"]
end
def convert(from_currency, to_currency, amount)
key_exist = @currencies_to_rate_hash.has_key? "#{from_currency}->#{to_currency}"
raise Unknown unless key_exist
- get(from_currency, to_currency) * amount
+ get(from_currency, to_currency) * amount.floor(2)
end
end
class Money
include Comparable
attr_accessor :amount, :currency
class IncompatibleCurrencies < RuntimeError
end
def initialize(amount, currency)
- @amount = amount
+ @amount = amount.floor(2)
@currency = currency
end
def amount
@amount
end
def currency
@currency
end
def to_s
self.amount.to_s("F") + ' ' + self.currency.to_s
end
def in(currency, exchange_rate)
Money.new exchange_rate.convert(self.currency, currency, self.amount), currency
end
def *(other)
Money.new(self.amount * other, self.currency)
end
def /(other)
Money.new(self.amount / other, self.currency)
end
def +(other)
raise IncompatibleCurrencies unless self.currency.eql? other.currency
raise ArgumentError unless other.is_a? Money
Money.new(self.amount + other.amount, self.currency)
end
def -(other)
raise IncompatibleCurrencies unless self.currency.eql? other.currency
raise ArgumentError unless other.is_a? Money
Money.new(self.amount - other.amount, self.currency)
end
def <=>(other)
raise IncompatibleCurrencies unless self.currency.eql? other.currency
raise ArgumentError unless other.is_a? Money
self.amount <=> other.amount
end
end