Skip to content

Commit

Permalink
delivery cost added
Browse files Browse the repository at this point in the history
  • Loading branch information
goodviber committed Jun 26, 2018
1 parent cda9bf9 commit 2657615
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
3 changes: 3 additions & 0 deletions lib/checkout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ def scan(item)

def total
price = 0
delivery_price = 0
basket.each do |product|
product_quantity = quantity_in_basket(product.code)
price += discount_manager.discount_price_for(product, quantity_in_basket(product.code))
delivery_price += product_quantity * product.del_price
end

result = discount_manager.discount_total(price)
result += delivery_price
result.round(2)
end

Expand Down
5 changes: 3 additions & 2 deletions lib/product.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class Product
attr_reader :code, :name, :price
attr_reader :code, :name, :price, :del_price

def initialize(code, name, price)
def initialize(code, name, price, del_price)
@code = code
@name = name
@price = price
@del_price = del_price
end
end
14 changes: 7 additions & 7 deletions spec/checkout_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

describe Checkout do
ProductStub = Struct.new(:code, :name, :price)
ProductStub = Struct.new(:code, :name, :price, :del_price)
subject { Checkout.new }

describe "on setup" do
Expand All @@ -15,19 +15,19 @@

context 'with no promo_rules' do
describe 'scan items' do
let(:product1) { ProductStub.new('001', 'name', 10) }
let(:product2) { ProductStub.new('002', 'here', 10) }
let(:product1) { ProductStub.new('001', 'name', 10, 5.00) }
let(:product2) { ProductStub.new('002', 'here', 10, 5.00) }

it 'should calculate total as the product price' do
it 'should calculate total as the product price plus delivery price' do
subject.scan(product1)
subject.scan(product2)
expect(subject.total).to eq(20)
expect(subject.total).to eq(30)
end

it 'should round total to two decimal places' do
product = ProductStub.new('001', 'name', 25.4552)
product = ProductStub.new('001', 'name', 25.4552, 5.00)
subject.scan(product)
expect(subject.total).to eq(25.46)
expect(subject.total).to eq(30.46)
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion spec/product_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

describe Product do
subject { Product.new('001', 'cheap chair', 9.25) }
subject { Product.new('001', 'cheap chair', 9.25, 5.00) }

it 'should return correct product code' do
expect(subject.code).to eq('001')
Expand All @@ -14,4 +14,8 @@
it 'should return correct price' do
expect(subject.price).to eq(9.25)
end

it 'should return del_price' do
expect(subject.del_price).to eq(5.00)
end
end
8 changes: 4 additions & 4 deletions vinterior.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ Gem::Specification.new do |spec|
spec.authors = ["maxmills"]
spec.email = ["[email protected]"]

spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.}
spec.description = %q{TODO: Write a longer description or delete this line.}
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.summary = %q{Write a short summary, because Rubygems requires one.}
spec.description = %q{Write a longer description or delete this line.}
spec.homepage = "https://rubygems.org/gems/example"

# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
# delete this section to allow pushing this gem to any host.
if spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
spec.metadata['allowed_push_host'] = "http://mygemserver.com'"
else
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
end
Expand Down

0 comments on commit 2657615

Please sign in to comment.