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

Will #1

Open
wants to merge 1 commit 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
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].delete("$").to_i
end
end
48 changes: 48 additions & 0 deletions lib/market.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,51 @@
class Market

attr_reader :name, :vendors

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

def add_vendor(vendor)
@vendors << vendor
end

def vendor_names
names = []
@vendors.each do |vendor|
names << vendor.name
end
names
end

def vendors_that_sell(item)
list = []
@vendors.each do |vendor|
if vendor.inventory.include?(item)
list << vendor
end
end
list
end

def sorted_item_list
sorted_items = []
@vendors.each do |vendor|
vendor.inventory.each do |item, quantity|
sorted_items << item.name
end
end
sorted_items.uniq.sort
end

#can't figure this one out
def overstocked_items
overstocked = []
@vendors.each do |vendor|
vendor.inventory.each do |item, quantity|
if
end
end
end
end
32 changes: 31 additions & 1 deletion lib/vendor.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
class Vendor

end
attr_reader :name, :inventory

def initialize(name)
@name = name
@inventory = {}
end

def check_stock(item)
if [email protected]?
@inventory[item]
else
0
end
end

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

#this test isn't passing, going to try to move on either way
def potential_revenue
revenue_sum = 0
@inventory.each do |item, quantity|
item_price = item.price.delete("$").to_f
revenue_sum += item_price * quantity
end
revenue_sum
end
end

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

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(Item)
end

it 'has attributes' do
expect(@item2.name).to eq('Tomato')
end
end
82 changes: 82 additions & 0 deletions spec/market_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require './lib/item'
require './lib/vendor'
require './lib/market'
require 'pry'

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_a(Market)
end

it 'can add vendors to the market' do
@market.add_vendor(@vendor1)
@market.add_vendor(@vendor2)
@market.add_vendor(@vendor3)

expect(@market.vendors).to eq([@vendor1, @vendor2, @vendor3])
end

it 'can list the vendors by 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 list vendors that sell a specific item' do
@market.add_vendor(@vendor1)
@market.add_vendor(@vendor2)
@market.add_vendor(@vendor3)

@vendor1.stock(@item1, 35)
@vendor1.stock(@item2, 7)
@vendor2.stock(@item4, 50)
@vendor2.stock(@item3, 25)
@vendor3.stock(@item1, 65)

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

it 'can sort all items in stock alphabetically' do
@market.add_vendor(@vendor1)
@market.add_vendor(@vendor2)
@market.add_vendor(@vendor3)

@vendor1.stock(@item1, 35)
@vendor1.stock(@item2, 7)
@vendor2.stock(@item4, 50)
@vendor2.stock(@item3, 25)
@vendor3.stock(@item1, 65)

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

it 'can determine if an item is overstocked at the market' do
@market.add_vendor(@vendor1)
@market.add_vendor(@vendor2)
@market.add_vendor(@vendor3)

@vendor1.stock(@item1, 35)
@vendor1.stock(@item2, 70)
@vendor2.stock(@item4, 50)
@vendor2.stock(@item3, 25)
@vendor3.stock(@item1, 65)

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

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_a(Vendor)
end

it 'can check stock' do
expect(@vendor1.check_stock(@item1)).to eq(0)
end

it 'can stock new items into inventory' do
@vendor1.stock(@item1, 30)
expect(@vendor1.inventory).to eq({@item1 => 30})
expect(@vendor1.check_stock(@item1)).to eq(30)

@vendor1.stock(@item1, 25)
expect(@vendor1.inventory).to eq({@item1 => 55})
expect(@vendor1.check_stock(@item1)).to eq(55)

@vendor1.stock(@item2, 12)
expect(@vendor1.inventory).to eq({@item1 => 55, @item2 => 12})
expect(@vendor1.check_stock(@item1)).to eq(55)
expect(@vendor1.check_stock(@item2)).to eq(12)
end


it 'can estimate the potential revenue for a vendor' 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)
expect(@vendor3.potential_revenue).to eq(48.75)
end
end