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

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

Към профила на Йоана Тодорова

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
attr_accessor :exchange_hash
def initialize
@exchange_hash = {}
end
def set(from_currency, to_currency, rate)
unless from_currency==to_currency
@exchange_hash[[from_currency, to_currency]] = rate
@exchange_hash[[to_currency, from_currency]] = 1/rate
end
end
def get(from_currency, to_currency)
return 1 if from_currency==to_currency
return nil unless @exchange_hash[[from_currency, to_currency]]
@exchange_hash[[from_currency, to_currency]]
end
def convert(from_currency, to_currency, amount)
return BigDecimal.new(amount) if from_currency==to_currency
raise ExchangeRate::Unknown unless @exchange_hash[[from_currency, to_currency]]
BigDecimal.new amount*@exchange_hash[[from_currency, to_currency]]
end
end
class Money
include Comparable
class IncompatibleCurrencies < ArgumentError
end
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def amount
@amount
end
def currency
@currency
end
def to_s #oshte ne e qsno kak shte stane
amount_s = @amount.round(2).to_f.to_s
amount_s.gsub!(/(\d+\.)(\d+)/, $1 + $2.ljust(3-$2.length, '0')) if amount_s=~/(\d+\.)(\d+)/
"#{amount_s} #{@currency}"
end
def in(currency, exchange_rate)
Money.new exchange_rate.convert(@currency, currency, @amount), currency
end
def *(numeric)
raise Money::IncompatibleCurrencies unless numeric.kind_of? Numeric
Money.new @amount*numeric.to_d, @currency
end
def /(numeric)
raise Money::IncompatibleCurrencies unless numeric.kind_of? Numeric
Money.new @amount/numeric.to_d, @currency
end
def +(other)
raise ArgumentError unless @currency == other.currency
Money.new @amount+other.amount.to_d, @currency
end
def -(other)
raise ArgumentError unless @currency == other.currency
Money.new @amount-other.amount.to_d, @currency
end
def <=>(other)
@amount <=> other.amount
end
end

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

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

Failures:

  1) 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-blldjg/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)>'

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

  4) Money arithmetic raises Money::IncompatibleCurrencies for - with money with different currencies
     Failure/Error: expect do
       expected Money::IncompatibleCurrencies, got #<ArgumentError: ArgumentError>
     # /tmp/d20130203-23049-blldjg/spec.rb:152: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)>'

  5) 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-blldjg/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)>'

  6) 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-blldjg/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)>'

  7) Money comparison with <=> raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `amount' for :larodi:Symbol>
     # /tmp/d20130203-23049-blldjg/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)>'

  8) Money comparison with <=> raises IncompatibleCurrencies when currencies differ
     Failure/Error: expect do
       expected Money::IncompatibleCurrencies but nothing was raised
     # /tmp/d20130203-23049-blldjg/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)>'

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

  10) Money comparison with == raises IncompatibleCurrencies when currencies differ
     Failure/Error: expect do
       expected Money::IncompatibleCurrencies but nothing was raised
     # /tmp/d20130203-23049-blldjg/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)>'

  11) Money comparison with < raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `amount' for :larodi:Symbol>
     # /tmp/d20130203-23049-blldjg/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)>'

  12) Money comparison with < raises IncompatibleCurrencies when currencies differ
     Failure/Error: expect do
       expected Money::IncompatibleCurrencies but nothing was raised
     # /tmp/d20130203-23049-blldjg/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)>'

  13) Money comparison with <= raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `amount' for :larodi:Symbol>
     # /tmp/d20130203-23049-blldjg/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)>'

  14) Money comparison with <= raises IncompatibleCurrencies when currencies differ
     Failure/Error: expect do
       expected Money::IncompatibleCurrencies but nothing was raised
     # /tmp/d20130203-23049-blldjg/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)>'

  15) Money comparison with > raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `amount' for :larodi:Symbol>
     # /tmp/d20130203-23049-blldjg/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)>'

  16) Money comparison with > raises IncompatibleCurrencies when currencies differ
     Failure/Error: expect do
       expected Money::IncompatibleCurrencies but nothing was raised
     # /tmp/d20130203-23049-blldjg/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)>'

  17) Money comparison with >= raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `amount' for :larodi:Symbol>
     # /tmp/d20130203-23049-blldjg/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)>'

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

Failed examples:

rspec /tmp/d20130203-23049-blldjg/spec.rb:136 # Money arithmetic + with numeric objects raises an ArgumentError
rspec /tmp/d20130203-23049-blldjg/spec.rb:151 # Money arithmetic raises Money::IncompatibleCurrencies for + with money with different currencies
rspec /tmp/d20130203-23049-blldjg/spec.rb:136 # Money arithmetic - with numeric objects raises an ArgumentError
rspec /tmp/d20130203-23049-blldjg/spec.rb:151 # Money arithmetic raises Money::IncompatibleCurrencies for - with money with different currencies
rspec /tmp/d20130203-23049-blldjg/spec.rb:159 # Money arithmetic + with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-blldjg/spec.rb:159 # Money arithmetic - with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-blldjg/spec.rb:194 # Money comparison with <=> raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-blldjg/spec.rb:200 # Money comparison with <=> raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-blldjg/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-blldjg/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-blldjg/spec.rb:194 # Money comparison with < raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-blldjg/spec.rb:200 # Money comparison with < raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-blldjg/spec.rb:194 # Money comparison with <= raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-blldjg/spec.rb:200 # Money comparison with <= raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-blldjg/spec.rb:194 # Money comparison with > raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-blldjg/spec.rb:200 # Money comparison with > raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-blldjg/spec.rb:194 # Money comparison with >= raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-blldjg/spec.rb:200 # Money comparison with >= raises IncompatibleCurrencies when currencies differ

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

Йоана обнови решението на 16.01.2013 14:13 (преди почти 12 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ class Unknown < RuntimeError
+ end
+
+ attr_accessor :exchange_hash
+ def initialize
+ @exchange_hash = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ unless from_currency==to_currency
+ @exchange_hash[[from_currency, to_currency]] = rate
+ @exchange_hash[[to_currency, from_currency]] = 1/rate
+ end
+ end
+
+ def get(from_currency, to_currency)
+ return 1 if from_currency==to_currency
+ return nil unless @exchange_hash[[from_currency, to_currency]]
+ @exchange_hash[[from_currency, to_currency]]
+ end
+
+ def convert(from_currency, to_currency, amount)
+ return BigDecimal.new(amount) if from_currency==to_currency
+ raise ExchangeRate::Unknown unless @exchange_hash[[from_currency, to_currency]]
+ BigDecimal.new amount*@exchange_hash[[from_currency, to_currency]]
+ end
+end
+
+class Money
+ include Comparable
+
+ class IncompatibleCurrencies < ArgumentError
+ end
+
+ def initialize(amount, currency)
+ @amount, @currency = amount, currency
+ end
+
+ def amount
+ @amount
+ end
+
+ def currency
+ @currency
+ end
+
+ def to_s #oshte ne e qsno kak shte stane
+ amount_s = @amount.round(2).to_f.to_s
+ amount_s.gsub!(/(\d+\.)(\d+)/, $1 + $2.ljust(3-$2.length, '0')) if amount_s=~/(\d+\.)(\d+)/
+ "#{amount_s} #{@currency}"
+ end
+
+ def in(currency, exchange_rate)
+ Money.new exchange_rate.convert(@currency, currency, @amount), currency
+ end
+
+ def *(numeric)
+ raise Money::IncompatibleCurrencies unless numeric.kind_of? Numeric
+ Money.new @amount*numeric.to_d, @currency
+ end
+
+ def /(numeric)
+ raise Money::IncompatibleCurrencies unless numeric.kind_of? Numeric
+ Money.new @amount/numeric.to_d, @currency
+ end
+
+ def +(other)
+ raise ArgumentError unless @currency == other.currency
+ Money.new @amount+other.amount.to_d, @currency
+ end
+
+ def -(other)
+ raise ArgumentError unless @currency == other.currency
+ Money.new @amount-other.amount.to_d, @currency
+ end
+
+ def <=>(other)
+ @amount <=> other.amount
+ end
+end