Решение на Шеста задача от Нели Хатева

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

Към профила на Нели Хатева

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
attr_accessor :exchange_rates
def initialize
@exchange_rates = {}
end
def set(from_currency, to_currency, rate)
if from_currency == to_currency
return
end
exchange_rates[[from_currency, to_currency]] = rate
exchange_rates[[to_currency, from_currency]] = 1 / rate
end
def get(from_currency, to_currency)
if from_currency == to_currency
return 1.to_d
end
exchange_rates[[from_currency, to_currency]]
end
def convert(from_currency, to_currency, amount)
if from_currency == to_currency
return amount
end
exchange_rate = get(from_currency, to_currency)
if exchange_rate == nil then raise ExchangeRate::Unknown
else amount * exchange_rate
end
end
class Unknown < RuntimeError
end
end
class Money
include Comparable
attr_accessor :amount, :currency
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
number_of_zeros = if amount.frac.to_s('f').length == 3 then 1 else 0 end
amount.round(2).to_s('f') + "0" * number_of_zeros + " " + currency.to_s
end
def in(to_currency, exchange_rate)
begin
exchange_rate.convert(currency, to_currency, amount)
rescue
raise
else
Money.new exchange_rate.convert(currency, to_currency, amount), to_currency
end
end
def *(number)
Money.new amount * number.to_d, currency
end
def /(number)
Money.new amount / number.to_d, currency
end
def +(other)
if self.class != other.class
raise ArgumentError
elsif currency != other.currency
raise Money::IncompatibleCurrencies
else
Money.new amount + other.amount, currency
end
end
def -(other)
if self.class != other.class
raise ArgumentError
elsif currency != other.currency
raise Money::IncompatibleCurrencies
else
Money.new amount - other.amount, currency
end
end
def <=>(other)
if self.class != other.class
raise ArgumentError
elsif currency != other.currency
raise Money::IncompatibleCurrencies
else
compare_to other
end
end
def compare_to(other)
if amount == other.amount then 0
elsif amount > other.amount then 1
else -1
end
end
class IncompatibleCurrencies < RuntimeError
end
end

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

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

Failures:

  1) Money arithmetic * with money objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `to_d' for 5.00 USD:Money>
     # /tmp/d20130203-23049-1tj8674/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)>'

  2) Money arithmetic / with money objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError, got #<NoMethodError: undefined method `to_d' for 5.00 USD:Money>
     # /tmp/d20130203-23049-1tj8674/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)>'

  3) Money arithmetic * with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-1tj8674/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)>'

  4) Money arithmetic / with other objects raises an ArgumentError
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-1tj8674/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)>'

  5) Money comparison with == raises ArgumentError when comparing with other objects
     Failure/Error: expect do
       expected ArgumentError but nothing was raised
     # /tmp/d20130203-23049-1tj8674/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 IncompatibleCurrencies when currencies differ
     Failure/Error: expect do
       expected Money::IncompatibleCurrencies but nothing was raised
     # /tmp/d20130203-23049-1tj8674/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.05806 seconds
47 examples, 6 failures

Failed examples:

rspec /tmp/d20130203-23049-1tj8674/spec.rb:128 # Money arithmetic * with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-1tj8674/spec.rb:128 # Money arithmetic / with money objects raises an ArgumentError
rspec /tmp/d20130203-23049-1tj8674/spec.rb:159 # Money arithmetic * with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1tj8674/spec.rb:159 # Money arithmetic / with other objects raises an ArgumentError
rspec /tmp/d20130203-23049-1tj8674/spec.rb:194 # Money comparison with == raises ArgumentError when comparing with other objects
rspec /tmp/d20130203-23049-1tj8674/spec.rb:200 # Money comparison with == raises IncompatibleCurrencies when currencies differ

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

Нели обнови решението на 11.01.2013 00:51 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ attr_accessor :hash_rate
+
+ def initialize
+ @hash_rate = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ if from_currency == to_currency
+ return
+ end
+ hash_rate[[from_currency, to_currency]] = rate
+ hash_rate[[to_currency, from_currency]] = 1 / rate
+ end
+
+ def get(from_currency, to_currency)
+ if from_currency == to_currency
+ return 1.to_d
+ end
+ hash_rate[[from_currency, to_currency]]
+ end
+
+ def convert(from_currency, to_currency, amount)
+ if from_currency == to_currency
+ return amount
+ end
+ exchange_rate = get(from_currency, to_currency)
+ if exchange_rate == nil then raise ExchangeRate::Unknown
+ else amount * get(from_currency, to_currency)
+ end
+ end
+
+ class Unknown < RuntimeError
+ end
+end
+
+class Money
+ attr_accessor :amount, :currency
+
+ def initialize(amount, currency)
+ @amount, @currency = amount, currency
+ end
+
+ def to_s
+ number_of_zeros = if amount.frac.to_s('f') .length == 3 then 1 else 0 end
+ amount.round(2).to_s('f') + "0" * number_of_zeros + " " + currency.to_s
+ end
+end

Нели обнови решението на 11.01.2013 19:13 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
attr_accessor :hash_rate
def initialize
@hash_rate = {}
end
def set(from_currency, to_currency, rate)
if from_currency == to_currency
return
end
hash_rate[[from_currency, to_currency]] = rate
hash_rate[[to_currency, from_currency]] = 1 / rate
end
def get(from_currency, to_currency)
if from_currency == to_currency
return 1.to_d
end
hash_rate[[from_currency, to_currency]]
end
def convert(from_currency, to_currency, amount)
if from_currency == to_currency
return amount
end
exchange_rate = get(from_currency, to_currency)
if exchange_rate == nil then raise ExchangeRate::Unknown
else amount * get(from_currency, to_currency)
end
end
class Unknown < RuntimeError
end
end
class Money
+ include Comparable
attr_accessor :amount, :currency
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
number_of_zeros = if amount.frac.to_s('f') .length == 3 then 1 else 0 end
amount.round(2).to_s('f') + "0" * number_of_zeros + " " + currency.to_s
+ end
+
+ def in(to_currency, exchange_rate)
+ begin
+ exchange_rate.convert(currency, to_currency, amount)
+ rescue
+ raise
+ else
+ Money.new exchange_rate.convert(currency, to_currency, amount), to_currency
+ end
+ end
+
+ def *(number)
+ Money.new amount * number.to_d,currency
+ end
+
+ def /(number)
+ Money.new amount / number.to_d,currency
+ end
+
+ def +(other)
+ if self.class != other.class
+ raise ArgumentError
+ elsif currency != other.currency
+ raise Money::IncompatibleCurrencies
+ else
+ Money.new amount + other.amount, currency
+ end
+ end
+
+ def -(other)
+ if self.class != other.class
+ raise ArgumentError
+ elsif currency != other.currency
+ raise Money::IncompatibleCurrencies
+ else
+ Money.new amount - other.amount, currency
+ end
+ end
+
+ def <=>(other)
+ if self.class != other.class
+ raise ArgumentError
+ elsif currency != other.currency
+ raise Money::IncompatibleCurrencies
+ else
+ compare_to other
+ end
+ end
+
+ def compare_to(other)
+ if amount == other.amount then 0
+ elsif amount > other.amount then 1
+ else -1
+ end
+ end
+
+ class IncompatibleCurrencies < RuntimeError
end
end

Нели обнови решението на 14.01.2013 00:01 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
- attr_accessor :hash_rate
+ attr_accessor :rates
def initialize
- @hash_rate = {}
+ @rates = {}
end
def set(from_currency, to_currency, rate)
if from_currency == to_currency
return
end
- hash_rate[[from_currency, to_currency]] = rate
- hash_rate[[to_currency, from_currency]] = 1 / rate
+ rates[[from_currency, to_currency]] = rate
+ rates[[to_currency, from_currency]] = 1 / rate
end
def get(from_currency, to_currency)
if from_currency == to_currency
return 1.to_d
end
- hash_rate[[from_currency, to_currency]]
+ rates[[from_currency, to_currency]]
end
def convert(from_currency, to_currency, amount)
if from_currency == to_currency
return amount
end
exchange_rate = get(from_currency, to_currency)
if exchange_rate == nil then raise ExchangeRate::Unknown
- else amount * get(from_currency, to_currency)
+ else amount * exchange_rate
end
end
class Unknown < RuntimeError
end
end
class Money
include Comparable
attr_accessor :amount, :currency
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
- number_of_zeros = if amount.frac.to_s('f') .length == 3 then 1 else 0 end
+ number_of_zeros = if amount.frac.to_s('f').length == 3 then 1 else 0 end
amount.round(2).to_s('f') + "0" * number_of_zeros + " " + currency.to_s
end
def in(to_currency, exchange_rate)
begin
exchange_rate.convert(currency, to_currency, amount)
rescue
raise
else
Money.new exchange_rate.convert(currency, to_currency, amount), to_currency
end
end
def *(number)
- Money.new amount * number.to_d,currency
+ Money.new amount * number.to_d, currency
end
def /(number)
- Money.new amount / number.to_d,currency
+ Money.new amount / number.to_d, currency
end
def +(other)
if self.class != other.class
raise ArgumentError
elsif currency != other.currency
raise Money::IncompatibleCurrencies
else
Money.new amount + other.amount, currency
end
end
def -(other)
if self.class != other.class
raise ArgumentError
elsif currency != other.currency
raise Money::IncompatibleCurrencies
else
Money.new amount - other.amount, currency
end
end
def <=>(other)
if self.class != other.class
raise ArgumentError
elsif currency != other.currency
raise Money::IncompatibleCurrencies
else
compare_to other
end
end
def compare_to(other)
if amount == other.amount then 0
elsif amount > other.amount then 1
else -1
end
end
class IncompatibleCurrencies < RuntimeError
end
end

Нели обнови решението на 14.01.2013 18:01 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
- attr_accessor :rates
+ attr_accessor :exchange_rates
def initialize
- @rates = {}
+ @exchange_rates = {}
end
def set(from_currency, to_currency, rate)
if from_currency == to_currency
return
end
- rates[[from_currency, to_currency]] = rate
- rates[[to_currency, from_currency]] = 1 / rate
+ exchange_rates[[from_currency, to_currency]] = rate
+ exchange_rates[[to_currency, from_currency]] = 1 / rate
end
def get(from_currency, to_currency)
if from_currency == to_currency
return 1.to_d
end
- rates[[from_currency, to_currency]]
+ exchange_rates[[from_currency, to_currency]]
end
def convert(from_currency, to_currency, amount)
if from_currency == to_currency
return amount
end
exchange_rate = get(from_currency, to_currency)
if exchange_rate == nil then raise ExchangeRate::Unknown
else amount * exchange_rate
end
end
class Unknown < RuntimeError
end
end
class Money
include Comparable
attr_accessor :amount, :currency
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
number_of_zeros = if amount.frac.to_s('f').length == 3 then 1 else 0 end
amount.round(2).to_s('f') + "0" * number_of_zeros + " " + currency.to_s
end
def in(to_currency, exchange_rate)
begin
exchange_rate.convert(currency, to_currency, amount)
rescue
raise
else
Money.new exchange_rate.convert(currency, to_currency, amount), to_currency
end
end
def *(number)
Money.new amount * number.to_d, currency
end
def /(number)
Money.new amount / number.to_d, currency
end
def +(other)
if self.class != other.class
raise ArgumentError
elsif currency != other.currency
raise Money::IncompatibleCurrencies
else
Money.new amount + other.amount, currency
end
end
def -(other)
if self.class != other.class
raise ArgumentError
elsif currency != other.currency
raise Money::IncompatibleCurrencies
else
Money.new amount - other.amount, currency
end
end
def <=>(other)
if self.class != other.class
raise ArgumentError
elsif currency != other.currency
raise Money::IncompatibleCurrencies
else
compare_to other
end
end
def compare_to(other)
if amount == other.amount then 0
elsif amount > other.amount then 1
else -1
end
end
class IncompatibleCurrencies < RuntimeError
end
end