Решение на Шеста задача от Даяна Беркова

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

Към профила на Даяна Беркова

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize
@rate = {}
end
def set(from_currency, to_currency, rate)
return if to_currency == from_currency
if @rate.has_key?(from_currency) then @rate[from_currency][to_currency] = rate
else @rate[from_currency] = { to_currency => rate }
end
if @rate.has_key?(to_currency) then @rate[to_currency][from_currency] = 1 / rate
else @rate[to_currency] = { from_currency => 1 / rate }
end
end
def get(from_currency, to_currency)
return 1.to_d if from_currency == to_currency
@rate[from_currency][to_currency] if @rate[from_currency] and @rate[to_currency]
end
def convert(from_currency, to_currency, amount)
if from_currency == to_currency then amount.to_d
elsif not @rate[from_currency][to_currency] then raise Unknown
else amount * @rate[from_currency][to_currency].to_d
end
end
end
class Money
include Comparable
class IncompatibleCurrencies < RuntimeError
end
attr_reader :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
'%.2f' % @amount.to_s + " " + @currency.to_s
end
def in(currency, exchange_rate)
Money.new(exchange_rate.convert(@currency, currency, @amount), currency)
end
def errors(other, it_is_numeric = false)
if it_is_numeric then raise ArgumentError if not other.is_a? Numeric
elsif not other.is_a? Money then raise ArgumentError
elsif other.currency != @currency then raise IncompatibleCurrencies
end
end
def <=>(other)
errors(other)
@amount <=> other.amount
end
def ==(other)
errors(other)
@amount == other.amount
end
def +(other)
errors(other)
Money.new(@amount + other.amount, @currency)
end
def -(other)
errors(other)
Money.new(@amount - other.amount, @currency)
end
def *(number)
errors(number, true)
Money.new(@amount * number, @currency)
end
def /(number)
errors(number, true)
Money.new(@amount / number, @currency)
end
end

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

.........F........F............................

Failures:

  1) ExchangeRate#convert raises an ExchangeRate::Unknown exception when the rate is not defined
     Failure/Error: expect do
       expected ExchangeRate::Unknown, got #<NoMethodError: undefined method `[]' for nil:NilClass>
     # /tmp/d20130203-23049-n2squm/spec.rb:56: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 convertion raises an ExchangeRate::Unknown exception for unknown rates
     Failure/Error: expect do
       expected ExchangeRate::Unknown, got #<NoMethodError: undefined method `[]' for nil:NilClass>
     # /tmp/d20130203-23049-n2squm/spec.rb:111: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)>'

Finished in 0.12214 seconds
47 examples, 2 failures

Failed examples:

rspec /tmp/d20130203-23049-n2squm/spec.rb:55 # ExchangeRate#convert raises an ExchangeRate::Unknown exception when the rate is not defined
rspec /tmp/d20130203-23049-n2squm/spec.rb:110 # Money convertion raises an ExchangeRate::Unknown exception for unknown rates

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

Даяна обнови решението на 15.01.2013 00:34 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ def initialize
+ @rate = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ return if to_currency == from_currency
+ if @rate.has_key?(from_currency) then @rate[from_currency][to_currency] = rate
+ else @rate[from_currency] = { to_currency => rate }
+ end
+ if @rate.has_key?(to_currency) then @rate[to_currency][from_currency] = 1 / rate
+ else @rate[to_currency] = { from_currency => 1 / rate }
+ end
+ end
+
+ def get(from_currency, to_currency)
+ return 1.to_d if from_currency == to_currency
+ @rate[from_currency][to_currency] if @rate[from_currency] and @rate[to_currency]
+ end
+
+ def convert(from_currency, to_currency, amount)
+ if from_currency == to_currency then amount.to_d
+ elsif not @rate[from_currency][to_currency] then raise RuntimeError, 'ExchangeRate::Unknown'
+ else amount * @rate[from_currency][to_currency].to_d
+ end
+ end
+end
+
+class Money
+ include Comparable
+ attr_reader :amount, :currency
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s
+ '%.2f' % @amount.to_s + " " + @currency.to_s
+ end
+
+ def in(currency, exchange_rate)
+ Money.new(exchange_rate.convert(@currency, currency, @amount), currency)
+ end
+
+ def errors(other, it_is_numeric = false)
+ if it_is_numeric then raise ArgumentError if not other.is_a? Numeric
+ elsif other.class != Money then raise ArgumentError
+ elsif other.currency != @currency then raise Money::IncompatibleCurrencies
+ end
+ end
+
+ def <=>(other)
+ errors(other)
+ @amount <=> other.amount
+ end
+
+ def +(other)
+ errors(other)
+ Money.new(@amount + other.amount, @currency)
+ end
+
+ def -(other)
+ errors(other)
+ Money.new(@amount - other.amount, @currency)
+ end
+
+ def *(number)
+ errors(number, true)
+ Money.new(@amount * number, @currency)
+ end
+
+ def /(number)
+ errors(number, true)
+ Money.new(@amount / number, @currency)
+ end
+end

Даяна обнови решението на 15.01.2013 18:12 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
def initialize
@rate = {}
end
def set(from_currency, to_currency, rate)
return if to_currency == from_currency
if @rate.has_key?(from_currency) then @rate[from_currency][to_currency] = rate
else @rate[from_currency] = { to_currency => rate }
end
if @rate.has_key?(to_currency) then @rate[to_currency][from_currency] = 1 / rate
else @rate[to_currency] = { from_currency => 1 / rate }
end
end
def get(from_currency, to_currency)
return 1.to_d if from_currency == to_currency
@rate[from_currency][to_currency] if @rate[from_currency] and @rate[to_currency]
end
def convert(from_currency, to_currency, amount)
if from_currency == to_currency then amount.to_d
elsif not @rate[from_currency][to_currency] then raise RuntimeError, 'ExchangeRate::Unknown'
else amount * @rate[from_currency][to_currency].to_d
end
end
end
class Money
include Comparable
attr_reader :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
'%.2f' % @amount.to_s + " " + @currency.to_s
end
def in(currency, exchange_rate)
Money.new(exchange_rate.convert(@currency, currency, @amount), currency)
end
def errors(other, it_is_numeric = false)
if it_is_numeric then raise ArgumentError if not other.is_a? Numeric
elsif other.class != Money then raise ArgumentError
elsif other.currency != @currency then raise Money::IncompatibleCurrencies
end
end
def <=>(other)
errors(other)
@amount <=> other.amount
end
+ def ==(other)
+ errors(other)
+ @amount == other.amount
+ end
+
def +(other)
errors(other)
Money.new(@amount + other.amount, @currency)
end
def -(other)
errors(other)
Money.new(@amount - other.amount, @currency)
end
def *(number)
errors(number, true)
Money.new(@amount * number, @currency)
end
def /(number)
errors(number, true)
Money.new(@amount / number, @currency)
end
end

Даяна обнови решението на 15.01.2013 21:56 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
+ class Unknown < RuntimeError
+ end
+
def initialize
@rate = {}
end
def set(from_currency, to_currency, rate)
return if to_currency == from_currency
if @rate.has_key?(from_currency) then @rate[from_currency][to_currency] = rate
else @rate[from_currency] = { to_currency => rate }
end
if @rate.has_key?(to_currency) then @rate[to_currency][from_currency] = 1 / rate
else @rate[to_currency] = { from_currency => 1 / rate }
end
end
def get(from_currency, to_currency)
return 1.to_d if from_currency == to_currency
@rate[from_currency][to_currency] if @rate[from_currency] and @rate[to_currency]
end
def convert(from_currency, to_currency, amount)
if from_currency == to_currency then amount.to_d
- elsif not @rate[from_currency][to_currency] then raise RuntimeError, 'ExchangeRate::Unknown'
+ elsif not @rate[from_currency][to_currency] then raise Unknown
else amount * @rate[from_currency][to_currency].to_d
end
end
end
class Money
include Comparable
+ class IncompatibleCurrencies < RuntimeError
+ end
+
attr_reader :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
'%.2f' % @amount.to_s + " " + @currency.to_s
end
def in(currency, exchange_rate)
Money.new(exchange_rate.convert(@currency, currency, @amount), currency)
end
def errors(other, it_is_numeric = false)
if it_is_numeric then raise ArgumentError if not other.is_a? Numeric
- elsif other.class != Money then raise ArgumentError
- elsif other.currency != @currency then raise Money::IncompatibleCurrencies
+ elsif not other.is_a? Money then raise ArgumentError
+ elsif other.currency != @currency then raise IncompatibleCurrencies
end
end
def <=>(other)
errors(other)
@amount <=> other.amount
end
def ==(other)
errors(other)
@amount == other.amount
end
def +(other)
errors(other)
Money.new(@amount + other.amount, @currency)
end
def -(other)
errors(other)
Money.new(@amount - other.amount, @currency)
end
def *(number)
errors(number, true)
Money.new(@amount * number, @currency)
end
def /(number)
errors(number, true)
Money.new(@amount / number, @currency)
end
end