Решение на Шеста задача от Илия Ячев

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

Към профила на Илия Ячев

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
def initialize
@rates = {}
end
def set(from_currency, to_currency, rate)
@rates[from_currency: to_currency] = rate
@rates[to_currency: from_currency] = 1 / rate
@rates[from_currency: from_currency] = BigDecimal.new '1'
@rates[to_currency: to_currency] = BigDecimal.new '1'
end
def get(from_currency, to_currency)
@rates[from_currency: to_currency]
end
class Unknown < RuntimeError
end
def convert(from_currency, to_currency, amount)
raise Unknown if @rates[from_currency: to_currency].nil?
amount * @rates[from_currency: to_currency]
end
end
class Money
include Comparable
attr_reader :amount, :currency
def initialize(amount,currency)
@amount, @currency = amount, currency
end
def to_s
amount.floor(2).to_s('F') + " "+ currency.to_s
end
def in(new_currency, exchange_rate)
Money.new exchange_rate.convert(currency, new_currency, amount), new_currency
end
def *(numeric_object)
Money.new amount * numeric_object, currency
end
def /(numeric_object)
Money.new amount / numeric_object, currency
end
class IncompatibleCurrencies < ArgumentError
end
def +(other)
raise IncompatibleCurrencies if currency != other.currency
Money.new amount + other.amount, currency
end
def -(other)
raise IncompatibleCurrencies if currency != other.currency
Money.new amount - other.amount, currency
end
def <=>(other)
raise ArgumentError if !(other.is_a?(Money))
raise IncompatibleCurrencies if currency != other.currency
amount <=> other.amount
end
end

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

...F..FF...FF..F.F..F.FF..F..FFFF....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:93d1d38,'0.125E1',18(18)>
            got: #<BigDecimal:93d1dd8,'0.1E1',9(18)>
       
       (compared using ==)
     # /tmp/d20130203-23049-1gis6o/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 returns the reversed rate between A and B, too
     Failure/Error: rate.get(:BGN, :EUR).should eq '0.5'.to_d
       
       expected: #<BigDecimal:8ce57cc,'0.5E0',9(18)>
            got: #<BigDecimal:8ce5de4,'0.1E1',9(18)>
       
       (compared using ==)
     # /tmp/d20130203-23049-1gis6o/spec.rb:41: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 always returns 1 as the exchange rate between two identical currencies
     Failure/Error: rate.get(:JPY, :JPY).should eq 1.to_d
       
       expected: #<BigDecimal:8cef484,'0.1E1',9(36)>
            got: nil
       
       (compared using ==)
     # /tmp/d20130203-23049-1gis6o/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)>'

  4) ExchangeRate#convert converts from B to A using an existing rate A -> B
     Failure/Error: rate.convert(:BGN, :EUR, 100.to_d).should eq 50.to_d
       
       expected: #<BigDecimal:9020dec,'0.5E2',9(36)>
            got: #<BigDecimal:9020e64,'0.1E3',9(36)>
       
       (compared using ==)
     # /tmp/d20130203-23049-1gis6o/spec.rb:68: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) 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-1gis6o/solution.rb:24:in `convert'
     # /tmp/d20130203-23049-1gis6o/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)>'

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

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

  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-1gis6o/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-1gis6o/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 numeric objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for 42:Fixnum>
     # /tmp/d20130203-23049-1gis6o/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 numeric objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for 42:Fixnum>
     # /tmp/d20130203-23049-1gis6o/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)>'

  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-1gis6o/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 #<NoMethodError: undefined method `currency' for "foobar":String>
     # /tmp/d20130203-23049-1gis6o/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-1gis6o/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 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-1gis6o/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)>'

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

Failed examples:

rspec /tmp/d20130203-23049-1gis6o/spec.rb:21 # ExchangeRate#set sets the exchange rate in both directions
rspec /tmp/d20130203-23049-1gis6o/spec.rb:39 # ExchangeRate#get returns the reversed rate between A and B, too
rspec /tmp/d20130203-23049-1gis6o/spec.rb:44 # ExchangeRate#get always returns 1 as the exchange rate between two identical currencies
rspec /tmp/d20130203-23049-1gis6o/spec.rb:66 # ExchangeRate#convert converts from B to A using an existing rate A -> B
rspec /tmp/d20130203-23049-1gis6o/spec.rb:71 # ExchangeRate#convert works for identical currencies without defining any rates
rspec /tmp/d20130203-23049-1gis6o/spec.rb:88 # Money has a custom to_s representation
rspec /tmp/d20130203-23049-1gis6o/spec.rb:106 # Money convertion does not change the amount if the same currency is passed
rspec /tmp/d20130203-23049-1gis6o/spec.rb:128 # Money arithmetic * with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-1gis6o/spec.rb:128 # Money arithmetic / with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-1gis6o/spec.rb:136 # Money arithmetic + with numeric objects raises an ArgumentError
rspec /tmp/d20130203-23049-1gis6o/spec.rb:136 # Money arithmetic - with numeric objects raises an ArgumentError
rspec /tmp/d20130203-23049-1gis6o/spec.rb:159 # Money arithmetic + with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1gis6o/spec.rb:159 # Money arithmetic - with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1gis6o/spec.rb:159 # Money arithmetic * with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1gis6o/spec.rb:159 # Money arithmetic / with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1gis6o/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-1gis6o/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ

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

Илия обнови решението на 16.01.2013 03:30 (преди почти 12 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ def initialize
+ @rates = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ @rates[from_currency: to_currency] = rate
+ @rates[to_currency: from_currency] = 1 / rate
+ @rates[from_currency: from_currency] = BigDecimal.new '1'
+ @rates[to_currency: to_currency] = BigDecimal.new '1'
+ end
+
+ def get(from_currency, to_currency)
+ @rates[from_currency: to_currency]
+ end
+
+ class Unknown < RuntimeError
+ end
+
+ def convert(from_currency, to_currency, amount)
+ raise Unknown if @rates[from_currency: to_currency].nil?
+ amount * @rates[from_currency: to_currency]
+ end
+end
+
+class Money
+ include Comparable
+ attr_reader :amount, :currency
+ def initialize(amount,currency)
+ @amount, @currency = amount, currency
+ end
+
+ def to_s
+ amount.floor(2).to_s('F') + " "+ currency.to_s
+ end
+
+ def in(new_currency, exchange_rate)
+ Money.new exchange_rate.convert(currency, new_currency, amount), new_currency
+ end
+
+ def *(numeric_object)
+ Money.new amount * numeric_object, currency
+ end
+
+ def /(numeric_object)
+ Money.new amount / numeric_object, currency
+ end
+
+ class IncompatibleCurrencies < ArgumentError
+ end
+
+ def +(other)
+ raise IncompatibleCurrencies if currency != other.currency
+ Money.new amount + other.amount, currency
+ end
+
+ def -(other)
+ raise IncompatibleCurrencies if currency != other.currency
+ Money.new amount - other.amount, currency
+ end
+
+ def <=>(other)
+ raise ArgumentError if !(other.is_a?(Money))
+ raise IncompatibleCurrencies if currency != other.currency
+ amount <=> other.amount
+ end
+end