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

Natasha Vasquez #8

Open
wants to merge 16 commits into
base: main
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
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(item_details)
Copy link
Contributor

Choose a reason for hiding this comment

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

Just item is probably a nice param name here.

@name = item_details[:name]
@price = item_details[: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 the price string to be cleaned (remove the $) and converted to a float.

end

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


attr_reader :name, :vendors
attr_accessor :sorted_item_list
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this variable need to be updated outside of this class? Probably not.


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

def add_vendor(vendor)
@vendors << vendor
end

def vendor_names
vendors.map do |vendor|
vendor.name
end
end

def vendors_that_sell(item)
vendors.select do |vendor|
vendor.check_stock(item) > 0
end
end

def sorted_item_list
all_item_names = vendors.flat_map do |vendor|
vendor.inventory.keys.map(&:name)
end
all_item_names.uniq.sort
end

def total_inventory
inventory = {}
vendors.each do |vendor|
vendor.inventory.each do |item, quantity|
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice logic in here!


if inventory[item]
inventory[item][:quantity] += quantity
inventory[item][:vendors] << vendor.name unless inventory[item][:vendors].include?(vendor.name)
else
inventory[item] = {
quantity: quantity,
vendors: [vendor.name]
}
end
end
end
inventory
end

def overstocked_items
overstocked = []
total_inventory.each do |item, data|
if data[:vendors].length > 1 && data[:quantity] > 50
Copy link
Contributor

Choose a reason for hiding this comment

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

Great conditional logic!

overstocked << item
end
end
overstocked
end
end
26 changes: 26 additions & 0 deletions lib/vendor.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
class Vendor

attr_reader :name, :inventory, :stock
attr_accessor :potential_revenue

def initialize(name)
@name = name
@inventory = Hash.new(0)
end

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

def stock(item, quantity)
@inventory[item] ||= 0
@inventory[item] += quantity
end

def potential_revenue
total_revenue = 0
@inventory.each do |item, quantity|
item_price = item.price.delete('$').to_f
total_revenue += item_price * quantity
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Indentation here! Really great work with this encapsulated class, though.

total_revenue
end
end
26 changes: 26 additions & 0 deletions spec/item_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'rspec'
require './lib/item'

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)
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
90 changes: 90 additions & 0 deletions spec/market_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
require 'rspec'
require './lib/item'
require './lib/vendor'
require './lib/market'

RSpec.describe Market do
before(:each)do
@market = Market.new("South Pearl Street Farmers Market")

end

it 'exists' do

Copy link
Contributor

Choose a reason for hiding this comment

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

Remove whitespace! And pay attention to indentation :)

expect(@market).to be_an_instance_of(Market)
end

it 'can return the market name' do

expect(@market.name).to eq("South Pearl Street Farmers Market")
end

it 'can list all vendors in the market' do

expect(@market.vendors).to eq([])
end

it 'can add vendors to the market' do
@vendor1 = Vendor.new("Rocky Mountain Fresh")
@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 = Vendor.new("Ba-Nom-a-Nom")
@vendor2.stock(@item4, 50)
@vendor2.stock(@item3, 25)

@vendor3 = Vendor.new("Palisade Peach Shack")
@vendor3.stock(@item1, 65)

@market.add_vendor(@vendor1)
@market.add_vendor(@vendor2)
@market.add_vendor(@vendor3)

expect(@market.vendors).to include(@vendor1, @vendor2, @vendor3)
end

it 'can return vendor names, items sold at vendors, and vendor revenue' do
@vendor1 = Vendor.new("Rocky Mountain Fresh")
Copy link
Contributor

Choose a reason for hiding this comment

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

Some of this setup could have been included in your beforeach

@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 = Vendor.new("Ba-Nom-a-Nom")
@vendor2.stock(@item4, 50)
@vendor2.stock(@item3, 25)

@vendor3 = Vendor.new("Palisade Peach Shack")
@vendor3.stock(@item1, 65)

@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"])

expect(@market.vendors_that_sell(@item1)).to eq([@vendor1, @vendor3])
expect(@market.vendors_that_sell(@item4)).to eq([@vendor2])

expect(@vendor1.potential_revenue).to eq(29.75)
expect(@vendor2.potential_revenue).to eq(345.00)
expect(@vendor3.potential_revenue).to eq(48.75)

expect(@market.sorted_item_list).to eq(["Banana Nice Cream", "Peach", "Peach-Raspberry Nice Cream", "Tomato"])

expected = {
@item1 => { quantity: 100, vendors: ["Rocky Mountain Fresh", "Palisade Peach Shack"] },
@item2 => { quantity: 7, vendors: ["Rocky Mountain Fresh"] }
}

expect(@market.total_inventory).to include(expected)

expect(@market.overstocked_items).to eq([@item1])
end
end
48 changes: 48 additions & 0 deletions spec/vendor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'rspec'
require './lib/item'
require './lib/vendor'

RSpec.describe Vendor do
before(:each) do
@vendor = Vendor.new("Rocky Mountain Fresh")
end

it 'exists' do

expect(@vendor).to be_an_instance_of(Vendor)
end

it 'can return the vendor name' do

expect(@vendor.name).to eq("Rocky Mountain Fresh")
end

it 'initially returns and empty hash of inventory' do

expect(@vendor.inventory).to eq({})
end

it 'can check the stock of an item' do

expect(@vendor.check_stock(@item1)).to eq(0)
end

it 'can stock items' do
@vendor.stock(@item1, 30)

expect(@vendor.inventory).to include(@item1)
expect(@vendor.check_stock(@item1)).to eq(30)

@vendor.stock(@item1, 25)

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

it 'can stock more items' do
@vendor.stock(@item1, 30)
@vendor.stock(@item1, 25)
@vendor.stock(@item2, 12)

expect(@vendor.inventory).to include(@item1, @item2)
end
end