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

Devlin Lynch #4

Open
wants to merge 6 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
10 changes: 10 additions & 0 deletions lib/item.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
class Item
attr_reader :name,
Copy link
Contributor

Choose a reason for hiding this comment

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

Great git commit history!

:price
def initialize(item_info)
@name = item_info[:name]
@price = price_to_float(item_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.

Nice!!

end

def price_to_float(price)
new_price = price.gsub("$","")
new_price.to_f
end
end
73 changes: 72 additions & 1 deletion lib/market.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,74 @@
require 'date'
class Market

attr_reader :name,
:vendors,
:date

def initialize(name)
@name = name
@vendors = []
@date = Date.today
end

def add_vendor(vendor)
@vendors << vendor
end

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

def vendors_that_sell(item)
vendor_list = @vendors.find_all do |vendor|
vendor.inventory.include?(item)
Copy link
Contributor

Choose a reason for hiding this comment

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

Great use of built in methods

end
vendor_list
end

def sorted_item_list
item_list = []
@vendors.each do |vendor|
vendor.inventory.each do |item|
item_list << item[0].name
end
end
item_list.uniq.sort
end

def overstocked_items
item_totals ={}
@vendors.each do |vendor|
vendor.inventory.each do |item,quantity|
if item_totals[item] == nil
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 checking the state of your hash.

item_totals[item] = quantity
else
item_totals[item] += quantity
end
end
end
items_above= []
item_totals.map do |item, quantity|
if quantity>50
items_above << item
end
end
items_above
end

def total_inventory
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.

There is probably an easier way to write this method! Could be worth asking chatgpt for refactor tips.

vendor.inventory.each do |item, quantity|
if inventory[item] == nil
inventory[item] = {total_quantity: quantity,vendors:[vendor]}
else
inventory[item][:total_quantity] += quantity
inventory[item][:vendors] <<vendor
end
end
end
inventory
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

attr_reader :name,
:inventory

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

def stock(item, quantity)
if check_stock(item) == 0
@inventory[item] = quantity
else
@inventory[item] +=quantity
end
end

def check_stock(item)
if @inventory[item] == nil
0
else
@inventory[item]
end
end

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

RSpec.describe Item do
it 'can initialize' do
item= Item.new({name: 'Peach', price: "$0.75"})

expect(item).to be_an_instance_of(Item)
expect(item.name).to eq("Peach")
expect(item.price).to eq(0.75)
end
end
105 changes: 105 additions & 0 deletions spec/market_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
require 'pry'
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)
end

describe "market" do
it 'can initialize' do
expect(@market).to be_an_instance_of(Market)
expect(@market.name).to eq("South Pearl Street Farmers Market")
expect(@market.vendors).to eq([])

allow(@market).to receive(:date).and_return("24/02/2023")
expect(@market.date).to eq("24/02/2023")
end
end

describe "#add_vendor and #vendor_names" do
it 'can add vendors' do
@market.add_vendor(@vendor1)
expect(@market.vendors).to eq([@vendor1])

@market.add_vendor(@vendor2)
@market.add_vendor(@vendor3)
expect(@market.vendors).to eq([@vendor1,@vendor2,@vendor3])
Copy link
Contributor

Choose a reason for hiding this comment

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

Love that you expect this works for one vendor, and multiples

end

it "can return list of 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
end

describe "#vendors_that_sell" do
it 'can list vendors selling specific items' 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 'can list vendors after additional items have been added' do
@market.add_vendor(@vendor1)
@market.add_vendor(@vendor2)
@market.add_vendor(@vendor3)
@vendor3.stock(@item4, 25)

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

end
end

describe "#sorted_item_list" do
it 'can provide a list alphabetically with no duplicates' 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
end

describe "#overstocked_items" do
it 'can give an array of overstocked items' do
@market.add_vendor(@vendor1)
@market.add_vendor(@vendor2)
@market.add_vendor(@vendor3)

expect(@market.overstocked_items).to eq([@item1])
end
end

describe "#total_inventory" do
it "can give total inventory by item and vendor" do
@market.add_vendor(@vendor1)
@market.add_vendor(@vendor2)
@market.add_vendor(@vendor3)

expect(@market.total_inventory.class).to eq(Hash)
expect(@market.total_inventory[@item1].class).to eq(Hash)
expect(@market.total_inventory[@item1].keys).to eq([:total_quantity,:vendors])
expect(@market.total_inventory[@item1][:total_quantity]).to eq(100)

end
end

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

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
describe "vendor" do
it 'can initialize' do
expect(@vendor).to be_an_instance_of(Vendor)
expect(@vendor.name).to eq("Rocky Mountain Fresh")
expect(@vendor.inventory).to eq({})
end
end

describe "#stock" do
it 'can add stock to inventory' do
@vendor.stock(@item1, 30)
expect(@vendor.inventory.keys).to eq([@item1])

@vendor.stock(@item2,20)
expect(@vendor.inventory.keys).to eq([@item1,@item2])
Copy link
Contributor

Choose a reason for hiding this comment

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

Stock is tested in isolation - nice!

end

it 'can add to the value of existing stock' do
@vendor.stock(@item1, 30)
@vendor.stock(@item1,10)

expect(@vendor.inventory[@item1]).to eq(40)
end
end

describe "#check_stock" do
it 'can check stock by item' do
expect(@vendor.check_stock(@item1)).to eq(0)

@vendor.stock(@item1, 30)
expect(@vendor.check_stock(@item1)).to eq(30)

@vendor.stock(@item1,15)
expect(@vendor.check_stock(@item1)).to eq(45)
end
end

describe "#potential_revenue" do
it 'can give potential revenue' 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"})
@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

end