Add rule implementation
This commit is contained in:
parent
2ce3c4d826
commit
19458df2dd
@ -3,6 +3,7 @@
|
|||||||
require_relative "planet_express_express/version"
|
require_relative "planet_express_express/version"
|
||||||
require_relative "planet_express_express/constants"
|
require_relative "planet_express_express/constants"
|
||||||
require_relative "planet_express_express/shipment_reader"
|
require_relative "planet_express_express/shipment_reader"
|
||||||
|
require_relative "planet_express_express/rules"
|
||||||
|
|
||||||
module PlanetExpressExpress
|
module PlanetExpressExpress
|
||||||
class Error < StandardError; end
|
class Error < StandardError; end
|
||||||
|
40
lib/planet_express_express/rules.rb
Normal file
40
lib/planet_express_express/rules.rb
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
module PlanetExpressExpress
|
||||||
|
class Rules
|
||||||
|
def initialize(prices)
|
||||||
|
@lowest_s_price = prices.select { |k,v| k[1] == :S }.values.min
|
||||||
|
end
|
||||||
|
|
||||||
|
def apply_rules(shipments)
|
||||||
|
s_rule(shipments[:S])
|
||||||
|
l_rule(shipments[:L])
|
||||||
|
|
||||||
|
shipments
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
attr_reader :lowest_s_price
|
||||||
|
|
||||||
|
def l_rule(monthly_shipments)
|
||||||
|
monthly_shipments.each do |month|
|
||||||
|
lp_count = 0
|
||||||
|
month.each do |shipment|
|
||||||
|
next if shipment[:provider] != :LP
|
||||||
|
|
||||||
|
lp_count += 1
|
||||||
|
next if lp_count != 3
|
||||||
|
|
||||||
|
shipment[:discount] = shipment[:price]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def s_rule(monthly_shipments)
|
||||||
|
monthly_shipments.each do |month|
|
||||||
|
month.each do |shipment|
|
||||||
|
shipment[:discount] += shipment[:price] - lowest_s_price
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user