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

Danielle Cardona #3

Open
wants to merge 3 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
8 changes: 8 additions & 0 deletions lib/item.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
class Item

attr_reader :name,
:price

def initialize(attributes)
@name = attributes[:name]
@price = attributes[: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 patterns asks to clean the string (remove the $) and convert to a float.

end

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

require 'date'

attr_reader :name,
:date

attr_accessor :vendors
Copy link
Contributor

Choose a reason for hiding this comment

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

This could probably just be a attr_reader, as this vendor data doesnt need to be updated outside of this class


def initialize(name)
@name = name
@vendors = []
@date = Date.today.strftime('%Y/%m/%d')
Copy link
Contributor

Choose a reason for hiding this comment

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

OO nice!

end

def add_vendor(vendor_object)
Copy link
Contributor

Choose a reason for hiding this comment

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

vendor is probably an appropriate param name here

@vendors << vendor_object
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_items = @vendors.flat_map do |vendor|
vendor.inventory.keys
end

all_items.uniq.sort_by do |item|
item.name
end
end

def total_inventory
total_inventory = {}

@vendors.each do |vendor|
vendor.inventory.each do |item, quantity|

if total_inventory[item]
total_inventory[item][:quantity] += quantity
total_inventory[item][:vendors] << vendor unless total_inventory[item][:vendors].include?(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 may be an easier way to do it! Might be worth asking chatgpt for some refactor tips.

else
total_inventory[item] = {
quantity: quantity,
vendors: [vendor]
}
end
end
end
total_inventory
end

def overstocked_items
overstocked_items = []

total_inventory.each do |item, info|
if info[:quantity] > 50 && info[:vendors].length > 1
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice conditional logic here!

overstocked_items << item
end
end
overstocked_items
end

def sell(item, quantity)
if total_inventory[item] && total_inventory[item][:quantity] >= quantity
total_inventory[item][:quantity] -= quantity
true
else
false
end
end
end
23 changes: 22 additions & 1 deletion 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(0)
end

def check_stock(item)
@inventory[item]
end

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

def potential_revenue
@inventory.sum do |item, quantity|
item.price.delete('$').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.

Cleaning the string down here - nice! Do it in the item class at the beginning :)

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

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

describe '#initialze' do
it "exists" do
expect(@item1).to be_instance_of(Item)
end

it 'has a name and a price' do
expect(@item1.name).to eq("Peach")
expect(@item1.price).to eq("$0.75")
end
end
end
117 changes: 117 additions & 0 deletions spec/market_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
require './lib/item'
require './lib/vendor'
require './lib/market'

RSpec.describe Market do
before(:each) do
@vendor1 = Vendor.new("Rocky Mountain Fresh")
@vendor2 = Vendor.new("Ba-Nom-a-Nom")
@vendor3 = Vendor.new("Palisade Peach Shack")
@vendor4 = Vendor.new("Choco Monster")
@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"})
@market = Market.new("South Pearl Street Farmers Market")
end

describe '#initialize' do
it "exists" do
expect(@market).to be_instance_of(Market)
end

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

it 'has a date' do
expect(@market.date).to eq("2024/09/16")
end

it 'starts with an empty array of vendors' do
expect(@market.vendors).to eq([])
end
end
Comment on lines +19 to +34
Copy link
Contributor

Choose a reason for hiding this comment

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

Functionality from the initialize method can be consolidated into a single test to save time and lines of code.


describe '#vendors' do
before(:each) do
@market.add_vendor(@vendor1)
@market.add_vendor(@vendor2)
@market.add_vendor(@vendor3)
end

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

it 'stores vendor names in an array' do
expect(@market.vendor_names).to eq(["Rocky Mountain Fresh", "Ba-Nom-a-Nom", "Palisade Peach Shack"])
end
end

describe '#stock' do
before(:each) 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)
end

it 'identifies vendors that sell items' do
expect(@market.vendors_that_sell(@item1)).to eq([@vendor1, @vendor3])
expect(@market.vendors_that_sell(@item2)).to eq([@vendor1])
expect(@market.vendors_that_sell(@item3)).to eq([@vendor2])
expect(@market.vendors_that_sell(@item4)).to eq([@vendor2])
end

it 'identifies all items in stock, sorted alphabetically' do
expect(@market.sorted_item_list).to eq([@item4, @item1, @item3, @item2])
end

it 'reports total inventory' do
expected = {
@item1 => { quantity: 100, vendors: [@vendor1, @vendor3] },
@item2 => { quantity: 7, vendors: [@vendor1] },
@item3 => { quantity: 25, vendors: [@vendor2] },
@item4 => { quantity: 50, vendors: [@vendor2] },
}

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

it 'updates when new items are added' do
@market.add_vendor(@vendor4)
@item5 = Item.new({name: 'Chocolate Almond Butter', price: "$6.25"})
@vendor4.stock(@item5, 14)

expected = {
@item1 => { quantity: 100, vendors: [@vendor1, @vendor3] },
@item2 => { quantity: 7, vendors: [@vendor1] },
@item3 => { quantity: 25, vendors: [@vendor2] },
@item4 => { quantity: 50, vendors: [@vendor2] },
@item5 => { quantity: 14, vendors: [@vendor4] }
}
end

it 'identifies overstocked items' do
expect(@market.overstocked_items).to eq([@item1])
end

it 'identifies more overstocked items as appropriate' do
@vendor1.stock(@item4, 75)

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

it 'indicates if it has insufficient stock' do
expect(@market.sell(@item2, 10)).to eq(false)
expect(@market.sell(@item1, 10)).to eq(true)
end

end
end

76 changes: 76 additions & 0 deletions spec/vendor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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 '#initialze' do
it "exists" do
expect(@vendor).to be_instance_of(Vendor)
end

it 'has a name' do
expect(@vendor.name).to eq("Rocky Mountain Fresh")
end
end

describe '#stock' do
it 'starts with an empty inventory hash' do
expect(@vendor.inventory).to eq({})
end

it 'checks inventory stock' do
expect(@vendor.check_stock(@item1)).to eq(0)
end

it 'adds inventory' do
@vendor.stock(@item1, 30)

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

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

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

it 'identifies items and amounts in inventory' do
@vendor.stock(@item1, 30)
@vendor.stock(@item1, 25)
@vendor.stock(@item2, 12)

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

describe '#revenue' do
before(:each) do
@vendor1 = Vendor.new("Rocky Mountain Fresh")
@vendor2 = Vendor.new("Ba-Nom-a-Nom")
@vendor3 = Vendor.new("Palisade Peach Shack")
@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

it 'calculates each vendors potential revenue' do
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