1
0

Implement a discount budget

A discount budget to represent the available budget for
discounts
This commit is contained in:
Tim Kächele 2021-03-06 17:38:59 +01:00
parent b6f2d46d91
commit 815615a03c
3 changed files with 59 additions and 0 deletions

View File

@ -4,6 +4,8 @@ require 'planet_express/version'
require 'planet_express/repository' require 'planet_express/repository'
require 'planet_express/discount_budget'
require 'planet_express/shipping_option' require 'planet_express/shipping_option'
require 'planet_express/shipping_option_repository' require 'planet_express/shipping_option_repository'

View 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

View 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