Skip to content

Commit

Permalink
checkout price matching
Browse files Browse the repository at this point in the history
  • Loading branch information
goodviber committed Jun 11, 2018
1 parent 3802c36 commit d79c07a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
33 changes: 31 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,44 @@ task :run do
item2 = Product.new('002', 'Little Table', 45.00)
item3 = Product.new('003', 'Funky Light', 19.95)

puts "discount for method"

puts "first basket"
dm = DiscountManager.new(promo_rules)
price = dm.discount_price_for(item1, 3)
puts price
puts PromoRule::TYPE[:multi_discount]
dm.promo_rules.each do |pr|

if pr.type == PromoRule::TYPE[:multi_discount]
puts "apply", pr.apply('001', 2)
end
end

co = Checkout.new(promo_rules)
co.scan(item1)
co.scan(item1)
puts co.total

co = Checkout.new(promo_rules)
puts "scanning"
co.scan(item1)
co.scan(item2)
co.scan(item3)

puts co.total

co = Checkout.new(promo_rules)
co.scan(item1)
co.scan(item3)
co.scan(item1)

puts co.total

co = Checkout.new(promo_rules)
co.scan(item1)
co.scan(item2)
co.scan(item1)
co.scan(item3)

puts co.total

end
9 changes: 6 additions & 3 deletions lib/checkout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Checkout
attr_accessor :basket

def initialize(promo_rules = [])
@dicount_manager = DiscountManager.new(promo_rules)
@discount_manager = DiscountManager.new(promo_rules)
@promo_rules = promo_rules
@basket = []
end
Expand All @@ -17,9 +17,12 @@ def total
price = 0
puts basket
basket.each do |product|
price += product.price
product_quantity = quantity_in_basket(product.code)
price += discount_manager.discount_price_for(product, quantity_in_basket(product.code))
end
price.round(2)

result = discount_manager.discount_total(price)
result.round(2)
end

def quantity_in_basket(code)
Expand Down
6 changes: 3 additions & 3 deletions lib/discount_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ def initialize(promo_rules = [])
end

def discount_price_for(product, quantity)
discount_price = 0
discount_price = nil

promo_rules.each do |promo_rule|
if promo_rule.type == PromoRule::TYPE[:multibuy_discount] #multi_discount
if promo_rule.type == 2 #multi_discount
discount_price = promo_rule.apply(product.code, quantity)
end
end
discount_price == 0 ? product.price : discount_price
discount_price == nil ? product.price : discount_price
end

def discount_total(total)
Expand Down
6 changes: 3 additions & 3 deletions lib/promo_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class PromoRule
TYPE = { discount: 1, multi_discount: 2 }

def initialize(type, &block)
@type = type
@block = block
@type = type
@block = block
end

def apply(*args)
Expand All @@ -24,7 +24,7 @@ def self.discount_rule(minimum_spend, percentage_discount)
if sum_total >= minimum_spend
sum_total.round(2) - sum_total.round(2)*percentage_discount/100
else
sum_total
sum_total.round(2)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/discount_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

describe '#discount_price_for' do
before do
allow(promo_rule).to receive(:type) { PromoRule::TYPE[:multibuy_discount] }
allow(promo_rule).to receive(:type) { PromoRule::TYPE[:multi_discount] }
allow(promo_rule).to receive(:apply) { 60 }
end

it 'should pass the calculation to the promo_rule with params' do
expect(promo_rule).to receive(:type) { PromoRule::TYPE[:multibuy_discount] }
expect(promo_rule).to receive(:type) { PromoRule::TYPE[:multi_discount] }
expect(promo_rule).to receive(:apply).with(product.code, 2)

subject.discount_price_for(product, 2)
end

it 'should return the discounted price when rule applied' do
price = subject.discount_price_for(product, 1)
price = subject.discount_price_for(product, 2)
expect(price).to eq(60)
end

Expand Down

0 comments on commit d79c07a

Please sign in to comment.