Решение на Шеста задача от Георги Пачов

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

Към профила на Георги Пачов

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
def initialize
@rates = {}
end
def convert(currency_1, currency_2, amount)
rate = get(currency_1, currency_2)
raise ExchangeRate::Unknown if rate == nil
rate * amount
end
def set(currency_1, currency_2, rate)
@rates[to_hash_key(currency_1, currency_2)] = rate
@rates[to_hash_key(currency_2, currency_1)] = 1/rate
end
def get(currency_1, currency_2)
return 1 if currency_1 == currency_2
@rates[to_hash_key(currency_1, currency_2)]
end
def to_hash_key (currency_1, currency_2)
currency_1.to_s + currency_2.to_s
end
def exception(message)
Unknown.new message
end
class Unknown < RuntimeError
end
end
class Money
include Comparable
attr_accessor :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
format("%.2f", @amount) + " " + @currency.to_s
end
def in(currency,exchange_rate)
Money.new(exchange_rate.convert(@currency,currency, @amount), currency)
end
def *(numeric)
Money.new(@amount*numeric, @currency)
end
def /(numeric)
Money.new(@amount/numeric, @currency)
end
def +(other)
check_argument(other)
raise Money::IncompatibleCurrencies unless other.currency == @currency
Money.new(@amount + other.amount, @currency)
end
def -(other)
check_argument(other)
raise Money::IncompatibleCurrencies unless other.currency == @currency
Money.new(@amount - other.amount, @currency)
end
def <=>(other)
check_argument(other)
raise Money::IncompatibleCurrencies unless other.currency == @currency
(self-other).amount
end
class IncompatibleCurrencies < RuntimeError
end
def check_argument(money)
raise ArgumentError unless money.kind_of? Money
end
end

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

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

Failures:

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

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

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

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

  5) Money comparison works when currencies are the same
     Failure/Error: (a <=> b).should eq 1
       
       expected: 1
            got: #<BigDecimal:9da344c,'0.149E2',18(36)>
       
       (compared using ==)
     # /tmp/d20130203-23049-1i3vuie/spec.rb:174: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-1i3vuie/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-1i3vuie/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.07639 seconds
47 examples, 7 failures

Failed examples:

rspec /tmp/d20130203-23049-1i3vuie/spec.rb:128 # Money arithmetic * with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-1i3vuie/spec.rb:128 # Money arithmetic / with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-1i3vuie/spec.rb:159 # Money arithmetic * with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1i3vuie/spec.rb:159 # Money arithmetic / with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1i3vuie/spec.rb:168 # Money comparison works when currencies are the same
rspec /tmp/d20130203-23049-1i3vuie/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-1i3vuie/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ

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

Георги обнови решението на 15.01.2013 01:10 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ def initialize
+ @rates = {}
+ end
+
+ def convert(currency_1, currency_2, amount)
+ rate = get(currency_1, currency_2)
+ raise ExchangeRate::Unknown if rate == nil
+ rate * amount
+ end
+
+ def set(currency_1, currency_2, rate)
+ @rates[to_hash_key(currency_1, currency_2)] = rate
+ @rates[to_hash_key(currency_2, currency_1)] = 1/rate
+ end
+
+ def get(currency_1, currency_2)
+ return 1 if currency_1 == currency_2
+ @rates[to_hash_key(currency_1, currency_2)]
+ end
+
+ def to_hash_key (currency_1, currency_2)
+ currency_1.to_s + currency_2.to_s
+ end
+
+ def exception(message)
+ Unknown.new message
+ end
+
+ class Unknown < RuntimeError
+ end
+
+end
+
+
+class Money
+ include Comparable
+ attr_accessor :amount, :currency
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s
+ format("%.2f", @amount) + " " + @currency.to_s
+ end
+
+ def in(currency,exchange_rate)
+ Money.new(exchange_rate.convert(@currency,currency, @amount), currency)
+ end
+
+ def *(numeric)
+ Money.new(@amount*numeric, @currency)
+ end
+
+ def /(numeric)
+ Money.new(@amount/numeric, @currency)
+ end
+
+ def +(other)
+ check_argument(other)
+ raise Money::IncompatibleCurrencies unless other.currency == @currency
+ Money.new(@amount + other.amount, @currency)
+ end
+
+ def -(other)
+ check_argument(other)
+ raise Money::IncompatibleCurrencies unless other.currency == @currency
+ Money.new(@amount - other.amount, @currency)
+ end
+
+ def <=>(other)
+ check_argument(other)
+ raise Money::IncompatibleCurrencies unless other.currency == @currency
+ (self-other).amount
+ end
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ def check_argument(money)
+ raise ArgumentError unless money.kind_of? Money
+ end
+
+end
+
+