diff --git a/lib/item.rb b/lib/item.rb index 85bb5fd..5217010 100644 --- a/lib/item.rb +++ b/lib/item.rb @@ -1,3 +1,8 @@ class Item + attr_reader :name, :price + def initialize(info) + @name = info[:name] + @price = info[:price].delete("$").to_i + end end diff --git a/lib/market.rb b/lib/market.rb index 12c4370..245fb9c 100644 --- a/lib/market.rb +++ b/lib/market.rb @@ -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 diff --git a/lib/vendor.rb b/lib/vendor.rb index 2a97a94..a630f4d 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -1,3 +1,33 @@ class Vendor -end + attr_reader :name, :inventory + + def initialize(name) + @name = name + @inventory = {} + end + + def check_stock(item) + if !@inventory.empty? + @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 + diff --git a/spec/item_spec.rb b/spec/item_spec.rb index e69de29..8d6125f 100644 --- a/spec/item_spec.rb +++ b/spec/item_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/market_spec.rb b/spec/market_spec.rb new file mode 100644 index 0000000..c5185a7 --- /dev/null +++ b/spec/market_spec.rb @@ -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 diff --git a/spec/vendor_spec.rb b/spec/vendor_spec.rb new file mode 100644 index 0000000..06287f4 --- /dev/null +++ b/spec/vendor_spec.rb @@ -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 \ No newline at end of file