Решение на Шеста задача от Изабела Недялкова

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

Към профила на Изабела Недялкова

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
attr_reader :rates
def initialize
@rates = Hash.new
end
def set(from_currency, to_currency, rate)
currencies = from_currency, to_currency
@rates[currencies] = rate
@rates[currencies.reverse] = 1 / rate
end
def get(from_currency, to_currency)
currencies = from_currency, to_currency
if from_currency == to_currency
'1'.to_d
else
@rates[currencies]
end
end
class Unknown < RuntimeError
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
raise Unknown.new("Not defined rate between #{from_currency} and #{to_currency}") unless rate
amount * rate
end
end
class Money
include Comparable
attr_reader :amount, :currency
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
"#{sprintf "%.02f", @amount} #{@currency}"
end
def in(currency, exchange_rate)
converted_amount = exchange_rate.convert @currency, currency, @amount
Money.new converted_amount, currency
end
def *(multiplier)
raise ArgumentError.new("Wrong type of arguments") unless multiplier.kind_of? Numeric
Money.new(@amount * multiplier, @currency)
end
def /(divisor)
raise ArgumentError.new("Wrong type of arguments") unless divisor.kind_of? Numeric
Money.new(@amount / divisor, @currency)
end
class IncompatibleCurrencies < RuntimeError
end
def +(other)
raise ArgumentError.new("Wrong type of arguments") unless other.kind_of? Money
raise IncompatibleCurrencies.new("Currencies aren't equal") unless @currency == other.currency
Money.new(@amount + other.amount, @currency)
end
def -(other)
raise ArgumentError.new("Wrong type of arguments") unless other.kind_of? Money
raise IncompatibleCurrencies.new("Currencies aren't equal") unless @currency == other.currency
Money.new(@amount - other.amount, @currency)
end
def <=>(other)
raise ArgumentError.new("Wrong type of arguments") unless other.kind_of? Money
raise IncompatibleCurrencies.new("Currencies aren't equal") unless @currency == other.currency
@amount <=> other.amount
end
end

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

.....................................FF........

Failures:

  1) Money comparison with == raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-lbvum4/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)>'

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

Failed examples:

rspec /tmp/d20130203-23049-lbvum4/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-lbvum4/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ

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

Изабела обнови решението на 16.01.2013 15:55 (преди почти 12 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ attr_reader :rates
+
+ def initialize
+ @rates = Hash.new
+ end
+
+ def set(from_currency, to_currency, rate)
+ currencies = from_currency, to_currency
+ @rates[currencies] = rate
+ @rates[currencies.reverse] = 1 / rate
+ end
+
+ def get(from_currency, to_currency)
+ currencies = from_currency, to_currency
+ if from_currency == to_currency
+ '1'.to_d
+ else
+ @rates[currencies]
+ end
+ end
+
+ class Unknown < RuntimeError
+ end
+
+ def convert(from_currency, to_currency, amount)
+ rate = get(from_currency, to_currency)
+ raise Unknown.new("Not defined rate between #{from_currency} and #{to_currency}") unless rate
+ amount * rate
+ end
+end
+
+class Money
+ include Comparable
+
+ attr_reader :amount, :currency
+
+ def initialize(amount, currency)
+ @amount, @currency = amount, currency
+ end
+
+ def to_s
+ "#{sprintf "%.02f", @amount} #{@currency}"
+ end
+
+ def in(currency, exchange_rate)
+ converted_amount = exchange_rate.convert @currency, currency, @amount
+ Money.new converted_amount, currency
+ end
+
+ def *(multiplier)
+ raise ArgumentError.new("Wrong type of arguments") unless multiplier.kind_of? Numeric
+ Money.new(@amount * multiplier, @currency)
+ end
+
+ def /(divisor)
+ raise ArgumentError.new("Wrong type of arguments") unless divisor.kind_of? Numeric
+ Money.new(@amount / divisor, @currency)
+ end
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ def +(other)
+ raise ArgumentError.new("Wrong type of arguments") unless other.kind_of? Money
+ raise IncompatibleCurrencies.new("Currencies aren't equal") unless @currency == other.currency
+ Money.new(@amount + other.amount, @currency)
+ end
+
+ def -(other)
+ raise ArgumentError.new("Wrong type of arguments") unless other.kind_of? Money
+ raise IncompatibleCurrencies.new("Currencies aren't equal") unless @currency == other.currency
+ Money.new(@amount - other.amount, @currency)
+ end
+
+ def <=>(other)
+ raise ArgumentError.new("Wrong type of arguments") unless other.kind_of? Money
+ raise IncompatibleCurrencies.new("Currencies aren't equal") unless @currency == other.currency
+ @amount <=> other.amount
+ end
+end