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])
shipments = PlanetExpressExpress::ShipmentReader.new(PlanetExpressExpress::PRICE_LIST).read_shipments(data)
output = StringIO.new
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)
output = StringIO.new
PlanetExpressExpress::Formatter.new(output).format(shipments)
output.rewind
puts output.read
output.rewind
end

View File

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

View File

@ -5,8 +5,7 @@ module PlanetExpressExpress
end
def format(shipments)
shipments[:all].each do |month|
month.each do |shipment|
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"
)
@ -14,4 +13,3 @@ module PlanetExpressExpress
end
end
end
end

View File

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

View File

@ -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
shipments[shipment.size].last.push(shipment)
shipments[:all].last.push(shipment)
end
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(" ")

View File

@ -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::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__)