1
0

Use structs instead of hashes for shipments

This commit is contained in:
Tim Kächele 2024-08-27 17:57:33 +02:00
parent 22ff5555a7
commit 4f7fb86601
5 changed files with 33 additions and 16 deletions

View File

@ -9,6 +9,7 @@ GEM
ast (2.4.2)
base64 (0.1.1)
diff-lcs (1.5.0)
docile (1.4.1)
json (2.6.3)
json (2.6.3-java)
language_server-protocol (3.17.0.3)
@ -54,6 +55,12 @@ GEM
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
ruby-progressbar (1.13.0)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
standard (1.31.0)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.0)
@ -70,12 +77,14 @@ GEM
PLATFORMS
arm64-darwin-22
arm64-darwin-23
universal-java-17
DEPENDENCIES
planet_express_express!
rake (~> 13.0)
rspec (~> 3.0)
simplecov (~> 0.22.0)
standard (~> 1.3)
BUNDLED WITH

View File

@ -9,10 +9,10 @@ module PlanetExpressExpress
available_budget = @limit_per_month
month.each do |shipment|
if shipment[:discount] > available_budget
shipment[:discount] = available_budget
if shipment.discount > available_budget
shipment.discount = available_budget
end
available_budget -= shipment[:discount]
available_budget -= shipment.discount
end
end
end

View File

@ -8,7 +8,7 @@ module PlanetExpressExpress
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"
"#{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

View File

@ -23,7 +23,7 @@ module PlanetExpressExpress
lp_count += 1
if lp_count == 3
shipment[:discount] = shipment[:price]
shipment.discount = shipment.price
break
end
end
@ -33,7 +33,7 @@ module PlanetExpressExpress
def s_rule(monthly_shipments)
monthly_shipments.each do |month|
month.each do |shipment|
shipment[:discount] += shipment[:price] - lowest_s_price
shipment.discount += shipment.price - lowest_s_price
end
end
end

View File

@ -1,6 +1,15 @@
module PlanetExpressExpress
require "date"
class ShipmentReader
Shipment = Struct.new(
:id,
:date,
:size,
:provider,
:price,
:discount,
keyword_init: true
)
def initialize(prices)
@prices = prices
@id = 0
@ -21,14 +30,14 @@ module PlanetExpressExpress
io.each_line do |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)
end
shipments[shipment[:size]].last.push(shipment)
shipments[shipment.size].last.push(shipment)
shipments[:all].last.push(shipment)
current_year = shipment[:date].year
current_month = shipment[:date].month
current_year = shipment.date.year
current_month = shipment.date.month
end
shipments
@ -47,19 +56,18 @@ module PlanetExpressExpress
def parse(line)
parts = line.split(" ")
shipment = {
shipment = Shipment.new(
id: sequential_id,
date: Date.strptime(parts[0], "%Y-%m-%d"),
size: parts[1]&.to_sym,
provider: parts[2]&.to_sym,
price: 0,
discount: 0,
valid: true
}
discount: 0
)
shipment[:price] = prices[
shipment.price = prices[
[
shipment[:provider], shipment[:size]
shipment.provider, shipment.size
]
]