Stop reading in all content
Process the shipments month by month instead
This commit is contained in:
parent
5e99d065ef
commit
e91e12c7ea
17
bin/process
17
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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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(" ")
|
||||
|
||||
|
@ -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__)
|
||||
|
Loading…
Reference in New Issue
Block a user