Решение на Трета задача от Диляна Тодорова

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

Към профила на Диляна Тодорова

Резултати

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

Код

##################################################
# 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

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

/tmp/d20111115-5847-1267ham/spec.rb:33:in `<top (required)>': uninitialized constant Object::Inventory (NameError)
	from /data/rails/evans/releases/20111115194838/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in `load'
	from /data/rails/evans/releases/20111115194838/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in `block in load_spec_files'
	from /data/rails/evans/releases/20111115194838/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459: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:459:in `load_spec_files'
	from /data/rails/evans/releases/20111115194838/vendor/bundle/ruby/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/command_line.rb:18: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 коментара)

Диляна обнови решението на 07.11.2011 16:24 (преди над 12 години)

+##################################################
+# 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
+