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

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

Към профила на Теодор Илиев

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class CurrencyPair < Struct.new(:from, :to)
end
class Unknown < RuntimeError
end
def initialize
@rates = {}
end
def set(from_currency, to_currency, rate)
return if from_currency == to_currency
@rates[CurrencyPair.new(from_currency, to_currency)] = rate
@rates[CurrencyPair.new(to_currency, from_currency)] = BigDecimal.new(1) / rate
end
def get(from_currency, to_currency)
@rates[CurrencyPair.new(from_currency, to_currency)]
end
def convert(from_currency, to_currency, amount)
return amount if from_currency == to_currency
raise Unknown, 'Unknown' unless @rates.has_key? CurrencyPair.new(from_currency, to_currency)
amount * @rates[CurrencyPair.new(from_currency, to_currency)]
end
end
class Money
attr_reader :amount, :currency
include Comparable
class IncompatibleCurrencies < StandardError
end
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def in(currency, exchange_rate)
Money.new(exchange_rate.convert(@currency, currency, @amount), currency)
end
def +(other)
raise ArgumentError, 'Incompatible types' unless other.instance_of? Money
raise IncompatibleCurrencies, 'Currencies not matching' unless self.currency == other.currency
Money.new (self.amount + other.amount), currency
end
def -(other)
raise ArgumentError, 'Incompatible types' unless other.instance_of? Money
raise IncompatibleCurrencies, 'Currencies not matching' unless self.currency == other.currency
Money.new (self.amount - other.amount), currency
end
def *(other)
raise ArgumentError, 'Incompatible types' unless other.is_a? Numeric
Money.new (@amount * other), currency
end
def /(other)
raise ArgumentError, 'Incompatible types' unless other.is_a? Numeric
Money.new (@amount / other), currency
end
def <=>(other)
raise ArgumentError, 'Bad type' unless other.instance_of? Money
raise IncompatibleCurrencies, 'Currencies not matching' unless self.currency == other.currency
self.amount <=> other.amount
end
def to_s
"%.2f #@currency" % @amount.truncate(2).to_s('F')
end
end

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

.......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:938f848,'0.1E1',9(36)>
            got: nil
       
       (compared using ==)
     # /tmp/d20130203-23049-b3plsi/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:939359c,'0.1E1',9(36)>
            got: nil
       
       (compared using ==)
     # /tmp/d20130203-23049-b3plsi/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) Money comparison with == raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-b3plsi/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)>'

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

Failed examples:

rspec /tmp/d20130203-23049-b3plsi/spec.rb:44 # ExchangeRate#get always returns 1 as the exchange rate between two identical currencies
rspec /tmp/d20130203-23049-b3plsi/spec.rb:48 # ExchangeRate#get does not allow changing the exchange rate between two identical currencies
rspec /tmp/d20130203-23049-b3plsi/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-b3plsi/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ

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

Теодор обнови решението на 16.01.2013 04:07 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ class CurrencyPair < Struct.new(:from, :to)
+ end
+
+ class Unknown < RuntimeError
+ end
+
+ def initialize
+ @rates = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ return if from_currency == to_currency
+ @rates[CurrencyPair.new(from_currency, to_currency)] = rate
+ @rates[CurrencyPair.new(to_currency, from_currency)] = BigDecimal.new(1) / rate
+ end
+
+ def get(from_currency, to_currency)
+ @rates[CurrencyPair.new(from_currency, to_currency)]
+ end
+
+ def convert(from_currency, to_currency, amount)
+ return amount if from_currency == to_currency
+ raise Unknown, 'Unknown' unless @rates.has_key? CurrencyPair.new(from_currency, to_currency)
+ amount * @rates[CurrencyPair.new(from_currency, to_currency)]
+ end
+end
+
+class Money
+ attr_reader :amount, :currency
+
+ include Comparable
+
+ class IncompatibleCurrencies < StandardError
+ end
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def in(currency, exchange_rate)
+ Money.new(exchange_rate.convert(@currency, currency, @amount), currency)
+ end
+
+ def +(other)
+ raise ArgumentError, 'Incompatible types' unless other.instance_of? Money
+ raise IncompatibleCurrencies, 'Currencies not matching' unless self.currency == other.currency
+ Money.new (self.amount + other.amount), currency
+ end
+
+ def -(other)
+ raise ArgumentError, 'Incompatible types' unless other.instance_of? Money
+ raise IncompatibleCurrencies, 'Currencies not matching' unless self.currency == other.currency
+ Money.new (self.amount - other.amount), currency
+ end
+
+ def *(other)
+ raise ArgumentError, 'Incompatible types' unless other.is_a? Numeric
+ Money.new (@amount * other), currency
+ end
+
+ def /(other)
+ raise ArgumentError, 'Incompatible types' unless other.is_a? Numeric
+ Money.new (@amount / other), currency
+ end
+
+ def <=>(other)
+ raise ArgumentError, 'Bad type' unless other.instance_of? Money
+ raise IncompatibleCurrencies, 'Currencies not matching' unless self.currency == other.currency
+ self.amount <=> other.amount
+ end
+
+ def to_s
+ "%.2f #@currency" % @amount.truncate(2).to_s('F')
+ end
+end