Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cart Challenge - All Tests Passing #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Item

attr_reader :name, :price

def initialize name, price
@name = name
@price = price
end

end

class Cart

def initialize tax=nil
@contents = []
if !tax.nil?
@tax_rate = tax[:tax_rate]
else
@tax_rate = tax
end
end

def add_item item
@contents.push(item)
end

def item_count
@contents.count
end

def contains? item
@contents.include?(item)
end

def tax_rate
@tax_rate || 10
end

def cost_before_tax
cbt = @contents.map { |c| c.price }
cbt.inject(:+).to_f
end

def cost_after_tax
tax = cost_before_tax * (tax_rate / 100.0)
cost_before_tax + tax
end

end
2 changes: 1 addition & 1 deletion tests.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'minitest/autorun'
require 'pry'
require './item'

class CartTest < MiniTest::Test

Expand Down Expand Up @@ -40,7 +41,6 @@ def test_carts_know_their_cost
def test_carts_know_their_tax_rates
cart = Cart.new
assert_equal cart.tax_rate, 10

special_cart = Cart.new({tax_rate: 20})
assert_equal special_cart.tax_rate, 20
end
Expand Down