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:
parent
26b3be7359
commit
2e3a68e285
@ -9,12 +9,16 @@ module PlanetExpress
|
|||||||
@applied_discounts = []
|
@applied_discounts = []
|
||||||
end
|
end
|
||||||
|
|
||||||
def apply_discount(discount)
|
def register_applied_discount(discount_amount)
|
||||||
possible_discount = maximum_possible_discount(discount)
|
possible_discount = maximum_possible_discount(discount_amount)
|
||||||
applied_discounts.push(possible_discount)
|
applied_discounts.push(possible_discount)
|
||||||
possible_discount
|
possible_discount
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def maximum_possible_discount(discount_amount)
|
||||||
|
[available_budget, discount_amount].min
|
||||||
|
end
|
||||||
|
|
||||||
def available_budget
|
def available_budget
|
||||||
budget - spent_budget
|
budget - spent_budget
|
||||||
end
|
end
|
||||||
@ -30,9 +34,5 @@ module PlanetExpress
|
|||||||
def spent_budget
|
def spent_budget
|
||||||
applied_discounts.sum
|
applied_discounts.sum
|
||||||
end
|
end
|
||||||
|
|
||||||
def maximum_possible_discount(discount)
|
|
||||||
[available_budget, discount].min
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -4,10 +4,10 @@ RSpec.describe PlanetExpress::DiscountBudget do
|
|||||||
let(:budget) { 500 }
|
let(:budget) { 500 }
|
||||||
subject { described_class.new(budget: budget) }
|
subject { described_class.new(budget: budget) }
|
||||||
|
|
||||||
describe '#apply_discount' do
|
describe '#register_applied_discount' do
|
||||||
context 'with sufficient remaining budget' do
|
context 'with sufficient remaining budget' do
|
||||||
it 'returns the full discount' 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.available_budget).to eq(380)
|
||||||
expect(subject.remaining_budget?).to eq(true)
|
expect(subject.remaining_budget?).to eq(true)
|
||||||
end
|
end
|
||||||
@ -15,10 +15,24 @@ RSpec.describe PlanetExpress::DiscountBudget do
|
|||||||
|
|
||||||
context 'with insufficient remaining budget' do
|
context 'with insufficient remaining budget' do
|
||||||
it 'returns only the 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.available_budget).to eq(0)
|
||||||
expect(subject.remaining_budget?).to eq(false)
|
expect(subject.remaining_budget?).to eq(false)
|
||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user