Improve available budget calculation
Don't store individual discounts, and instead sum them up directly.
This commit is contained in:
parent
a09cecacb7
commit
fbebe75e2d
@ -2,16 +2,16 @@
|
||||
|
||||
module PlanetExpress
|
||||
class DiscountBudget
|
||||
attr_reader :budget
|
||||
attr_reader :budget, :available_budget
|
||||
|
||||
def initialize(budget:)
|
||||
@budget = budget
|
||||
@applied_discounts = []
|
||||
@available_budget = budget
|
||||
end
|
||||
|
||||
def register_applied_discount(discount_amount)
|
||||
possible_discount = maximum_possible_discount(discount_amount)
|
||||
applied_discounts.push(possible_discount)
|
||||
@available_budget = available_budget - possible_discount
|
||||
possible_discount
|
||||
end
|
||||
|
||||
@ -19,20 +19,8 @@ module PlanetExpress
|
||||
[available_budget, discount_amount].min
|
||||
end
|
||||
|
||||
def available_budget
|
||||
budget - spent_budget
|
||||
end
|
||||
|
||||
def remaining_budget?
|
||||
!available_budget.zero?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :applied_discounts
|
||||
|
||||
def spent_budget
|
||||
applied_discounts.sum
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user