1
0

Rename discount budget methods and expose maximum possible discount

- Rename #apply_discount to #register_applied_discount to make it
  more clear that it only registers discounts and does not apply them
- Expose maximum possible discount to make it possible to get get the possible
  discount without registering it in the budget
This commit is contained in:
Tim Kächele 2021-03-07 11:32:05 +01:00
parent 26b3be7359
commit 2e3a68e285
2 changed files with 23 additions and 9 deletions

View File

@ -9,12 +9,16 @@ module PlanetExpress
@applied_discounts = []
end
def apply_discount(discount)
possible_discount = maximum_possible_discount(discount)
def register_applied_discount(discount_amount)
possible_discount = maximum_possible_discount(discount_amount)
applied_discounts.push(possible_discount)
possible_discount
end
def maximum_possible_discount(discount_amount)
[available_budget, discount_amount].min
end
def available_budget
budget - spent_budget
end
@ -30,9 +34,5 @@ module PlanetExpress
def spent_budget
applied_discounts.sum
end
def maximum_possible_discount(discount)
[available_budget, discount].min
end
end
end

View File

@ -4,10 +4,10 @@ RSpec.describe PlanetExpress::DiscountBudget do
let(:budget) { 500 }
subject { described_class.new(budget: budget) }
describe '#apply_discount' do
describe '#register_applied_discount' do
context 'with sufficient remaining budget' do
it 'returns the full discount' do
expect(subject.apply_discount(120)).to eq(120)
expect(subject.register_applied_discount(120)).to eq(120)
expect(subject.available_budget).to eq(380)
expect(subject.remaining_budget?).to eq(true)
end
@ -15,10 +15,24 @@ RSpec.describe PlanetExpress::DiscountBudget do
context 'with insufficient remaining budget' do
it 'returns only the remaining budget' do
expect(subject.apply_discount(650)).to eq(500)
expect(subject.register_applied_discount(650)).to eq(500)
expect(subject.available_budget).to eq(0)
expect(subject.remaining_budget?).to eq(false)
end
end
end
describe '#maximum_possible_discount' do
context 'with sufficient budget' do
it 'returns the full discount amount' do
expect(subject.maximum_possible_discount(300)).to eq(300)
end
end
context 'with insufficient budget' do
it 'returns the maximum possible amount' do
expect(subject.maximum_possible_discount(600)).to eq(500)
end
end
end
end