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

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

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

Резултати

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

Код

require 'bigdecimal'
require 'bigdecimal/util'
class Inventory
def initialize
@products=Hash.new(0)
@promotion=Hash.new(0)
@coupon=Hash.new(0)
end
def register name, price, promo={}
if name.length >= 41
raise "Invalid product name lenght."
end
if price.to_d <= BigDecimal('0') and price.to_d >= BigDecimal('1000')
raise "Invalid product price."
end
if @products[name] != 0
raise "Product already exists"
end
@products[name] = price
@promotion[name] = promo
end
def new_cart
Cart.new(@products,@promotion,@coupon)
end
def register_coupon name, hash
@coupon[name]=hash
end
end
class Cart
def initialize (list1,list2,list3)
@cart = Hash.new(0)
@products = list1
@promotions = list2
@coupon = list3
@coupon_name = nil
end
def add name, times = 1
if @products[name] == nil
raise "Product can be added not in invetory"
end
if times == 0
raise "Invalid add cart 0 times"
end
@cart[name] += times
if @cart[name] >= 100
raise "Cart product more than 99 times"
end
end
def total
sum = BigDecimal('0')
@cart.each do |key,value|
name = @promotions[key]
minus = include_promo(value,name.keys.shift,name.values.shift,key)
sum += (@products[key].to_d * value - minus)
end
if @coupon_name
return coupon_price(coupon_name,sum)
end
return sum
end
def check_package value,count, percent
return @products[name].to_d*(value*percent / count*100 )
end
def check_threshold value,count,percent
if value - count >=0
return @products[name].to_d*(value - count)*percent/100
end
end
def include_promo value,promo,number,name
if promo == :get_one_free and value - number >= 0
return @products[name].to_d*(value / number)
end
if promo == :package
return check_package(value,number.keys.shift,number.values.shift)
end
if promo == :threshold
check_threshold(value,number.keys.shift,number.values.shift)
end
return 0
end
def use name
@coupon_name = name
end
def coupon_price name,sum
type = @coupon[name].keys.shift
number = @coupon[name].values.shift
if type == :percent
return sum*number/100
end
if type == :amount
if sum - number.to_d <0
return 0
else
return sum-number.to_d
end
end
end
#def invoice
# array = Array.new()
#header = '+------------------------------------------------+----------+'
#array.put header
#@cart.each do |key,value|
#text_field ='| '
#spaces = 48 - key.lenght - value.to_s.length
end

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

.FF..FFFFFFFFFFFFFF

Failures:

  1) Inventory with no discounts has some constraints on prices and counts
     Failure/Error: expect { inventory.register 'Negative', '-10.00' }.to raise_error
       expected Exception but nothing was raised
     # /tmp/d20111115-5847-1wiga45/spec.rb:55: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 can print an invoice
     Failure/Error: cart.invoice.should eq <<INVOICE
     NoMethodError:
       undefined method `invoice' for #<Cart:0x95e5c00>
     # /tmp/d20111115-5847-1wiga45/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)>'

  3) 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:0x95e361c>
     # /tmp/d20111115-5847-1wiga45/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)>'

  4) 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
     NameError:
       undefined local variable or method `name' for #<Cart:0x95e28fc>
     # /tmp/d20111115-5847-1wiga45/solution.rb:75:in `check_package'
     # /tmp/d20111115-5847-1wiga45/solution.rb:89:in `include_promo'
     # /tmp/d20111115-5847-1wiga45/solution.rb:65:in `block in total'
     # /tmp/d20111115-5847-1wiga45/solution.rb:63:in `each'
     # /tmp/d20111115-5847-1wiga45/solution.rb:63:in `total'
     # /tmp/d20111115-5847-1wiga45/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)>'

  5) 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
     NameError:
       undefined local variable or method `name' for #<Cart:0x942b1a8>
     # /tmp/d20111115-5847-1wiga45/solution.rb:75:in `check_package'
     # /tmp/d20111115-5847-1wiga45/solution.rb:89:in `include_promo'
     # /tmp/d20111115-5847-1wiga45/solution.rb:65:in `block in total'
     # /tmp/d20111115-5847-1wiga45/solution.rb:63:in `each'
     # /tmp/d20111115-5847-1wiga45/solution.rb:63:in `total'
     # /tmp/d20111115-5847-1wiga45/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)>'

  6) 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:0x942a578>
     # /tmp/d20111115-5847-1wiga45/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)>'

  7) 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 `name' for #<Cart:0x9429858>
     # /tmp/d20111115-5847-1wiga45/solution.rb:80:in `check_threshold'
     # /tmp/d20111115-5847-1wiga45/solution.rb:92:in `include_promo'
     # /tmp/d20111115-5847-1wiga45/solution.rb:65:in `block in total'
     # /tmp/d20111115-5847-1wiga45/solution.rb:63:in `each'
     # /tmp/d20111115-5847-1wiga45/solution.rb:63:in `total'
     # /tmp/d20111115-5847-1wiga45/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)>'

  8) 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 '10.00'.to_d
     NameError:
       undefined local variable or method `name' for #<Cart:0x9428c00>
     # /tmp/d20111115-5847-1wiga45/solution.rb:80:in `check_threshold'
     # /tmp/d20111115-5847-1wiga45/solution.rb:92:in `include_promo'
     # /tmp/d20111115-5847-1wiga45/solution.rb:65:in `block in total'
     # /tmp/d20111115-5847-1wiga45/solution.rb:63:in `each'
     # /tmp/d20111115-5847-1wiga45/solution.rb:63:in `total'
     # /tmp/d20111115-5847-1wiga45/spec.rb:184: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 shows the discount in the ivnoice
     Failure/Error: cart.invoice.should eq <<INVOICE
     NoMethodError:
       undefined method `invoice' for #<Cart:0x98b5b38>
     # /tmp/d20111115-5847-1wiga45/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)>'

  10) Inventory with a '% off' coupon gives % off of the total
     Failure/Error: cart.total.should eq '8.00'.to_d
     NameError:
       undefined local variable or method `coupon_name' for #<Cart:0x98b4df0>
     # /tmp/d20111115-5847-1wiga45/solution.rb:69:in `total'
     # /tmp/d20111115-5847-1wiga45/spec.rb:220: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 applies the coupon discount after product promotions
     Failure/Error: cart.total.should eq '9.00'.to_d
     NameError:
       undefined local variable or method `coupon_name' for #<Cart:0x98b40e4>
     # /tmp/d20111115-5847-1wiga45/solution.rb:69:in `total'
     # /tmp/d20111115-5847-1wiga45/spec.rb:230: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 shows the discount in the invoice
     Failure/Error: cart.invoice.should eq <<INVOICE
     NoMethodError:
       undefined method `invoice' for #<Cart:0x98b348c>
     # /tmp/d20111115-5847-1wiga45/spec.rb:240: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 an 'amount off' coupon subtracts the amount form the total
     Failure/Error: cart.total.should eq '2.00'.to_d
     NameError:
       undefined local variable or method `coupon_name' for #<Cart:0x98b271c>
     # /tmp/d20111115-5847-1wiga45/solution.rb:69:in `total'
     # /tmp/d20111115-5847-1wiga45/spec.rb:261: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 does not result in a negative total
     Failure/Error: cart.total.should eq '0.00'.to_d
     NameError:
       undefined local variable or method `coupon_name' for #<Cart:0x945ea30>
     # /tmp/d20111115-5847-1wiga45/solution.rb:69:in `total'
     # /tmp/d20111115-5847-1wiga45/spec.rb:271: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 shows the discount in the invoice
     Failure/Error: cart.invoice.should eq <<INVOICE
     NoMethodError:
       undefined method `invoice' for #<Cart:0x945ddec>
     # /tmp/d20111115-5847-1wiga45/spec.rb:281: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 multiple discounts can print an invoice
     Failure/Error: cart.invoice.should eq <<INVOICE
     NoMethodError:
       undefined method `invoice' for #<Cart:0x945ce60>
     # /tmp/d20111115-5847-1wiga45/spec.rb:309: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.55494 seconds
19 examples, 16 failures

Failed examples:

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

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

Емилиян обнови решението на 07.11.2011 13:51 (преди над 12 години)

+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class Inventory
+
+ def initialize
+ @products=Hash.new(0)
+ @promotion=Hash.new(0)
+ @coupon=Hash.new(0)
+ end
+
+ def register name, price, promo={}
+
+ if name.length >= 41
+ raise "Invalid product name lenght."
+ end
+ if price.to_d <= BigDecimal('0') and price.to_d >= BigDecimal('1000')
+ raise "Invalid product price."
+ end
+ if @products[name] != 0
+ raise "Product already exists"
+ end
+ @products[name] = price
+ @promotion[name] = promo
+ end
+
+ def new_cart
+ Cart.new(@products,@promotion,@coupon)
+ end
+
+ def register_coupon name, hash
+ @coupon[name]=hash
+ end
+
+
+end
+
+class Cart
+
+ def initialize (list1,list2,list3)
+ @cart = Hash.new(0)
+ @products = list1
+ @promotions = list2
+ @coupon = list3
+ @coupon_name = nil
+ end
+
+ def add name, times = 1
+ if @products[name] == nil
+ raise "Product can be added not in invetory"
+ end
+ if times == 0
+ raise "Invalid add cart 0 times"
+ end
+ @cart[name] += times
+ if @cart[name] >= 100
+ raise "Cart product more than 99 times"
+ end
+ end
+
+ def total
+ sum = BigDecimal('0')
+ @cart.each do |key,value|
+ name = @promotions[key]
+ minus = include_promo(value,name.keys.shift,name.values.shift,key)
+ sum += (@products[key].to_d * value - minus)
+ end
+ if @coupon_name
+ return coupon_price(coupon_name,sum)
+ end
+ return sum
+ end
+
+ def check_package value,count, percent
+ return @products[name].to_d*(value*percent / count*100 )
+ end
+
+ def check_threshold value,count,percent
+ if value - count >=0
+ return @products[name].to_d*(value - count)*percent/100
+ end
+ end
+
+ def include_promo value,promo,number,name
+ if promo == :get_one_free and value - number >= 0
+ return @products[name].to_d*(value / number)
+ end
+ if promo == :package
+ return check_package(value,number.keys.shift,number.values.shift)
+ end
+ if promo == :threshold
+ check_threshold(value,number.keys.shift,number.values.shift)
+ end
+ return 0
+ end
+
+ def use name
+ @coupon_name = name
+ end
+
+ def coupon_price name,sum
+ type = @coupon[name].keys.shift
+ number = @coupon[name].values.shift
+ if type == :percent
+ return sum*number/100
+ end
+ if type == :amount
+ if sum - number.to_d <0
+ return 0
+ else
+ return sum-number.to_d
+ end
+ end
+ end
+
+ #def invoice
+ # array = Array.new()
+ #header = '+------------------------------------------------+----------+'
+ #array.put header
+ #@cart.each do |key,value|
+ #text_field ='| '
+ #spaces = 48 - key.lenght - value.to_s.length
+
+
+
+end