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

Joseph Bloom #7

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
50 changes: 50 additions & 0 deletions lib/event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class Event
attr_reader :name, :food_trucks
def initialize(name)
@name = name
@food_trucks = []
end

def add_food_truck(truck)
@food_trucks << truck
end

def food_truck_names
@food_trucks.map do |truck|
truck.name
end
end

def food_trucks_that_sell(item)
@food_trucks.find_all do |truck|
if truck.inventory.has_key?(item)
truck
end
end
end

def sorted_item_list
sorted_items = []
@food_trucks.each do |truck|
truck.inventory.each do |item, quantity|
sorted_items << item.name
end
end
sorted_items.uniq.sort
end

def overstocked_items
overstock = Hash.new {
|hash, key| hash[key] = { quantity: 0, trucks: 0 }
}
@food_trucks.each do |truck|
truck.inventory.each do |item, quantity|
overstock[item][:quantity] += quantity
overstock[item][:trucks] += 1
end
end
overstock.select do |item, info|
info[:trucks] > 1 && info[:quantity] > 50
end.keys
end
end
27 changes: 27 additions & 0 deletions lib/food_truck.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

class FoodTruck
attr_reader :name, :inventory

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

def check_stock(item)
@inventory[item] || 0
end

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

def potential_revenue
@inventory.sum do |item, quantity|
item.price.delete('$').to_f * quantity
end
end
end
8 changes: 8 additions & 0 deletions lib/item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

class Item
attr_reader :name, :price
def initialize(item)
@name = item[:name]
@price = item[:price]
end
end
78 changes: 78 additions & 0 deletions spec/event_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require './lib/food_truck'
require './lib/item'
require './lib/event'
require 'pry'

describe Event do
before :each do
@event = Event.new("South Pearl Street Farmers Market")
@food_truck1 = FoodTruck.new("Rocky Mountain Pies")
@food_truck2 = FoodTruck.new("Ba-Nom-a-Nom")
@food_truck3 = FoodTruck.new("Palisade Peach Shack")
@item1 = Item.new({name: 'Peach Pie (Slice)', price: "$3.75"})
@item2 = Item.new({name: 'Apple Pie (Slice)', price: '$2.50'})
@item3 = Item.new({name: "Peach-Raspberry Nice Cream", price: "$5.30"})
@item4 = Item.new({name: "Banana Nice Cream", price: "$4.25"})
@event.add_food_truck(@food_truck1)
@event.add_food_truck(@food_truck2)
@event.add_food_truck(@food_truck3)
end

describe '#initialize' do
it 'can initialize' do
expect(@event).to be_a(Event)
end
end

describe '#add_food_truck' do
it 'adds a food truck to the food_trucks array' do
expect(@event.food_trucks.count).to eq(3)
end
end

describe '#food_truck_names' do
it 'returns an array of food truck names' do
expect(@event.food_truck_names).to eq(["Rocky Mountain Pies", "Ba-Nom-a-Nom", "Palisade Peach Shack"])
end
end

describe '#food_trucks_that_sell(item)' do
it 'returns food trucks that sell specified item' do
@food_truck1.stock(@item1, 35)
expect(@event.food_trucks_that_sell(@item1).count).to eq(1)
@food_truck3.stock(@item1, 65)
expect(@event.food_trucks_that_sell(@item1).count).to eq(2)
expect(@event.food_trucks_that_sell(@item2).count).to eq(0)
@food_truck1.stock(@item2, 7)
expect(@event.food_trucks_that_sell(@item2).count).to eq(1)
end
end

describe '#sorted_item_list' do
it 'returns an alphabetical array of all foodtruck item names' do
@food_truck1.stock(@item1, 35)
expect(@event.sorted_item_list).to eq(['Peach Pie (Slice)'])
@food_truck1.stock(@item2, 7)
expect(@event.sorted_item_list).to eq(['Apple Pie (Slice)', 'Peach Pie (Slice)'])
@food_truck3.stock(@item1, 65)
expect(@event.sorted_item_list).to eq(['Apple Pie (Slice)', 'Peach Pie (Slice)'])
@food_truck3.stock(@item3, 45)
expect(@event.sorted_item_list).to eq(['Apple Pie (Slice)', 'Peach Pie (Slice)', 'Peach-Raspberry Nice Cream'])
end
end

describe '#overstocked_items' do
it 'returns an array of item objects sold by 2 or more food trucks and have quantity over 50' do
@food_truck1.stock(@item1, 35)
expect(@event.overstocked_items).to eq([])
@food_truck2.stock(@item1, 5)
expect(@event.overstocked_items).to eq([])
@food_truck2.stock(@item1, 15)
expect(@event.overstocked_items.count).to eq(1)
@food_truck3.stock(@item3, 55)
expect(@event.overstocked_items.count).to eq(1)
@food_truck1.stock(@item3, 10)
expect(@event.overstocked_items.count).to eq(2)
end
end
end
49 changes: 49 additions & 0 deletions spec/food_truck_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require './lib/food_truck'
require './lib/item'

describe FoodTruck do
before :each do
@food_truck = FoodTruck.new("Rocky Mountain Pies")
@item1 = Item.new({name: 'Peach Pie (Slice)', price: "$3.75"})
@item2 = Item.new({name: 'Apple Pie (Slice)', price: '$2.50'})
end

describe '#initialize' do
it 'can initialize' do
expect(@food_truck).to be_a(FoodTruck)
end
end

describe 'inventory' do
it 'can return inventory' do
expect(@food_truck.inventory).to eq({})
end
end

describe '#check_stock' do
it 'can return stock of specified item' do
expect(@food_truck.check_stock(@item1)).to eq(0)
end
end

describe '#stock' do
it 'can stock items' do
@food_truck.stock(@item1, 30)
expect(@food_truck.check_stock(@item1)).to eq(30)
@food_truck.stock(@item1, 25)
expect(@food_truck.check_stock(@item1)).to eq(55)
expect(@food_truck.check_stock(@item2)).to eq(0)
@food_truck.stock(@item2, 12)
expect(@food_truck.check_stock(@item2)).to eq(12)
end
end

describe '#potential_revenue' do
it 'returns total amount of potential revenue from stock' do
@food_truck.stock(@item1, 35)
expect(@food_truck.potential_revenue).to eq(131.25)
@food_truck.stock(@item2, 7)
expect(@food_truck.potential_revenue).to eq(148.75)
end
end
end
15 changes: 15 additions & 0 deletions spec/item_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require './lib/item'

describe Item do
before :each do
@item1 = Item.new({name: 'Peach Pie (Slice)', price: "$3.75"})
@item2 = Item.new({name: 'Apple Pie (Slice)', price: '$2.50'})
end

describe '#initialize' do
it 'can initialize' do
expect(@item1).to be_a(Item)
expect(@item2).to be_a(Item)
end
end
end