Решение на Шеста задача от Емил Гоцев

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

Към профила на Емил Гоцев

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize
@exchange_rates = {}
end
def set(from_currency, to_currency, rate)
unless from_currency == to_currency
@exchange_rates["#{from_currency}-#{to_currency}"] = rate
@exchange_rates["#{to_currency}-#{from_currency}"] = 1 / rate
end
end
def get(from_currency, to_currency)
if from_currency != to_currency
@exchange_rates["#{from_currency}-#{to_currency}"]
else
'1'.to_d
end
end
def convert(from_currency, to_currency, amount)
return amount if from_currency == to_currency
rate = @exchange_rates["#{from_currency}-#{to_currency}"]
if rate != nil
amount * rate
else
raise Unknown, "This convertion is unknown!"
end
end
end
class Money
include Comparable
class IncompatibleCurrencies < RuntimeError
end
attr_accessor :amount, :currency
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
"#{'%.2f' % @amount} #{@currency}"
end
def in(currency, exchange_rate)
result_amount = exchange_rate.convert @currency, currency, @amount
Money.new result_amount, currency
end
def +(other)
raise ArgumentError unless other.instance_of? Money
if @currency == other.currency
Money.new(@amount + other.amount, @currency)
else
raise IncompatibleCurrencies, "Can't add Money of incompatible currencies!"
end
end
def -(other)
raise ArgumentError unless other.instance_of? Money
if @currency == other.currency
Money.new(@amount - other.amount, @currency)
else
raise IncompatibleCurrencies, "Can't substract Money of incompatible currencies!"
end
end
def *(other)
raise ArgumentError unless other.is_a? Numeric
Money.new(@amount * other, @currency)
end
def /(other)
raise ArgumentError unless other.is_a? Numeric
Money.new(@amount / other, @currency)
end
def <=>(other)
raise ArgumentError unless other.instance_of? Money
raise IncompatibleCurrencies 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-ayrzfz/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-ayrzfz/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.05795 seconds
47 examples, 2 failures

Failed examples:

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

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

Емил обнови решението на 16.01.2013 16:13 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ class Unknown < RuntimeError
+ end
+
+ def initialize
+ @exchange_rates = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ unless from_currency == to_currency
+ @exchange_rates["#{from_currency}-#{to_currency}"] = rate
+ @exchange_rates["#{to_currency}-#{from_currency}"] = 1 / rate
+ end
+ end
+
+ def get(from_currency, to_currency)
+ if from_currency != to_currency
+ @exchange_rates["#{from_currency}-#{to_currency}"]
+ else
+ '1'.to_d
+ end
+ end
+
+ def convert(from_currency, to_currency, amount)
+ return amount if from_currency == to_currency
+ rate = @exchange_rates["#{from_currency}-#{to_currency}"]
+ if rate != nil
+ amount * rate
+ else
+ raise Unknown, "This convertion is unknown!"
+ end
+ end
+end
+
+class Money
+ attr_accessor :amount, :currency
+
+ def initialize(amount, currency)
+ @amount, @currency = amount, currency
+ end
+
+ def to_s
+ "#{'%.2f' % @amount} #{@currency}"
+ end
+
+ def in(currency, exchange_rate)
+ result_amount = exchange_rate.convert @currency, currency, @amount
+ Money.new result_amount, currency
+ end
+end

Емил обнови решението на 16.01.2013 16:40 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize
@exchange_rates = {}
end
def set(from_currency, to_currency, rate)
unless from_currency == to_currency
@exchange_rates["#{from_currency}-#{to_currency}"] = rate
@exchange_rates["#{to_currency}-#{from_currency}"] = 1 / rate
end
end
def get(from_currency, to_currency)
if from_currency != to_currency
@exchange_rates["#{from_currency}-#{to_currency}"]
else
'1'.to_d
end
end
def convert(from_currency, to_currency, amount)
return amount if from_currency == to_currency
rate = @exchange_rates["#{from_currency}-#{to_currency}"]
if rate != nil
amount * rate
else
raise Unknown, "This convertion is unknown!"
end
end
end
class Money
+ include Comparable
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
attr_accessor :amount, :currency
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
"#{'%.2f' % @amount} #{@currency}"
end
def in(currency, exchange_rate)
result_amount = exchange_rate.convert @currency, currency, @amount
Money.new result_amount, currency
+ end
+
+ def +(other)
+ raise ArgumentError unless other.instance_of? Money
+ if @currency == other.currency
+ Money.new(@amount + other.amount, @currency)
+ else
+ raise IncompatibleCurrencies, "Can't add Money of incompatible currencies!"
+ end
+ end
+
+ def -(other)
+ raise ArgumentError unless other.instance_of? Money
+ if @currency == other.currency
+ Money.new(@amount - other.amount, @currency)
+ else
+ raise IncompatibleCurrencies, "Can't substract Money of incompatible currencies!"
+ end
+ end
+
+ def *(other)
+ raise ArgumentError unless other.is_a? Numeric
+ Money.new(@amount * other, @currency)
+ end
+
+ def /(other)
+ raise ArgumentError unless other.is_a? Numeric
+ Money.new(@amount / other, @currency)
+ end
+
+ def <=>(other)
+ raise ArgumentError unless other.instance_of? Money
+ raise IncompatibleCurrencies unless @currency == other.currency
+ @amount <=> other.amount
end
end