1
0

Improve available budget calculation

Don't store individual discounts, and instead sum them up directly.
This commit is contained in:
Tim Kächele 2021-11-22 18:36:35 +01:00
parent a09cecacb7
commit fbebe75e2d

View File

@ -2,16 +2,16 @@
module PlanetExpress module PlanetExpress
class DiscountBudget class DiscountBudget
attr_reader :budget attr_reader :budget, :available_budget
def initialize(budget:) def initialize(budget:)
@budget = budget @budget = budget
@applied_discounts = [] @available_budget = budget
end end
def register_applied_discount(discount_amount) def register_applied_discount(discount_amount)
possible_discount = maximum_possible_discount(discount_amount) possible_discount = maximum_possible_discount(discount_amount)
applied_discounts.push(possible_discount) @available_budget = available_budget - possible_discount
possible_discount possible_discount
end end
@ -19,20 +19,8 @@ module PlanetExpress
[available_budget, discount_amount].min [available_budget, discount_amount].min
end end
def available_budget
budget - spent_budget
end
def remaining_budget? def remaining_budget?
!available_budget.zero? !available_budget.zero?
end end
private
attr_reader :applied_discounts
def spent_budget
applied_discounts.sum
end
end end
end end