Решение на Шеста задача от Радослав Върбанов

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

Към профила на Радослав Върбанов

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
def exception(message)
end
end
def initialize
@rates = {}
end
def set(from_c, to_c, rate)
if from_c != to_c
@rates[from_c] ||= {}
@rates[from_c][to_c] = rate
end
end
def get(from_c, to_c)
if from_c == to_c then BigDecimal.new('1')
elsif rate_exists(from_c, to_c) then BigDecimal.new(@rates[from_c][to_c])
elsif rate_exists(to_c, from_c) then BigDecimal.new(1/@rates[to_c][from_c])
end
end
def convert(from_c, to_c, amount)
begin
amount * get(from_c, to_c)
rescue
raise Unknown
end
end
private
def rate_exists(from_c, to_c)
@rates.has_key?(from_c) && @rates[from_c].has_key?(to_c)
end
end
class Money
include Comparable
attr_reader :currency, :amount
class IncompatibleCurrencies < StandardError
def exception
end
end
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
amount = @amount.round(2).to_digits.to_s
while not /[\d]{1,}\.\d\d/.match amount
amount << '0'
end
amount + ' ' + @currency.to_s
end
def in(currency, exchange_rate)
Money.new exchange_rate.convert(@currency, currency, amount), currency
end
def /(number)
if number.is_a? Numeric
Money.new @amount / number, @currency
else raise ArgumentError
end
end
def *(number)
if number.is_a? Numeric
Money.new @amount * number, @currency
else raise ArgumentError
end
end
def +(other)
if other.is_a? Money
if other.currency == @currency
Money.new @amount + other.amount, @currency
else raise IncompatibleCurrencies
end
else raise ArgumentError
end
end
def -(other)
if other.is_a? Money
if other.currency == @currency
Money.new @amount - other.amount, @currency
else raise IncompatibleCurrencies
end
else raise ArgumentError
end
end
def <=>(other)
if other.is_a? Money
if other.currency == @currency
@amount <=> other.amount
else raise IncompatibleCurrencies
end
else raise ArgumentError
end
end
end

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

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

Failures:

  1) ExchangeRate#set sets the exchange rate in both directions
     Failure/Error: rate.get(:EUR, :BGN).should eq '1.25'.to_d
       
       expected: #<BigDecimal:94c0f50,'0.125E1',18(18)>
            got: #<BigDecimal:94c0fc8,'0.2E1',9(36)>
       
       (compared using ==)
     # /tmp/d20130203-23049-2lr8wl/spec.rb:25: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)>'

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

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

Failed examples:

rspec /tmp/d20130203-23049-2lr8wl/spec.rb:21 # ExchangeRate#set sets the exchange rate in both directions
rspec /tmp/d20130203-23049-2lr8wl/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-2lr8wl/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ

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

Радослав обнови решението на 16.01.2013 16:14 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ class Unknown < RuntimeError
+ def exception(message)
+ end
+ end
+
+ def initialize
+ @rates = {}
+ end
+
+ def set(from_c, to_c, rate)
+ if from_c != to_c
+ @rates[from_c] ||= {}
+ @rates[from_c][to_c] = rate
+ end
+ end
+
+ def get(from_c, to_c)
+ if from_c == to_c then BigDecimal.new('1')
+ elsif rate_exists(from_c, to_c) then BigDecimal.new(@rates[from_c][to_c])
+ elsif rate_exists(to_c, from_c) then BigDecimal.new(1/@rates[to_c][from_c])
+ end
+ end
+
+ def convert(from_c, to_c, amount)
+ begin
+ amount * get(from_c, to_c)
+ rescue
+ raise Unknown
+ end
+ end
+
+ private
+ def rate_exists(from_c, to_c)
+ @rates.has_key?(from_c) && @rates[from_c].has_key?(to_c)
+ end
+end
+
+class Money
+ include Comparable
+
+ attr_reader :currency, :amount
+
+ class IncompatibleCurrencies < StandardError
+ def exception
+ end
+ end
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s
+ amount = @amount.round(2).to_digits.to_s
+ while not /[\d]{1,}\.\d\d/.match amount
+ amount << '0'
+ end
+ amount + ' ' + @currency.to_s
+ end
+
+ def in(currency, exchange_rate)
+ Money.new exchange_rate.convert(@currency, currency, amount), currency
+ end
+
+ def /(number)
+ if number.is_a? Numeric
+ Money.new @amount / number, @currency
+ else raise ArgumentError
+ end
+ end
+
+ def *(number)
+ if number.is_a? Numeric
+ Money.new @amount * number, @currency
+ else raise ArgumentError
+ end
+ end
+
+ def +(other)
+ if other.is_a? Money
+ if other.currency == @currency
+ Money.new @amount + other.amount, @currency
+ else raise IncompatibleCurrencies
+ end
+ else raise ArgumentError
+ end
+ end
+
+ def -(other)
+ if other.is_a? Money
+ if other.currency == @currency
+ Money.new @amount - other.amount, @currency
+ else raise IncompatibleCurrencies
+ end
+ else raise ArgumentError
+ end
+ end
+
+ def <=>(other)
+ if other.is_a? Money
+ if other.currency == @currency
+ @amount <=> other.amount
+ else raise IncompatibleCurrencies
+ end
+ else raise ArgumentError
+ end
+ end
+end