Диляна обнови решението на 07.11.2011 16:24 (преди около 13 години)
+##################################################
+# requirements
+##################################################
+
+require 'bigdecimal'
+require 'bigdecimal/util'
+
+##################################################
+# class Product - contains only name and price
+##################################################
+
+class Product
+ def initialize(name,price)
+ @name, @price = name, price
+ end
+
+ def name
+ @name
+ end
+
+ def name=(other)
+ if other.length <= 40
+ name = other
+ else raise "Name of product is not correct"
+ end
+ end
+
+ def price
+ @price.to_d
+ end
+
+ def price=(other)
+ if other <= 99.99 and other >= 0.01
+ @price = other.to_d
+ else
+ raise "Price of the product is not correct"
+ end
+ end
+
+ def to_s
+ puts @name + "\t" + @price.to_s
+ end
+end #end of class Product
+
+#################################################
+# class Inventary controls the card and promotions
+#################################################
+
+class Inventary
+ attr_accessor :products
+ def initialize
+ @products = Array.new
+ end
+
+ def register(n,price, promotion_tag = {})
+ if products.any? { | i | i.product.name == n } then raise "Error."
+ else
+ @product = Product.new(n,price)
+ @promotional_product = case promotion_tag.keys.fetch(0,nil)
+ when nil then Promotion.new(@product)
+ when :get_one_free then Get_one_free.new(@product,promotion_tag[:get_one_free])
+ when :package then Package.new(@product,promotion_tag[:package])
+ when :threshold then Threshold.new(@product,promotion_tag[:threshold])
+ end
+ end
+ self
+ end
+
+ def new_cart
+ @cart = Cart.new(@products)
+ end
+end #end of class Inventary
+
+#################################
+# Invoice - module - prints the invoice
+##################################
+
+module Invoice
+ @@rule = "+------------------------------------------------+----------+"
+
+ def print_invoice
+ puts @@rule
+ puts "|" + "Name" + "\t\t\t\t\t " + "qty" + "|" + " " + "price" + "|"
+ puts @@rule
+ puts @bought_products.each { | itm | itm.print_item }
+ puts @@rule
+ puts "total: #{@total}"
+ puts @@rule
+ end
+
+end #end invoice
+
+##################################################
+# Cart - contains all the products and quantity for every product
+##################################################
+
+class Cart
+ include Invoice
+ attr_accessor :items
+ def initialize(products)
+ @total = 0.0
+ @bought_products = []
+ @products = products
+ end
+
+ def add(product, quantity = 1)
+ #TO-DO make same interface for collection of products
+ @item = @products.select { | itm | itm.product.name == product }[0]
+ @item.quantity += quantity unless @item == nil
+ @bought_products << @item unless @item == nil
+ @total += quantity * @item.product.price unless @item == nil
+ self
+ end
+
+ def total
+ @total
+ end
+
+ def calculate_promotion
+ @money_off = @bought_products.each { | itm, d = 0 | d += itm.calculate_money_off }
+ end
+
+ def invoice
+ print_invoice
+ end
+end #ends Inventary
+
+
+##########################################
+# Promotions and base class Promotion
+##########################################
+
+class Promotion
+ attr_accessor :product, :quantity
+ def initialize(product)
+ @product = product
+ @message = ""
+ @quantity = 0
+ end
+
+ def to_s
+ @price = @product.price * @quantity
+ "|" + @product.name.to_s + "\t" + @quantity.to_s + "|" + "\t" + @price.to_s("F") + "|"
+ end
+
+ def calculate_money_off
+ return money_off = 0
+ end
+
+ def print_item
+ @price = @product.price * @quantity
+ "|" + @product.name.to_s + "\t" + @quantity.to_s + "|" + "\t " + @price.to_s + "|"
+ end
+end
+
+################################
+# Get One free promotion
+################################
+
+class Get_one_free < Promotion
+ def initialize(product,quantity)
+ super(product)
+ @promotion_quantity = quantity
+ @message = "buy #{@promotion_quantity - 1}, get 1 free"
+ end
+
+ def to_s
+ @discount = calculate_money_off*(-1)
+ super + "\n" + @message + "\t\t\t" + "|" + @discount.to_s
+ end
+
+ def calculate_money_off
+ money_off = ( @quantity % @promotion_quantity ) * @product.price
+ end
+
+ def print_item
+ @discount = calculate_money_off*(-1)
+ super + "\n" + @message + "\t\t\t" + "|" + @discount.to_s
+ end
+
+end #end get one free
+
+#################################
+# Package Promotion
+#################################
+
+class Package < Promotion
+ def initialize(product,hash)
+ super(product)
+ @promotion_quantity = hash.keys[0]
+ @percent = hash[@promotion_quantity]
+ @message = "get #{@percen}% off for every #{@promotion_quantity}"
+ end
+
+ def calculate_money_off
+ money_off = ( @quantity % @promotion_quantity ) * @product.price*'0,2'.to_d
+ end
+
+ def print_item
+ @discount = calculate_money_off
+ super + "\n" + @message + "\t\t\t" + "|" + "-" + @discount.to_s
+ end
+
+ def to_s
+ @discount = calculate_money_off
+ super + "\n" + @message + "\t\t\t" + "|" + "-" + @discount.to_s
+ end
+end #end package
+
+#####################################
+# Threshold Promotion
+#####################################
+
+class Threshold < Promotion
+ def initialize(product,hash)
+ super(product)
+ @promotion_quantity = hash.keys[0]
+ @percent = hash[@promotion_quantity]
+ @message = "#{@percent}% off of every after the #{@promotion_quantity}rd"
+ end
+
+ def calculate_money_off
+ m = ( @quantity % @promotion_quantity) * (@product.price - @product.price * @percent)
+ end
+
+ def print_item
+ @discount = calculate_money_off*(-1)
+ super + "\n" + @message + "\t\t\t" + "|" + @discount.to_s("F")
+ end
+
+ def to_s
+ @discount = calculate_money_off*(-1)
+ super + "\n" + @message + "\t\t\t" + "|" + @discount.to_s("F")
+ end
+end #end threshold
+
+#########################################################################
+# Coupon - Base Class
+#########################################################################
+
+class Coupon
+
+end
+