1
0

Add rule implementation

This commit is contained in:
Tim Kächele 2023-09-02 08:43:02 +02:00
parent 2ce3c4d826
commit 19458df2dd
2 changed files with 41 additions and 0 deletions

View File

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

View 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