-
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
IC #5 Montana Pfeifer #12
base: main
Are you sure you want to change the base?
Changes from all commits
5f2385a
5f91fb2
48cbca0
ac0960d
078eebb
226d381
ee192f2
e06f7c3
2cf78fd
1c0b792
8a43110
a7d88cb
85fcb57
965d399
bf41220
15ef055
83c5da0
0ee7fb8
61da707
2e42161
8571f47
52d107a
cef5ce7
12cd29b
b0452af
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 |
---|---|---|
@@ -1,3 +1,10 @@ | ||
class Item | ||
|
||
attr_reader :name, | ||
:price | ||
|
||
def initialize(details) | ||
@name = details[:name] | ||
@price = details[:price].delete('$').to_f | ||
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(&:name) | ||
end | ||
|
||
def vendors_that_sell(item) | ||
@vendors.select { |vendor| vendor.check_stock(item) > 0} | ||
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. These one liners are nice !! |
||
end | ||
|
||
def total_inventory | ||
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. There is a simpler solution here! Might be worth asking ChatGPT :) |
||
inventory = Hash.new { |hash, key| hash[key] = { quantity: 0, vendors: [] } } | ||
|
||
@vendors.each do |vendor| | ||
vendor.inventory.each do |item, details| | ||
inventory[item][:quantity] += details[:quantity] | ||
inventory[item][:vendors] << vendor unless inventory[item][:vendors].include?(vendor) | ||
end | ||
end | ||
inventory | ||
end | ||
|
||
def overstocked_items(threshold = 50) | ||
total_inventory.select { |item, details| details[:quantity] >= threshold } | ||
end | ||
end |
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 { |hash, key| hash[key] = { quantity: 0 } } | ||
end | ||
|
||
def check_stock(item) | ||
@inventory[item][:quantity] | ||
end | ||
|
||
def stock(item, quantity) | ||
@inventory[item][:quantity] += 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. Was expecting something a little more like this here |
||
end | ||
|
||
def potential_revenue | ||
@inventory.sum do |item, details| | ||
item.price * details[:quantity] | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require './lib/item' | ||
|
||
RSpec.describe Item do | ||
before(:each) do | ||
@item = Item.new({name: 'Peach', price: "$0.75"}) | ||
end | ||
|
||
it 'exists' do | ||
expect(@item).to be_a(Item) | ||
end | ||
|
||
it 'has a name' do | ||
expect(@item.name).to eq("Peach") | ||
end | ||
|
||
it 'has a price' do | ||
expect(@item.price).to eq(0.75) | ||
end | ||
|
||
it 'handles different prices' do | ||
@item2 = Item.new({name: 'Tomato', price: "$0.50"}) | ||
|
||
expect(@item2.price).to eq(0.50) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
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) | ||
@vendor3.stock(@item3, 10) | ||
|
||
end | ||
|
||
it 'exists and has attributes' do | ||
expect(@market).to be_a(Market) | ||
expect(@market.name).to eq("South Pearl Street Farmers Market") | ||
expect(@market.vendors).to eq([]) | ||
end | ||
|
||
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 the vendors 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 find vendors who sell a specific item' 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 "calculates potential_revenue" do | ||
expect(@vendor1.potential_revenue).to eq(29.75) | ||
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. I like that you are testing different vendors (scenarios) and testing how the outputs differ. |
||
expect(@vendor2.potential_revenue).to eq(345.00) | ||
expect(@vendor3.potential_revenue).to eq(101.75) | ||
end | ||
|
||
it 'returns a sorted list of items' 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 | ||
|
||
it 'returns total inventory' do | ||
@market.add_vendor(@vendor1) | ||
@market.add_vendor(@vendor2) | ||
@market.add_vendor(@vendor3) | ||
|
||
expected = { | ||
@item1 => { quantity: 100, vendors: [@vendor1, @vendor3] }, | ||
@item2 => { quantity: 7, vendors: [@vendor1] }, | ||
@item3 => { quantity: 35, vendors: [@vendor2, @vendor3] }, | ||
@item4 => { quantity: 50, vendors: [@vendor2] } | ||
} | ||
expect(@market.total_inventory).to eq(expected) | ||
end | ||
|
||
it 'returns overstocked items' do | ||
@market.add_vendor(@vendor1) | ||
@market.add_vendor(@vendor2) | ||
@market.add_vendor(@vendor3) | ||
|
||
expected = { | ||
@item1 => { quantity: 100, vendors: [@vendor1, @vendor3] }, | ||
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. Per the interaction pattern, the items should looks like
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. Where the item object is the key, and the quantity is the value. |
||
@item4 => { quantity: 50, vendors: [@vendor2] } | ||
} | ||
|
||
expect(@market.overstocked_items).to eq(expected) | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require './lib/vendor' | ||
require './lib/item' | ||
|
||
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 | ||
|
||
it 'exists' do | ||
expect(@vendor).to be_a(Vendor) | ||
expect(@vendor.name).to eq("Rocky Mountain Fresh") | ||
end | ||
|
||
it 'has an inventory' do | ||
expect(@vendor.inventory).to eq({}) | ||
end | ||
|
||
it 'checks stocks of items' do | ||
expect(@vendor.check_stock(@item1)).to eq(0) | ||
end | ||
|
||
it 'stocks items in inventory' do | ||
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. These tests are failing - we're getting an object back that looks like |
||
@vendor.stock(@item1, 30) | ||
expect(@vendor.inventory).to eq({@item1 => 30}) | ||
expect(@vendor.check_stock(@item1)).to eq(30) | ||
|
||
@vendor.stock(@item1, 25) | ||
expect(@vendor.check_stock(@item1)).to eq(55) | ||
end | ||
|
||
it 'stocks multiple items'do | ||
@vendor.stock(@item1, 30) | ||
@vendor.stock(@item2, 12) | ||
|
||
expect(@vendor.inventory).to eq({@item1 => 30, @item2 => 12}) | ||
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.
Way to clean the string input, and convert it to the correct data type.