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

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

Към профила на Илиан Маджаров

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize
@rates = {}
end
def set(from_currency, to_currency, rate)
return if from_currency == to_currency
@rates[from_currency.to_s + to_currency.to_s] = rate
@rates[to_currency.to_s + from_currency.to_s] = 1 / rate
end
def get(from_currency, to_currency)
return '1'.to_d if from_currency == to_currency
@rates[from_currency.to_s + to_currency.to_s]
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
raise ExchangeRate::Unknown if rate == nil
rate * amount
end
end
class Money
include Comparable
class IncompatibleCurrencies < RuntimeError
end
attr_reader :amount, :currency
def initialize(amount, currency)
#self.@amount, self.@currency = amount, currency
@amount, @currency = amount, currency
end
def to_s
@amount.round(2).to_s + ' ' + @currency.to_s
end
def in(currency, exchange_rate)
Money.new(exchange_rate.convert(@currency, currency, @amount), currency)
end
def *(multiplier)
raise ArgumentError, 'Invalid parameter type!' unless multiplier.kind_of? Numeric
Money.new @amount * multiplier, @currency
end
def /(divisor)
raise ArgumentError, 'Invalid parameter type!' unless divisor.kind_of? Numeric
Money.new @amount / divisor, @currency
self
end
def +(other)
raise ArgumentError, 'Invalid parameter type!' unless other.kind_of? Money
raise Money::IncompatibleCurrencies unless @currency == other.currency
Money.new @amount + other.amount, @currency
end
def -(other)
raise ArgumentError, 'Invalid parameter type!' unless other.kind_of? Money
raise Money::IncompatibleCurrencies unless @currency == other.currency
Money.new @amount - other.amount, @currency
end
def <=>(other)
raise ArgumentError, 'Invalid parameter type!' unless other.kind_of? Money
raise Money::IncompatibleCurrencies unless @currency == other.currency
@amount <=> other.amount
end
end

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

...............F.....F...............FF........

Failures:

  1) Money has a custom to_s representation
     Failure/Error: Money.new('12.99'.to_d, :USD).to_s.should eq '12.99 USD'
       
       expected: "12.99 USD"
            got: "0.1299E2 USD"
       
       (compared using ==)
     # /tmp/d20130203-23049-8dro9s/spec.rb:89: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)>'

  2) Money arithmetic allows / with a numeric
     Failure/Error: result.amount.should eq bucks.amount.public_send(operation, numeric)
       
       expected: #<BigDecimal:9de2520,'0.1190476190 47619048E0',18(36)>
            got: #<BigDecimal:9de2b10,'0.5E1',9(36)>
       
       (compared using ==)
     # /tmp/d20130203-23049-8dro9s/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)>'

  3) Money comparison with == raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-8dro9s/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-8dro9s/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.05796 seconds
47 examples, 4 failures

Failed examples:

rspec /tmp/d20130203-23049-8dro9s/spec.rb:88 # Money has a custom to_s representation
rspec /tmp/d20130203-23049-8dro9s/spec.rb:119 # Money arithmetic allows / with a numeric
rspec /tmp/d20130203-23049-8dro9s/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-8dro9s/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ

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

Илиан обнови решението на 15.01.2013 22:49 (преди около 12 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ class Unknown < RuntimeError
+ end
+
+ def initialize
+ @rates = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ return if from_currency == to_currency
+ @rates[from_currency.to_s + to_currency.to_s] = rate
+ @rates[to_currency.to_s + from_currency.to_s] = 1 / rate
+ end
+
+ def get(from_currency, to_currency)
+ return '1'.to_d if from_currency == to_currency
+ @rates[from_currency.to_s + to_currency.to_s]
+ end
+
+ def convert(from_currency, to_currency, amount)
+ rate = get(from_currency, to_currency)
+ raise ExchangeRate::Unknown if rate == nil
+ rate * amount
+ end
+end
+
+class Money
+ include Comparable
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ attr_reader :amount, :currency
+ def initialize(amount, currency)
+ #self.@amount, self.@currency = amount, currency
+ @amount, @currency = amount, currency
+ end
+
+ def to_s
+ @amount.round(2).to_s + ' ' + @currency.to_s
+ end
+
+ def in(currency, exchange_rate)
+ Money.new(exchange_rate.convert(@currency, currency, @amount), currency)
+ end
+
+ def *(multiplier)
+ raise ArgumentError, 'Invalid parameter type!' unless multiplier.kind_of? Numeric
+ Money.new @amount * multiplier, @currency
+ end
+
+ def /(divisor)
+ raise ArgumentError, 'Invalid parameter type!' unless divisor.kind_of? Numeric
+ Money.new @amount / divisor, @currency
+ self
+ end
+
+ def +(other)
+ raise ArgumentError, 'Invalid parameter type!' unless other.kind_of? Money
+ raise Money::IncompatibleCurrencies unless @currency == other.currency
+ Money.new @amount + other.amount, @currency
+ end
+
+ def -(other)
+ raise ArgumentError, 'Invalid parameter type!' unless other.kind_of? Money
+ raise Money::IncompatibleCurrencies unless @currency == other.currency
+ Money.new @amount - other.amount, @currency
+ end
+
+ def <=>(other)
+ raise ArgumentError, 'Invalid parameter type!' unless other.kind_of? Money
+ raise Money::IncompatibleCurrencies unless @currency == other.currency
+ @amount <=> other.amount
+ end
+end