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

Coral - its worse #2

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
13 changes: 13 additions & 0 deletions lib/item.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
class Item
attr_reader :name, :price

def initialize(info)
@name = info[:name]
@price = info[:price].delete("$").to_f
end

def name
@name
end

def price
@price
end
end
34 changes: 34 additions & 0 deletions lib/market.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
class Market
attr_reader :name, :vendors

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

def add_vendor(vendor)
@vendors << vendor
end

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

def vendors_that_sell(item)

end

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

end
23 changes: 23 additions & 0 deletions lib/vendor.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
class Vendor
attr_reader :name, :inventory

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

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

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

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

RSpec.describe Item do
before(:each) do
@item1 = Item.new({name: 'Peach', price: "$0.75"}) #=> #<Item:0x007f9c56740d48...>
@item2 = Item.new({name: 'Tomato', price: '$0.50'}) #=> #<Item:0x007f9c565c0ce8...>
end

it 'exists' do
expect(@item1).to be_a(Item)
expect(@item2).to be_a(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.5)
end
end
106 changes: 106 additions & 0 deletions spec/market_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
require './lib/item'
require './lib/vendor'
require './lib/market'

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

it 'exists' do #1
expect(@market).to be_a(Market)
end

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

it 'defults empty array for vendors' do #3
expect(@market.vendors).to eq([])
end

describe '#add_vendor' do
it 'adds vendors to array' do #4
@market.add_vendor(@vendor1)
@market.add_vendor(@vendor2)
@market.add_vendor(@vendor3)

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

describe '#vendor_names' do
it 'returns list of vendor names' do #5
@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 'returns list of vendor names' do #6
@vendor1.stock(@item1, 35)
@vendor1.stock(@item2, 7)
@vendor2.stock(@item4, 50)
@vendor2.stock(@item3, 25)
@vendor3.stock(@item1, 65)
@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
end

describe '#sorted_item_list' do
it 'returns list in stock item names sorted alphabetically' do #7
@vendor1.stock(@item1, 35)
@vendor1.stock(@item2, 7)
@vendor2.stock(@item4, 50)
@vendor2.stock(@item3, 25)
@vendor3.stock(@item1, 65)
@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", "Totmato"])
end
end

describe '#total_inventory' do #8
it 'Reports the quantities of all items sold at the market. Specifically, it should return a hash with items as keys and hashes as values - this sub-hash should have two key/value pairs: quantity pointing to total inventory for that item and vendors pointing to an array of the vendors that sell that item.' do
@market.add_vendor(@vendor1)
@market.add_vendor(@vendor2)
@market.add_vendor(@vendor3)

expect(@market.total_inventory).to eq({})
end
end

describe '#overstocked_items' do #9
it 'An array of `Item` objects that are overstocked. An item is overstocked if it is sold by more than 1 vendor AND the total quantity is greater than 50.' do
@vendor1.stock(@item1, 35)
@vendor1.stock(@item2, 7)
@vendor2.stock(@item4, 50)
@vendor2.stock(@item3, 25)
@vendor3.stock(@item1, 65)
@market.add_vendor(@vendor1)
@market.add_vendor(@vendor2)
@market.add_vendor(@vendor3)

expect(@market.overstocked_items).to eq([@item1])
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
@item1 = Item.new({name: 'Peach', price: "$0.75"})
@item2 = Item.new({name: 'Tomato', price: '$0.50'})
@vendor1 = Vendor.new("Rocky Mountain Fresh")
@vendor2 = Vendor.new("Ba-Nom-a-Nom")
@vendor3 = Vendor.new("Palisade Peach Shack")
end

it 'exists' do #1
expect(@vendor1).to be_a(Vendor)
end

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

it 'has defaults an empty hash' do #3
expect(@vendor1.inventory).to eq({})
end

describe '#stock' do
it 'adds item and quantity to inventory hash' do #4
@vendor1.stock(@item1, 30)

expect(@vendor1.inventory).to eq({@item1 => 30})
end

it 'can add multiple items and keep a running tally of quantity' do #5
@vendor1.stock(@item1, 30)
@vendor1.stock(@item1, 25)
@vendor1.stock(@item2, 12)
expect(@vendor1.inventory).to eq({@item1 => 55, @item2 => 12})
end
end

describe '#check_stock' do
it 'defaults to zero stock' do #6
expect(@vendor1.check_stock(@item1)).to eq(0)
end

it 'provides quantity of item' do #7
@vendor1.stock(@item1, 30)

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

it 'provides accurate running tally of stock' do #8
@vendor1.stock(@item1, 30)
@vendor1.stock(@item1, 25)

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

describe '#potential_revenue' do
it 'accurately determines potential revenue' do #9
@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