Решение на Трета задача от Явор Лилянов

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

Към профила на Явор Лилянов

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class Inventory
@name_prize
@promotion
public
def initialize
@name_prize=Hash.new
@promotion=Hash.new
end
def register(init_name,init_prize,prom={})
if((init_name.length>40)or(init_prize.to_d<0.01)or(init_prize.to_d>999.9))
raise "Invalid parameters passed. "
end
@name_prize[init_name]=init_prize
@promotion=prom
end
def new_cart
cart=Cart.new(@name_prize, @promotion)
end
end
class Cart < Inventory
@name_count
public
def initialize(products,prom)
@name_prize=products
@name_count=Hash.new
@promotion=prom
end
def add(name,count=1)
if((@name_prize.any? { |name1,prize| name1==name})and(count>0))
if(@name_count.any? { |name1,v| name1==name})
@name_count[name]+=count
else
@name_count[name]=count
end
else
raise "Invalid parameters passed"
end
end
def total
if(@promotion.has_key?(:get_one_free))
return self.get_one_free
elsif(@promotion.has_key?(:package))
return self.package
else
sum=0.0
@name_count.each { |name,count| sum+=v*(@name_prize[name].to_d) }
return sum
end
end
def get_one_free
sum=0.0
@name_count.each do |name,count| temp=count
sum+=v*(@name_prize[name].to_d)
while(temp%@promociq[:get_one_free]==0)
sum-=@name_prize[n].to_d
temp/=@promociq[:get_one_free]
end
end
sum
end
def package
sum=0.0
@name_count.each do |name,count| temp=count
n=@name_prize[name].to_d
while(temp%@promotion[:package].keys[0]==0)
temp/=2
n-=n*(@promotion[:package].values[0]/100.0)
end
sum+=count*n
end
sum
end
def invoise
puts '+---------------------+----------+'
puts '|Name qty| price|'
puts '+---------------------+----------+'
@name_count.each do |name,count|
puts '|'+name.to_s.ljust(20)+count.to_s+'|'+@name_prize[name].to_s.rjust(10)+'|'
end
puts '+---------------------+----------+'
puts '|TOTAL'.ljust(22)+'|'+self.total.to_s.rjust(10)+'|'
puts '+---------------------+----------+'
end
end

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

FFFFFFFFFFFFFFFFFFF

Failures:

  1) Inventory with no discounts can tell the total price of all products
     Failure/Error: cart.total.should eq '3.98'.to_d
     NameError:
       undefined local variable or method `v' for #<Cart:0xa8741c8>
     # /tmp/d20111115-5847-a89pfx/solution.rb:55:in `block in total'
     # /tmp/d20111115-5847-a89pfx/solution.rb:55:in `each'
     # /tmp/d20111115-5847-a89pfx/solution.rb:55:in `total'
     # /tmp/d20111115-5847-a89pfx/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: inventory.register 'Top price', '999.99'
     RuntimeError:
       Invalid parameters passed. 
     # /tmp/d20111115-5847-a89pfx/solution.rb:15:in `register'
     # /tmp/d20111115-5847-a89pfx/spec.rb:49: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 #<Cart:0xaa4f6c8>
     # /tmp/d20111115-5847-a89pfx/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
     NameError:
       undefined local variable or method `v' for #<Cart:0xaa4e804>
     # /tmp/d20111115-5847-a89pfx/solution.rb:63:in `block in get_one_free'
     # /tmp/d20111115-5847-a89pfx/solution.rb:62:in `each'
     # /tmp/d20111115-5847-a89pfx/solution.rb:62:in `get_one_free'
     # /tmp/d20111115-5847-a89pfx/solution.rb:50:in `total'
     # /tmp/d20111115-5847-a89pfx/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
     NameError:
       undefined local variable or method `v' for #<Cart:0xaa4dbd4>
     # /tmp/d20111115-5847-a89pfx/solution.rb:63:in `block in get_one_free'
     # /tmp/d20111115-5847-a89pfx/solution.rb:62:in `each'
     # /tmp/d20111115-5847-a89pfx/solution.rb:62:in `get_one_free'
     # /tmp/d20111115-5847-a89pfx/solution.rb:50:in `total'
     # /tmp/d20111115-5847-a89pfx/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 #<Cart:0xaa4cf90>
     # /tmp/d20111115-5847-a89pfx/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 '6.40'.to_d
       
       expected: #<BigDecimal:aa4baa0,'0.64E1',8(8)>
            got: 5.12
       
       (compared using ==)
     # /tmp/d20111115-5847-a89pfx/spec.rb:134: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 '2.80'.to_d
       
       expected: #<BigDecimal:aa70f80,'0.28E1',8(8)>
            got: 3.0
       
       (compared using ==)
     # /tmp/d20111115-5847-a89pfx/spec.rb:144: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 #<Cart:0xaa700bc>
     # /tmp/d20111115-5847-a89pfx/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
     NameError:
       undefined local variable or method `v' for #<Cart:0xaa6f298>
     # /tmp/d20111115-5847-a89pfx/solution.rb:55:in `block in total'
     # /tmp/d20111115-5847-a89pfx/solution.rb:55:in `each'
     # /tmp/d20111115-5847-a89pfx/solution.rb:55:in `total'
     # /tmp/d20111115-5847-a89pfx/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
     NameError:
       undefined local variable or method `v' for #<Cart:0xaa6e668>
     # /tmp/d20111115-5847-a89pfx/solution.rb:55:in `block in total'
     # /tmp/d20111115-5847-a89pfx/solution.rb:55:in `each'
     # /tmp/d20111115-5847-a89pfx/solution.rb:55:in `total'
     # /tmp/d20111115-5847-a89pfx/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 #<Cart:0xabe07bc>
     # /tmp/d20111115-5847-a89pfx/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 `register_coupon' for #<Inventory:0xabdfb14 @name_prize={"Tea"=>"1.00"}, @promotion={}>
     # /tmp/d20111115-5847-a89pfx/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 `register_coupon' for #<Inventory:0xabdefc0>
     # /tmp/d20111115-5847-a89pfx/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 `register_coupon' for #<Inventory:0xabde570>
     # /tmp/d20111115-5847-a89pfx/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 `register_coupon' for #<Inventory:0xabdd88c @name_prize={"Tea"=>"1.00"}, @promotion={}>
     # /tmp/d20111115-5847-a89pfx/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 `register_coupon' for #<Inventory:0xad32a84 @name_prize={"Tea"=>"1.00"}, @promotion={}>
     # /tmp/d20111115-5847-a89pfx/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 `register_coupon' for #<Inventory:0xad31ff8>
     # /tmp/d20111115-5847-a89pfx/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 `register_coupon' for #<Inventory:0xad312d8>
     # /tmp/d20111115-5847-a89pfx/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.5765 seconds
19 examples, 19 failures

Failed examples:

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

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

Явор обнови решението на 07.11.2011 16:22 (преди над 12 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class Inventory
+ @name_prize
+ @promotion
+ public
+ def initialize
+ @name_prize=Hash.new
+ @promotion=Hash.new
+ end
+
+ def register(init_name,init_prize,prom={})
+ if((init_name.length>40)or(init_prize.to_d<0.01)or(init_prize.to_d>999.9))
+ raise "Invalid parameters passed. "
+ end
+ @name_prize[init_name]=init_prize
+ @promotion=prom
+ end
+
+ def new_cart
+ cart=Cart.new(@name_prize, @promotion)
+ end
+end
+
+class Cart < Inventory
+ @name_count
+ public
+ def initialize(products,prom)
+ @name_prize=products
+ @name_count=Hash.new
+ @promotion=prom
+ end
+
+ def add(name,count=1)
+
+ if((@name_prize.any? { |name1,prize| name1==name})and(count>0))
+ if(@name_count.any? { |name1,v| name1==name})
+ @name_count[name]+=count
+ else
+ @name_count[name]=count
+ end
+ else
+ raise "Invalid parameters passed"
+ end
+ end
+
+ def total
+ if(@promotion.has_key?(:get_one_free))
+ return self.get_one_free
+ elsif(@promotion.has_key?(:package))
+ return self.package
+ else
+ sum=0.0
+ @name_count.each { |name,count| sum+=v*(@name_prize[name].to_d) }
+ return sum
+ end
+ end
+
+ def get_one_free
+ sum=0.0
+ @name_count.each do |name,count| temp=count
+ sum+=v*(@name_prize[name].to_d)
+ while(temp%@promociq[:get_one_free]==0)
+ sum-=@name_prize[n].to_d
+ temp/=@promociq[:get_one_free]
+ end
+ end
+ sum
+ end
+
+ def package
+ sum=0.0
+ @name_count.each do |name,count| temp=count
+ n=@name_prize[name].to_d
+ while(temp%@promotion[:package].keys[0]==0)
+ temp/=2
+ n-=n*(@promotion[:package].values[0]/100.0)
+ end
+ sum+=count*n
+ end
+ sum
+ end
+
+ def invoise
+ puts '+---------------------+----------+'
+ puts '|Name qty| price|'
+ puts '+---------------------+----------+'
+ @name_count.each do |name,count|
+ puts '|'+name.to_s.ljust(20)+count.to_s+'|'+@name_prize[name].to_s.rjust(10)+'|'
+ end
+ puts '+---------------------+----------+'
+ puts '|TOTAL'.ljust(22)+'|'+self.total.to_s.rjust(10)+'|'
+ puts '+---------------------+----------+'
+ end
+end