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

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

Към профила на Мартина Величкова

Резултати

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

Код

class CurrencyPair
attr_reader :from_currency, :to_currency
def initialize(from_currency, to_currency)
@from_currency, @to_currency = from_currency, to_currency
end
def ==(other)
@from_currency == other.from_currency and
@to_currency == other.to_currency
end
alias eql? ==
def hash
[@from_currency, @to_currency].hash
end
end
class ExchangeRate
require 'bigdecimal'
require 'bigdecimal/util'
class Unknown < RuntimeError
end
def initialize
@quotations = {}
end
def set(from_currency, to_currency, rate)
@quotations[CurrencyPair.new(from_currency, to_currency)] = rate
@quotations[CurrencyPair.new(to_currency, from_currency)] = 1.quo(rate)
end
def get(from_currency, to_currency)
if from_currency == to_currency
'1'.to_d
else
@quotations[CurrencyPair.new(from_currency, to_currency)]
end
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
raise Unknown if rate.nil?
BigDecimal.new(amount * rate)
end
end
class Money
require 'bigdecimal'
require 'bigdecimal/util'
attr_reader :amount, :currency
class IncompatibleCurrencies < RuntimeError
end
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
"#{amount.ceil(2).to_digits} #{currency}"
end
def in(to_currency, exchange_rate)
new_amount = exchange_rate.convert(currency, to_currency, amount)
Money.new(new_amount, to_currency)
end
def *(other)
raise ArgumentError unless other.kind_of?(Numeric)
new_amount = other * amount
Money.new(new_amount, currency)
end
def /(other)
raise ArgumentError unless other.kind_of?(Numeric)
new_amount = amount / other
Money.new(new_amount, currency)
end
def +(other)
raise ArgumentError unless other.kind_of?(Money)
raise IncompatibleCurrencies unless currency == other.currency
new_amount = amount + other.amount
Money.new(new_amount, currency)
end
def -(other)
raise ArgumentError unless other.kind_of?(Money)
raise IncompatibleCurrencies unless currency == other.currency
new_amount = amount - other.amount
Money.new(new_amount, currency)
end
def <(other)
raise ArgumentError unless other.kind_of?(Money)
raise IncompatibleCurrencies unless currency == other.currency
amount < other.amount
end
def <=(other)
raise ArgumentError unless other.kind_of?(Money)
raise IncompatibleCurrencies unless currency == other.currency
amount <= other.amount
end
def >=(other)
raise ArgumentError unless other.kind_of?(Money)
raise IncompatibleCurrencies unless currency == other.currency
amount >= other.amount
end
def ==(other)
raise ArgumentError unless other.kind_of?(Money)
raise IncompatibleCurrencies unless currency == other.currency
amount == other.amount
end
def <=>(other)
raise ArgumentError unless other.kind_of?(Money)
raise IncompatibleCurrencies unless currency == other.currency
amount <=> other.amount
end
end

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

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

Failures:

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

  2) Money comparison works when currencies are the same
     Failure/Error: (a > b).should be_true
     NoMethodError:
       undefined method `>' for 12.45 USD:Money
     # /tmp/d20130203-23049-jb0aoz/spec.rb:176: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 comparison with > raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `>' for 12.0 USD:Money>
     # /tmp/d20130203-23049-jb0aoz/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)>'

  4) Money comparison with > raises IncompatibleCurrencies when currencies differ
     Failure/Error: expect do
       expected Money::IncompatibleCurrencies, got #<NoMethodError: undefined method `>' for 12.0 USD:Money>
     # /tmp/d20130203-23049-jb0aoz/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.07476 seconds
47 examples, 4 failures

Failed examples:

rspec /tmp/d20130203-23049-jb0aoz/spec.rb:88 # Money has a custom to_s representation
rspec /tmp/d20130203-23049-jb0aoz/spec.rb:168 # Money comparison works when currencies are the same
rspec /tmp/d20130203-23049-jb0aoz/spec.rb:194 # Money comparison with > raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-jb0aoz/spec.rb:200 # Money comparison with > raises IncompatibleCurrencies when currencies differ

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

Мартина обнови решението на 14.01.2013 20:23 (преди почти 12 години)

+class CurrencyPair
+ attr_reader :from_currency, :to_currency
+
+ def initialize(from_currency, to_currency)
+ @from_currency, @to_currency = from_currency, to_currency
+ end
+
+ def ==(other)
+ @from_currency == other.from_currency and
+ @to_currency == other.to_currency
+ end
+
+ alias eql? ==
+
+ def hash
+ [@from_currency, @to_currency].hash
+ end
+end
+
+class ExchangeRate
+ require 'bigdecimal'
+ require 'bigdecimal/util'
+
+ class Unknown < RuntimeError
+ end
+
+ def initialize
+ @quotations = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ @quotations[CurrencyPair.new(from_currency, to_currency)] = rate
+ @quotations[CurrencyPair.new(to_currency, from_currency)] = 1.quo(rate)
+ end
+
+ def get(from_currency, to_currency)
+ if from_currency == to_currency
+ '1'.to_d
+ else
+ @quotations[CurrencyPair.new(from_currency, to_currency)]
+ end
+ end
+
+ def convert(from_currency, to_currency, amount)
+ rate = get(from_currency, to_currency)
+ raise Unknown if rate.nil?
+ BigDecimal.new(amount * rate)
+ end
+end
+
+class Money
+ require 'bigdecimal'
+ require 'bigdecimal/util'
+
+ attr_reader :amount, :currency
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ def initialize(amount, currency)
+ @amount, @currency = amount, currency
+ end
+
+ def to_s
+ "#{amount.ceil(2).to_digits} #{currency}"
+ end
+
+ def in(to_currency, exchange_rate)
+ new_amount = exchange_rate.convert(currency, to_currency, amount)
+ Money.new(new_amount, to_currency)
+ end
+
+ def *(other)
+ raise ArgumentError unless other.kind_of?(Numeric)
+ new_amount = other * amount
+ Money.new(new_amount, currency)
+ end
+
+ def /(other)
+ raise ArgumentError unless other.kind_of?(Numeric)
+ new_amount = amount / other
+ Money.new(new_amount, currency)
+ end
+
+ def +(other)
+ raise ArgumentError unless other.kind_of?(Money)
+ raise IncompatibleCurrencies unless currency == other.currency
+ new_amount = amount + other.amount
+ Money.new(new_amount, currency)
+ end
+
+ def -(other)
+ raise ArgumentError unless other.kind_of?(Money)
+ raise IncompatibleCurrencies unless currency == other.currency
+ new_amount = amount - other.amount
+ Money.new(new_amount, currency)
+ end
+
+ def <(other)
+ raise ArgumentError unless other.kind_of?(Money)
+ raise IncompatibleCurrencies unless currency == other.currency
+ amount < other.amount
+ end
+
+ def <=(other)
+ raise ArgumentError unless other.kind_of?(Money)
+ raise IncompatibleCurrencies unless currency == other.currency
+ amount <= other.amount
+ end
+
+ def >=(other)
+ raise ArgumentError unless other.kind_of?(Money)
+ raise IncompatibleCurrencies unless currency == other.currency
+ amount >= other.amount
+ end
+
+ def ==(other)
+ raise ArgumentError unless other.kind_of?(Money)
+ raise IncompatibleCurrencies unless currency == other.currency
+ amount == other.amount
+ end
+
+ def <=>(other)
+ raise ArgumentError unless other.kind_of?(Money)
+ raise IncompatibleCurrencies unless currency == other.currency
+ amount <=> other.amount
+ end
+end