Радина обнови решението на 06.11.2011 23:48 (преди около 13 години)
+class Inventory
+ def initialize
+ @products = Product.new
+ @new_cart = Cart.new
+ @coupon_name = Coupon.new
+ end
+ def register
+ @products
+ end
+ def new_cart
+ @new_cart.add
+ end
+ def register_coupon
+ @coupon_name
+ end
+end
+
+class Coupon <Cart
+ @discount = [amount: 10, percent: 20]
+ def name
+ @name
+ end
+
+ def discount
+ @discount
+ if amount == nil
+ total = (total*0.2).to_d
+ elsif
+ (total- 10 ) < 0 total == 0.to_d
+ else
+ total = (total - 10)
+ end
+ end
+end
+
+class Product
+ @promotion=[ get_one_free:4, package: {3=>20}, threshold: {10 => 50}]
+
+ def initialize(name, price, promotion)
+ @name = name and name.length < 40
+ @price = price and price.between? (0.01, 999.99)
+ @promotion = promotion
+ end
+
+ def price
+ @price
+ end
+
+ def promotion
+ if @promotion.key = get_one_free: and quantity>=4
+ @promotion = (quantity mod 4)*price + (quantity/4).floor *3/4*price
+ end
+ if @promotion.key = package: and quantity>=3
+ @promotion = (quantity/3).floor*price*0.2 + (quantity mod 3)*price) end
+
+ if @promotion.key = threshold: and quantity>10
+ @promotion = 10*price + (quantity-10)*price/2 end
+ end
+
+end
+
+class Cart
+ @product = Product.new
+ def add(product, quantity(default = 1))
+ @product = product and quantity > 0
+ end
+ def use(coupon_name)
+ coupon_name = Coupon.new
+ puts coupon_name.name
+ end
+ def total
+ while @product
+ do
+ {|@product.price,@product.promotion| @product.price* quantity-@product.promotion }
+ end
+ end
+
+end
+