diff --git a/bin/process b/bin/process index d5fc472..02819a7 100755 --- a/bin/process +++ b/bin/process @@ -7,13 +7,12 @@ require "stringio" data = File.read(ARGV[0]) -shipments = PlanetExpressExpress::ShipmentReader.new(PlanetExpressExpress::PRICE_LIST).read_shipments(data) -PlanetExpressExpress::Rules.new(PlanetExpressExpress::PRICE_LIST).apply_rules(shipments) -PlanetExpressExpress::DiscountBudget.new(PlanetExpressExpress::DISCOUNT_LIMIT).limit_discounts(shipments) - output = StringIO.new - -PlanetExpressExpress::Formatter.new(output).format(shipments) - -output.rewind -puts output.read +PlanetExpressExpress::ShipmentReader.new(PlanetExpressExpress::PRICE_LIST).read_shipments(data) do |shipments| + PlanetExpressExpress::Rules.new(PlanetExpressExpress::PRICE_LIST).apply_rules(shipments) + PlanetExpressExpress::DiscountBudget.new(PlanetExpressExpress::DISCOUNT_LIMIT).limit_discounts(shipments) + PlanetExpressExpress::Formatter.new(output).format(shipments) + output.rewind + puts output.read + output.rewind +end diff --git a/lib/planet_express_express/discount_budget.rb b/lib/planet_express_express/discount_budget.rb index 7a0fecc..0ce432d 100644 --- a/lib/planet_express_express/discount_budget.rb +++ b/lib/planet_express_express/discount_budget.rb @@ -5,15 +5,13 @@ module PlanetExpressExpress end def limit_discounts(shipments) - shipments[:all].each do |month| - available_budget = @limit_per_month + available_budget = @limit_per_month - month.each do |shipment| - if shipment.discount > available_budget - shipment.discount = available_budget - end - available_budget -= shipment.discount + shipments[:all].each do |shipment| + if shipment.discount > available_budget + shipment.discount = available_budget end + available_budget -= shipment.discount end end end diff --git a/lib/planet_express_express/formatter.rb b/lib/planet_express_express/formatter.rb index b7538f3..d6f6664 100644 --- a/lib/planet_express_express/formatter.rb +++ b/lib/planet_express_express/formatter.rb @@ -5,12 +5,10 @@ module PlanetExpressExpress end def format(shipments) - shipments[:all].each do |month| - month.each do |shipment| - @io.write( - "#{shipment.date.iso8601} #{shipment.size} #{shipment.provider} #{(shipment.price - shipment.discount).to_s.rjust(3, "0").insert(-3, ".")} #{shipment.discount.zero? ? "-" : shipment.discount.to_s.rjust(3, "0").insert(-3, ".")}\n" - ) - end + shipments[:all].each do |shipment| + @io.write( + "#{shipment.date.iso8601} #{shipment.size} #{shipment.provider} #{(shipment.price - shipment.discount).to_s.rjust(3, "0").insert(-3, ".")} #{shipment.discount.zero? ? "-" : shipment.discount.to_s.rjust(3, "0").insert(-3, ".")}\n" + ) end end end diff --git a/lib/planet_express_express/rules.rb b/lib/planet_express_express/rules.rb index d3ce9a3..b8ec371 100644 --- a/lib/planet_express_express/rules.rb +++ b/lib/planet_express_express/rules.rb @@ -15,26 +15,22 @@ module PlanetExpressExpress 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 + def l_rule(month) + lp_count = 0 + month.each do |shipment| + next if shipment[:provider] != :LP - lp_count += 1 - if lp_count == 3 - shipment.discount = shipment.price - break - end + lp_count += 1 + if lp_count == 3 + shipment.discount = shipment.price + break 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 + def s_rule(month) + month.each do |shipment| + shipment.discount += shipment.price - lowest_s_price end end end diff --git a/lib/planet_express_express/shipment_reader.rb b/lib/planet_express_express/shipment_reader.rb index 61e46c6..f04d583 100644 --- a/lib/planet_express_express/shipment_reader.rb +++ b/lib/planet_express_express/shipment_reader.rb @@ -30,28 +30,25 @@ module PlanetExpressExpress shipment = parse(line) if shipment.date.year != current_year || shipment.date.month != current_month - close_month(shipments) + yield shipments + shipments.each do |_,v| + v.clear + end end - shipments[shipment.size].last.push(shipment) - shipments[:all].last.push(shipment) + shipments[shipment.size].push(shipment) + shipments[:all].push(shipment) current_year = shipment.date.year current_month = shipment.date.month end - shipments + yield shipments end private attr_reader :prices - def close_month(shipments) - shipments.keys.each do |key| - shipments[key].push([]) - end - end - def parse(line) parts = line.split(" ") diff --git a/spec/planet_express_express_spec.rb b/spec/planet_express_express_spec.rb index 88cc8d9..181c6ef 100644 --- a/spec/planet_express_express_spec.rb +++ b/spec/planet_express_express_spec.rb @@ -7,13 +7,13 @@ RSpec.describe PlanetExpressExpress do "r" ) - shipments = PlanetExpressExpress::ShipmentReader.new(PlanetExpressExpress::PRICE_LIST).read_shipments(input_file) - PlanetExpressExpress::Rules.new(PlanetExpressExpress::PRICE_LIST).apply_rules(shipments) - PlanetExpressExpress::DiscountBudget.new(PlanetExpressExpress::DISCOUNT_LIMIT).limit_discounts(shipments) - output = StringIO.new - PlanetExpressExpress::Formatter.new(output).format(shipments) + PlanetExpressExpress::ShipmentReader.new(PlanetExpressExpress::PRICE_LIST).read_shipments(input_file) do |shipments| + PlanetExpressExpress::Rules.new(PlanetExpressExpress::PRICE_LIST).apply_rules(shipments) + PlanetExpressExpress::DiscountBudget.new(PlanetExpressExpress::DISCOUNT_LIMIT).limit_discounts(shipments) + PlanetExpressExpress::Formatter.new(output).format(shipments) + end expected_output = File.read( File.expand_path("../fixtures/output.txt", __FILE__)