Решение на Шеста задача от Живко Чобанов

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

Към профила на Живко Чобанов

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize
@rates = {}
end
def set(from_currency, to_currency, rate)
@rates[to_currency] ||= {}
@rates[to_currency][from_currency] = 1 / rate
@rates[from_currency] ||= {}
@rates[from_currency][to_currency] = rate
end
def get(from_currency, to_currency)
if from_currency == to_currency
1
elsif @rates.include? from_currency and @rates[from_currency].include? to_currency
@rates[from_currency][to_currency]
else
nil
end
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
if rate
rate * amount
else
raise ExchangeRate::Unknown, "unknown currency #{from_currency} or #{to_currency}"
end
end
private
end
class Money
module Operations
module OperationHelpers
def operation_with_numeric(operation, other)
if other.is_a? Numeric
Money.new @amount.send(operation, other), @currency
else
raise ArgumentError
end
end
def operation_with_money(operation, other)
if other.is_a? Money
if @currency == other.currency
Money.new @amount.send(operation, other.amount), @currency
else
raise Money::IncompatibleCurrencies
end
else
raise ArgumentError
end
end
def comparison(operator, other)
if other.is_a? Money
if @currency == other.currency
@amount.send operator, other.amount
else
raise Money::IncompatibleCurrencies
end
else
raise ArgumentError
end
end
end
def *(other)
operation_with_numeric :*, other
end
def /(other)
operation_with_numeric :/, other
end
def +(other)
operation_with_money :+, other
end
def -(other)
operation_with_money :-, other
end
def <=>(other)
comparison :<=>, other
end
def ==(other)
comparison :==, other
end
def <(other)
comparison :<, other
end
def >(other)
comparison :>, other
end
def >=(other)
comparison :>=, other
end
def <=(other)
comparison :<=, other
end
private
include OperationHelpers
end
include Operations
class IncompatibleCurrencies < RuntimeError
end
attr_reader :amount, :currency
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
"#{"%.2f" % @amount} #@currency"
end
def in(new_currency, exchange_rate)
exchange_rate.convert @currency, new_currency, @amount
end
end

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

................FF.............................

Failures:

  1) Money convertion allows convertion to other currencies
     Failure/Error: levas.amount.should eq '39.2'.to_d
     NoMethodError:
       undefined method `amount' for #<BigDecimal:9404a1c,'0.392E2',18(45)>
     # /tmp/d20130203-23049-1qdxg7b/spec.rb:102: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 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
     NoMethodError:
       undefined method `amount' for #<BigDecimal:93697ec,'0.5E1',9(36)>
     # /tmp/d20130203-23049-1qdxg7b/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)>'

Finished in 0.058 seconds
47 examples, 2 failures

Failed examples:

rspec /tmp/d20130203-23049-1qdxg7b/spec.rb:95 # Money convertion allows convertion to other currencies
rspec /tmp/d20130203-23049-1qdxg7b/spec.rb:106 # Money convertion does not change the amount if the same currency is passed

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

Живко обнови решението на 16.01.2013 13:43 (преди над 11 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ class Unknown < RuntimeError
+ end
+
+ def initialize
+ @rates = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ @rates[to_currency] ||= {}
+ @rates[to_currency][from_currency] = 1 / rate
+ @rates[from_currency] ||= {}
+ @rates[from_currency][to_currency] = rate
+ end
+
+ def get(from_currency, to_currency)
+ if from_currency == to_currency
+ 1
+ elsif @rates.include? from_currency and @rates[from_currency].include? to_currency
+ @rates[from_currency][to_currency]
+ else
+ nil
+ end
+ end
+
+ def convert(from_currency, to_currency, amount)
+ rate = get(from_currency, to_currency)
+ if rate
+ rate * amount
+ else
+ raise ExchangeRate::Unknown, "unknown currency #{from_currency} or #{to_currency}"
+ end
+ end
+
+ private
+end
+
+class Money
+ attr_reader :amount, :currency
+
+ def initialize(amount, currency)
+ @amount, @currency = amount, currency
+ end
+
+ def to_s
+ "#{"%.2f" % @amount} #@currency"
+ end
+
+ def in(new_currency, exchange_rate)
+ exchange_rate.convert @currency, new_currency, @amount
+ end
+
+ def *(other)
+ if other.is_a? Money
+ if @currency == other.currency
+ Money.new @amount * other.amount, @currency
+ else
+ raise Money::IncompatibleCurrencies
+ end
+ else
+ Money.new @amount * other, @currency
+ end
+ end
+
+ def <=>(other)
+ if other.is_a? Money
+ if @currency == other.currency
+ @amount <=> other.amount
+ else
+ raise Money::IncompatibleCurrencies
+ end
+ else
+ raise ArgumentError
+ end
+ end
+
+ def ==(other)
+ if other.is_a? Money
+ if @currency == other.currency
+ @amount == other.amount
+ else
+ raise Money::IncompatibleCurrencies
+ end
+ else
+ raise ArgumentError
+ end
+ end
+end

Живко обнови решението на 16.01.2013 14:37 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize
@rates = {}
end
def set(from_currency, to_currency, rate)
@rates[to_currency] ||= {}
@rates[to_currency][from_currency] = 1 / rate
@rates[from_currency] ||= {}
@rates[from_currency][to_currency] = rate
end
def get(from_currency, to_currency)
if from_currency == to_currency
1
elsif @rates.include? from_currency and @rates[from_currency].include? to_currency
@rates[from_currency][to_currency]
else
nil
end
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
if rate
rate * amount
else
raise ExchangeRate::Unknown, "unknown currency #{from_currency} or #{to_currency}"
end
end
private
end
class Money
+ module Operations
+ private
+
+ def operation_with_numeric(operation, other)
+ if other.is_a? Numeric
+ Money.new @amount.send(operation, other), @currency
+ else
+ raise ArgumentError
+ end
+ end
+
+ def operation_with_money(operation, other)
+ if other.is_a? Money
+ if @currency == other.currency
+ Money.new @amount.send(operation, other.amount), @currency
+ else
+ raise Money::IncompatibleCurrencies
+ end
+ else
+ raise ArgumentError
+ end
+ end
+ end
+
+ include Operations
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
attr_reader :amount, :currency
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
"#{"%.2f" % @amount} #@currency"
end
def in(new_currency, exchange_rate)
exchange_rate.convert @currency, new_currency, @amount
end
def *(other)
- if other.is_a? Money
- if @currency == other.currency
- Money.new @amount * other.amount, @currency
- else
- raise Money::IncompatibleCurrencies
- end
- else
- Money.new @amount * other, @currency
- end
+ operation_with_numeric :*, other
+ end
+
+ def /(other)
+ operation_with_numeric :/, other
+ end
+
+ def +(other)
+ operation_with_money :+, other
+ end
+
+ def -(other)
+ operation_with_money :-, other
end
def <=>(other)
if other.is_a? Money
if @currency == other.currency
@amount <=> other.amount
else
raise Money::IncompatibleCurrencies
end
else
raise ArgumentError
end
end
def ==(other)
if other.is_a? Money
if @currency == other.currency
@amount == other.amount
else
raise Money::IncompatibleCurrencies
end
else
raise ArgumentError
end
end
end

Живко обнови решението на 16.01.2013 16:51 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize
@rates = {}
end
def set(from_currency, to_currency, rate)
@rates[to_currency] ||= {}
@rates[to_currency][from_currency] = 1 / rate
@rates[from_currency] ||= {}
@rates[from_currency][to_currency] = rate
end
def get(from_currency, to_currency)
if from_currency == to_currency
1
elsif @rates.include? from_currency and @rates[from_currency].include? to_currency
@rates[from_currency][to_currency]
else
nil
end
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
if rate
rate * amount
else
raise ExchangeRate::Unknown, "unknown currency #{from_currency} or #{to_currency}"
end
end
private
end
class Money
module Operations
+ module OperationHelpers
+ def *(other)
+ operation_with_numeric :*, other
+ end
+
+ def /(other)
+ operation_with_numeric :/, other
+ end
+
+ def +(other)
+ operation_with_money :+, other
+ end
+
+ def -(other)
+ operation_with_money :-, other
+ end
+
+ def <=>(other)
+ comparison :<=>, other
+ end
+
+ def ==(other)
+ comparison :==, other
+ end
+
+ def <(other)
+ comparison :<, other
+ end
+
+ def >(other)
+ comparison :>, other
+ end
+
+ def >=(other)
+ comparison :>=, other
+ end
+
+ def <=(other)
+ comparison :<=, other
+ end
+ end
+
+ include OperationHelpers
+
private
def operation_with_numeric(operation, other)
if other.is_a? Numeric
Money.new @amount.send(operation, other), @currency
else
raise ArgumentError
end
end
def operation_with_money(operation, other)
if other.is_a? Money
if @currency == other.currency
Money.new @amount.send(operation, other.amount), @currency
else
raise Money::IncompatibleCurrencies
end
else
raise ArgumentError
end
end
+
+ def comparison(operator, other)
+ if other.is_a? Money
+ if @currency == other.currency
+ @amount.send operator, other.amount
+ else
+ raise Money::IncompatibleCurrencies
+ end
+ else
+ raise ArgumentError
+ end
+ end
end
include Operations
class IncompatibleCurrencies < RuntimeError
end
attr_reader :amount, :currency
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
"#{"%.2f" % @amount} #@currency"
end
def in(new_currency, exchange_rate)
exchange_rate.convert @currency, new_currency, @amount
- end
-
- def *(other)
- operation_with_numeric :*, other
- end
-
- def /(other)
- operation_with_numeric :/, other
- end
-
- def +(other)
- operation_with_money :+, other
- end
-
- def -(other)
- operation_with_money :-, other
- end
-
- def <=>(other)
- if other.is_a? Money
- if @currency == other.currency
- @amount <=> other.amount
- else
- raise Money::IncompatibleCurrencies
- end
- else
- raise ArgumentError
- end
- end
-
- def ==(other)
- if other.is_a? Money
- if @currency == other.currency
- @amount == other.amount
- else
- raise Money::IncompatibleCurrencies
- end
- else
- raise ArgumentError
- end
end
end

Живко обнови решението на 16.01.2013 16:54 (преди над 11 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize
@rates = {}
end
def set(from_currency, to_currency, rate)
@rates[to_currency] ||= {}
@rates[to_currency][from_currency] = 1 / rate
@rates[from_currency] ||= {}
@rates[from_currency][to_currency] = rate
end
def get(from_currency, to_currency)
if from_currency == to_currency
1
elsif @rates.include? from_currency and @rates[from_currency].include? to_currency
@rates[from_currency][to_currency]
else
nil
end
end
def convert(from_currency, to_currency, amount)
rate = get(from_currency, to_currency)
if rate
rate * amount
else
raise ExchangeRate::Unknown, "unknown currency #{from_currency} or #{to_currency}"
end
end
private
end
class Money
module Operations
module OperationHelpers
- def *(other)
- operation_with_numeric :*, other
+ def operation_with_numeric(operation, other)
+ if other.is_a? Numeric
+ Money.new @amount.send(operation, other), @currency
+ else
+ raise ArgumentError
+ end
end
- def /(other)
- operation_with_numeric :/, other
+ def operation_with_money(operation, other)
+ if other.is_a? Money
+ if @currency == other.currency
+ Money.new @amount.send(operation, other.amount), @currency
+ else
+ raise Money::IncompatibleCurrencies
+ end
+ else
+ raise ArgumentError
+ end
end
- def +(other)
- operation_with_money :+, other
+ def comparison(operator, other)
+ if other.is_a? Money
+ if @currency == other.currency
+ @amount.send operator, other.amount
+ else
+ raise Money::IncompatibleCurrencies
+ end
+ else
+ raise ArgumentError
+ end
end
+ end
- def -(other)
- operation_with_money :-, other
- end
+ def *(other)
+ operation_with_numeric :*, other
+ end
- def <=>(other)
- comparison :<=>, other
- end
+ def /(other)
+ operation_with_numeric :/, other
+ end
- def ==(other)
- comparison :==, other
- end
+ def +(other)
+ operation_with_money :+, other
+ end
- def <(other)
- comparison :<, other
- end
+ def -(other)
+ operation_with_money :-, other
+ end
- def >(other)
- comparison :>, other
- end
+ def <=>(other)
+ comparison :<=>, other
+ end
- def >=(other)
- comparison :>=, other
- end
+ def ==(other)
+ comparison :==, other
+ end
- def <=(other)
- comparison :<=, other
- end
+ def <(other)
+ comparison :<, other
end
- include OperationHelpers
-
- private
-
- def operation_with_numeric(operation, other)
- if other.is_a? Numeric
- Money.new @amount.send(operation, other), @currency
- else
- raise ArgumentError
- end
+ def >(other)
+ comparison :>, other
end
- def operation_with_money(operation, other)
- if other.is_a? Money
- if @currency == other.currency
- Money.new @amount.send(operation, other.amount), @currency
- else
- raise Money::IncompatibleCurrencies
- end
- else
- raise ArgumentError
- end
+ def >=(other)
+ comparison :>=, other
end
- def comparison(operator, other)
- if other.is_a? Money
- if @currency == other.currency
- @amount.send operator, other.amount
- else
- raise Money::IncompatibleCurrencies
- end
- else
- raise ArgumentError
- end
+ def <=(other)
+ comparison :<=, other
end
+
+ private
+
+ include OperationHelpers
end
include Operations
class IncompatibleCurrencies < RuntimeError
end
attr_reader :amount, :currency
def initialize(amount, currency)
@amount, @currency = amount, currency
end
def to_s
"#{"%.2f" % @amount} #@currency"
end
def in(new_currency, exchange_rate)
exchange_rate.convert @currency, new_currency, @amount
end
end