Решение на Шеста задача от Стоян Найденов

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

Към профила на Стоян Найденов

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 36 успешни тест(а)
  • 11 неуспешни тест(а)

Код

require 'bigdecimal'
require 'bigdecimal/util'
class Money
attr_reader :amount, :currency
include Comparable
class IncompatibleCurrencies < RuntimeError
end
def initialize(amount, currency)
@amount = BigDecimal.new(amount)
@currency = currency
end
def to_s
"#{amount.ceil(2).to_s('F')} #{currency}"
end
def in(currency, exchange_rate)
rate = exchange_rate.get @currency, currency
Money.new rate * @amount, currency
end
def <=>(other)
raise ArgumentsError if other.class != self.class
raise IncompatibleCurrencies if other.currency != self.currency
return 1 if self.amount > other.amount
self.amount < other.amount ? -1 : 0
end
def *(other)
if other.kind_of? Numeric
Money.new @amount * other, @currency
else
raise ArgumentError
end
end
def /(other)
if other.kind_of? Numeric
Money.new @amount / other, @currency
else
raise ArgumentError
end
end
def -(other)
if self.class == other.class
if(self.currency == other.currency)
Money.new amount - other.amount, currency
else
raise IncompatibleCurrencies
end
else
raise ArgumentError
end
end
def +(other)
if self.class == other.class
if(self.currency == other.currency)
Money.new amount + other.amount, currency
else
raise IncompatibleCurrencies
end
else
raise ArgumentError
end
end
end
class Rate
def initialize(from_currency, to_currency, rate)
@from_currency = from_currency
@to_currency = to_currency
@rate = rate
end
def get(from_currency, to_currency)
if from_currency == @from_currency and to_currency == @to_currency
@rate
elsif from_currency == @to_currency and to_currency == @from_currency
(1 / @rate)
end
end
end
class ExchangeRate #would call it ExchangeRates, upper class would be ExchangeRate
class Unknown < RuntimeError
end
def initialize
@exchange_rates = []
end
def set(from_currency, to_currency, rate)
@exchange_rates << Rate.new(from_currency, to_currency, rate)
end
def get(from_currency, to_currency)
return BigDecimal.new(1) if from_currency == to_currency
rate = @exchange_rates.find { |rate| rate.get(from_currency, to_currency) != nil }
return nil if rate == nil
rate.get(from_currency, to_currency)
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
raise Unknown if rate == nil
return rate * amount
end
end

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

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

Failures:

  1) ExchangeRate#set allows rates to be updated if called multiple times
     Failure/Error: expect do
       result should have been changed to #<BigDecimal:a60dc40,'0.149E1',18(18)>, but is now #<BigDecimal:a4d6cdc,'0.15E1',18(18)>
     # /tmp/d20130203-23049-pf9na9/spec.rb:16: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#set sets the exchange rate in both directions
     Failure/Error: rate.get(:EUR, :BGN).should eq '1.25'.to_d
       
       expected: #<BigDecimal:a3da068,'0.125E1',18(18)>
            got: #<BigDecimal:a60c37c,'0.2E1',9(36)>
       
       (compared using ==)
     # /tmp/d20130203-23049-pf9na9/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)>'

  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-pf9na9/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 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-pf9na9/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)>'

  5) Money comparison with <=> raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NameError: uninitialized constant Money::ArgumentsError>
     # /tmp/d20130203-23049-pf9na9/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)>'

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

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

  8) Money comparison with < raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NameError: uninitialized constant Money::ArgumentsError>
     # /tmp/d20130203-23049-pf9na9/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)>'

  9) Money comparison with <= raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NameError: uninitialized constant Money::ArgumentsError>
     # /tmp/d20130203-23049-pf9na9/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 ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NameError: uninitialized constant Money::ArgumentsError>
     # /tmp/d20130203-23049-pf9na9/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)>'

  11) Money comparison with >= raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NameError: uninitialized constant Money::ArgumentsError>
     # /tmp/d20130203-23049-pf9na9/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)>'

Finished in 0.06787 seconds
47 examples, 11 failures

Failed examples:

rspec /tmp/d20130203-23049-pf9na9/spec.rb:13 # ExchangeRate#set allows rates to be updated if called multiple times
rspec /tmp/d20130203-23049-pf9na9/spec.rb:21 # ExchangeRate#set sets the exchange rate in both directions
rspec /tmp/d20130203-23049-pf9na9/spec.rb:88 # Money has a custom to_s representation
rspec /tmp/d20130203-23049-pf9na9/spec.rb:110 # Money convertion raises an ExchangeRate::Unknown exception for unknown rates
rspec /tmp/d20130203-23049-pf9na9/spec.rb:194 # Money comparison with <=> raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-pf9na9/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-pf9na9/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-pf9na9/spec.rb:194 # Money comparison with < raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-pf9na9/spec.rb:194 # Money comparison with <= raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-pf9na9/spec.rb:194 # Money comparison with > raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-pf9na9/spec.rb:194 # Money comparison with >= raises ArgumentError when comparing with other objects

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

Стоян обнови решението на 16.01.2013 16:53 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class Money
+ attr_reader :amount, :currency
+
+ include Comparable
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ def initialize(amount, currency)
+ @amount = BigDecimal.new(amount)
+ @currency = currency
+ end
+
+ def to_s
+ "#{amount.ceil(2).to_s('F')} #{currency}"
+ end
+
+ def in(currency, exchange_rate)
+ rate = exchange_rate.get @currency, currency
+ Money.new rate * @amount, currency
+ end
+
+ def <=>(other)
+ raise ArgumentsError if other.class != self.class
+ raise IncompatibleCurrencies if other.currency != self.currency
+ return 1 if self.amount > other.amount
+ self.amount < other.amount ? -1 : 0
+ end
+
+ def *(other)
+ if other.kind_of? Numeric
+ Money.new @amount * other, @currency
+ else
+ raise ArgumentError
+ end
+ end
+
+ def /(other)
+ if other.kind_of? Numeric
+ Money.new @amount / other, @currency
+ else
+ raise ArgumentError
+ end
+ end
+
+ def -(other)
+ if self.class == other.class
+ if(self.currency == other.currency)
+ Money.new amount - other.amount, currency
+ else
+ raise IncompatibleCurrencies
+ end
+ else
+ raise ArgumentError
+ end
+ end
+
+ def +(other)
+ if self.class == other.class
+ if(self.currency == other.currency)
+ Money.new amount + other.amount, currency
+ else
+ raise IncompatibleCurrencies
+ end
+ else
+ raise ArgumentError
+ end
+ end
+end
+
+class Rate
+ def initialize(from_currency, to_currency, rate)
+ @from_currency = from_currency
+ @to_currency = to_currency
+ @rate = rate
+ end
+
+ def get(from_currency, to_currency)
+ if from_currency == @from_currency and to_currency == @to_currency
+ @rate
+ elsif from_currency == @to_currency and to_currency == @from_currency
+ (1 / @rate)
+ end
+ end
+end
+
+class ExchangeRate #would call it ExchangeRates, upper class would be ExchangeRate
+ def initialize
+ @exchange_rates = []
+ end
+
+ def set(from_currency, to_currency, rate)
+ @exchange_rates << Rate.new(from_currency, to_currency, rate)
+ end
+
+ def get(from_currency, to_currency)
+ return BigDecimal.new(1) if from_currency == to_currency
+ rate = @exchange_rates.find { |rate| rate.get(from_currency, to_currency) != nil }
+ return nil if rate == nil
+ rate.get(from_currency, to_currency)
+ end
+
+ def convert(from_currency, to_currency, amount)
+ rate = get(from_currency, to_currency)
+ return rate * amount
+ end
+end

Стоян обнови решението на 16.01.2013 16:57 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class Money
attr_reader :amount, :currency
include Comparable
class IncompatibleCurrencies < RuntimeError
end
def initialize(amount, currency)
@amount = BigDecimal.new(amount)
@currency = currency
end
def to_s
"#{amount.ceil(2).to_s('F')} #{currency}"
end
def in(currency, exchange_rate)
rate = exchange_rate.get @currency, currency
Money.new rate * @amount, currency
end
def <=>(other)
raise ArgumentsError if other.class != self.class
raise IncompatibleCurrencies if other.currency != self.currency
return 1 if self.amount > other.amount
self.amount < other.amount ? -1 : 0
end
def *(other)
if other.kind_of? Numeric
Money.new @amount * other, @currency
else
raise ArgumentError
end
end
def /(other)
if other.kind_of? Numeric
Money.new @amount / other, @currency
else
raise ArgumentError
end
end
def -(other)
if self.class == other.class
if(self.currency == other.currency)
Money.new amount - other.amount, currency
else
raise IncompatibleCurrencies
end
else
raise ArgumentError
end
end
def +(other)
if self.class == other.class
if(self.currency == other.currency)
Money.new amount + other.amount, currency
else
raise IncompatibleCurrencies
end
else
raise ArgumentError
end
end
end
class Rate
def initialize(from_currency, to_currency, rate)
@from_currency = from_currency
@to_currency = to_currency
@rate = rate
end
def get(from_currency, to_currency)
if from_currency == @from_currency and to_currency == @to_currency
@rate
elsif from_currency == @to_currency and to_currency == @from_currency
(1 / @rate)
end
end
end
class ExchangeRate #would call it ExchangeRates, upper class would be ExchangeRate
+ class Unknown < RuntimeError
+ end
+
def initialize
@exchange_rates = []
end
def set(from_currency, to_currency, rate)
@exchange_rates << Rate.new(from_currency, to_currency, rate)
end
def get(from_currency, to_currency)
return BigDecimal.new(1) if from_currency == to_currency
rate = @exchange_rates.find { |rate| rate.get(from_currency, to_currency) != nil }
return nil if rate == nil
rate.get(from_currency, to_currency)
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
+ raise Unknown if rate == nil
return rate * amount
end
end