diff --git a/lib/attendee.rb b/lib/attendee.rb index 690e5c9..bd9bca4 100644 --- a/lib/attendee.rb +++ b/lib/attendee.rb @@ -1,8 +1,21 @@ class Attendee - attr_reader :name, :budget, :items + attr_reader :name, :budget def initialize(info) @name = info[:name] - @budget = info[:budget] + @budget = info[:budget].delete("$").to_i + bidding_open = true + end + + def add_bid(attendee, amount) + @bids[attendee] = amount if @bidding_open + end + + def current_high_bid + @bids.values.max + end + + def closed_bidding + @bidding_open = false end end \ No newline at end of file diff --git a/lib/auction.rb b/lib/auction.rb index 4d53f20..6effd5c 100644 --- a/lib/auction.rb +++ b/lib/auction.rb @@ -1,3 +1,5 @@ +require 'date' + class Auction attr_reader :items @@ -6,10 +8,37 @@ def initialize end def add_item(item) - @items.unshift(item) + @items << item end def item_names - raise NotImplementedError + @items.map(&:name) + end + + def unpopular_items + @items.select { |item| item.bids.empty? } + end + + def potential_revenue + @items.sum { |item| item.current_highest_bid.to_i } + end + + def bidders + @items.map { |item| item.bids.keys.map(&:name) }.flatten.uniq + end + + def bidder_info + info = Hash.new { |hash, key| hash[key] = { budget: key.budget, items: [] } } + + @items.each do |item| + item.bids.each_key do |attendee| + info[attendee][:items] << item + end + end + info + end + + def date + Date.today.strftime("%d/%m/%Y") end end \ No newline at end of file diff --git a/lib/item.rb b/lib/item.rb index e198fd3..98f531a 100644 --- a/lib/item.rb +++ b/lib/item.rb @@ -1,7 +1,29 @@ -class Items - attr_reader :name +class Item + attr_reader :name, :bids def initialize(name) - @name = "Chalkware Piggy Bank" + @name = name + @bids = {} + @bidding_open = true + end + + def add_bid(attendee, amount) + @bids[attendee] = amount + end + + def current_highest_bid + @bids.values.max + end + + def add_bid(attendee, amount) + @bids[attendee] = amount if @bidding_open + end + + def current_high_bid + @bids.values.max + end + + def closed_bidding + @bidding_open = false end end \ No newline at end of file diff --git a/spec/auction_spec.rb b/spec/auction_spec.rb index 31d12e0..b001597 100644 --- a/spec/auction_spec.rb +++ b/spec/auction_spec.rb @@ -1,18 +1,26 @@ require './lib/item' +require './lib/attendee' require './lib/auction' +require 'date' RSpec.describe Auction do before(:each) do @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') @auction = Auction.new + @attendee1 = Attendee.new({ name: 'Megan', budget: '$50' }) + @attendee2 = Attendee.new({ name: 'Bob', budget: '$75' }) + @attendee3 = Attendee.new({ name: 'Mike', budget: '$100' }) end it 'exists' do expect(@auction).to be_a(Auction) end - it 'has attributes' do + it 'has empty attributes' do expect(@auction.items).to eq([]) end @@ -27,6 +35,81 @@ @auction.add_item(@item1) @auction.add_item(@item2) - expect(@auction.item_names).to eq(["Chalkware Piggy Bank", "Bamboo Picture Frame"]) + expect(@auction.item_names).to eq([ + "Chalkware Piggy Bank", + "Bamboo Picture Frame", + ]) end + + it 'identifies (unpopular_items) - items with no bids' 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, 20) + expect(@auction.unpopular_items).to eq([@item2, @item3, @item4, @item5]) + + @item2.add_bid(@attendee2, 15) + expect(@auction.unpopular_items).to eq([@item3, @item4, @item5]) + end + + it 'calculates (potential revenue) - highest bids' 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) + expect(@auction.unpopular_items).to eq([@item2, @item3, @item4, @item5]) + + @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) # 22 (item1 - highest bid) + 15 (item3) + 50 (item4) + end + + it 'returns array of bidders names as string' do + @auction.add_item(@item1) + @auction.add_item(@item2) + + @item1.add_bid(@attendee1, 20) + @item1.add_bid(@attendee2, 30) + @item2.add_bid(@attendee3, 15) + + expect(@auction.bidders).to eq(["Megan", "Bob", "Mike"]) + end + + it 'returns bidder_info with hash value' do + @auction.add_item(@item1) + @auction.add_item(@item2) + @auction.add_item(@item3) + + @item1.add_bid(@attendee1, 20) + @item1.add_bid(@attendee2, 30) + @item2.add_bid(@attendee2, 25) + @item3.add_bid(@attendee3, 15) + + expected_info = { + @attendee1 => { budget: 50, items: [@item1] }, + @attendee2 => { budget: 75, items: [@item1, @item2] }, + @attendee3 => { budget: 100, items: [@item3] } + } + + expect(@auction.bidder_info).to eq(expected_info) + end + + it 'has date for auction' do + allow(Date).to receive(:today).and_return(Date.new(2020, 2, 24)) + + auction_date = Auction.new + expect(auction_date.date).to eq("24/02/2020") + end + end \ No newline at end of file diff --git a/spec/item_spec.rb b/spec/item_spec.rb index 4073cc4..d820765 100644 --- a/spec/item_spec.rb +++ b/spec/item_spec.rb @@ -14,4 +14,25 @@ expect(@item1.name).to eq("Chalkware Piggy Bank") expect(@item2.name).to eq("Bamboo Picture Frame") end + + it 'can initialize bids with empty hash' do + expect(@item1.bids).to eq({}) + end + + it 'can add bids to return highest bids' do + @item1.add_bid(@attendee1, 20) + @item1.add_bid(@attendee1, 22) + + expect(@item1.bids).to eq({ @attendee1 => 20, @attendee2 => 22}) + expect(@item1.current_high_bid).to eq(22) + end + it 'stops accepting new bids after bidding is closed' do + @item1.add_bid(@attendee1, 20) + expect(@item1.bids).to eq({ @attendee1 => 20 }) + + @item1.closed_bidding + @item1.add_bid(@attendee2, 24) # This bid shouldnt be added + + expect(@item1.bids).to eq({ @attendee1 => 20 }) + end end \ No newline at end of file