Решение на Трета задача от Радина Петкова

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

Към профила на Радина Петкова

Резултати

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

Код

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

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

/data/rails/evans/releases/20111115194838/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:256:in `require': /tmp/d20111115-5847-jovaxg/solution.rb:29: syntax error, unexpected tIDENTIFIER, expecting keyword_then or ';' or '\n' (SyntaxError)
        (total- 10 ) < 0 total == 0.to_d
                              ^
/tmp/d20111115-5847-jovaxg/solution.rb:41: syntax error, unexpected ',', expecting ')'
    @price = price and price.between? (0.01, 999.99) 
                                            ^
/tmp/d20111115-5847-jovaxg/solution.rb:50: syntax error, unexpected tLABEL
    if  @promotion.key = get_one_free: and quantity>=4
                                      ^
/tmp/d20111115-5847-jovaxg/solution.rb:51: syntax error, unexpected tSTAR, expecting keyword_end
	@promotion = (quantity mod 4)*price + (quantity/4).floor *3/4*price
	                                                          ^
/tmp/d20111115-5847-jovaxg/solution.rb:53: syntax error, unexpected tLABEL
    if  @promotion.key = package: and quantity>=3
                                 ^
/tmp/d20111115-5847-jovaxg/solution.rb:54: syntax error, unexpected ')', expecting keyword_end
...e*0.2 + (quantity mod 3)*price) end
...                               ^
/tmp/d20111115-5847-jovaxg/solution.rb:56: syntax error, unexpected tLABEL
    if  @promotion.key = threshold: and quantity>10
                                   ^
/tmp/d20111115-5847-jovaxg/solution.rb:58: syntax error, unexpected keyword_end, expecting $end
  end
     ^
	from /data/rails/evans/releases/20111115194838/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:256:in `block in requires='
	from /data/rails/evans/releases/20111115194838/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:256:in `map'
	from /data/rails/evans/releases/20111115194838/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:256:in `requires='
	from /data/rails/evans/releases/20111115194838/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration_options.rb:19:in `block in configure'
	from /data/rails/evans/releases/20111115194838/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration_options.rb:18:in `each'
	from /data/rails/evans/releases/20111115194838/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration_options.rb:18:in `configure'
	from /data/rails/evans/releases/20111115194838/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/command_line.rb:17:in `run'
	from /data/rails/evans/releases/20111115194838/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:80:in `run_in_process'
	from /data/rails/evans/releases/20111115194838/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:69:in `run'
	from /data/rails/evans/releases/20111115194838/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:10:in `block in autorun'

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

Радина обнови решението на 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
+