Решение на Трета задача от Ивана Йовчева

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

Към профила на Ивана Йовчева

Резултати

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

Код

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

Лог от изпълнението

FFFFFFFFFFFFFFFFFFF

Failures:

  1) Inventory with no discounts can tell the total price of all products
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20111115-5847-17fufch/solution.rb:11

  2) Inventory with no discounts has some constraints on prices and counts
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20111115-5847-17fufch/solution.rb:11

  3) Inventory with no discounts can print an invoice
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20111115-5847-17fufch/solution.rb:11

  4) Inventory with a 'buy X, get one free' promotion grants every nth item for free
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20111115-5847-17fufch/solution.rb:11

  5) Inventory with a 'buy X, get one free' promotion grants 2 items free, when 8 purchased and every 3rd is free
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20111115-5847-17fufch/solution.rb:11

  6) Inventory with a 'buy X, get one free' promotion shows the discount in the invoice
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20111115-5847-17fufch/solution.rb:11

  7) Inventory with a '% off for every n' promotion gives % off for every group of n
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20111115-5847-17fufch/solution.rb:11

  8) Inventory with a '% off for every n' promotion does not discount for extra items, that don't fit in a group
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20111115-5847-17fufch/solution.rb:11

  9) Inventory with a '% off for every n' promotion shows the discount in the invoice
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20111115-5847-17fufch/solution.rb:11

  10) Inventory with a '% off of every item after the nth' promotion gives a discount for every item after the nth
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20111115-5847-17fufch/solution.rb:11

  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: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20111115-5847-17fufch/solution.rb:11

  12) Inventory with a '% off of every item after the nth' promotion shows the discount in the ivnoice
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20111115-5847-17fufch/solution.rb:11

  13) Inventory with a '% off' coupon gives % off of the total
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20111115-5847-17fufch/solution.rb:11

  14) Inventory with a '% off' coupon applies the coupon discount after product promotions
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20111115-5847-17fufch/solution.rb:11

  15) Inventory with a '% off' coupon shows the discount in the invoice
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20111115-5847-17fufch/solution.rb:11

  16) Inventory with an 'amount off' coupon subtracts the amount form the total
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20111115-5847-17fufch/solution.rb:11

  17) Inventory with an 'amount off' coupon does not result in a negative total
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20111115-5847-17fufch/solution.rb:11

  18) Inventory with an 'amount off' coupon shows the discount in the invoice
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20111115-5847-17fufch/solution.rb:11

  19) Inventory with multiple discounts can print an invoice
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20111115-5847-17fufch/solution.rb:11

Finished in 0.65899 seconds
19 examples, 19 failures

Failed examples:

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

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

Ивана обнови решението на 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)