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

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

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

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
def initialize
@rates = {}
end
def set(from_currency, to_currency, rate)
add_rate(from_currency, to_currency, rate)
add_rate(to_currency, from_currency, 1.to_d / rate)
end
def get(from_currency, to_currency)
if from_currency == to_currency
1.to_d
else
@rates[from_currency] and @rates[from_currency][to_currency]
end
end
def convert(from_currency, to_currency, amount)
factor = get(from_currency, to_currency)
raise Unknown.new("Missing rate #{from_currency}<->#{to_currency}") unless factor
amount * factor
end
private
def add_rate(from_currency, to_currency, rate)
@rates[from_currency] ||= {}
@rates[from_currency][to_currency] = rate
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
"%.2f" % @amount + " " + @currency.to_s
end
def in(currency, exchange_rate)
Money.new exchange_rate.convert(@currency, currency, @amount), currency
end
def +(money)
check(money) and Money.new(@amount + money.amount, @currency)
end
def -(money)
check(money) and Money.new(@amount - money.amount, @currency)
end
def *(factor)
raise ArgumentError unless factor.is_a? Numeric
Money.new(@amount * factor, @currency)
end
def /(factor)
raise ArgumentError unless factor.is_a? Numeric
Money.new(@amount / factor, @currency)
end
def <=>(money)
check(money) and @amount <=> money.amount
end
def ==(money)
check(money) and @amount == money.amount
end
private
def check(argument)
raise ArgumentError.new unless argument.instance_of? Money
argument.currency == @currency or raise IncompatibleCurrencies.new
end
class IncompatibleCurrencies < RuntimeError
end
end

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

...............................................

Finished in 0.05727 seconds
47 examples, 0 failures

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

Георги обнови решението на 16.01.2013 11:55 (преди почти 12 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+ def initialize
+ @rates = {}
+ end
+
+ def set(from_currency, to_currency, rate)
+ add_rate(from_currency, to_currency, rate)
+ add_rate(to_currency, from_currency, 1.to_d / rate)
+ end
+
+ def get(from_currency, to_currency)
+ if from_currency == to_currency
+ 1.to_d
+ else
+ @rates[from_currency] and @rates[from_currency][to_currency]
+ end
+ end
+
+ def convert(from_currency, to_currency, amount)
+ factor = get(from_currency, to_currency)
+ unless(factor) then raise Unknown.new("Missing rate #{from_currency}<->#{to_currency}") end
+ amount * factor
+ end
+
+ private
+
+ def add_rate(from_currency, to_currency, rate)
+ @rates[from_currency] ||= {}
+ @rates[from_currency][to_currency] = rate
+ 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
+ "%.2f" % @amount + " " + @currency.to_s
+ end
+
+ def in(currency, exchange_rate)
+ Money.new exchange_rate.convert(@currency, currency, @amount), currency
+ end
+
+ def +(money)
+ check(money) and Money.new(@amount + money.amount, @currency)
+ end
+
+ def -(money)
+ check(money) and Money.new(@amount - money.amount, @currency)
+ end
+
+ def *(factor)
+ raise ArgumentError unless factor.is_a? Numeric
+ Money.new(@amount * factor, @currency)
+ end
+
+ def /(factor)
+ raise ArgumentError unless factor.is_a? Numeric
+ Money.new(@amount / factor, @currency)
+ end
+
+ def <=>(money)
+ check(money) and @amount <=> money.amount
+ end
+
+ def ==(money)
+ check(money) and @amount == money.amount
+ end
+
+ private
+
+ def check(argument)
+ raise ArgumentError.new unless argument.instance_of? Money
+ argument.currency == @currency or raise IncompatibleCurrencies.new
+ end
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+end

Георги обнови решението на 16.01.2013 13:39 (преди почти 12 години)

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
def initialize
@rates = {}
end
def set(from_currency, to_currency, rate)
add_rate(from_currency, to_currency, rate)
add_rate(to_currency, from_currency, 1.to_d / rate)
end
def get(from_currency, to_currency)
if from_currency == to_currency
1.to_d
else
@rates[from_currency] and @rates[from_currency][to_currency]
end
end
def convert(from_currency, to_currency, amount)
factor = get(from_currency, to_currency)
- unless(factor) then raise Unknown.new("Missing rate #{from_currency}<->#{to_currency}") end
+ raise Unknown.new("Missing rate #{from_currency}<->#{to_currency}") unless factor
amount * factor
end
private
def add_rate(from_currency, to_currency, rate)
@rates[from_currency] ||= {}
@rates[from_currency][to_currency] = rate
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
"%.2f" % @amount + " " + @currency.to_s
end
def in(currency, exchange_rate)
Money.new exchange_rate.convert(@currency, currency, @amount), currency
end
def +(money)
check(money) and Money.new(@amount + money.amount, @currency)
end
def -(money)
check(money) and Money.new(@amount - money.amount, @currency)
end
def *(factor)
raise ArgumentError unless factor.is_a? Numeric
Money.new(@amount * factor, @currency)
end
def /(factor)
raise ArgumentError unless factor.is_a? Numeric
Money.new(@amount / factor, @currency)
end
def <=>(money)
check(money) and @amount <=> money.amount
end
def ==(money)
check(money) and @amount == money.amount
end
private
def check(argument)
raise ArgumentError.new unless argument.instance_of? Money
argument.currency == @currency or raise IncompatibleCurrencies.new
end
class IncompatibleCurrencies < RuntimeError
end
end