Решение на Шеста задача от Чанита Иванова

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

Към профила на Чанита Иванова

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class ExchangeRate
class Unknown < RuntimeError
end
def initialize()
@hash = Hash.new { |hash, key| hash[key] = [[key,(BigDecimal.new '1.0')]] }
end
def set(from_currency, to_currency, rate)
return if(from_currency == to_currency)
@hash[from_currency] << [to_currency,rate]
@hash[to_currency] << [from_currency,BigDecimal("1.0")/rate]
end
def get(from_currency, to_currency)
@hash[from_currency].map{|x| return x[1] if(x[0] == to_currency)}
nil
end
def convert(from_currency, to_currency, amount)
@hash[from_currency].map{ |x| return amount*x[1] if(x[0] == to_currency)}
raise Unknown
end
end
class Money
include Comparable
attr_reader :amount, :currency
class IncompatibleCurrencies < RuntimeError
end
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def to_s
return amount.round(2).to_s('F') + "0 " + currency.to_s if(amount.frac == 0)
amount.round(2).to_s('F') + " " + currency.to_s
end
def in(currency, exchange_rate)
Money.new(exchange_rate.convert(self.currency,currency,amount),currency)
end
def *(other)
raise ArgumentError unless(other.kind_of? Numeric)
Money.new(amount * other, currency)
end
def /(other)
raise ArgumentError unless(other.kind_of? Numeric)
Money.new(amount / other, currency)
end
def +(other)
raise ArgumentError unless(other.instance_of? Money)
raise IncompatibleCurrencies if(currency != other.currency)
Money.new(amount + other.amount, currency)
end
def -(other)
raise ArgumentError unless(other.instance_of? Money)
raise IncompatibleCurrencies if(currency != other.currency)
Money.new(amount - other.amount, currency)
end
def <=>(other)
raise ArgumentError if(!other.instance_of? Money)
raise IncompatibleCurrencies if(currency != other.currency)
amount <=> other.amount
end
def ==(other)
raise ArgumentError if(!other.instance_of? Money)
raise IncompatibleCurrencies if(currency != other.currency)
amount == other.amount
end
end

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

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

Failures:

  1) ExchangeRate#set allows rates to be updated if called multiple times
     Failure/Error: expect do
       result should have been changed to #<BigDecimal:a746ecc,'0.149E1',18(18)>, but is now #<BigDecimal:a60cae8,'0.15E1',18(18)>
     # /tmp/d20130203-23049-5s5p3r/spec.rb:16: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#set sets the exchange rate in both directions
     Failure/Error: rate.get(:EUR, :BGN).should eq '1.25'.to_d
       
       expected: #<BigDecimal:a513a88,'0.125E1',18(18)>
            got: #<BigDecimal:a745e14,'0.2E1',9(36)>
       
       (compared using ==)
     # /tmp/d20130203-23049-5s5p3r/spec.rb:25: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 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-5s5p3r/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)>'

Finished in 0.0583 seconds
47 examples, 3 failures

Failed examples:

rspec /tmp/d20130203-23049-5s5p3r/spec.rb:13 # ExchangeRate#set allows rates to be updated if called multiple times
rspec /tmp/d20130203-23049-5s5p3r/spec.rb:21 # ExchangeRate#set sets the exchange rate in both directions
rspec /tmp/d20130203-23049-5s5p3r/spec.rb:88 # Money has a custom to_s representation

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

Чанита обнови решението на 13.01.2013 23:01 (преди около 12 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class ExchangeRate
+
+ class Unknown < RuntimeError
+ end
+
+ def initialize()
+ @hash = Hash.new { |hash, key| hash[key] = [[key,(BigDecimal.new '1.0')]] }
+ end
+
+ def set(from_currency, to_currency, rate)
+ return if(from_currency == to_currency)
+ @hash[from_currency] << [to_currency,rate]
+ @hash[to_currency] << [from_currency,BigDecimal("1.0")/rate]
+ end
+
+ def get(from_currency, to_currency)
+ @hash[from_currency].map{|x| return x[1] if(x[0] == to_currency)}
+ nil
+ end
+
+ def convert(from_currency, to_currency, amount)
+ @hash[from_currency].map{ |x| return amount*x[1] if(x[0] == to_currency)}
+ raise Unknown
+ end
+
+end
+
+class Money
+ include Comparable
+
+ attr_reader :amount, :currency
+
+ class IncompatibleCurrencies < RuntimeError
+ end
+
+ def initialize(amount, currency)
+ @amount = amount
+ @currency = currency
+ end
+
+ def to_s
+ return amount.round(2).to_s('F') + "0 " + currency.to_s if(amount.frac == 0)
+ amount.round(2).to_s('F') + " " + currency.to_s
+ end
+
+ def in(currency, exchange_rate)
+ Money.new(exchange_rate.convert(self.currency,currency,amount),currency)
+ end
+
+ def *(other)
+ raise ArgumentError unless(other.kind_of? Numeric)
+ Money.new(amount * other, currency)
+ end
+
+ def /(other)
+ raise ArgumentError unless(other.kind_of? Numeric)
+ Money.new(amount / other, currency)
+ end
+
+ def +(other)
+ raise ArgumentError unless(other.instance_of? Money)
+ raise IncompatibleCurrencies if(currency != other.currency)
+ Money.new(amount + other.amount, currency)
+ end
+
+ def -(other)
+ raise ArgumentError unless(other.instance_of? Money)
+ raise IncompatibleCurrencies if(currency != other.currency)
+ Money.new(amount - other.amount, currency)
+ end
+
+ def <=>(other)
+ raise ArgumentError if(!other.instance_of? Money)
+ raise IncompatibleCurrencies if(currency != other.currency)
+ amount <=> other.amount
+ end
+
+ def ==(other)
+ raise ArgumentError if(!other.instance_of? Money)
+ raise IncompatibleCurrencies if(currency != other.currency)
+ amount == other.amount
+ end
+
+end