Решение на Трета задача от Атанас Иларионов

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

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

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class Inventory
def initialize
@products = Hash.new
end
def register(product, price)
raise "Invalid parameters passed." if @products.has_key?(product)
raise "Invalid parameters passed." if ((price.to_d < 0.01) || (price.to_d > 999.99))
raise "Invalid parameters passed." if product.length() > 40
@products[product] = price
end
def new_cart()
cart = Cart.new(@products)
return cart
end
end
class Cart
def initialize(products)
@cart_products = Hash.new
@inventory = products
end
def add(product, quantity = 1)
raise "Invalid parameters passed." if not @inventory.has_key?(product)
raise "Invalid parameters passed." if quantity < 0
if @cart_products.has_key?(product)
current_quantity = @cart_products[product]
@cart_products[product] = current_quantity + quantity
else
@cart_products[product] = quantity
end
raise "Invalid parameters passed." if @cart_products[product] > 99
end
def total
result = @cart_products.map { |k, v| v * @inventory[k].to_d }
result = result.inject{|sum,x| sum + x }
result
end
end

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

..FFFFFFFFFFFFFFFFF

Failures:

  1) Inventory with no discounts can print an invoice
     Failure/Error: cart.invoice.should eq <<INVOICE
     NoMethodError:
       undefined method `invoice' for #<Cart:0xa60f914>
     # /tmp/d20111115-5847-oycpqh/spec.rb:73: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) Inventory with a 'buy X, get one free' promotion grants every nth item for free
     Failure/Error: inventory.register 'Gum', '1.00', get_one_free: 4
     ArgumentError:
       wrong number of arguments (3 for 2)
     # /tmp/d20111115-5847-oycpqh/solution.rb:10:in `register'
     # /tmp/d20111115-5847-oycpqh/spec.rb:89: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) Inventory with a 'buy X, get one free' promotion grants 2 items free, when 8 purchased and every 3rd is free
     Failure/Error: inventory.register 'Gum', '1.00', get_one_free: 3
     ArgumentError:
       wrong number of arguments (3 for 2)
     # /tmp/d20111115-5847-oycpqh/solution.rb:10:in `register'
     # /tmp/d20111115-5847-oycpqh/spec.rb:97: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) Inventory with a 'buy X, get one free' promotion shows the discount in the invoice
     Failure/Error: inventory.register 'Green Tea', '1.00', get_one_free: 3
     ArgumentError:
       wrong number of arguments (3 for 2)
     # /tmp/d20111115-5847-oycpqh/solution.rb:10:in `register'
     # /tmp/d20111115-5847-oycpqh/spec.rb:105: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)>'

  5) Inventory with a '% off for every n' promotion gives % off for every group of n
     Failure/Error: inventory.register 'Sandwich', '1.00', package: {4 => 20}
     ArgumentError:
       wrong number of arguments (3 for 2)
     # /tmp/d20111115-5847-oycpqh/solution.rb:10:in `register'
     # /tmp/d20111115-5847-oycpqh/spec.rb:128: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)>'

  6) Inventory with a '% off for every n' promotion does not discount for extra items, that don't fit in a group
     Failure/Error: inventory.register 'Sandwich', '0.50', package: {4 => 10}
     ArgumentError:
       wrong number of arguments (3 for 2)
     # /tmp/d20111115-5847-oycpqh/solution.rb:10:in `register'
     # /tmp/d20111115-5847-oycpqh/spec.rb:138: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)>'

  7) Inventory with a '% off for every n' promotion shows the discount in the invoice
     Failure/Error: inventory.register 'Green Tea', '1.00', package: {4 => 10}
     ArgumentError:
       wrong number of arguments (3 for 2)
     # /tmp/d20111115-5847-oycpqh/solution.rb:10:in `register'
     # /tmp/d20111115-5847-oycpqh/spec.rb:148: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)>'

  8) Inventory with a '% off of every item after the nth' promotion gives a discount for every item after the nth
     Failure/Error: inventory.register 'Coke', '2.00', threshold: {5 => 10}
     ArgumentError:
       wrong number of arguments (3 for 2)
     # /tmp/d20111115-5847-oycpqh/solution.rb:10:in `register'
     # /tmp/d20111115-5847-oycpqh/spec.rb:171: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)>'

  9) Inventory with a '% off of every item after the nth' promotion does not give a discount if there are no more than n items in the cart
     Failure/Error: inventory.register 'Coke', '1.00', threshold: {10 => 20}
     ArgumentError:
       wrong number of arguments (3 for 2)
     # /tmp/d20111115-5847-oycpqh/solution.rb:10:in `register'
     # /tmp/d20111115-5847-oycpqh/spec.rb:178: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)>'

  10) Inventory with a '% off of every item after the nth' promotion shows the discount in the ivnoice
     Failure/Error: inventory.register 'Green Tea', '1.00', threshold: {10 => 10}
     ArgumentError:
       wrong number of arguments (3 for 2)
     # /tmp/d20111115-5847-oycpqh/solution.rb:10:in `register'
     # /tmp/d20111115-5847-oycpqh/spec.rb:191: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)>'

  11) Inventory with a '% off' coupon gives % off of the total
     Failure/Error: inventory.register_coupon 'TEATIME', percent: 20
     NoMethodError:
       undefined method `register_coupon' for #<Inventory:0xa88cb10 @products={"Tea"=>"1.00"}>
     # /tmp/d20111115-5847-oycpqh/spec.rb:215: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)>'

  12) Inventory with a '% off' coupon applies the coupon discount after product promotions
     Failure/Error: inventory.register 'Tea', '1.00', get_one_free: 6
     ArgumentError:
       wrong number of arguments (3 for 2)
     # /tmp/d20111115-5847-oycpqh/solution.rb:10:in `register'
     # /tmp/d20111115-5847-oycpqh/spec.rb:224: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)>'

  13) Inventory with a '% off' coupon shows the discount in the invoice
     Failure/Error: inventory.register_coupon 'TEA-TIME', percent: 20
     NoMethodError:
       undefined method `register_coupon' for #<Inventory:0xa88b6c0 @products={"Green Tea"=>"1.00"}>
     # /tmp/d20111115-5847-oycpqh/spec.rb:235: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)>'

  14) Inventory with an 'amount off' coupon subtracts the amount form the total
     Failure/Error: inventory.register_coupon 'TEATIME', amount: '10.00'.to_d
     NoMethodError:
       undefined method `register_coupon' for #<Inventory:0xa88aa04 @products={"Tea"=>"1.00"}>
     # /tmp/d20111115-5847-oycpqh/spec.rb:256: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)>'

  15) Inventory with an 'amount off' coupon does not result in a negative total
     Failure/Error: inventory.register_coupon 'TEATIME', amount: '10.00'.to_d
     NoMethodError:
       undefined method `register_coupon' for #<Inventory:0xa2bf534 @products={"Tea"=>"1.00"}>
     # /tmp/d20111115-5847-oycpqh/spec.rb:266: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)>'

  16) Inventory with an 'amount off' coupon shows the discount in the invoice
     Failure/Error: inventory.register_coupon 'TEA-TIME', amount: '10.00'.to_d
     NoMethodError:
       undefined method `register_coupon' for #<Inventory:0xa2bead0 @products={"Green Tea"=>"1.00"}>
     # /tmp/d20111115-5847-oycpqh/spec.rb:276: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)>'

  17) Inventory with multiple discounts can print an invoice
     Failure/Error: inventory.register 'Green Tea',    '2.79', get_one_free: 2
     ArgumentError:
       wrong number of arguments (3 for 2)
     # /tmp/d20111115-5847-oycpqh/solution.rb:10:in `register'
     # /tmp/d20111115-5847-oycpqh/spec.rb:296: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.55685 seconds
19 examples, 17 failures

Failed examples:

rspec /tmp/d20111115-5847-oycpqh/spec.rb:64 # Inventory with no discounts can print an invoice
rspec /tmp/d20111115-5847-oycpqh/spec.rb:88 # Inventory with a 'buy X, get one free' promotion grants every nth item for free
rspec /tmp/d20111115-5847-oycpqh/spec.rb:96 # Inventory with a 'buy X, get one free' promotion grants 2 items free, when 8 purchased and every 3rd is free
rspec /tmp/d20111115-5847-oycpqh/spec.rb:104 # Inventory with a 'buy X, get one free' promotion shows the discount in the invoice
rspec /tmp/d20111115-5847-oycpqh/spec.rb:127 # Inventory with a '% off for every n' promotion gives % off for every group of n
rspec /tmp/d20111115-5847-oycpqh/spec.rb:137 # Inventory with a '% off for every n' promotion does not discount for extra items, that don't fit in a group
rspec /tmp/d20111115-5847-oycpqh/spec.rb:147 # Inventory with a '% off for every n' promotion shows the discount in the invoice
rspec /tmp/d20111115-5847-oycpqh/spec.rb:170 # Inventory with a '% off of every item after the nth' promotion gives a discount for every item after the nth
rspec /tmp/d20111115-5847-oycpqh/spec.rb:177 # Inventory with a '% off of every item after the nth' promotion does not give a discount if there are no more than n items in the cart
rspec /tmp/d20111115-5847-oycpqh/spec.rb:190 # Inventory with a '% off of every item after the nth' promotion shows the discount in the ivnoice
rspec /tmp/d20111115-5847-oycpqh/spec.rb:213 # Inventory with a '% off' coupon gives % off of the total
rspec /tmp/d20111115-5847-oycpqh/spec.rb:223 # Inventory with a '% off' coupon applies the coupon discount after product promotions
rspec /tmp/d20111115-5847-oycpqh/spec.rb:233 # Inventory with a '% off' coupon shows the discount in the invoice
rspec /tmp/d20111115-5847-oycpqh/spec.rb:254 # Inventory with an 'amount off' coupon subtracts the amount form the total
rspec /tmp/d20111115-5847-oycpqh/spec.rb:264 # Inventory with an 'amount off' coupon does not result in a negative total
rspec /tmp/d20111115-5847-oycpqh/spec.rb:274 # Inventory with an 'amount off' coupon shows the discount in the invoice
rspec /tmp/d20111115-5847-oycpqh/spec.rb:295 # Inventory with multiple discounts can print an invoice

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

Атанас обнови решението на 07.11.2011 00:43 (преди около 13 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class Inventory
+
+ def initialize
+ @products = Hash.new
+ end
+
+ def register(product, price)
+ raise "Invalid parameters passed." if @products.has_key?(product)
+ raise "Invalid parameters passed." if ((price.to_d < 0.01) || (price.to_d > 999.99))
+ raise "Invalid parameters passed." if product.length() > 40
+ @products[product] = price
+ end
+
+ def new_cart()
+ cart = Cart.new(@products)
+ return cart
+ end
+end
+
+class Cart
+
+ def initialize(products)
+ @cart_products = Hash.new
+ @inventory = products
+ end
+
+ def add(product, quantity = 1)
+ raise "Invalid parameters passed." if not @inventory.has_key?(product)
+ raise "Invalid parameters passed." if quantity < 0
+ if @cart_products.has_key?(product)
+ current_quantity = @cart_products[product]
+ @cart_products[product] = current_quantity + quantity
+ else
+ @cart_products[product] = quantity
+ end
+ raise "Invalid parameters passed." if @cart_products[product] > 99
+ end
+
+ def total
+ result = @cart_products.map { |k, v| v * @inventory[k].to_d }
+ result = result.inject{|sum,x| sum + x }
+ result
+ end
+
+end