Решение на Шеста задача от Цвета Гергичанова

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

Към профила на Цвета Гергичанова

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
def initialize
@currency_table = {}
end
def set(from_currency, to_currency, rate)
if from_currency != to_currency
@currency_table[[from_currency, to_currency]] = rate
@currency_table[[to_currency, from_currency]] = 1/rate
end
end
def get(from_currency, to_currency)
if from_currency == to_currency
1
elsif @currency_table.has_key?([from_currency, to_currency])
@currency_table[[from_currency, to_currency]]
else
nil
end
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
if rate == nil
raise ExchangeRate::Unknown < RuntimeError
else
(amount*rate).to_d
end
end
end
class Money
include Comparable
attr_accessor :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
if amount%1 == 0
amount.round(2).to_s + "0 " + currency.to_s
else
amount.round(2).to_s('F') + " " + currency.to_s
end
end
def in(currency, exchange_rate)
Money.new exchange_rate.convert(@currency, currency, @amount), currency
end
def *(number)
Money.new(@amount * number, @currency)
end
def /(number)
Money.new(@amount / number, @currency)
end
def +(other)
if other.class != Money
raise ArgumentError
elsif @currency == other.currency
Money.new(@amount + other.amount, @currency)
else
raise Money::IncompatibleCurrencies
end
end
def -(other)
if other.class != Money
raise ArgumentError
elsif @currency == other.currency
Money.new(@amount - other.amount, @currency)
else
raise Money::IncompatibleCurrencies
end
end
def <=>(other)
if other.class != Money
raise ArgumentError
elsif @currency == other.currency
amount <=> other.amount
else
raise Money::IncompatibleCurrencies
end
end
end

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

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

Failures:

  1) ExchangeRate#convert raises an ExchangeRate::Unknown exception when the rate is not defined
     Failure/Error: end.to raise_error(ExchangeRate::Unknown)
     NameError:
       uninitialized constant ExchangeRate::Unknown
     # /tmp/d20130203-23049-96rq71/spec.rb:58: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) 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-96rq71/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)>'

  3) Money convertion raises an ExchangeRate::Unknown exception for unknown rates
     Failure/Error: end.to raise_error(ExchangeRate::Unknown)
     NameError:
       uninitialized constant ExchangeRate::Unknown
     # /tmp/d20130203-23049-96rq71/spec.rb:113: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-96rq71/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-96rq71/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 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-96rq71/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 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-96rq71/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)>'

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

  11) Money comparison with == raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-96rq71/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: end.to raise_error(Money::IncompatibleCurrencies)
     NameError:
       uninitialized constant Money::IncompatibleCurrencies
     # /tmp/d20130203-23049-96rq71/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-96rq71/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-96rq71/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-96rq71/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-96rq71/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.06827 seconds
47 examples, 16 failures

Failed examples:

rspec /tmp/d20130203-23049-96rq71/spec.rb:55 # ExchangeRate#convert raises an ExchangeRate::Unknown exception when the rate is not defined
rspec /tmp/d20130203-23049-96rq71/spec.rb:88 # Money has a custom to_s representation
rspec /tmp/d20130203-23049-96rq71/spec.rb:110 # Money convertion raises an ExchangeRate::Unknown exception for unknown rates
rspec /tmp/d20130203-23049-96rq71/spec.rb:128 # Money arithmetic * with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-96rq71/spec.rb:128 # Money arithmetic / with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-96rq71/spec.rb:151 # Money arithmetic raises Money::IncompatibleCurrencies for + with money with different currencies
rspec /tmp/d20130203-23049-96rq71/spec.rb:151 # Money arithmetic raises Money::IncompatibleCurrencies for - with money with different currencies
rspec /tmp/d20130203-23049-96rq71/spec.rb:159 # Money arithmetic * with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-96rq71/spec.rb:159 # Money arithmetic / with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-96rq71/spec.rb:200 # Money comparison with <=> raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-96rq71/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-96rq71/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-96rq71/spec.rb:200 # Money comparison with < raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-96rq71/spec.rb:200 # Money comparison with <= raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-96rq71/spec.rb:200 # Money comparison with > raises IncompatibleCurrencies when currencies differ
rspec /tmp/d20130203-23049-96rq71/spec.rb:200 # Money comparison with >= raises IncompatibleCurrencies when currencies differ

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

Цвета обнови решението на 16.01.2013 12:57 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ def initialize
+ @currency_table = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ if from_currency != to_currency
+ @currency_table[[from_currency, to_currency]] = rate
+ @currency_table[[to_currency, from_currency]] = 1/rate
+ end
+ end
+
+ def get(from_currency, to_currency)
+ if @currency_table.has_key?([from_currency, to_currency])
+ @currency_table[[from_currency, to_currency]]
+ else
+ nil
+ end
+ end
+
+ def convert(from_currency, to_currency, amount)
+ rate = get(from_currency, to_currency)
+ if rate == nil
+ raise ExchangeRate::Unknown < RuntimeError
+ else
+ (amount*rate).to_d
+ end
+ end
+end
+
+class Money
+ include Comparable
+
+ attr_accessor :amount, :currency
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s
+ if amount%1 == 0
+ amount.round(2).to_s + "0 " + currency.to_s
+ else
+ amount.round(2).to_s('F') + " " + currency.to_s
+ end
+ end
+
+ def in(currency, exchange_rate)
+ Money.new exchange_rate.convert(@currency, currency, @amount), currency
+ end
+
+ def *(number)
+ Money.new(@amount * number, @currency)
+ end
+
+ def /(number)
+ Money.new(@amount / number, @currency)
+ end
+
+ def +(other)
+ if other.class != Money
+ raise ArgumentError
+ elsif @currency == other.currency
+ Money.new(@amount + other.amount, @currency)
+ else
+ raise Money::IncompatibleCurrencies
+ end
+ end
+
+ def -(other)
+ if other.class != Money
+ raise ArgumentError
+ elsif @currency == other.currency
+ Money.new(@amount - other.amount, @currency)
+ else
+ raise Money::IncompatibleCurrencies
+ end
+ end
+
+ def <=>(other)
+ if other.class != Money
+ raise ArgumentError
+ elsif @currency == other.currency
+ amount <=> other.amount
+ else
+ raise Money::IncompatibleCurrencies
+ end
+ end
+end

Цвета обнови решението на 16.01.2013 13:23 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
def initialize
@currency_table = {}
end
def set(from_currency, to_currency, rate)
if from_currency != to_currency
@currency_table[[from_currency, to_currency]] = rate
@currency_table[[to_currency, from_currency]] = 1/rate
end
end
def get(from_currency, to_currency)
- if @currency_table.has_key?([from_currency, to_currency])
+ if from_currency == to_currency
+ 1
+ elsif @currency_table.has_key?([from_currency, to_currency])
@currency_table[[from_currency, to_currency]]
else
nil
end
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
if rate == nil
raise ExchangeRate::Unknown < RuntimeError
else
(amount*rate).to_d
end
end
end
class Money
include Comparable
attr_accessor :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
if amount%1 == 0
amount.round(2).to_s + "0 " + currency.to_s
else
amount.round(2).to_s('F') + " " + currency.to_s
end
end
def in(currency, exchange_rate)
Money.new exchange_rate.convert(@currency, currency, @amount), currency
end
def *(number)
Money.new(@amount * number, @currency)
end
def /(number)
Money.new(@amount / number, @currency)
end
def +(other)
if other.class != Money
raise ArgumentError
elsif @currency == other.currency
Money.new(@amount + other.amount, @currency)
else
raise Money::IncompatibleCurrencies
end
end
def -(other)
if other.class != Money
raise ArgumentError
elsif @currency == other.currency
Money.new(@amount - other.amount, @currency)
else
raise Money::IncompatibleCurrencies
end
end
def <=>(other)
if other.class != Money
raise ArgumentError
elsif @currency == other.currency
amount <=> other.amount
else
raise Money::IncompatibleCurrencies
end
end
end