Ивана обнови решението на 05.11.2011 15:20 (преди около 13 години)
+require 'bigdecimal'
+require 'bigdecimal/util'
+
+class Inventory
+
+ def product
+ @product = {}
+ end
+
+ def new_promo
+ @new_promo = Promotions.new
+ new_promo.promo(self)
+ end
+
+ def has_promotion
+ @has_promotion = {}
+ end
+
+ def make_promoted(name_of_product)
+ has_promotion[name_of_product] = true
+ end
+
+ def has_promotion?(name_of_product)
+ has_promotion[name_of_product] == true
+ end
+
+ def already_existing?(name_of_product)
+ product.has_key? name_of_product
+ end
+
+ def register(new_product, price, promotion = {})
+ if new_product.length > 40 or
+ price.to_d > 999.99 or price.to_d < 0.01 or already_existing?(new_product)
+ raise "Invalid parameters passed."
+ end
+ product[new_product] = price
+ if not promotion.empty?
+ new_promo.promo[new_product] = promotion.to_a
+ make_promoted(new_product)
+ else has_promotion[new_product] = false
+ end
+ end
+
+ def new_cart
+ @new_cart = ShopCar.new
+ new_cart.shop_car(self)
+ end
+
+ def all_coupons
+ @all_coupons = {}
+ end
+
+ def register_coupon(name, coupon = {})
+ if not coupon.empty?
+ all_coupons[name] = coupon.to_a
+ end
+ end
+
+end
+
+class ShopCar
+
+ def shop_car()
+ @shop_car = {}
+ end
+
+ def inventory
+ @inventory = Inventory.new
+ end
+
+ def sum_promo
+ @sum_promo = '0.00'.to_d
+ end
+
+ def cpn
+ @cpn = Coupons.new
+ end
+
+ def sum_promotions(product, quantity)
+ sum_promo += count_promotion(product, quantity)
+ end
+
+ def add(choice, pairs = 1)
+ if pairs <= 0 or pairs > 99 or not inventory.already_existing?(choice)
+ raise "Invalid parameters passed."
+ end
+ shop_car[choice] += pairs
+ if inventory.has_promotion?(choice)
+ sum_promotions(choice, shop_car[choice])
+ end
+ end
+
+ def count_promotion(product, quantity)
+ pack = inventory.new_promo.promo[product][0]
+ value = inventory.new_promo.promo[product][1]
+ one_s_price = inventory.product[product]
+ unless inventory.new_promo.promo[product].kind_of? Hash
+ inventory.new_promo.get_one_free(product, one_s_price, quantity, value)
+ else inventory.new_promo.promo_type(product, one_s_price, quantity, pack, value)
+ end
+ end
+
+ def total
+ total_price = String.new
+ sum_of_prices = '0.00'.to_d
+ shop_car.each do |item, value|
+ sum_of_prices += inventory.product[item].to_d * value
+ end
+ result = sum_of_prices - sum_promo
+ total_price = result.to_s
+ return total_price.to_d
+ end
+
+ def use(name_of_coupon)
+ cena = total
+ t = '0.00'.to_d
+ helper = inventory.all_coupons[name_of_coupon]
+ t = helper[0] == :percent ? cpn.percent(cena, helper[1]) : cpn.amount(cena, helper[1])
+ # съжалявам за съкращенията, но трябваше да се сместя в sceptic
+ sum_promo += tmp
+ ( price + sum_promo ) > '0.00'.to_d ? sum_promo : '0.00'.to_d
+ end
+
+ def invoice
+ Receipt.new.print_receipt
+ end
+
+end
+
+class Receipt
+
+ def print_receipt
+ #?????????????
+ end
+
+end
+
+class Promotions
+
+ def promo()
+ @promo = {}
+ end
+
+ def function(w, x, y, z)
+ - ( ( ( w - x ) * y.to_d ) * ( z / 100 ))
+ end
+
+ def get_one_free( product, one_s_price, quantity, number)
+ if promo[product].has_key?(:get_one_free) and quantity > number
+ return ( - ( (quantity / number) * one_s_price.to_d ) )
+ else return '0.00'.to_d
+ end
+ end
+
+ def package(product, one_s_price, quantity, pack, value)
+ out_of_pack = quantity % pack
+ if promo[product].has_key?(:package) and quantity >= pack
+ return function(quantity, out_of_pack, one_s_price, value)
+ else return '0.00'.to_d
+ end
+ end
+
+ def threshold(product, one_s_price, quantity, after, value)
+ if promo[product].has_key?(:threshold) and quantity > after
+ return function(quantity, after, one_s_price, value)
+ else return '0.00'.to_d
+ end
+ end
+
+ def promo_type(product, one_s_price, quantity, pack, value)
+ promo2 = package(product, one_s_price, quantity, pack, value)
+ promo3 = threshold(product, one_s_price, quantity, pack, value)
+ return promo2 + promo3
+ end
+
+end
+
+class Coupons
+
+ def percent(total_price, descent)
+ return ( - ( total_price * ( descent / 100 ) ) )
+ end
+
+ def amount(total_price, descent)
+ return ( - ( total_price - descent.to_d ) )
+ end
+
+end
- Какво по-точно е
ShopCar
/shop_car
? :) - Относно въпроса ти за
Inventory#new_cart
, според мен твоят клас за количка просто не трябва да наследява от класа за инвентар. - Оставяй по един празен ред между дефинициите на методи и класове/модули за по-добра прегледност и четимост на кода ти; един празен ред след блокът с
require
-директиви също ще помогне за прегледността - Спокойно може да ползваш
'999.99'.to_d
вместоBigDecimal('999.99')
- В Ruby не се слагат интервали около отварящата и затварящата скоба както при деклариране, така и при извикване на методи; например,
already_existing( name_of_product )
иproduct.already_existing( choice )
е по-добре да се напишат катоalready_existing(name_of_product)
иproduct.already_existing(choice)