Решение на Шеста задача от Румен Палетов

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

Към профила на Румен Палетов

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize()
@currencies = {}
end
def set(from_currency, to_currency, rate)
@currencies[get_from_to_key(from_currency, to_currency)] = rate
@currencies[get_to_from_key(to_currency, from_currency)] = 1.to_d / rate
end
def get(from_currency, to_currency)
if from_currency == to_currency then 1
else @currencies[get_from_to_key(from_currency, to_currency)]
end
end
def convert(from_currency, to_currency, amount)
if get(from_currency, to_currency) == nil then raise Unknown
else get(from_currency, to_currency) * amount
end
end
private
def get_from_to_key(from_currency, to_currency)
"#{from_currency.to_s}->#{to_currency}"
end
def get_to_from_key(to_currency, from_currency)
"#{to_currency.to_s}->#{from_currency}"
end
end
class Money
include Comparable
class IncompatibleCurrencies < RuntimeError
end
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def amount
@amount
end
def currency
@currency
end
def to_s
"#{"%.2f" % @amount} #{@currency.to_s}"
end
def in(currency, exchange_rate)
Money.new exchange_rate.convert(@currency, currency, @amount), currency
end
def *(other)
if other.is_a? Numeric then Money.new @amount * other, @currency
elsif validate(other) then Money.new @amount * other.amount, @currency
else nil
end
end
def /(other)
if other.is_a? Numeric then Money.new @amount / other, @currency
elsif validate(other) then Money.new @amount / other.amount, @currency
else nil
end
end
def +(other)
if validate(other) then Money.new @amount + other.amount, @currency
else nil
end
end
def -(other)
if validate(other) then Money.new @amount - other.amount, @currency
else nil
end
end
def <=>(other)
@amount <=> other.amount if validate(other)
end
private
def validate(other)
if !(other.is_a? Money) then raise ArgumentError
elsif @currency != other.currency then raise IncompatibleCurrencies
else true
end
end
end

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

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

Failures:

  1) Money arithmetic * with money objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-7af732/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 but nothing was raised
     # /tmp/d20130203-23049-7af732/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 comparison with == raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-7af732/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-7af732/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.05836 seconds
47 examples, 4 failures

Failed examples:

rspec /tmp/d20130203-23049-7af732/spec.rb:128 # Money arithmetic * with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-7af732/spec.rb:128 # Money arithmetic / with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-7af732/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-7af732/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ

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

Румен обнови решението на 13.01.2013 18:52 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ class Unknown < RuntimeError
+ end
+
+ def initialize()
+ @currencies = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ @currencies[get_from_to_key(from_currency, to_currency)] = rate
+ @currencies[get_to_from_key(to_currency, from_currency)] = 1.to_d / rate
+ end
+
+ def get(from_currency, to_currency)
+ if from_currency == to_currency then 1
+ else @currencies[get_from_to_key(from_currency, to_currency)]
+ end
+ end
+
+ def convert(from_currency, to_currency, amount)
+ if get(from_currency, to_currency) == nil then raise Unknown
+ else get(from_currency, to_currency) * amount
+ end
+ end
+
+ private
+
+ def get_from_to_key(from_currency, to_currency)
+ "#{from_currency.to_s}->#{to_currency}"
+ end
+
+ def get_to_from_key(to_currency, from_currency)
+ "#{to_currency.to_s}->#{from_currency}"
+ end
+end
+
+class Money
+ include Comparable
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def amount
+ @amount
+ end
+
+ def currency
+ @currency
+ end
+
+ def to_s
+ "#{"%.2f" % @amount} #{@currency.to_s}"
+ end
+
+ def in(currency, exchange_rate)
+ Money.new exchange_rate.convert(@currency, currency, @amount), currency
+ end
+
+ def *(other)
+ if other.is_a? Numeric then Money.new @amount * other, @currency
+ elsif validate(other) then Money.new @amount * other.amount, @currency
+ else nil
+ end
+ end
+
+ def /(other)
+ if (other.is_a? Money) == false then Money.new @amount / other, @currency
+ elsif validate(other) then Money.new @amount / other.amount, @currency
+ else nil
+ end
+ end
+
+ def +(other)
+ if validate(other) then Money.new @amount + other.amount, @currency
+ else nil
+ end
+ end
+
+ def -(other)
+ if validate(other) then Money.new @amount - other.amount, @currency
+ else nil
+ end
+ end
+
+ def <=>(other)
+ @amount <=> other.amount if validate(other)
+ end
+
+ private
+
+ def validate(other)
+ if !(other.is_a? Money) then raise ArgumentError
+ elsif @currency != other.currency then raise IncompatibleCurrencies
+ else true
+ end
+ end
+end

Румен обнови решението на 13.01.2013 23:14 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize()
@currencies = {}
end
def set(from_currency, to_currency, rate)
@currencies[get_from_to_key(from_currency, to_currency)] = rate
@currencies[get_to_from_key(to_currency, from_currency)] = 1.to_d / rate
end
def get(from_currency, to_currency)
if from_currency == to_currency then 1
else @currencies[get_from_to_key(from_currency, to_currency)]
end
end
def convert(from_currency, to_currency, amount)
if get(from_currency, to_currency) == nil then raise Unknown
else get(from_currency, to_currency) * amount
end
end
private
def get_from_to_key(from_currency, to_currency)
"#{from_currency.to_s}->#{to_currency}"
end
def get_to_from_key(to_currency, from_currency)
"#{to_currency.to_s}->#{from_currency}"
end
end
class Money
include Comparable
class IncompatibleCurrencies < RuntimeError
end
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def amount
@amount
end
def currency
@currency
end
def to_s
"#{"%.2f" % @amount} #{@currency.to_s}"
end
def in(currency, exchange_rate)
Money.new exchange_rate.convert(@currency, currency, @amount), currency
end
def *(other)
if other.is_a? Numeric then Money.new @amount * other, @currency
elsif validate(other) then Money.new @amount * other.amount, @currency
else nil
end
end
def /(other)
- if (other.is_a? Money) == false then Money.new @amount / other, @currency
+ if other.is_a? Numeric then Money.new @amount / other, @currency
elsif validate(other) then Money.new @amount / other.amount, @currency
else nil
end
end
def +(other)
if validate(other) then Money.new @amount + other.amount, @currency
else nil
end
end
def -(other)
if validate(other) then Money.new @amount - other.amount, @currency
else nil
end
end
def <=>(other)
@amount <=> other.amount if validate(other)
end
private
def validate(other)
if !(other.is_a? Money) then raise ArgumentError
elsif @currency != other.currency then raise IncompatibleCurrencies
else true
end
end
end