Решение на Шеста задача от Орлин Христов

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

Към профила на Орлин Христов

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize
@rates = Hash.new
end
def set(from_currency, to_currency, rate)
if from_currency == to_currency
rate = 1
end
@rates[relation from_currency, to_currency] = rate
@rates[relation to_currency, from_currency] = 1/rate
end
def get(from_currency, to_currency)
@rates[relation from_currency, to_currency]
end
def convert(from_currency, to_currency, amount)
raise Unknown, "Unknown exchange rate" if !@rates.has_key? relation from_currency, to_currency
get(from_currency, to_currency) * amount
end
private
def relation(from_currency, to_currency)
(from_currency.to_s + '->' + to_currency.to_s).to_sym
end
end
class Money
include Comparable
class IncompatibleCurrencies < StandardError
end
attr_reader :amount, :currency
def initialize amount, currency
@amount = amount
@currency = currency
end
def to_s
"%.2f #{currency}" % amount
end
def in(exchange_currency, exchange_rate)
Money.new exchange_rate.convert(currency, exchange_currency, amount), exchange_currency
end
def *(multiplicator)
Money.new amount * multiplicator, currency
end
def /(denuminator)
Money.new amount denuminator, currency
end
def +(money)
validate_money money
Money.new self.amount + money.amount, currency
end
def -(money)
validate_money money
Money.new self.amount - money.amount, currency
end
def <=>(money)
validate_money money
self.amount <=> money.amount
end
private
def validate_money(money)
raise IncompatibleCurrencies if money.currency != self.currency
raise ArgumentError if !money.is_a? Money
end
end

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

.......F....F....F..FF.F..F..FFF...F.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:87ca954,'0.1E1',9(36)>
            got: nil
       
       (compared using ==)
     # /tmp/d20130203-23049-1bifk7e/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#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:
       Unknown exchange rate
     # /tmp/d20130203-23049-1bifk7e/solution.rb:25:in `convert'
     # /tmp/d20130203-23049-1bifk7e/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)>'

  3) 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:
       Unknown exchange rate
     # /tmp/d20130203-23049-1bifk7e/solution.rb:25:in `convert'
     # /tmp/d20130203-23049-1bifk7e/solution.rb:55:in `in'
     # /tmp/d20130203-23049-1bifk7e/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)>'

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

  5) Money arithmetic allows / with a numeric
     Failure/Error: result = bucks.public_send(operation, numeric)
     ArgumentError:
       wrong number of arguments (2 for 0)
     # /tmp/d20130203-23049-1bifk7e/solution.rb:63:in `/'
     # /tmp/d20130203-23049-1bifk7e/spec.rb:123:in `public_send'
     # /tmp/d20130203-23049-1bifk7e/spec.rb:123: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)>'

  6) 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-1bifk7e/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)>'

  7) 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-1bifk7e/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)>'

  8) 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-1bifk7e/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)>'

  9) 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-1bifk7e/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)>'

  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-1bifk7e/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 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-1bifk7e/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)>'

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

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

  15) 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-1bifk7e/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)>'

  16) 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-1bifk7e/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)>'

  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-1bifk7e/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.06204 seconds
47 examples, 17 failures

Failed examples:

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

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

Орлин обнови решението на 16.01.2013 16:17 (преди почти 12 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ class Unknown < RuntimeError
+ end
+
+ def initialize
+ @rates = Hash.new
+ end
+
+ def set(from_currency, to_currency, rate)
+ if from_currency == to_currency
+ rate = 1
+ end
+ @rates[relation from_currency, to_currency] = rate
+ @rates[relation to_currency, from_currency] = 1/rate
+ end
+
+ def get(from_currency, to_currency)
+ @rates[relation from_currency, to_currency]
+ end
+
+ def convert(from_currency, to_currency, amount)
+ raise Unknown, "Unknown exchange rate" if !@rates.has_key? relation from_currency, to_currency
+ get(from_currency, to_currency) * amount
+ end
+
+ private
+
+ def relation(from_currency, to_currency)
+ (from_currency.to_s + '->' + to_currency.to_s).to_sym
+ end
+end
+
+
+class Money
+ include Comparable
+
+ class IncompatibleCurrencies < StandardError
+ end
+
+ attr_reader :amount, :currency
+
+ def initialize amount, currency
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s
+ "%.2f #{currency}" % amount
+ end
+
+ def in(exchange_currency, exchange_rate)
+ Money.new exchange_rate.convert(currency, exchange_currency, amount), exchange_currency
+ end
+
+ def *(multiplicator)
+ Money.new amount * multiplicator, currency
+ end
+
+ def /(denuminator)
+ Money.new amount denuminator, currency
+ end
+
+ def +(money)
+ validate_money money
+ Money.new self.amount + money.amount, currency
+ end
+
+ def -(money)
+ validate_money money
+ Money.new self.amount - money.amount, currency
+ end
+
+ def <=>(money)
+ validate_money money
+ self.amount <=> money.amount
+ end
+
+ private
+
+ def validate_money(money)
+ raise IncompatibleCurrencies if money.currency != self.currency
+ raise ArgumentError if !money.is_a? Money
+ end
+end