Implement a discount budget
A discount budget to represent the available budget for discounts
This commit is contained in:
parent
b6f2d46d91
commit
815615a03c
@ -4,6 +4,8 @@ require 'planet_express/version'
|
||||
|
||||
require 'planet_express/repository'
|
||||
|
||||
require 'planet_express/discount_budget'
|
||||
|
||||
require 'planet_express/shipping_option'
|
||||
require 'planet_express/shipping_option_repository'
|
||||
|
||||
|
34
lib/planet_express/discount_budget.rb
Normal file
34
lib/planet_express/discount_budget.rb
Normal file
@ -0,0 +1,34 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module PlanetExpress
|
||||
class DiscountBudget
|
||||
attr_reader :budget
|
||||
|
||||
def initialize(budget:)
|
||||
@budget = budget
|
||||
@applied_discounts = []
|
||||
end
|
||||
|
||||
def apply_discount(discount)
|
||||
possible_discount = maximum_possible_discount(discount)
|
||||
applied_discounts.push(possible_discount)
|
||||
possible_discount
|
||||
end
|
||||
|
||||
def available_budget
|
||||
budget - spent_budget
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :applied_discounts
|
||||
|
||||
def spent_budget
|
||||
applied_discounts.sum
|
||||
end
|
||||
|
||||
def maximum_possible_discount(discount)
|
||||
[available_budget, discount].min
|
||||
end
|
||||
end
|
||||
end
|
23
spec/discount_budget_spec.rb
Normal file
23
spec/discount_budget_spec.rb
Normal file
@ -0,0 +1,23 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe PlanetExpress::DiscountBudget do
|
||||
let(:budget) { 500 }
|
||||
subject { described_class.new(budget: budget) }
|
||||
|
||||
describe '#apply_discount' do
|
||||
|
||||
context 'with sufficient remaining budget' do
|
||||
it 'returns the full discount' do
|
||||
expect(subject.apply_discount(120)).to eq(120)
|
||||
expect(subject.available_budget).to eq(380)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with insufficient remaining budget' do
|
||||
it 'returns the full discount' do
|
||||
expect(subject.apply_discount(650)).to eq(500)
|
||||
expect(subject.available_budget).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user