-
Notifications
You must be signed in to change notification settings - Fork 14
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
Paul Knapp #11
base: main
Are you sure you want to change the base?
Paul Knapp #11
Changes from all commits
eab53f1
bb3553d
795cccd
4a8afd0
5b2cda3
b17e6a6
1bfe330
e67318d
f0a175b
5939dfa
aae555b
cc6561c
2152aa7
bdae433
3c4545d
4a12313
ee83934
0f76e5e
f7ed38d
7512b0c
d6c7566
d2fc879
c34bb1a
672f9e7
578d5d6
4c29e1c
99771d6
31786ff
14a8d97
1a4fbe4
ace885e
4f5c045
9b1f1be
f3e5400
e99ffe5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--format documentation | ||
--color | ||
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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The interaction pattern asks to clean the string (remove the dollar sign) and convert to a float! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great commit history - nice stuff Paul! |
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,42 @@ | ||
class Market | ||
|
||
attr_reader :name, :vendors | ||
|
||
def initialize(name) | ||
@name = name | ||
@vendors = ([]) | ||
end | ||
|
||
def add_vendor(vendor) | ||
@vendors << vendor | ||
end | ||
|
||
def vendor_names | ||
@vendors.map { |vendor| vendor.name } | ||
end | ||
|
||
def vendors_that_sell(item) | ||
@vendors.select do |vendor| | ||
vendor.inventory.include?(item) | ||
end | ||
end | ||
|
||
def sorted_item_list | ||
vendors.flat_map { |vendor| vendor.inventory.keys.map(&:name)}.uniq.sort | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice one liner! |
||
end | ||
|
||
def total_inventory | ||
all_inventory = {} | ||
|
||
@vendors.each do |vendor| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Theres probably an easier way to do this. Ask perplexity for some refactoring tips! |
||
vendor.inventory.each do |item, quantity| | ||
if all_inventory[item] | ||
all_inventory[item][:quantity] += quantity | ||
all_inventory[item][:vendors] << vendor.name unless all_inventory[item][:vendors].include?(vendor.name) | ||
else | ||
all_inventory[item] = {quantity: quantity, vendors: [vendor.name]} | ||
end | ||
end | ||
end | ||
all_inventory | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,21 @@ | ||
class Vendor | ||
|
||
attr_reader :name, :inventory | ||
def initialize(name) | ||
@inventory = {} | ||
@name = name | ||
end | ||
|
||
def check_stock(item) | ||
@inventory[item] || 0 | ||
end | ||
|
||
def stock(item, quantity) | ||
@inventory[item] = (@inventory[item] || 0 )+ quantity | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OO I like this |
||
end | ||
|
||
def potential_revenue | ||
@inventory.sum do |item,quantity| | ||
item.price.gsub('$', '').to_f * quantity | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Youre cleaning the string here!! Nice. Do it on the item instantiation, though. |
||
end.round(2) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'spec_helper' | ||
|
||
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_instance_of (Item) | ||
expect(@item2).to be_an_instance_of (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.50') | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require 'spec_helper' | ||
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_an_instance_of(Market) | ||
end | ||
|
||
it '#has a name' do | ||
expect(@market.name).to eq ("South Pearl Street Farmers Market") | ||
end | ||
|
||
it '#has no vendors' do | ||
expect(@market.vendors).to eq([]) | ||
end | ||
Comment on lines
+14
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Functionality that happens in the instantiation can be consolidated into a single test, if you'd like to save time and lines. |
||
|
||
it '#can have vendors' do | ||
@market.add_vendor(@vendor1) | ||
@market.add_vendor(@vendor2) | ||
@market.add_vendor(@vendor3) | ||
expect(@market.vendors).to eq([@vendor1, @vendor2, @vendor3]) | ||
end | ||
|
||
it '#can tell 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 | ||
|
||
it '#can show vendors that sell an item' do | ||
@vendor1.stock(@item1, 35) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider testing stock in isolation. This is a nice test though, I have no problem with what you did here. |
||
@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 | ||
|
||
it '#can show all items in inventory' 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.sorted_item_list).to eq(["Banana Nice Cream", "Peach", "Peach-Raspberry Nice Cream", "Tomato"]) | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require 'pry' | ||
require './lib/item' | ||
require './lib/vendor' | ||
require './lib/market' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require 'spec_helper' | ||
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_an_instance_of(Vendor) | ||
expect(@vendor2).to be_an_instance_of(Vendor) | ||
expect(@vendor3).to be_an_instance_of(Vendor) | ||
end | ||
|
||
it '#has a name' do | ||
expect(@vendor1.name).to eq ("Rocky Mountain Fresh") | ||
expect(@vendor2.name).to eq ("Ba-Nom-a-Nom") | ||
expect(@vendor3.name).to eq ("Palisade Peach Shack") | ||
end | ||
|
||
it '#has inventory' do | ||
expect(@vendor1.inventory).to eq({}) | ||
expect(@vendor2.inventory).to eq({}) | ||
expect(@vendor3.inventory).to eq({}) | ||
end | ||
|
||
it '#has stock' do | ||
expect(@vendor1.check_stock(@item1)).to eq(0) | ||
expect(@vendor2.check_stock(@item1)).to eq(0) | ||
expect(@vendor3.check_stock(@item1)).to eq(0) | ||
end | ||
|
||
it '#can stock items' do | ||
@vendor1.stock(@item1, 30) | ||
expect(@vendor1.check_stock(@item1)).to eq(30) | ||
expect(@vendor1.check_stock(@item1)).to_not eq(0) | ||
expect(@vendor2.check_stock(@item1)).to eq(0) | ||
@vendor1.stock(@item1, 25) | ||
expect(@vendor1.check_stock(@item1)).to eq(55) | ||
expect(@vendor2.check_stock(@item1)).to eq(0) | ||
end | ||
|
||
it '#can have items in inventory' do | ||
@vendor1.stock(@item1, 25) | ||
@vendor1.stock(@item2, 25) | ||
expect(@vendor1.inventory).to eq({@item1 => 25 , @item2 => 25}) | ||
end | ||
|
||
it '#can determine potential revenue for vendors' 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.00) | ||
expect(@vendor3.potential_revenue).to eq (48.75) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!