Недислав обнови решението на 06.11.2011 18:52 (преди около 13 години)
+class Inventory
+ attr_accessor :inventory
+ attr_accessor :coupon
+ attr_accessor :promo
+
+ def initialize
+ @inventory = Hash[]
+ @coupon = Hash[]
+ @promo = Hash[]
+ end
+
+ def register(name, price, pr_promo = Hash[])
+ if inventory.has_key? name then raise "Duplicate products!" end
+ if check_name? name and check_price? price then inventory[name] = price.to_d end
+ if pr_promo != Hash[] then product_promo(name, pr_promo) end
+ end
+
+ def product_promo(name, pr_promo)
+ if pr_promo.has_key? :get_one_free
+ promo[name] = Hash[:get_one_free, pr_promo[:get_one_free]]
+ end
+ if pr_promo.has_key? :package
+ promo[name] = Hash[:package, pr_promo[:package]]
+ end
+ if pr_promo.has_key? :threshold
+ promo[name] = Hash[:threshold, pr_promo[:threshold]]
+ end
+ end
+
+ def check_name?(name)
+ if name.size > 0 and name.size <= 40
+ return true
+ end
+ raise "Wrong 'name' input!"
+ end
+
+ def check_price?(price)
+ if price.to_d > 0.00 and price.to_d <= 999.99
+ return true
+ end
+ raise"Wrong 'price' input!"
+ end
+
+ def register_coupon(name, kind)
+ if kind.has_key? :amount or kind.has_key? :percent
+ coupon[name.upcase] = kind
+ end
+ end
+
+ def new_cart
+ return Cart.new inventory, promo, coupon
+ end
+end
+
+class Cart
+ attr_accessor :cart
+ attr_reader :inventory
+ attr_reader :promo
+ attr_reader :promotion
+ attr_reader :coupon
+ attr_reader :coupon_discount
+ attr_accessor :coupon_used
+
+ def initialize(inventory, promo, coupon)
+ @cart = Hash[]
+ @inventory = inventory
+ @promo = promo
+ @promotion = Promotion.new
+ @coupon = coupon
+ @coupon_discount = 0
+ @coupon_used = false
+ end
+
+ def add(name, qty = 1)
+ if inventory.has_key? name
+ if !cart.has_key? name and qty > 0
+ cart[name] = qty
+ else
+ cart_add name, qty
+ end
+ else
+ raise "No such product in inventory!"
+ end
+ end
+
+ def cart_add(name, qty)
+ if ((cart[name]+qty) < 99) and qty > 0
+ cart[name] += qty
+ else
+ raise "Product quantity reached 99 or input 'qty' under 0!"
+ end
+ end
+
+ def use(name)
+ if coupon.has_key? name.upcase and coupon[name.upcase].has_key? :amount
+ @coupon_used = true
+ if coupon[name.upcase][:amount] > total
+ @coupon_discount = total
+ else
+ @coupon_discount = coupon[name.upcase][:amount]
+ end
+ end
+ if coupon.has_key? name.upcase and coupon[name.upcase].has_key? :percent
+ @coupon_used = true
+ @coupon_discount = (total * (coupon[name.upcase][:percent] / 100.0)).to_d
+ end
+ end
+
+ def print_coupon
+ if coupon_used
+ if coupon[coupon.keys[0]].has_key? :amount
+ name = coupon.keys[0]
+ amount = coupon[name][:amount]
+ output = "| Coupon #{name} - #{amount}".ljust(49)+"|"
+ puts output+("-%.2f" % @coupon_discount).rjust(9)+" |"
+ end
+ if coupon[coupon.keys[0]].has_key? :percent
+ print_coupon_percent
+ end
+ end
+ end
+
+ def print_coupon_percent
+ name = coupon.keys[0]
+ percent = coupon[name][:percent]
+ output = "| Coupon #{name} - #{percent}% off".ljust(49)+"|"
+ puts output+("-%.2f" % @coupon_discount).rjust(9)+" |"
+ end
+
+ def total
+ result = 0
+ cart.each do |key, value|
+ pr_promo = if promo.has_key? key then calc_promo(key, value) else 0 end
+ result += inventory[key]*value-pr_promo
+ end
+ return (result - coupon_discount).to_d
+ end
+
+ def calc_promo(key, value)
+ result = 0
+ if promo[key].has_key? :get_one_free
+ result = promotion.calc_get_one_free(inventory, cart, promo, key, value)
+ end
+ if promo[key].has_key? :package
+ result = promotion.calc_package(inventory, cart, promo, key, value)
+ end
+ if promo[key].has_key? :threshold and promo[key][:threshold].keys[0]<cart[key]
+ result = promotion.calc_threshold(inventory, cart, promo, key, value)
+ end
+ return result
+ end
+
+ def invoice
+ puts "+#{"-"*48}+----------+
+| Name#{" "*39}qty | price |
++#{"-"*48}+----------+
+"
+ print_products
+ print_coupon
+ puts "+#{"-"*48}+----------+
+| TOTAL#{" "*42}|#{(" %.2f" % total.to_s).rjust(9) + " |"}
++#{"-"*48}+----------+
+"
+ end
+
+ def print_products
+ cart.each do |key, value|
+ sum = value*inventory[key]
+ puts "| #{key.ljust(44)+value.to_s.rjust(2)} |"+("%.2f" % sum).rjust(9)+" |"
+ promotion.print_promotions(inventory, cart, promo, key, value)
+ end
+ end
+end
+
+class Promotion
+
+ def calc_get_one_free(inventory, cart, promo, key, value)
+ free = promo[key][:get_one_free]
+ return (cart[key]/free)*inventory[key]
+ end
+
+ def calc_package(inventory, cart, promo, key, value)
+ pack = promo[key][:package].keys[0]
+ percent = promo[key][:package].values[0]
+ result = (cart[key]/pack)*pack*inventory[key]*(percent/100.0)
+ return result
+ end
+
+ def calc_threshold(inventory, cart, promo, key, value)
+ price = inventory[key]
+ discount = promo[key][:threshold].values[0] / 100.0
+ threshold = promo[key][:threshold].keys[0]
+ result = (discount/price)*(cart[key] - threshold)
+ return result
+ end
+
+ def print_promotions(inventory, cart, promo, key, value)
+ if promo.has_key? key
+ if promo[key].has_key? :get_one_free
+ puts_get_one_free(inventory, cart, promo, key, value)
+ end
+ if promo[key].has_key? :package
+ puts_package(inventory, cart, promo, key, value)
+ end
+ if promo[key].has_key? :threshold
+ puts_threshold(inventory, cart, promo, key, value)
+ end
+ end
+ end
+
+ def puts_get_one_free(inventory, cart, promo, key, value)
+ free = promo[key][:get_one_free]
+ result = (cart[key]/free)*inventory[key]
+ puts "| (buy #{free - 1}, get 1 free)#{" "*26}|"+("-%.2f" % result).rjust(9)+" |"
+ end
+
+ def puts_package(inventory, cart, promo, key, value)
+ pack = promo[key][:package].keys[0]
+ percent = promo[key][:package].values[0]
+ result = (cart[key]/pack)*pack*inventory[key]*(percent/100.0)
+ output = "| (get #{percent}% off for every #{pack})#{" "*20}|"
+ puts output+("-%.2f" % result).rjust(9)+" |"
+ end
+
+ def puts_threshold(inventory, cart, promo, key, value)
+ price = inventory[key]
+ discount = promo[key][:threshold].values[0]
+ threshold = promo[key][:threshold].keys[0]
+ result = ((discount/100.0)/price)*(cart[key] - threshold)
+ output = "| (#{discount}% off of every after the #{threshold}rd)#{" "*13}|"
+ puts output+("-%.2f" % result).rjust(9)+" |"
+ end
+end