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

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

Към профила на Стоян Стоянов

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
def initialize
@hash = Hash.new
end
def set(from_currency, to_currency, rate)
@hash[[from_currency,to_currency]] = rate
@hash[[to_currency,from_currency]] = 1/rate
@hash[[from_currency,from_currency]] = 1 if from_currency == to_currency
end
def get(from_currency, to_currency)
@hash[[from_currency, to_currency]]
end
def convert(from_currency, to_currency, amount)
raise Unknown unless @hash.has_key? [from_currency, to_currency]
get(from_currency, to_currency) * amount
end
class Unknown < RuntimeError
end
end
class Money
include Comparable
attr_accessor :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
return '0.00 ' + @currency.to_s if @amount.zero?
multiplied = (@amount.truncate(2) * 100).to_i.to_s
return multiplied.insert(-3,'0.') + ' ' + @currency.to_s if @amount < 1 and @amount > -1
multiplied.insert(-3,'.') + ' ' + @currency.to_s
end
def in(currency, exchange_rate)
Money.new(exchange_rate.convert(@currency,currency,@amount),currency)
end
def *(multiplier)
Money.new(@amount * multiplier,@currency)
end
def /(devisor)
Money.new(@amount / devisor, @currency)
end
def +(cash)
raise ArgumentError unless cash.instance_of? Money
raise Money::IncompatibleCurrencies if @currency != cash.currency
Money.new(@amount + cash.amount,@currency)
end
def -(cash)
Money.new(@amount - cash.amount,@currency)
end
def <=>(other)
raise ArgumentError unless other.instance_of? Money
raise IncompatibleCurrencies if @currency != other.currency
@amount <=> other.amount
end
def ==(other)
raise ArgumentError unless other.instance_of? Money
raise IncompatibleCurrencies if @currency != other.currency
@amount == other.amount
end
class IncompatibleCurrencies < StandardError
end
end

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

.......F....F....F..F.F...F.F.FFF..............

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:975381c,'0.1E1',9(36)>
            got: nil
       
       (compared using ==)
     # /tmp/d20130203-23049-19n49n2/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-19n49n2/solution.rb:20:in `convert'
     # /tmp/d20130203-23049-19n49n2/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 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-19n49n2/solution.rb:20:in `convert'
     # /tmp/d20130203-23049-19n49n2/solution.rb:47:in `in'
     # /tmp/d20130203-23049-19n49n2/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)>'

  4) Money arithmetic * with money objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<TypeError: Money can't be coerced into BigDecimal>
     # /tmp/d20130203-23049-19n49n2/spec.rb:129: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 money objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<TypeError: Money can't be coerced into BigDecimal>
     # /tmp/d20130203-23049-19n49n2/spec.rb:129: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 numeric objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `amount' for 42:Fixnum>
     # /tmp/d20130203-23049-19n49n2/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)>'

  7) Money arithmetic raises Money::IncompatibleCurrencies for - with money with different currencies
     Failure/Error: expect do
       expected Money::IncompatibleCurrencies but nothing was raised
     # /tmp/d20130203-23049-19n49n2/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)>'

  8) Money arithmetic - with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `amount' for "foobar":String>
     # /tmp/d20130203-23049-19n49n2/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)>'

  9) Money arithmetic * with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<TypeError: String can't be coerced into BigDecimal>
     # /tmp/d20130203-23049-19n49n2/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 #<TypeError: String can't be coerced into BigDecimal>
     # /tmp/d20130203-23049-19n49n2/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)>'

Finished in 0.07617 seconds
47 examples, 10 failures

Failed examples:

rspec /tmp/d20130203-23049-19n49n2/spec.rb:44 # ExchangeRate#get always returns 1 as the exchange rate between two identical currencies
rspec /tmp/d20130203-23049-19n49n2/spec.rb:71 # ExchangeRate#convert works for identical currencies without defining any rates
rspec /tmp/d20130203-23049-19n49n2/spec.rb:106 # Money convertion does not change the amount if the same currency is passed
rspec /tmp/d20130203-23049-19n49n2/spec.rb:128 # Money arithmetic * with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-19n49n2/spec.rb:128 # Money arithmetic / with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-19n49n2/spec.rb:136 # Money arithmetic - with numeric objects raises an ArgumentError
rspec /tmp/d20130203-23049-19n49n2/spec.rb:151 # Money arithmetic raises Money::IncompatibleCurrencies for - with money with different currencies
rspec /tmp/d20130203-23049-19n49n2/spec.rb:159 # Money arithmetic - with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-19n49n2/spec.rb:159 # Money arithmetic * with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-19n49n2/spec.rb:159 # Money arithmetic / with other objects raises an ArgumentError

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

Стоян обнови решението на 15.01.2013 00:47 (преди около 12 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ def initialize
+ @hash = Hash.new
+ end
+
+ def set(from_currency, to_currency, rate)
+ @hash[[from_currency,to_currency]] = rate
+ @hash[[to_currency,from_currency]] = 1/rate
+ @hash[[from_currency,from_currency]] = 1 if from_currency == to_currency
+ end
+
+ def get(from_currency, to_currency)
+ @hash[[from_currency, to_currency]]
+ end
+
+ def convert(from_currency, to_currency, amount)
+ raise Unknown unless @hash.has_key? [from_currency, to_currency]
+ get(from_currency, to_currency) * amount
+ end
+
+ class Unknown < RuntimeError
+ end
+
+end
+
+class Money
+ include Comparable
+
+ attr_accessor :amount, :currency
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s
+ return '0.00 ' + @currency.to_s if @amount.zero?
+ multiplied = (@amount.truncate(2) * 100).to_i.to_s
+ return multiplied.insert(-3,'0.') + ' ' + @currency.to_s if @amount < 1 and @amount > -1
+ multiplied.insert(-3,'.') + ' ' + @currency.to_s
+ end
+
+ def in(currency, exchange_rate)
+ Money.new(exchange_rate.convert(@currency,currency,@amount),currency)
+ end
+
+ def *(multiplier)
+ Money.new(@amount * multiplier,@currency)
+ end
+
+ def /(devisor)
+ Money.new(@amount / devisor, @currency)
+ end
+
+ def +(cash)
+ raise ArgumentError unless cash.instance_of? Money
+ raise Money::IncompatibleCurrencies if @currency != cash.currency
+ Money.new(@amount + cash.amount,@currency)
+ end
+
+ def -(cash)
+ Money.new(@amount - cash.amount,@currency)
+ end
+
+ def <=>(other)
+ raise ArgumentError unless other.instance_of? Money
+ raise Money::IncompatibleCurrencies if @currency != other.currency
+ @amount <=> other.amount
+ end
+
+ class IncompatibleCurrencies < StandardError
+ end
+end

Стоян обнови решението на 15.01.2013 00:58 (преди около 12 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
def initialize
@hash = Hash.new
end
def set(from_currency, to_currency, rate)
@hash[[from_currency,to_currency]] = rate
@hash[[to_currency,from_currency]] = 1/rate
@hash[[from_currency,from_currency]] = 1 if from_currency == to_currency
end
def get(from_currency, to_currency)
@hash[[from_currency, to_currency]]
end
def convert(from_currency, to_currency, amount)
raise Unknown unless @hash.has_key? [from_currency, to_currency]
get(from_currency, to_currency) * amount
end
class Unknown < RuntimeError
end
end
class Money
include Comparable
attr_accessor :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
return '0.00 ' + @currency.to_s if @amount.zero?
multiplied = (@amount.truncate(2) * 100).to_i.to_s
return multiplied.insert(-3,'0.') + ' ' + @currency.to_s if @amount < 1 and @amount > -1
multiplied.insert(-3,'.') + ' ' + @currency.to_s
end
def in(currency, exchange_rate)
Money.new(exchange_rate.convert(@currency,currency,@amount),currency)
end
def *(multiplier)
Money.new(@amount * multiplier,@currency)
end
def /(devisor)
Money.new(@amount / devisor, @currency)
end
def +(cash)
raise ArgumentError unless cash.instance_of? Money
raise Money::IncompatibleCurrencies if @currency != cash.currency
Money.new(@amount + cash.amount,@currency)
end
def -(cash)
Money.new(@amount - cash.amount,@currency)
end
def <=>(other)
raise ArgumentError unless other.instance_of? Money
- raise Money::IncompatibleCurrencies if @currency != other.currency
+ raise IncompatibleCurrencies if @currency != other.currency
@amount <=> other.amount
+ end
+
+ def ==(other)
+ raise ArgumentError unless other.instance_of? Money
+ raise IncompatibleCurrencies if @currency != other.currency
+ @amount == other.amount
end
class IncompatibleCurrencies < StandardError
end
end