Use structs instead of hashes for shipments
This commit is contained in:
parent
22ff5555a7
commit
4f7fb86601
@ -9,6 +9,7 @@ GEM
|
|||||||
ast (2.4.2)
|
ast (2.4.2)
|
||||||
base64 (0.1.1)
|
base64 (0.1.1)
|
||||||
diff-lcs (1.5.0)
|
diff-lcs (1.5.0)
|
||||||
|
docile (1.4.1)
|
||||||
json (2.6.3)
|
json (2.6.3)
|
||||||
json (2.6.3-java)
|
json (2.6.3-java)
|
||||||
language_server-protocol (3.17.0.3)
|
language_server-protocol (3.17.0.3)
|
||||||
@ -54,6 +55,12 @@ GEM
|
|||||||
rubocop (>= 1.7.0, < 2.0)
|
rubocop (>= 1.7.0, < 2.0)
|
||||||
rubocop-ast (>= 0.4.0)
|
rubocop-ast (>= 0.4.0)
|
||||||
ruby-progressbar (1.13.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)
|
standard (1.31.0)
|
||||||
language_server-protocol (~> 3.17.0.2)
|
language_server-protocol (~> 3.17.0.2)
|
||||||
lint_roller (~> 1.0)
|
lint_roller (~> 1.0)
|
||||||
@ -70,12 +77,14 @@ GEM
|
|||||||
|
|
||||||
PLATFORMS
|
PLATFORMS
|
||||||
arm64-darwin-22
|
arm64-darwin-22
|
||||||
|
arm64-darwin-23
|
||||||
universal-java-17
|
universal-java-17
|
||||||
|
|
||||||
DEPENDENCIES
|
DEPENDENCIES
|
||||||
planet_express_express!
|
planet_express_express!
|
||||||
rake (~> 13.0)
|
rake (~> 13.0)
|
||||||
rspec (~> 3.0)
|
rspec (~> 3.0)
|
||||||
|
simplecov (~> 0.22.0)
|
||||||
standard (~> 1.3)
|
standard (~> 1.3)
|
||||||
|
|
||||||
BUNDLED WITH
|
BUNDLED WITH
|
||||||
|
@ -9,10 +9,10 @@ module PlanetExpressExpress
|
|||||||
available_budget = @limit_per_month
|
available_budget = @limit_per_month
|
||||||
|
|
||||||
month.each do |shipment|
|
month.each do |shipment|
|
||||||
if shipment[:discount] > available_budget
|
if shipment.discount > available_budget
|
||||||
shipment[:discount] = available_budget
|
shipment.discount = available_budget
|
||||||
end
|
end
|
||||||
available_budget -= shipment[:discount]
|
available_budget -= shipment.discount
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -8,7 +8,7 @@ module PlanetExpressExpress
|
|||||||
shipments[:all].each do |month|
|
shipments[:all].each do |month|
|
||||||
month.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
|
||||||
|
@ -23,7 +23,7 @@ module PlanetExpressExpress
|
|||||||
|
|
||||||
lp_count += 1
|
lp_count += 1
|
||||||
if lp_count == 3
|
if lp_count == 3
|
||||||
shipment[:discount] = shipment[:price]
|
shipment.discount = shipment.price
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -33,7 +33,7 @@ module PlanetExpressExpress
|
|||||||
def s_rule(monthly_shipments)
|
def s_rule(monthly_shipments)
|
||||||
monthly_shipments.each do |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
|
||||||
|
@ -1,6 +1,15 @@
|
|||||||
module PlanetExpressExpress
|
module PlanetExpressExpress
|
||||||
require "date"
|
require "date"
|
||||||
class ShipmentReader
|
class ShipmentReader
|
||||||
|
Shipment = Struct.new(
|
||||||
|
:id,
|
||||||
|
:date,
|
||||||
|
:size,
|
||||||
|
:provider,
|
||||||
|
:price,
|
||||||
|
:discount,
|
||||||
|
keyword_init: true
|
||||||
|
)
|
||||||
def initialize(prices)
|
def initialize(prices)
|
||||||
@prices = prices
|
@prices = prices
|
||||||
@id = 0
|
@id = 0
|
||||||
@ -21,14 +30,14 @@ module PlanetExpressExpress
|
|||||||
io.each_line do |line|
|
io.each_line do |line|
|
||||||
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)
|
close_month(shipments)
|
||||||
end
|
end
|
||||||
shipments[shipment[:size]].last.push(shipment)
|
shipments[shipment.size].last.push(shipment)
|
||||||
shipments[:all].last.push(shipment)
|
shipments[:all].last.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
|
shipments
|
||||||
@ -47,19 +56,18 @@ module PlanetExpressExpress
|
|||||||
def parse(line)
|
def parse(line)
|
||||||
parts = line.split(" ")
|
parts = line.split(" ")
|
||||||
|
|
||||||
shipment = {
|
shipment = Shipment.new(
|
||||||
id: sequential_id,
|
id: sequential_id,
|
||||||
date: Date.strptime(parts[0], "%Y-%m-%d"),
|
date: Date.strptime(parts[0], "%Y-%m-%d"),
|
||||||
size: parts[1]&.to_sym,
|
size: parts[1]&.to_sym,
|
||||||
provider: parts[2]&.to_sym,
|
provider: parts[2]&.to_sym,
|
||||||
price: 0,
|
price: 0,
|
||||||
discount: 0,
|
discount: 0
|
||||||
valid: true
|
)
|
||||||
}
|
|
||||||
|
|
||||||
shipment[:price] = prices[
|
shipment.price = prices[
|
||||||
[
|
[
|
||||||
shipment[:provider], shipment[:size]
|
shipment.provider, shipment.size
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user