Решение на Трета задача от Димитър Илиев

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

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

Резултати

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

Код

class Fixnum
def to_a
self
end
end
class Inventory
require 'bigdecimal'
require 'bigdecimal/util'
attr_accessor :box, :inventar
def initialize()
@box = {}
end
def register(name,price,special={})
raise "Produts is already defined" if @box.key? name
@box[name] = Product.new(name,price,special)
end
def register_coupon(coupon_name,discount={})
end
def new_cart
ShoppingCart.new self
end
def get_product_price product, amount
@box[product].total_price amount || 0
end
end
class ShoppingCart
attr_accessor :products, :coupon, :inventar
def initialize(inventar)
@products = {}
@inventar = inventar
end
def add prod_name,amount = 1
if amount <= 0 then
raise "Incorect amount"
end
current_amound = @products[prod_name] || 0
@products[prod_name] = current_amound + amount
end
def use coupon
if @coupon.nil? then
@coupon = coupon
else
raise "Only one coupon can be used in a cart"
end
end
def total
tot = 0
@products.each do |n,v|
tot += @inventar.box[n].total_price v
end
tot
end
def invoice
#print the bill
end
end
class Product
attr_accessor :name, :price, :mod
def initialize(name, price, modifier = {})
@name = name
@price = price
@mod = modifier
end
def total_price amount
calc = Calculator.new
type = @mod.to_a.flatten[0].to_s
if type == 'get_one_free' then
return calc.get_free(amount, @mod[type.to_sym].to_a, @price)
end
extend_total_price amount
end
def extend_total_price amount
if type == 'threshold' then
return calc.get_threshold(amount, @mod[type.to_sym].to_a, @price)
end
if type == 'package' then
return calc.get_package_price(amount, @mod[:package], @price)
end
amount * @price
end
end
class Calculator
def get_free total_amount, free_amount, price
if total_amount > free_amount then
(total_amount - total_amount / free_amount ) * price
else
total_amount * price
end
end
def get_threshold amount, disc_hash, price
disc_hash = disc_hash.flatten
if amount > disc_hash[0]
(amount-disc_hash[0]) * new_price(price,disc_hash[1]) + disc_hash[0] * price
else
amount*price
end
end
def get_package_price amount, disc_hash, price
disc_hash = disc_hash.flatten
not_in_package_price = (amount - amount % disc_hash[0]) * price
package_price = (amount / disc_hash[0]).to_i * new_price(pice,disc_hash[1])
not_in_package_price + package_price
end
def new_price price, perc
price * ( 100 - perc )/100
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 `type' for #<Product:0x9ab2d3c>
     # /tmp/d20111115-5847-16fnwz8/solution.rb:98:in `extend_total_price'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:93:in `total_price'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:65:in `block in total'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `each'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `total'
     # /tmp/d20111115-5847-16fnwz8/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 { inventory.register 'Negative', '-10.00' }.to raise_error
       expected Exception but nothing was raised
     # /tmp/d20111115-5847-16fnwz8/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)>'

  3) Inventory with no discounts can print an invoice
     Failure/Error: cart.invoice.should eq <<INVOICE
       
       expected: "+------------------------------------------------+----------+\n| Name                                       qty |    price |\n+------------------------------------------------+----------+\n| Green Tea                                    1 |     0.79 |\n| Earl Grey                                    3 |     2.97 |\n| Black Coffee                                 2 |     3.98 |\n+------------------------------------------------+----------+\n| TOTAL                                          |     7.74 |\n+------------------------------------------------+----------+\n"
            got: nil
       
       (compared using ==)
     # /tmp/d20111115-5847-16fnwz8/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
     TypeError:
       String can't be coerced into Fixnum
     # /tmp/d20111115-5847-16fnwz8/solution.rb:114:in `*'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:114:in `get_free'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:91:in `total_price'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:65:in `block in total'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `each'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `total'
     # /tmp/d20111115-5847-16fnwz8/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
     TypeError:
       String can't be coerced into Fixnum
     # /tmp/d20111115-5847-16fnwz8/solution.rb:112:in `*'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:112:in `get_free'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:91:in `total_price'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:65:in `block in total'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `each'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `total'
     # /tmp/d20111115-5847-16fnwz8/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
       
       expected: "+------------------------------------------------+----------+\n| Name                                       qty |    price |\n+------------------------------------------------+----------+\n| Green Tea                                    3 |     3.00 |\n|   (buy 2, get 1 free)                          |    -1.00 |\n| Red Tea                                      8 |    16.00 |\n|   (buy 4, get 1 free)                          |    -2.00 |\n+------------------------------------------------+----------+\n| TOTAL                                          |    16.00 |\n+------------------------------------------------+----------+\n"
            got: nil
       
       (compared using ==)
     # /tmp/d20111115-5847-16fnwz8/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
     NameError:
       undefined local variable or method `type' for #<Product:0x9c2febc>
     # /tmp/d20111115-5847-16fnwz8/solution.rb:98:in `extend_total_price'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:93:in `total_price'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:65:in `block in total'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `each'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `total'
     # /tmp/d20111115-5847-16fnwz8/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
     NameError:
       undefined local variable or method `type' for #<Product:0x9c2f2f0>
     # /tmp/d20111115-5847-16fnwz8/solution.rb:98:in `extend_total_price'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:93:in `total_price'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:65:in `block in total'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `each'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `total'
     # /tmp/d20111115-5847-16fnwz8/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
       
       expected: "+------------------------------------------------+----------+\n| Name                                       qty |    price |\n+------------------------------------------------+----------+\n| Green Tea                                    4 |     4.00 |\n|   (get 10% off for every 4)                    |    -0.40 |\n| Red Tea                                      8 |    16.00 |\n|   (get 20% off for every 5)                    |    -2.00 |\n+------------------------------------------------+----------+\n| TOTAL                                          |    17.60 |\n+------------------------------------------------+----------+\n"
            got: nil
       
       (compared using ==)
     # /tmp/d20111115-5847-16fnwz8/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 `type' for #<Product:0x9c2d8b0>
     # /tmp/d20111115-5847-16fnwz8/solution.rb:98:in `extend_total_price'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:93:in `total_price'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:65:in `block in total'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `each'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `total'
     # /tmp/d20111115-5847-16fnwz8/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 `type' for #<Product:0x9c2cce4>
     # /tmp/d20111115-5847-16fnwz8/solution.rb:98:in `extend_total_price'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:93:in `total_price'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:65:in `block in total'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `each'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `total'
     # /tmp/d20111115-5847-16fnwz8/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
       
       expected: "+------------------------------------------------+----------+\n| Name                                       qty |    price |\n+------------------------------------------------+----------+\n| Green Tea                                   12 |    12.00 |\n|   (10% off of every after the 10th)            |    -0.20 |\n| Red Tea                                     20 |    40.00 |\n|   (20% off of every after the 15th)            |    -2.00 |\n+------------------------------------------------+----------+\n| TOTAL                                          |    49.80 |\n+------------------------------------------------+----------+\n"
            got: nil
       
       (compared using ==)
     # /tmp/d20111115-5847-16fnwz8/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: cart.total.should eq '8.00'.to_d
     NameError:
       undefined local variable or method `type' for #<Product:0x9f1c2d8 @name="Tea", @price="1.00", @mod={}>
     # /tmp/d20111115-5847-16fnwz8/solution.rb:98:in `extend_total_price'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:93:in `total_price'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:65:in `block in total'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `each'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `total'
     # /tmp/d20111115-5847-16fnwz8/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)>'

  14) Inventory with a '% off' coupon applies the coupon discount after product promotions
     Failure/Error: cart.total.should eq '9.00'.to_d
     TypeError:
       String can't be coerced into Fixnum
     # /tmp/d20111115-5847-16fnwz8/solution.rb:112:in `*'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:112:in `get_free'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:91:in `total_price'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:65:in `block in total'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `each'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `total'
     # /tmp/d20111115-5847-16fnwz8/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)>'

  15) Inventory with a '% off' coupon shows the discount in the invoice
     Failure/Error: cart.invoice.should eq <<INVOICE
       
       expected: "+------------------------------------------------+----------+\n| Name                                       qty |    price |\n+------------------------------------------------+----------+\n| Green Tea                                   10 |    10.00 |\n| Coupon TEA-TIME - 20% off                      |    -2.00 |\n+------------------------------------------------+----------+\n| TOTAL                                          |     8.00 |\n+------------------------------------------------+----------+\n"
            got: nil
       
       (compared using ==)
     # /tmp/d20111115-5847-16fnwz8/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)>'

  16) 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 `type' for #<Product:0x9f199c0 @name="Tea", @price="1.00", @mod={}>
     # /tmp/d20111115-5847-16fnwz8/solution.rb:98:in `extend_total_price'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:93:in `total_price'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:65:in `block in total'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `each'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `total'
     # /tmp/d20111115-5847-16fnwz8/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)>'

  17) 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 `type' for #<Product:0x9a3a850 @name="Tea", @price="1.00", @mod={}>
     # /tmp/d20111115-5847-16fnwz8/solution.rb:98:in `extend_total_price'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:93:in `total_price'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:65:in `block in total'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `each'
     # /tmp/d20111115-5847-16fnwz8/solution.rb:64:in `total'
     # /tmp/d20111115-5847-16fnwz8/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)>'

  18) Inventory with an 'amount off' coupon shows the discount in the invoice
     Failure/Error: cart.invoice.should eq <<INVOICE
       
       expected: "+------------------------------------------------+----------+\n| Name                                       qty |    price |\n+------------------------------------------------+----------+\n| Green Tea                                    5 |     5.00 |\n| Coupon TEA-TIME - 10.00 off                    |    -5.00 |\n+------------------------------------------------+----------+\n| TOTAL                                          |     0.00 |\n+------------------------------------------------+----------+\n"
            got: nil
       
       (compared using ==)
     # /tmp/d20111115-5847-16fnwz8/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)>'

  19) Inventory with multiple discounts can print an invoice
     Failure/Error: cart.invoice.should eq <<INVOICE
       
       expected: "+------------------------------------------------+----------+\n| Name                                       qty |    price |\n+------------------------------------------------+----------+\n| Green Tea                                    8 |    22.32 |\n|   (buy 1, get 1 free)                          |   -11.16 |\n| Black Coffee                                 5 |    14.95 |\n|   (get 20% off for every 2)                    |    -2.39 |\n| Milk                                         5 |     8.95 |\n|   (30% off of every after the 3rd)             |    -1.07 |\n| Cereal                                       3 |     7.47 |\n| Coupon BREAKFAST - 10% off                     |    -3.91 |\n+------------------------------------------------+----------+\n| TOTAL                                          |    35.16 |\n+------------------------------------------------+----------+\n"
            got: nil
       
       (compared using ==)
     # /tmp/d20111115-5847-16fnwz8/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.55633 seconds
19 examples, 19 failures

Failed examples:

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

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

Димитър обнови решението на 06.11.2011 22:46 (преди около 13 години)

+class Fixnum
+ def to_a
+ self
+ end
+end
+
+class Inventory
+ require 'bigdecimal'
+ require 'bigdecimal/util'
+
+
+ attr_accessor :box, :inventar
+
+ def initialize()
+ @box = {}
+ end
+
+
+ def register(name,price,special={})
+ raise "Produts is already defined" if @box.key? name
+ @box[name] = Product.new(name,price,special)
+ end
+
+ def register_coupon(coupon_name,discount={})
+
+ end
+
+ def new_cart
+ ShoppingCart.new self
+ end
+
+ def get_product_price product, amount
+ @box[product].total_price amount || 0
+ end
+end
+
+class ShoppingCart
+
+ attr_accessor :products, :coupon, :inventar
+
+ def initialize(inventar)
+ @products = {}
+ @inventar = inventar
+ end
+
+ def add prod_name,amount = 1
+ if amount <= 0 then
+ raise "Incorect amount"
+ end
+ current_amound = @products[prod_name] || 0
+ @products[prod_name] = current_amound + amount
+ end
+
+ def use coupon
+ if @coupon.nil? then
+ @coupon = coupon
+ else
+ raise "Only one coupon can be used in a cart"
+ end
+ end
+
+ def total
+ tot = 0
+ @products.each do |n,v|
+ tot += @inventar.box[n].total_price v
+ end
+ tot
+ end
+
+ def invoice
+ #print the bill
+ end
+
+end
+
+
+class Product
+
+ attr_accessor :name, :price, :mod
+
+ def initialize(name, price, modifier = {})
+ @name = name
+ @price = price
+ @mod = modifier
+ end
+
+ def total_price amount
+ calc = Calculator.new
+ type = @mod.to_a.flatten[0].to_s
+ if type == 'get_one_free' then
+ return calc.get_free(amount, @mod[type.to_sym].to_a, @price)
+ end
+ extend_total_price amount
+
+ end
+
+ def extend_total_price amount
+ if type == 'threshold' then
+ return calc.get_threshold(amount, @mod[type.to_sym].to_a, @price)
+ end
+ if type == 'package' then
+ return calc.get_package_price(amount, @mod[:package], @price)
+ end
+ amount * @price
+ end
+end
+
+class Calculator
+
+ def get_free total_amount, free_amount, price
+ if total_amount > free_amount then
+ (total_amount - total_amount / free_amount ) * price
+ else
+ total_amount * price
+ end
+ end
+
+ def get_threshold amount, disc_hash, price
+ disc_hash = disc_hash.flatten
+ if amount > disc_hash[0]
+ (amount-disc_hash[0]) * new_price(price,disc_hash[1]) + disc_hash[0] * price
+ else
+ amount*price
+ end
+ end
+
+ def get_package_price amount, disc_hash, price
+ disc_hash = disc_hash.flatten
+ not_in_package_price = (amount - amount % disc_hash[0]) * price
+ package_price = (amount / disc_hash[0]).to_i * new_price(pice,disc_hash[1])
+ not_in_package_price + package_price
+ end
+
+ def new_price price, perc
+ price * ( 100 - perc )/100
+ end
+
+end