Решение на Трета задача от Антоний Цветков

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

Към профила на Антоний Цветков

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class Inventory
def initialize(products={}, cart_products={})
@products={}
products.each { |key, value| @products[key]=value}
@cart_products={}
cart_products.each { |key, value| @cart_products[key]=value}
end
def register(product, price, promotion={})
promotion_type=promotion.keys.shift
raise "Too long name!" if product.length > 40
raise "Product exists!" if @products[product]
@products[product]={}
@products[product][:price]=price.to_d
@products[product][promotion_type]=promotion[promotion_type] if promotion_type
raise "Incorect price!" if @products[product][:price] < '0.01'.to_d
raise "Price too high!" if @products[product][:price] > '999.99'.to_d
end
def register_coupon(name, coupon)
@coupon[name]={}
coupon_discount=coupon.keys.shift
@coupon[name][coupon_discount]=coupon[discount]
end
def new_cart(products={})
@cart_products=Inventory.new @products, products
end
def add(product, quantity=1)
if @products[product]
@cart_products[product]=0 unless @cart_products[product]
@cart_products[product]+=quantity
else
raise "Product doesn't exist!"
end
end
def use(coupon_name)
@coupone[name]
end
def total
total=0
@cart_products.each do
|key, value|
total+=value*@products[key][:price]
puts "#{value} * #{@products[key][:price]}\n"
end
puts "TOTAL: #{total}\n"
end
end

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

2 * 0.199E1
TOTAL: 0.398E1
FFF4 * 0.1E1
TOTAL: 0.4E1
F8 * 0.1E1
TOTAL: 0.8E1
FF4 * 0.1E1
TOTAL: 0.4E1
F4 * 0.5E0
TOTAL: 0.2E1
FF8 * 0.2E1
TOTAL: 0.16E2
F8 * 0.1E1
TOTAL: 0.8E1
FFFFFFFFF

Failures:

  1) Inventory with no discounts can tell the total price of all products
     Failure/Error: cart.total.should eq '3.98'.to_d
       
       expected: #<BigDecimal:8f622d4,'0.398E1',8(8)>
            got: nil
       
       (compared using ==)
     # /tmp/d20111115-5847-qlr01p/spec.rb:44: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 no discounts has some constraints on prices and counts
     Failure/Error: expect { cart.add 'Existing', 100 }.to raise_error
       expected Exception but nothing was raised
     # /tmp/d20111115-5847-qlr01p/spec.rb:60: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 no discounts can print an invoice
     Failure/Error: cart.invoice.should eq <<INVOICE
     NoMethodError:
       undefined method `invoice' for #<Inventory:0x8f7dbec>
     # /tmp/d20111115-5847-qlr01p/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)>'

  4) Inventory with a 'buy X, get one free' promotion grants every nth item for free
     Failure/Error: cart.total.should eq '3.00'.to_d
       
       expected: #<BigDecimal:9365660,'0.3E1',4(8)>
            got: nil
       
       (compared using ==)
     # /tmp/d20111115-5847-qlr01p/spec.rb:93: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 'buy X, get one free' promotion grants 2 items free, when 8 purchased and every 3rd is free
     Failure/Error: cart.total.should eq '6.00'.to_d
       
       expected: #<BigDecimal:936465c,'0.6E1',4(8)>
            got: nil
       
       (compared using ==)
     # /tmp/d20111115-5847-qlr01p/spec.rb:101: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 'buy X, get one free' promotion shows the discount in the invoice
     Failure/Error: cart.invoice.should eq <<INVOICE
     NoMethodError:
       undefined method `invoice' for #<Inventory:0x9363860>
     # /tmp/d20111115-5847-qlr01p/spec.rb:111: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 gives % off for every group of n
     Failure/Error: cart.total.should eq '3.20'.to_d
       
       expected: #<BigDecimal:9362870,'0.32E1',8(8)>
            got: nil
       
       (compared using ==)
     # /tmp/d20111115-5847-qlr01p/spec.rb:131: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 for every n' promotion does not discount for extra items, that don't fit in a group
     Failure/Error: cart.total.should eq '1.80'.to_d
       
       expected: #<BigDecimal:9087358,'0.18E1',8(8)>
            got: nil
       
       (compared using ==)
     # /tmp/d20111115-5847-qlr01p/spec.rb:141: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 for every n' promotion shows the discount in the invoice
     Failure/Error: cart.invoice.should eq <<INVOICE
     NoMethodError:
       undefined method `invoice' for #<Inventory:0x9086534>
     # /tmp/d20111115-5847-qlr01p/spec.rb:154: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 gives a discount for every item after the nth
     Failure/Error: cart.total.should eq '15.40'.to_d
       
       expected: #<BigDecimal:9085544,'0.154E2',8(8)>
            got: nil
       
       (compared using ==)
     # /tmp/d20111115-5847-qlr01p/spec.rb:174: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 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: cart.total.should eq '8.00'.to_d
       
       expected: #<BigDecimal:908452c,'0.8E1',4(8)>
            got: nil
       
       (compared using ==)
     # /tmp/d20111115-5847-qlr01p/spec.rb:181: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 of every item after the nth' promotion shows the discount in the ivnoice
     Failure/Error: cart.invoice.should eq <<INVOICE
     NoMethodError:
       undefined method `invoice' for #<Inventory:0x9083708>
     # /tmp/d20111115-5847-qlr01p/spec.rb:197: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 gives % off of the total
     Failure/Error: inventory.register_coupon 'TEATIME', percent: 20
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     # /tmp/d20111115-5847-qlr01p/solution.rb:24:in `register_coupon'
     # /tmp/d20111115-5847-qlr01p/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)>'

  14) Inventory with a '% off' coupon applies the coupon discount after product promotions
     Failure/Error: inventory.register_coupon 'TEATIME', percent: 10
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     # /tmp/d20111115-5847-qlr01p/solution.rb:24:in `register_coupon'
     # /tmp/d20111115-5847-qlr01p/spec.rb:225: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 a '% off' coupon shows the discount in the invoice
     Failure/Error: inventory.register_coupon 'TEA-TIME', percent: 20
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     # /tmp/d20111115-5847-qlr01p/solution.rb:24:in `register_coupon'
     # /tmp/d20111115-5847-qlr01p/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)>'

  16) 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 `[]=' for nil:NilClass
     # /tmp/d20111115-5847-qlr01p/solution.rb:24:in `register_coupon'
     # /tmp/d20111115-5847-qlr01p/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)>'

  17) 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 `[]=' for nil:NilClass
     # /tmp/d20111115-5847-qlr01p/solution.rb:24:in `register_coupon'
     # /tmp/d20111115-5847-qlr01p/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)>'

  18) 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 `[]=' for nil:NilClass
     # /tmp/d20111115-5847-qlr01p/solution.rb:24:in `register_coupon'
     # /tmp/d20111115-5847-qlr01p/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)>'

  19) Inventory with multiple discounts can print an invoice
     Failure/Error: inventory.register_coupon 'BREAKFAST', percent: 10
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     # /tmp/d20111115-5847-qlr01p/solution.rb:24:in `register_coupon'
     # /tmp/d20111115-5847-qlr01p/spec.rb:301: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.56298 seconds
19 examples, 19 failures

Failed examples:

rspec /tmp/d20111115-5847-qlr01p/spec.rb:38 # Inventory with no discounts can tell the total price of all products
rspec /tmp/d20111115-5847-qlr01p/spec.rb:47 # Inventory with no discounts has some constraints on prices and counts
rspec /tmp/d20111115-5847-qlr01p/spec.rb:64 # Inventory with no discounts can print an invoice
rspec /tmp/d20111115-5847-qlr01p/spec.rb:88 # Inventory with a 'buy X, get one free' promotion grants every nth item for free
rspec /tmp/d20111115-5847-qlr01p/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-qlr01p/spec.rb:104 # Inventory with a 'buy X, get one free' promotion shows the discount in the invoice
rspec /tmp/d20111115-5847-qlr01p/spec.rb:127 # Inventory with a '% off for every n' promotion gives % off for every group of n
rspec /tmp/d20111115-5847-qlr01p/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-qlr01p/spec.rb:147 # Inventory with a '% off for every n' promotion shows the discount in the invoice
rspec /tmp/d20111115-5847-qlr01p/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-qlr01p/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-qlr01p/spec.rb:190 # Inventory with a '% off of every item after the nth' promotion shows the discount in the ivnoice
rspec /tmp/d20111115-5847-qlr01p/spec.rb:213 # Inventory with a '% off' coupon gives % off of the total
rspec /tmp/d20111115-5847-qlr01p/spec.rb:223 # Inventory with a '% off' coupon applies the coupon discount after product promotions
rspec /tmp/d20111115-5847-qlr01p/spec.rb:233 # Inventory with a '% off' coupon shows the discount in the invoice
rspec /tmp/d20111115-5847-qlr01p/spec.rb:254 # Inventory with an 'amount off' coupon subtracts the amount form the total
rspec /tmp/d20111115-5847-qlr01p/spec.rb:264 # Inventory with an 'amount off' coupon does not result in a negative total
rspec /tmp/d20111115-5847-qlr01p/spec.rb:274 # Inventory with an 'amount off' coupon shows the discount in the invoice
rspec /tmp/d20111115-5847-qlr01p/spec.rb:295 # Inventory with multiple discounts can print an invoice

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

Антоний обнови решението на 02.11.2011 10:49 (преди около 13 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class Inventory
+ def initialize(products={}, cart_products={})
+ @products={}
+ products.each { |key, value| @products[key]=value}
+ @cart_products={}
+ cart_products.each { |key, value| @cart_products[key]=value}
+ end
+
+ def register(product, price, promotion={})
+ promotion_type=promotion.keys.shift
+ raise "Too long name!" if product.length > 40
+ raise "Product exists!" if @products[product]
+ @products[product]={}
+ @products[product][:price]=price.to_d
+ @products[product][promotion_type]=promotion[promotion_type] if promotion_type
+ raise "Incorect price!" if @products[product][:price] < '0.01'.to_d
+ raise "Price too high!" if @products[product][:price] > '999.99'.to_d
+ end
+
+ def register_coupon(name, coupon)
+ @coupon[name]={}
+ coupon_discount=coupon.keys.shift
+ @coupon[name][coupon_discount]=coupon[discount]
+ end
+
+ def new_cart(products={})
+ @cart_products=Inventory.new @products, products
+ end
+
+ def add(product, quantity=1)
+ if @products[product]
+ @cart_products[product]=0 unless @cart_products[product]
+ @cart_products[product]+=quantity
+ else
+ raise "Product doesn't exist!"
+ end
+ end
+
+ def use(coupon_name)
+ @coupone[name]
+ end
+
+ def total
+ total=0
+ @cart_products.each do
+ |key, value|
+ total+=value*@products[key][:price]
+ puts "#{value} * #{@products[key][:price]}\n"
+ end
+ puts "TOTAL: #{total}\n"
+ end
+end

Аз смятам още да правя по задачата, просто да видя, ако ми дадете някакъв съвет :)

Доколкото за равното. Винаги присвояването го пиша без whitespace, а за сравненията оставям whitespace. Така по-лесно се откриват някои глупави грешки