1
0

Add reader for shipments

This commit is contained in:
Tim Kächele 2023-09-02 08:42:40 +02:00
parent 4afb514743
commit 2ce3c4d826
2 changed files with 67 additions and 1 deletions

View File

@ -2,7 +2,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"
module PlanetExpressExpress module PlanetExpressExpress
class Error < StandardError; end class Error < StandardError; end

View File

@ -0,0 +1,66 @@
module PlanetExpressExpress
require 'date'
class ShipmentReader
def initialize(prices)
@prices = prices
end
# Expects to read a file
def read_shipments(io)
shipments = {
S: [],
M: [],
L: []
}
current_month = 0
current_year = 0
x = 0
io.lines.each do |line|
shipment = parse(line)
if shipment[:date].year != current_year || shipment[:date].month != current_month
close_month(shipments)
end
shipments[shipment[:size]].last.push(shipment)
current_year = shipment[:date].year
current_month = shipment[:date].month
x += 1
end
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(" ")
shipment = {
date: Date.parse(parts[0]),
size: parts[1]&.to_sym,
provider: parts[2]&.to_sym,
price: 0,
discount: 0,
valid: true
}
shipment[:price] = prices[
[
shipment[:provider], shipment[:size]
]
]
shipment
end
end
end