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

Paul Knapp #11

Open
wants to merge 35 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
eab53f1
test:add spec helper
Paul-Knapp Sep 16, 2024
bb3553d
test:add before each and instantiation test
Paul-Knapp Sep 16, 2024
795cccd
test:add test for price and name
Paul-Knapp Sep 16, 2024
4a8afd0
refactor: improve formatting
Paul-Knapp Sep 16, 2024
5b2cda3
feat: create initialize and attr reader
Paul-Knapp Sep 16, 2024
b17e6a6
bug: change variables to instance variables
Paul-Knapp Sep 16, 2024
1bfe330
test: create test file for vendor class
Paul-Knapp Sep 16, 2024
e67318d
test: add before block and initialize test
Paul-Knapp Sep 16, 2024
f0a175b
test: add all instantiations for testing
Paul-Knapp Sep 16, 2024
5939dfa
test:add test for name attribute
Paul-Knapp Sep 16, 2024
aae555b
test: add test for stocking items
Paul-Knapp Sep 16, 2024
cc6561c
test: add test for adding inventory
Paul-Knapp Sep 16, 2024
2152aa7
test: add vendor to helper
Paul-Knapp Sep 16, 2024
bdae433
test: fix syntax to match interaction pattern
Paul-Knapp Sep 16, 2024
3c4545d
feat: add check stock method
Paul-Knapp Sep 16, 2024
4a12313
feat: add stock feature
Paul-Knapp Sep 16, 2024
ee83934
test: fix my mental math
Paul-Knapp Sep 16, 2024
0f76e5e
test: add market to helper
Paul-Knapp Sep 16, 2024
f7ed38d
test: change name for easier testing
Paul-Knapp Sep 16, 2024
7512b0c
test: add before each block for testing
Paul-Knapp Sep 16, 2024
d6c7566
test: add initialize tests
Paul-Knapp Sep 16, 2024
d2fc879
test: all interaction patterns being tested
Paul-Knapp Sep 16, 2024
c34bb1a
test: add formatting preferances for testing
Paul-Knapp Sep 16, 2024
672f9e7
test: test for all patterns
Paul-Knapp Sep 16, 2024
578d5d6
bug: fix typo in describe block
Paul-Knapp Sep 16, 2024
4c29e1c
add vendor query methods to market class
Paul-Knapp Sep 16, 2024
99771d6
feat:add vendors that sell method.
Paul-Knapp Sep 16, 2024
31786ff
feat: add potential revenue calculation
Paul-Knapp Sep 16, 2024
14a8d97
test: add test for sorted items
Paul-Knapp Sep 16, 2024
1a4fbe4
test: add test for revenue method
Paul-Knapp Sep 16, 2024
ace885e
feat: potential revenue method fixed
Paul-Knapp Sep 16, 2024
4f5c045
bug: fix test to actually test for alphabetical order
Paul-Knapp Sep 16, 2024
9b1f1be
feat: add method to sort all items at the market
Paul-Knapp Sep 16, 2024
f3e5400
test: add pry to spec helper
Paul-Knapp Sep 16, 2024
e99ffe5
feat:add total inventory method to market class
Paul-Knapp Sep 16, 2024
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
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--color
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

5 changes: 5 additions & 0 deletions lib/item.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
class Item
attr_reader :name, :price

def initialize(info)
@name = info[:name]
@price = info[:price]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The interaction pattern asks to clean the string (remove the dollar sign) and convert to a float!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great commit history - nice stuff Paul!

end
end
41 changes: 40 additions & 1 deletion lib/market.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
class Market

attr_reader :name, :vendors

def initialize(name)
@name = name
@vendors = ([])
end

def add_vendor(vendor)
@vendors << vendor
end

def vendor_names
@vendors.map { |vendor| vendor.name }
end

def vendors_that_sell(item)
@vendors.select do |vendor|
vendor.inventory.include?(item)
end
end

def sorted_item_list
vendors.flat_map { |vendor| vendor.inventory.keys.map(&:name)}.uniq.sort
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one liner!

end

def total_inventory
all_inventory = {}

@vendors.each do |vendor|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theres probably an easier way to do this. Ask perplexity for some refactoring tips!

vendor.inventory.each do |item, quantity|
if all_inventory[item]
all_inventory[item][:quantity] += quantity
all_inventory[item][:vendors] << vendor.name unless all_inventory[item][:vendors].include?(vendor.name)
else
all_inventory[item] = {quantity: quantity, vendors: [vendor.name]}
end
end
end
all_inventory
end
end
20 changes: 19 additions & 1 deletion lib/vendor.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
class Vendor

attr_reader :name, :inventory
def initialize(name)
@inventory = {}
@name = name
end

def check_stock(item)
@inventory[item] || 0
end

def stock(item, quantity)
@inventory[item] = (@inventory[item] || 0 )+ quantity
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OO I like this

end

def potential_revenue
@inventory.sum do |item,quantity|
item.price.gsub('$', '').to_f * quantity
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Youre cleaning the string here!! Nice. Do it on the item instantiation, though.

end.round(2)
end
end
23 changes: 23 additions & 0 deletions spec/item_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'spec_helper'

RSpec.describe Item do
before(:each) do
@item1 = Item.new({name: 'Peach', price: "$0.75"})
@item2 = Item.new({name: 'Tomato', price: '$0.50'})
end

it '#exists' do
expect(@item1).to be_an_instance_of (Item)
expect(@item2).to be_an_instance_of (Item)
end

it 'has a name' do
expect(@item1.name).to eq('Peach')
expect(@item2.name).to eq('Tomato')
end

it 'has a price' do
expect(@item1.price).to eq('$0.75')
expect(@item2.price).to eq('$0.50')
end
end
65 changes: 65 additions & 0 deletions spec/market_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'spec_helper'
RSpec.describe Market do
before(:each) do
@market = Market.new("South Pearl Street Farmers Market")
@vendor1 = Vendor.new("Rocky Mountain Fresh")
@vendor2 = Vendor.new("Ba-Nom-a-Nom")
@vendor3 = Vendor.new("Palisade Peach Shack")
@item1 = Item.new({name: 'Peach', price: "$0.75"})
@item2 = Item.new({name: 'Tomato', price: '$0.50'})
@item3 = Item.new({name: "Peach-Raspberry Nice Cream", price: "$5.30"})
@item4 = Item.new({name: "Banana Nice Cream", price: "$4.25"})
end

it '#exists' do
expect(@market).to be_an_instance_of(Market)
end

it '#has a name' do
expect(@market.name).to eq ("South Pearl Street Farmers Market")
end

it '#has no vendors' do
expect(@market.vendors).to eq([])
end
Comment on lines +14 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functionality that happens in the instantiation can be consolidated into a single test, if you'd like to save time and lines.


it '#can have vendors' do
@market.add_vendor(@vendor1)
@market.add_vendor(@vendor2)
@market.add_vendor(@vendor3)
expect(@market.vendors).to eq([@vendor1, @vendor2, @vendor3])
end

it '#can tell vendor names' do
@market.add_vendor(@vendor1)
@market.add_vendor(@vendor2)
@market.add_vendor(@vendor3)
expect(@market.vendor_names).to eq(["Rocky Mountain Fresh", "Ba-Nom-a-Nom", "Palisade Peach Shack"])
end

it '#can show vendors that sell an item' do
@vendor1.stock(@item1, 35)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider testing stock in isolation. This is a nice test though, I have no problem with what you did here.

@vendor1.stock(@item2, 7)
@vendor2.stock(@item4, 50)
@vendor2.stock(@item3, 25)
@vendor3.stock(@item1, 65)
@market.add_vendor(@vendor1)
@market.add_vendor(@vendor2)
@market.add_vendor(@vendor3)
expect(@market.vendors_that_sell(@item1)).to eq([@vendor1, @vendor3])
expect(@market.vendors_that_sell(@item4)).to eq([@vendor2])
end

it '#can show all items in inventory' do
@vendor1.stock(@item1, 35)
@vendor1.stock(@item2, 7)
@vendor2.stock(@item4, 50)
@vendor2.stock(@item3, 25)
@vendor3.stock(@item1, 65)
@market.add_vendor(@vendor1)
@market.add_vendor(@vendor2)
@market.add_vendor(@vendor3)
expect(@market.sorted_item_list).to eq(["Banana Nice Cream", "Peach", "Peach-Raspberry Nice Cream", "Tomato"])
end
end

4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'pry'
require './lib/item'
require './lib/vendor'
require './lib/market'
64 changes: 64 additions & 0 deletions spec/vendor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'spec_helper'
RSpec.describe Vendor do
before(:each) do

@vendor1 = Vendor.new("Rocky Mountain Fresh")
@vendor2 = Vendor.new("Ba-Nom-a-Nom")
@vendor3 = Vendor.new("Palisade Peach Shack")
@item1 = Item.new({name: 'Peach', price: "$0.75"})
@item2 = Item.new({name: 'Tomato', price: '$0.50'})
@item3 = Item.new({name: "Peach-Raspberry Nice Cream", price: "$5.30"})
@item4 = Item.new({name: "Banana Nice Cream", price: "$4.25"})
end

it '#exists' do
expect(@vendor1).to be_an_instance_of(Vendor)
expect(@vendor2).to be_an_instance_of(Vendor)
expect(@vendor3).to be_an_instance_of(Vendor)
end

it '#has a name' do
expect(@vendor1.name).to eq ("Rocky Mountain Fresh")
expect(@vendor2.name).to eq ("Ba-Nom-a-Nom")
expect(@vendor3.name).to eq ("Palisade Peach Shack")
end

it '#has inventory' do
expect(@vendor1.inventory).to eq({})
expect(@vendor2.inventory).to eq({})
expect(@vendor3.inventory).to eq({})
end

it '#has stock' do
expect(@vendor1.check_stock(@item1)).to eq(0)
expect(@vendor2.check_stock(@item1)).to eq(0)
expect(@vendor3.check_stock(@item1)).to eq(0)
end

it '#can stock items' do
@vendor1.stock(@item1, 30)
expect(@vendor1.check_stock(@item1)).to eq(30)
expect(@vendor1.check_stock(@item1)).to_not eq(0)
expect(@vendor2.check_stock(@item1)).to eq(0)
@vendor1.stock(@item1, 25)
expect(@vendor1.check_stock(@item1)).to eq(55)
expect(@vendor2.check_stock(@item1)).to eq(0)
end

it '#can have items in inventory' do
@vendor1.stock(@item1, 25)
@vendor1.stock(@item2, 25)
expect(@vendor1.inventory).to eq({@item1 => 25 , @item2 => 25})
end

it '#can determine potential revenue for vendors' do
@vendor1.stock(@item1, 35)
@vendor1.stock(@item2, 7)
@vendor2.stock(@item4, 50)
@vendor2.stock(@item3, 25)
@vendor3.stock(@item1, 65)
expect(@vendor1.potential_revenue).to eq (29.75)
expect(@vendor2.potential_revenue).to eq (345.00)
expect(@vendor3.potential_revenue).to eq (48.75)
end
end