From a7e03057955e6034761b8b6ac543e61d02fbf143 Mon Sep 17 00:00:00 2001 From: Zion Mekonnen Date: Wed, 22 Jan 2025 10:58:46 -0800 Subject: [PATCH] IC Week 6 retake --- README.md | 20 +++++----- lib/attendee.rb | 8 ++++ lib/auction.rb | 46 +++++++++++++++++++++++ lib/item.rb | 16 ++++++++ spec/attendee_spec.rb | 20 ++++++++++ spec/auction_spec.rb | 87 +++++++++++++++++++++++++++++++++++++++++++ spec/item_spec.rb | 24 ++++++++++++ 7 files changed, 211 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 5b5e472..bfb1f7a 100644 --- a/README.md +++ b/README.md @@ -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 #=> { # # => 20, # # => 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 #=> [#, #, #] -pry(main)> item3.add_bid(attendee2, 15) +#pry(main)> item3.add_bid(attendee2, 15) -pry(main)> auction.unpopular_items +#pry(main)> auction.unpopular_items #=> [#, #] -pry(main)> auction.potential_revenue +#pry(main)> auction.potential_revenue #=> 87 ``` diff --git a/lib/attendee.rb b/lib/attendee.rb index e69de29..4cb2ad3 100644 --- a/lib/attendee.rb +++ b/lib/attendee.rb @@ -0,0 +1,8 @@ +class Attendee + attr_reader :name, :budget + + def initialize(details) + @name = details[:name] + @budget = details[:budget].delete('$').to_i + end +end \ No newline at end of file diff --git a/lib/auction.rb b/lib/auction.rb index e69de29..23a4696 100644 --- a/lib/auction.rb +++ b/lib/auction.rb @@ -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 \ No newline at end of file diff --git a/lib/item.rb b/lib/item.rb index e69de29..4678293 100644 --- a/lib/item.rb +++ b/lib/item.rb @@ -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 \ No newline at end of file diff --git a/spec/attendee_spec.rb b/spec/attendee_spec.rb index e69de29..5cb1333 100644 --- a/spec/attendee_spec.rb +++ b/spec/attendee_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/auction_spec.rb b/spec/auction_spec.rb index e69de29..af1cb79 100644 --- a/spec/auction_spec.rb +++ b/spec/auction_spec.rb @@ -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 + + + + diff --git a/spec/item_spec.rb b/spec/item_spec.rb index e69de29..6c3d7c9 100644 --- a/spec/item_spec.rb +++ b/spec/item_spec.rb @@ -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 \ No newline at end of file