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

Zion Mekonnen - IC Week 6 retake #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
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,33 +109,33 @@ pry(main)> auction.add_item(item4)

pry(main)> auction.add_item(item5)

pry(main)> item1.bids
#pry(main)> item1.bids
#=> {}

pry(main)> item1.add_bid(attendee2, 20)
#pry(main)> item1.add_bid(attendee2, 20)

pry(main)> item1.add_bid(attendee1, 22)
#pry(main)> item1.add_bid(attendee1, 22)

pry(main)> item1.bids
#pry(main)> item1.bids
#=> {
# #<Attendee:0x00007fdc071131c8 ...> => 20,
# #<Attendee:0x00007fdc088f0e08 ...> => 22
# }

pry(main)> item1.current_high_bid
#pry(main)> item1.current_high_bid
#=> 22

pry(main)> item4.add_bid(attendee3, 50)
#pry(main)> item4.add_bid(attendee3, 50)

pry(main)> auction.unpopular_items
#pry(main)> auction.unpopular_items
#=> [#<Item:0x00007fdc07925280 ...>, #<Item:0x00007fdc071ab040 ...>, #<Item:0x00007fdc071695f0 ...>]

pry(main)> item3.add_bid(attendee2, 15)
#pry(main)> item3.add_bid(attendee2, 15)

pry(main)> auction.unpopular_items
#pry(main)> auction.unpopular_items
#=> [#<Item:0x00007fdc07925280 ...>, #<Item:0x00007fdc071695f0 ...>]

pry(main)> auction.potential_revenue
#pry(main)> auction.potential_revenue
#=> 87

```
Expand Down
8 changes: 8 additions & 0 deletions lib/attendee.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Attendee
attr_reader :name, :budget

def initialize(details)
@name = details[:name]
@budget = details[:budget].delete('$').to_i
end
end
46 changes: 46 additions & 0 deletions lib/auction.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Auction
attr_reader :items
def initialize
@items = []
end

def add_item(item)
@items << item
end

def item_names
@items.map do |item|
item.name
end
end

def unpopular_items
@items.find_all do |item|
item.bids.empty?
end
end

# def potential_revenue
# total_revenue = 0
# @items.each do |item|
# total_revenue += item.current_high_bid if item.current_high_bid
# end
# total_revenue
# end

def potential_revenue
@items.sum do |item|
item.current_high_bid || 0
end
end

# def bidders
# @items.flat_map do |item|
# item.bids.keys.compact.map do |attendee|
# attendee.name
# end
# end.uniq

# end

end
16 changes: 16 additions & 0 deletions lib/item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Item
attr_reader :name, :bids

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

def add_bid(attendee, amount)
@bids[@attendee] = amount
end

def current_high_bid
@bids.values.max
end
end
20 changes: 20 additions & 0 deletions spec/attendee_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require './lib/attendee'
require 'rspec'

RSpec.describe Attendee do
before(:each) do
@attendee = Attendee.new({name: 'Megan', budget: '$50'})
@attendee2 = Attendee.new({name: 'Bob', budget: '$75'})
@attendee3 = Attendee.new({name: 'Mike', budget: '$100'})

end

it 'exists and has attributes' do
expect(@attendee).to be_an_instance_of(Attendee)
expect(@attendee.name).to eq('Megan')
expect(@attendee.budget).to eq(50)
expect(@attendee2.name).to eq('Bob')
expect(@attendee3.name).to eq('Mike')

end
end
87 changes: 87 additions & 0 deletions spec/auction_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require './lib/auction'
require './lib/item'
require './lib/attendee'
require 'rspec'

RSpec.describe Auction do
before(:each) do
@auction = Auction.new

@attendee1 = Attendee.new({name: 'Megan', budget: '$50'})
@attendee2 = Attendee.new({name: 'Bob', budget: '$75'})
@attendee3 = Attendee.new({name: 'Mike', budget: '$100'})

@item1 = Item.new('Chalkware Piggy Bank')
@item2 = Item.new('Bamboo Picture Frame')
@item3 = Item.new('Homemade Chocolate Chip Cookies')
@item4 = Item.new('2 Days Dogsitting')
@item5 = Item.new('Forever Stamps')

end


it 'exists' do
expect(@auction).to be_an_instance_of(Auction)
expect(@auction.items).to eq([])
end


it 'adds items to the auction' do
@auction.add_item(@item1)
@auction.add_item(@item2)
@auction.add_item(@item3)
@auction.add_item(@item4)
@auction.add_item(@item5)

expect(@auction.items).to eq([@item1, @item2, @item3, @item4, @item5])
end

it 'returns names of items' do
@auction.add_item(@item1)
@auction.add_item(@item2)
@auction.add_item(@item3)
@auction.add_item(@item4)
@auction.add_item(@item5)

expect(@auction.item_names).to eq(['Chalkware Piggy Bank', 'Bamboo Picture Frame', 'Homemade Chocolate Chip Cookies', '2 Days Dogsitting', 'Forever Stamps'])
end

it 'returns items with no bid and calculates potential revenue' do
@auction.add_item(@item1)
@auction.add_item(@item2)
@auction.add_item(@item3)
@auction.add_item(@item4)
@auction.add_item(@item5)

@item1.add_bid(@attendee2, 20)
@item1.add_bid(@attendee1, 22)
@item4.add_bid(@attendee3, 50)

expect(@auction.unpopular_items).to eq([@item2, @item3, @item5])

@item3.add_bid(@attendee2, 15)
expect(@auction.unpopular_items).to eq([@item2, @item5])

expect(@auction.potential_revenue).to eq(87)
end

# it 'returns the name of the attendees that made a bid' do
# @auction.add_item(@item1)
# @auction.add_item(@item2)
# @auction.add_item(@item3)
# @auction.add_item(@item4)
# @auction.add_item(@item5)

# @item1.add_bid(@attendee1, 22)
# @item1.add_bid(@attendee2, 20)
# @item4.add_bid(@attendee3, 50)

# expect(@auction.bidders).to eq(['Megan', 'Bob', 'Mike'])
# 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'
require 'rspec'

RSpec.describe Item do
before(:each) do
@item1 = Item.new('Chalkware Piggy Bank')
end

it 'exists and has attributes' do
expect(@item1).to be_an_instance_of(Item)
expect(@item1.name).to eq('Chalkware Piggy Bank')
expect(@item1.bids).to eq({})
end

it 'can add bids and returns the sum of all the highest bids' do
@item1.add_bid(@attendee2, 20)
@item1.add_bid(@attendee1, 22)

expect(@item1.bids).to eq({@attendee2 => 20, @attendee1 => 22})
expect(@item1.current_high_bid).to eq(22)

end

end