1
0

Stop reading in all content

Process the shipments month by month instead
This commit is contained in:
Tim Kächele 2024-12-11 18:06:04 +01:00
parent 5e99d065ef
commit e91e12c7ea
6 changed files with 40 additions and 52 deletions

View File

@ -7,13 +7,12 @@ require "stringio"
data = File.read(ARGV[0]) 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 output = StringIO.new
PlanetExpressExpress::ShipmentReader.new(PlanetExpressExpress::PRICE_LIST).read_shipments(data) do |shipments|
PlanetExpressExpress::Formatter.new(output).format(shipments) PlanetExpressExpress::Rules.new(PlanetExpressExpress::PRICE_LIST).apply_rules(shipments)
PlanetExpressExpress::DiscountBudget.new(PlanetExpressExpress::DISCOUNT_LIMIT).limit_discounts(shipments)
output.rewind PlanetExpressExpress::Formatter.new(output).format(shipments)
puts output.read output.rewind
puts output.read
output.rewind
end

View File

@ -5,10 +5,9 @@ module PlanetExpressExpress
end end
def limit_discounts(shipments) def limit_discounts(shipments)
shipments[:all].each do |month|
available_budget = @limit_per_month available_budget = @limit_per_month
month.each do |shipment| shipments[:all].each do |shipment|
if shipment.discount > available_budget if shipment.discount > available_budget
shipment.discount = available_budget shipment.discount = available_budget
end end
@ -16,5 +15,4 @@ module PlanetExpressExpress
end end
end end
end end
end
end end

View File

@ -5,13 +5,11 @@ module PlanetExpressExpress
end end
def format(shipments) def format(shipments)
shipments[:all].each do |month| shipments[:all].each do |shipment|
month.each do |shipment|
@io.write( @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" "#{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 end
end end
end
end end

View File

@ -15,8 +15,7 @@ module PlanetExpressExpress
attr_reader :lowest_s_price attr_reader :lowest_s_price
def l_rule(monthly_shipments) def l_rule(month)
monthly_shipments.each do |month|
lp_count = 0 lp_count = 0
month.each do |shipment| month.each do |shipment|
next if shipment[:provider] != :LP next if shipment[:provider] != :LP
@ -28,14 +27,11 @@ module PlanetExpressExpress
end end
end end
end end
end
def s_rule(monthly_shipments) def s_rule(month)
monthly_shipments.each do |month|
month.each do |shipment| month.each do |shipment|
shipment.discount += shipment.price - lowest_s_price shipment.discount += shipment.price - lowest_s_price
end end
end end
end end
end
end end

View File

@ -30,28 +30,25 @@ module PlanetExpressExpress
shipment = parse(line) shipment = parse(line)
if shipment.date.year != current_year || shipment.date.month != current_month 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) end
shipments[:all].last.push(shipment) shipments[shipment.size].push(shipment)
shipments[:all].push(shipment)
current_year = shipment.date.year current_year = shipment.date.year
current_month = shipment.date.month current_month = shipment.date.month
end end
shipments yield shipments
end end
private private
attr_reader :prices attr_reader :prices
def close_month(shipments)
shipments.keys.each do |key|
shipments[key].push([])
end
end
def parse(line) def parse(line)
parts = line.split(" ") parts = line.split(" ")

View File

@ -7,13 +7,13 @@ RSpec.describe PlanetExpressExpress do
"r" "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 output = StringIO.new
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) PlanetExpressExpress::Formatter.new(output).format(shipments)
end
expected_output = File.read( expected_output = File.read(
File.expand_path("../fixtures/output.txt", __FILE__) File.expand_path("../fixtures/output.txt", __FILE__)