Решение на Шеста задача от Николай Колев

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

Към профила на Николай Колев

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeType
attr_reader :from_currency, :to_currency
def initialize(from_currency, to_currency)
@from_currency = from_currency
@to_currency = to_currency
end
def reverse_exchange_type
ExchangeType.new(@to_currency, @from_currency)
end
def eql?(other)
@from_currency == other.from_currency and
@to_currency == other.to_currency
end
def hash
@from_currency.hash ^ @to_currency.hash
end
end
class ExchangeRate
def initialize
@exchange_rates = Hash.new
end
def set(from_currency, to_currency, rate)
exchange_type = ExchangeType.new(from_currency, to_currency)
reverse_exchange_type = exchange_type.reverse_exchange_type
rate_decimal = rate.to_d
@exchange_rates[exchange_type] = rate_decimal
@exchange_rates[reverse_exchange_type] = 1 / rate_decimal
end
def get(from_currency, to_currency)
if from_currency == to_currency
1
else
exchange_type = ExchangeType.new(from_currency, to_currency)
@exchange_rates[exchange_type]
end
end
def convert(from_currency, to_currency, amount)
exchange_rate = get(from_currency, to_currency)
raise ExchangeRate::Unknown if exchange_rate.nil?
exchange_rate * amount
end
class Unknown < RuntimeError
end
end
class Money
attr_reader :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
amount_string = '%.2f' % @amount
"#{amount_string} #{@currency.to_s}"
end
def in(currency, exchange_rate)
amount = exchange_rate.convert(@currency, currency, @amount)
Money.new(amount, currency)
end
[:*, :/].each do |operator|
define_method(operator) do |number|
amount = @amount.send(operator, number)
Money.new(amount, @currency)
end
end
[:+, :-, :<=>, :==, :<, :<=, :>, :>=].each do |operator|
define_method(operator) do |other|
ensure_correct_money_argument(other)
amount = @amount.send(operator, other.amount)
Money.new(amount, @currency)
end
end
def ensure_correct_money_argument(argument)
raise ArgumentError unless argument.is_a? Money
raise Money::IncompatibleCurrencies unless @currency == argument.currency
end
class IncompatibleCurrencies < StandardError
end
end

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

....................F.F........FFFF............

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-1wz1rh4/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-1wz1rh4/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-1wz1rh4/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-1wz1rh4/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 be_false
     TypeError:
       can't convert false into Float
     # /tmp/d20130203-23049-1wz1rh4/solution.rb:67:in `%'
     # /tmp/d20130203-23049-1wz1rh4/solution.rb:67:in `to_s'
     # /tmp/d20130203-23049-1wz1rh4/spec.rb:172: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 works for equality when currencies are the same
     Failure/Error: (Money.new('12.45'.to_d, :BGN) == Money.new('12.451'.to_d, :BGN)).should be_false
     TypeError:
       can't convert false into Float
     # /tmp/d20130203-23049-1wz1rh4/solution.rb:67:in `%'
     # /tmp/d20130203-23049-1wz1rh4/solution.rb:67:in `to_s'
     # /tmp/d20130203-23049-1wz1rh4/spec.rb:190: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)>'

Finished in 0.06214 seconds
47 examples, 6 failures

Failed examples:

rspec /tmp/d20130203-23049-1wz1rh4/spec.rb:128 # Money arithmetic * with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-1wz1rh4/spec.rb:128 # Money arithmetic / with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-1wz1rh4/spec.rb:159 # Money arithmetic * with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1wz1rh4/spec.rb:159 # Money arithmetic / with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1wz1rh4/spec.rb:168 # Money comparison works when currencies are the same
rspec /tmp/d20130203-23049-1wz1rh4/spec.rb:188 # Money comparison works for equality when currencies are the same

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

Николай обнови решението на 11.01.2013 19:44 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeType
+ attr_reader :from_currency, :to_currency
+
+ def initialize(from_currency, to_currency)
+ @from_currency = from_currency
+ @to_currency = to_currency
+ end
+
+ def reverse_exchange_type
+ ExchangeType.new(@to_currency, @from_currency)
+ end
+
+ def eql?(other)
+ @from_currency == other.from_currency and
+ @to_currency == other.to_currency
+ end
+
+ def hash
+ @from_currency.hash ^ @to_currency.hash
+ end
+end
+
+class ExchangeRate
+ def initialize
+ @exchange_rates = Hash.new
+ end
+
+ def set(from_currency, to_currency, rate)
+ exchange_type = ExchangeType.new(from_currency, to_currency)
+ reverse_exchange_type = exchange_type.reverse_exchange_type
+ rate_decimal = rate.to_d
+ @exchange_rates[exchange_type] = rate_decimal
+ @exchange_rates[reverse_exchange_type] = 1 / rate_decimal
+ end
+
+ def get(from_currency, to_currency)
+ if from_currency == to_currency
+ 1
+ else
+ exchange_type = ExchangeType.new(from_currency, to_currency)
+ @exchange_rates[exchange_type]
+ end
+ end
+
+ def convert(from_currency, to_currency, amount)
+ exchange_rate = get(from_currency, to_currency)
+ raise ExchangeRate::Unknown if exchange_rate.nil?
+ exchange_rate * amount
+ end
+
+ class Unknown < RuntimeError
+ end
+end
+
+class Money
+ attr_reader :amount, :currency
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s
+ amount_string = '%.2f' % @amount
+ "#{amount_string} #{@currency.to_s}"
+ end
+
+ def in(currency, exchange_rate)
+ amount = exchange_rate.convert(@currency, currency, @amount)
+ Money.new(amount, currency)
+ end
+
+ [:*, :/].each do |operator|
+ define_method(operator) do |number|
+ amount = @amount.send(operator, number)
+ Money.new(amount, @currency)
+ end
+ end
+
+ [:+, :-, :<=>, :==, :<, :<=, :>, :>=].each do |operator|
+ define_method(operator) do |other|
+ ensure_correct_money_argument(other)
+ amount = @amount.send(operator, other.amount)
+ Money.new(amount, @currency)
+ end
+ end
+
+ def ensure_correct_money_argument(argument)
+ raise ArgumentError unless argument.is_a? Money
+ raise Money::IncompatibleCurrencies unless @currency == argument.currency
+ end
+
+ class IncompatibleCurrencies < StandardError
+ end
+end