Решение на Шеста задача от Филарета Йорданова

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

Към профила на Филарета Йорданова

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 31 успешни тест(а)
  • 16 неуспешни тест(а)

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
attr_reader :rates
class Unknown < RuntimeError
end
def initialize
@rates = {}
end
def set(from_currency, to_currency, rate)
if from_currency == to_currency
@rates[{from_currency => to_currency}] = BigDecimal.new 1
else
@rates[{from_currency => to_currency}] = rate
@rates[{to_currency => from_currency}] = 1 / rate
end
end
def get(from_currency, to_currency)
@rates.each do |currency, rate|
@rate = rate if currency.has_key? from_currency and currency.has_value? to_currency
end
@rate
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
if rate
rate * amount
else
raise Unknown.new
end
end
end
class Money
include Comparable
attr_accessor :amount, :currency
class IncompatiableCurrencies < RuntimeError
end
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
amount.round(2).to_s('F') + ' ' + currency.to_s
end
def in(currency_, exchange_rate)
new_amount = exchange_rate.convert(currency, currency_, amount)
Money.new new_amount, currency_
end
def +(other)
if currency == other.currency
Money.new(amount + other.amount, currency)
else
raise IncompatiableCurrencies.new
end
end
def -(other)
if currency == other.currency
Money.new(amount - other.amount, currency)
else
raise IncompatiableCurrencies.new
end
end
def *(number)
if number > 0
Money.new(amount * number, currency)
else
raise ArgumentError.new("Invalid operation for money")
end
end
def /(number)
if number > 0
Money.new(amount / number, currency)
else
raise ArgumentError.new("Invalid operation for money")
end
end
def ==(other)
raise ArgumentError.new('Not a suitable type') unless self.class == other.class
raise IncompatiableCurrencies.new unless currency == other.currency
return amount == other.amount
end
def <=>(other)
raise ArgumentError.new('Not a suitable type') unless self.class == other.class
raise IncompatiableCurrencies.new unless currency == other.currency
return -1 if amount < other.amount
return 0 if amount == other.amount
return 1 if amount > other.amount
end
end

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

.......F....F..F.F.....F.FF.FFF.....F.F.F.F.F.F

Failures:

  1) ExchangeRate#get always returns 1 as the exchange rate between two identical currencies
     Failure/Error: rate.get(:JPY, :JPY).should eq 1.to_d
       
       expected: #<BigDecimal:8e52eac,'0.1E1',9(36)>
            got: nil
       
       (compared using ==)
     # /tmp/d20130203-23049-gk3b9b/spec.rb:45: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) ExchangeRate#convert works for identical currencies without defining any rates
     Failure/Error: rate.convert(:JPY, :JPY, 123.to_d).should eq 123.to_d
     ExchangeRate::Unknown:
       ExchangeRate::Unknown
     # /tmp/d20130203-23049-gk3b9b/solution.rb:35:in `convert'
     # /tmp/d20130203-23049-gk3b9b/spec.rb:72: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)>'

  3) Money has a custom to_s representation
     Failure/Error: Money.new('12.1'.to_d, :USD).to_s.should eq '12.10 USD'
       
       expected: "12.10 USD"
            got: "12.1 USD"
       
       (compared using ==)
     # /tmp/d20130203-23049-gk3b9b/spec.rb:90:in `block (2 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)>'

  4) Money convertion does not change the amount if the same currency is passed
     Failure/Error: Money.new(5.to_d, :EUR).in(:EUR, ExchangeRate.new).amount.should eq 5.to_d
     ExchangeRate::Unknown:
       ExchangeRate::Unknown
     # /tmp/d20130203-23049-gk3b9b/solution.rb:35:in `convert'
     # /tmp/d20130203-23049-gk3b9b/solution.rb:57:in `in'
     # /tmp/d20130203-23049-gk3b9b/spec.rb:107: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)>'

  5) Money arithmetic + with numeric objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for 42:Fixnum>
     # /tmp/d20130203-23049-gk3b9b/spec.rb:137: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)>'

  6) Money arithmetic raises Money::IncompatibleCurrencies for + with money with different currencies
     Failure/Error: end.to raise_error(Money::IncompatibleCurrencies)
     NameError:
       uninitialized constant Money::IncompatibleCurrencies
     # /tmp/d20130203-23049-gk3b9b/spec.rb:154: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)>'

  7) Money arithmetic - with numeric objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for 42:Fixnum>
     # /tmp/d20130203-23049-gk3b9b/spec.rb:137: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)>'

  8) Money arithmetic raises Money::IncompatibleCurrencies for - with money with different currencies
     Failure/Error: end.to raise_error(Money::IncompatibleCurrencies)
     NameError:
       uninitialized constant Money::IncompatibleCurrencies
     # /tmp/d20130203-23049-gk3b9b/spec.rb:154: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)>'

  9) Money arithmetic + with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for "foobar":String>
     # /tmp/d20130203-23049-gk3b9b/spec.rb:160: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)>'

  10) Money arithmetic - with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `currency' for "foobar":String>
     # /tmp/d20130203-23049-gk3b9b/spec.rb:160: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)>'

  11) Money comparison with <=> raises IncompatibleCurrencies when currencies differ
     Failure/Error: end.to raise_error(Money::IncompatibleCurrencies)
     NameError:
       uninitialized constant Money::IncompatibleCurrencies
     # /tmp/d20130203-23049-gk3b9b/spec.rb:203: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)>'

  12) Money comparison with == raises IncompatibleCurrencies when currencies differ
     Failure/Error: end.to raise_error(Money::IncompatibleCurrencies)
     NameError:
       uninitialized constant Money::IncompatibleCurrencies
     # /tmp/d20130203-23049-gk3b9b/spec.rb:203: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)>'

  13) Money comparison with < raises IncompatibleCurrencies when currencies differ
     Failure/Error: end.to raise_error(Money::IncompatibleCurrencies)
     NameError:
       uninitialized constant Money::IncompatibleCurrencies
     # /tmp/d20130203-23049-gk3b9b/spec.rb:203: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)>'

  14) Money comparison with <= raises IncompatibleCurrencies when currencies differ
     Failure/Error: end.to raise_error(Money::IncompatibleCurrencies)
     NameError:
       uninitialized constant Money::IncompatibleCurrencies
     # /tmp/d20130203-23049-gk3b9b/spec.rb:203: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)>'

  15) Money comparison with > raises IncompatibleCurrencies when currencies differ
     Failure/Error: end.to raise_error(Money::IncompatibleCurrencies)
     NameError:
       uninitialized constant Money::IncompatibleCurrencies
     # /tmp/d20130203-23049-gk3b9b/spec.rb:203: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)>'

  16) Money comparison with >= raises IncompatibleCurrencies when currencies differ
     Failure/Error: end.to raise_error(Money::IncompatibleCurrencies)
     NameError:
       uninitialized constant Money::IncompatibleCurrencies
     # /tmp/d20130203-23049-gk3b9b/spec.rb:203: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.06523 seconds
47 examples, 16 failures

Failed examples:

rspec /tmp/d20130203-23049-gk3b9b/spec.rb:44 # ExchangeRate#get always returns 1 as the exchange rate between two identical currencies
rspec /tmp/d20130203-23049-gk3b9b/spec.rb:71 # ExchangeRate#convert works for identical currencies without defining any rates
rspec /tmp/d20130203-23049-gk3b9b/spec.rb:88 # Money has a custom to_s representation
rspec /tmp/d20130203-23049-gk3b9b/spec.rb:106 # Money convertion does not change the amount if the same currency is passed
rspec /tmp/d20130203-23049-gk3b9b/spec.rb:136 # Money arithmetic + with numeric objects raises an ArgumentError
rspec /tmp/d20130203-23049-gk3b9b/spec.rb:151 # Money arithmetic raises Money::IncompatibleCurrencies for + with money with different currencies
rspec /tmp/d20130203-23049-gk3b9b/spec.rb:136 # Money arithmetic - with numeric objects raises an ArgumentError
rspec /tmp/d20130203-23049-gk3b9b/spec.rb:151 # Money arithmetic raises Money::IncompatibleCurrencies for - with money with different currencies
rspec /tmp/d20130203-23049-gk3b9b/spec.rb:159 # Money arithmetic + with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-gk3b9b/spec.rb:159 # Money arithmetic - with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-gk3b9b/spec.rb:200 # Money comparison with <=> raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-gk3b9b/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-gk3b9b/spec.rb:200 # Money comparison with < raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-gk3b9b/spec.rb:200 # Money comparison with <= raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-gk3b9b/spec.rb:200 # Money comparison with > raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-gk3b9b/spec.rb:200 # Money comparison with >= raises IncompatibleCurrencies when currencies differ

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

Филарета обнови решението на 13.01.2013 17:57 (преди почти 12 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ attr_reader :from_currency, :to_currency, :rate
+
+ class Unknown < RuntimeError
+ end
+
+ def set(from_currency, to_currency, rate)
+ @from_currency, @to_currency = from_currency, to_currency
+ @rate = rate
+ end
+
+ def get(from_currency, to_currency)
+ return @rate if @from_currency == from_currency and @to_currency == to_currency
+ return 1 / @rate if @from_currency == to_currency and @to_currency == from_currency
+ end
+
+ def convert(from_currency, to_currency, amount)
+ if @from_currency == from_currency and @to_currency == to_currency then @rate * amount
+ elsif @from_currency == to_currency and @to_currency == from_currency then 1 / @rate * amount
+ else raise Unknown.new
+ end
+ end
+end
+
+class Money
+ include Comparable
+ attr_accessor :amount, :currency
+
+ class IncompatiableCurrencies < RuntimeError
+ end
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s
+ amount.round(2).to_s('F') + ' ' + currency.to_s
+ end
+
+ def in(currency_, exchange_rate)
+ new_amount = exchange_rate.convert(currency, currency_, amount)
+ Money.new new_amount, currency_
+ end
+
+ def +(other)
+ if currency == other.currency
+ Money.new(amount + other.amount, currency)
+ else
+ raise IncompatiableCurrencies.new
+ end
+ end
+
+ def -(other)
+ if currency == other.currency
+ Money.new(amount - other.amount, currency)
+ else
+ raise IncompatiableCurrencies.new
+ end
+ end
+
+ def *(number)
+ Money.new(amount * number, currency)
+ end
+
+ def /(number)
+ Money.new(amount / number, currency)
+ end
+
+ def ==(other)
+ raise ArgumentError.new('Not a suitable type') unless self.class == other.class
+ raise IncompatiableCurrencies.new unless currency == other.currency
+ return amount == other.amount
+ end
+
+ def <=>(other)
+ raise ArgumentError.new('Not a suitable type') unless self.class == other.class
+ raise IncompatiableCurrencies.new unless currency == other.currency
+ return -1 if amount < other.amount
+ return 0 if amount == other.amount
+ return 1 if amount > other.amount
+ end
+end

Филарета обнови решението на 14.01.2013 01:36 (преди почти 12 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
- attr_reader :from_currency, :to_currency, :rate
+ attr_reader :rates
class Unknown < RuntimeError
end
+ def initialize
+ @rates = {}
+ end
+
def set(from_currency, to_currency, rate)
- @from_currency, @to_currency = from_currency, to_currency
- @rate = rate
+ if from_currency == to_currency
+ @rates[{from_currency => to_currency}] = BigDecimal.new 1
+ else
+ @rates[{from_currency => to_currency}] = rate
+ @rates[{to_currency => from_currency}] = 1 / rate
+ end
end
def get(from_currency, to_currency)
- return @rate if @from_currency == from_currency and @to_currency == to_currency
- return 1 / @rate if @from_currency == to_currency and @to_currency == from_currency
+ @rates.each do |currency, rate|
+ @rate = rate if currency.has_key? from_currency and currency.has_value? to_currency
+ end
+ @rate
end
def convert(from_currency, to_currency, amount)
- if @from_currency == from_currency and @to_currency == to_currency then @rate * amount
- elsif @from_currency == to_currency and @to_currency == from_currency then 1 / @rate * amount
- else raise Unknown.new
+ rate = get(from_currency, to_currency)
+ if rate
+ rate * amount
+ else
+ raise Unknown.new
end
end
end
class Money
include Comparable
attr_accessor :amount, :currency
class IncompatiableCurrencies < RuntimeError
end
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
amount.round(2).to_s('F') + ' ' + currency.to_s
end
def in(currency_, exchange_rate)
new_amount = exchange_rate.convert(currency, currency_, amount)
Money.new new_amount, currency_
end
def +(other)
if currency == other.currency
Money.new(amount + other.amount, currency)
else
raise IncompatiableCurrencies.new
end
end
def -(other)
if currency == other.currency
Money.new(amount - other.amount, currency)
else
raise IncompatiableCurrencies.new
end
end
def *(number)
- Money.new(amount * number, currency)
+ if number > 0
+ Money.new(amount * number, currency)
+ else
+ raise ArgumentError.new("Invalid operation for money")
+ end
end
def /(number)
- Money.new(amount / number, currency)
+ if number > 0
+ Money.new(amount / number, currency)
+ else
+ raise ArgumentError.new("Invalid operation for money")
+ end
end
def ==(other)
raise ArgumentError.new('Not a suitable type') unless self.class == other.class
raise IncompatiableCurrencies.new unless currency == other.currency
return amount == other.amount
end
def <=>(other)
raise ArgumentError.new('Not a suitable type') unless self.class == other.class
raise IncompatiableCurrencies.new unless currency == other.currency
return -1 if amount < other.amount
return 0 if amount == other.amount
return 1 if amount > other.amount
end
end