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

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

Към профила на Никола Таушанов

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
def initialize
@rates = {}
end
def set(from_currency, to_currency, rate)
set_currency from_currency, to_currency, rate
set_currency to_currency, from_currency, '1'.to_d / rate
end
def get(from_currency, to_currency)
return '1'.to_d if from_currency == to_currency
@rates[from_currency].nil? ? nil : @rates[from_currency][to_currency]
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
raise Unknown if rate.nil?
amount * rate
end
class Unknown < RuntimeError
end
private
def set_currency(from_currency, to_currency, rate)
@rates[from_currency] ||= {}
@rates[from_currency][to_currency] = rate
end
end
class Money
include Comparable
attr_reader :amount, :currency
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def in(currency, exchange_rate)
Money.new exchange_rate.convert(@currency, currency, @amount), currency
end
def *(coefficient)
validate_numeric coefficient
Money.new @amount * coefficient, @currency
end
def /(coefficient)
validate_numeric coefficient
Money.new @amount / coefficient, @currency
end
def +(money)
validate_money money
Money.new @amount + money.amount, @currency
end
def -(money)
validate_money money
Money.new @amount - money.amount, @currency
end
def <=>(money)
validate_money money
@amount <=> money.amount
end
def to_s
"%.2f #{@currency}" % amount
end
class IncompatibleCurrencies < RuntimeError
end
private
def validate_numeric(coefficient)
raise ArgumentError if not coefficient.is_a? Numeric
end
def validate_money(money)
raise ArgumentError if not money.is_a? Money
raise IncompatibleCurrencies if @currency != money.currency
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-1rq4plb/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-1rq4plb/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.0576 seconds
47 examples, 2 failures

Failed examples:

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

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

Никола обнови решението на 16.01.2013 16:41 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ def initialize
+ @rates = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ set_currency from_currency, to_currency, rate
+ set_currency to_currency, from_currency, '1'.to_d / rate
+ end
+
+ def get(from_currency, to_currency)
+ return '1'.to_d if from_currency == to_currency
+
+ @rates[from_currency].nil? ? nil : @rates[from_currency][to_currency]
+ end
+
+ def convert(from_currency, to_currency, amount)
+ rate = get(from_currency, to_currency)
+
+ raise Unknown if rate.nil?
+
+ amount * rate
+ end
+
+ class Unknown < RuntimeError
+ end
+
+ private
+
+ def set_currency(from_currency, to_currency, rate)
+ @rates[from_currency] ||= {}
+ @rates[from_currency][to_currency] = rate
+ end
+end
+
+class Money
+ include Comparable
+
+ attr_reader :amount, :currency
+
+ def initialize(amount, currency)
+ @amount, @currency = amount, currency
+ end
+
+ def in(currency, exchange_rate)
+ Money.new exchange_rate.convert(@currency, currency, @amount), currency
+ end
+
+ def *(coefficient)
+ validate_numeric coefficient
+
+ Money.new @amount * coefficient, @currency
+ end
+
+ def /(coefficient)
+ validate_numeric coefficient
+
+ Money.new @amount / coefficient, @currency
+ end
+
+
+ def +(money)
+ validate_money money
+
+ Money.new @amount + money.amount, @currency
+ end
+
+ def -(money)
+ validate_money money
+
+ Money.new @amount - money.amount, @currency
+ end
+
+ def <=>(money)
+ validate_money money
+
+ @amount <=> money.amount
+ end
+
+ def to_s
+ "%.2f #{@currency}" % amount
+ end
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ private
+
+ def validate_numeric(coefficient)
+ raise ArgumentError if not coefficient.is_a? Numeric
+ end
+
+ def validate_money(money)
+ raise ArgumentError if not money.is_a? Money
+ raise IncompatibleCurrencies if @currency != money.currency
+ end
+end

Никола обнови решението на 16.01.2013 16:51 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
def initialize
@rates = {}
end
def set(from_currency, to_currency, rate)
set_currency from_currency, to_currency, rate
set_currency to_currency, from_currency, '1'.to_d / rate
end
def get(from_currency, to_currency)
return '1'.to_d if from_currency == to_currency
@rates[from_currency].nil? ? nil : @rates[from_currency][to_currency]
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
raise Unknown if rate.nil?
amount * rate
end
class Unknown < RuntimeError
end
private
def set_currency(from_currency, to_currency, rate)
@rates[from_currency] ||= {}
@rates[from_currency][to_currency] = rate
end
end
class Money
include Comparable
attr_reader :amount, :currency
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def in(currency, exchange_rate)
Money.new exchange_rate.convert(@currency, currency, @amount), currency
end
def *(coefficient)
validate_numeric coefficient
Money.new @amount * coefficient, @currency
end
def /(coefficient)
validate_numeric coefficient
Money.new @amount / coefficient, @currency
end
def +(money)
validate_money money
Money.new @amount + money.amount, @currency
end
def -(money)
validate_money money
Money.new @amount - money.amount, @currency
end
def <=>(money)
validate_money money
@amount <=> money.amount
end
def to_s
"%.2f #{@currency}" % amount
end
class IncompatibleCurrencies < RuntimeError
end
private
def validate_numeric(coefficient)
raise ArgumentError if not coefficient.is_a? Numeric
end
def validate_money(money)
raise ArgumentError if not money.is_a? Money
raise IncompatibleCurrencies if @currency != money.currency
end
-end
+end
+
+