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

IC #5 Montana Pfeifer #12

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5f2385a
Initial commit
Montana-Pfeifer Sep 16, 2024
5f91fb2
added first test for vendor
Montana-Pfeifer Sep 16, 2024
48cbca0
Added inventory test
Montana-Pfeifer Sep 16, 2024
ac0960d
Added check item stock test
Montana-Pfeifer Sep 16, 2024
078eebb
Added stocks items into inventory test
Montana-Pfeifer Sep 16, 2024
226d381
Added stocks multiple items test
Montana-Pfeifer Sep 16, 2024
ee192f2
added Vendor class attr to pass first 2 tests
Montana-Pfeifer Sep 16, 2024
e06f7c3
added first test to item
Montana-Pfeifer Sep 16, 2024
2cf78fd
added last of the test to item
Montana-Pfeifer Sep 16, 2024
1c0b792
Added attr to item class to make all tests pass
Montana-Pfeifer Sep 16, 2024
8a43110
added stock methods and changed hash to .new because showed nil on tests
Montana-Pfeifer Sep 16, 2024
a7d88cb
missed an end
Montana-Pfeifer Sep 16, 2024
85fcb57
made 2 tests following interaction pattern
Montana-Pfeifer Sep 16, 2024
965d399
Finish making tests to follow interaction pattern
Montana-Pfeifer Sep 16, 2024
bf41220
added attr to pass 1st test
Montana-Pfeifer Sep 16, 2024
15ef055
changed expected out come because of error
Montana-Pfeifer Sep 16, 2024
83c5da0
added vendor methods to pass tests
Montana-Pfeifer Sep 16, 2024
0ee7fb8
added vendor stock to make tests pass and follow interaction
Montana-Pfeifer Sep 16, 2024
61da707
added vendors_that_sell method
Montana-Pfeifer Sep 16, 2024
2e42161
Added potential_revenue test and method
Montana-Pfeifer Sep 16, 2024
8571f47
moved potential revenue to vendor class all tests still pass
Montana-Pfeifer Sep 16, 2024
52d107a
added sorted_item_list test and method
Montana-Pfeifer Sep 16, 2024
cef5ce7
added return_total inventory and test
Montana-Pfeifer Sep 16, 2024
12cd29b
added more details to make total_inventory pass and also pass all pri…
Montana-Pfeifer Sep 16, 2024
b0452af
added overstocked_items method and test
Montana-Pfeifer 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
7 changes: 7 additions & 0 deletions lib/item.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
class Item

attr_reader :name,
:price

def initialize(details)
@name = details[:name]
@price = details[:price].delete('$').to_f
Copy link
Contributor

Choose a reason for hiding this comment

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

Way to clean the string input, and convert it to the correct data type.

end
end
39 changes: 39 additions & 0 deletions 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(&:name)
end

def vendors_that_sell(item)
@vendors.select { |vendor| vendor.check_stock(item) > 0}
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.

These one liners are nice !!

end

def total_inventory
Copy link
Contributor

Choose a reason for hiding this comment

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

There is a simpler solution here! Might be worth asking ChatGPT :)

inventory = Hash.new { |hash, key| hash[key] = { quantity: 0, vendors: [] } }

@vendors.each do |vendor|
vendor.inventory.each do |item, details|
inventory[item][:quantity] += details[:quantity]
inventory[item][:vendors] << vendor unless inventory[item][:vendors].include?(vendor)
end
end
inventory
end

def overstocked_items(threshold = 50)
total_inventory.select { |item, details| details[:quantity] >= threshold }
end
end
21 changes: 21 additions & 0 deletions lib/vendor.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
class Vendor

attr_reader :name,
:inventory

def initialize(name)
@name = name
@inventory = Hash.new { |hash, key| hash[key] = { quantity: 0 } }
end

def check_stock(item)
@inventory[item][:quantity]
end

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

Choose a reason for hiding this comment

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

Was expecting something a little more like this here @inventory[item] += quantity

end

def potential_revenue
@inventory.sum do |item, details|
item.price * details[:quantity]
end
end
end
25 changes: 25 additions & 0 deletions spec/item_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require './lib/item'

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

it 'exists' do
expect(@item).to be_a(Item)
end

it 'has a name' do
expect(@item.name).to eq("Peach")
end

it 'has a price' do
expect(@item.price).to eq(0.75)
end

it 'handles different prices' do
@item2 = Item.new({name: 'Tomato', price: "$0.50"})

expect(@item2.price).to eq(0.50)
end
end
95 changes: 95 additions & 0 deletions spec/market_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
require './lib/item'
require './lib/vendor'
require './lib/market'

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"})

@vendor1.stock(@item1, 35)
@vendor1.stock(@item2, 7)

@vendor2.stock(@item4, 50)
@vendor2.stock(@item3, 25)

@vendor3.stock(@item1, 65)
@vendor3.stock(@item3, 10)

end

it 'exists and has attributes' do
expect(@market).to be_a(Market)
expect(@market.name).to eq("South Pearl Street Farmers Market")
expect(@market.vendors).to eq([])
end

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 the vendors name' 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 find vendors who sell a specific item' do
@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 "calculates potential_revenue" do
expect(@vendor1.potential_revenue).to eq(29.75)
Copy link
Contributor

Choose a reason for hiding this comment

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

I like that you are testing different vendors (scenarios) and testing how the outputs differ.

expect(@vendor2.potential_revenue).to eq(345.00)
expect(@vendor3.potential_revenue).to eq(101.75)
end

it 'returns a sorted list of items' do
@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

it 'returns total inventory' do
@market.add_vendor(@vendor1)
@market.add_vendor(@vendor2)
@market.add_vendor(@vendor3)

expected = {
@item1 => { quantity: 100, vendors: [@vendor1, @vendor3] },
@item2 => { quantity: 7, vendors: [@vendor1] },
@item3 => { quantity: 35, vendors: [@vendor2, @vendor3] },
@item4 => { quantity: 50, vendors: [@vendor2] }
}
expect(@market.total_inventory).to eq(expected)
end

it 'returns overstocked items' do
@market.add_vendor(@vendor1)
@market.add_vendor(@vendor2)
@market.add_vendor(@vendor3)

expected = {
@item1 => { quantity: 100, vendors: [@vendor1, @vendor3] },
Copy link
Contributor

Choose a reason for hiding this comment

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

Per the interaction pattern, the items should looks like

pry(main)> vendor.inventory
#=> {#<Item:0x007f9c56740d48...> => 55, #<Item:0x007f9c565c0ce8...> => 12}

Copy link
Contributor

Choose a reason for hiding this comment

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

Where the item object is the key, and the quantity is the value.

@item4 => { quantity: 50, vendors: [@vendor2] }
}

expect(@market.overstocked_items).to eq(expected)
end
end

39 changes: 39 additions & 0 deletions spec/vendor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require './lib/vendor'
require './lib/item'

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

it 'exists' do
expect(@vendor).to be_a(Vendor)
expect(@vendor.name).to eq("Rocky Mountain Fresh")
end

it 'has an inventory' do
expect(@vendor.inventory).to eq({})
end

it 'checks stocks of items' do
expect(@vendor.check_stock(@item1)).to eq(0)
end

it 'stocks items in inventory' do
Copy link
Contributor

Choose a reason for hiding this comment

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

These tests are failing - we're getting an object back that looks like +#<Item:0x00007f9a182d3de0 @name="Peach", @price=0.75> => {:quantity=>30} - I'd like to see something more like <Item:0x00007f7f2c2cfb48 @name="Tomato", @price=0.5> => 10. What you did here works but doesnt quite follow the interaction pattern.

@vendor.stock(@item1, 30)
expect(@vendor.inventory).to eq({@item1 => 30})
expect(@vendor.check_stock(@item1)).to eq(30)

@vendor.stock(@item1, 25)
expect(@vendor.check_stock(@item1)).to eq(55)
end

it 'stocks multiple items'do
@vendor.stock(@item1, 30)
@vendor.stock(@item2, 12)

expect(@vendor.inventory).to eq({@item1 => 30, @item2 => 12})
end
end