diff --git a/lib/attendee.rb b/lib/attendee.rb index 690e5c9..fbeeef3 100644 --- a/lib/attendee.rb +++ b/lib/attendee.rb @@ -3,6 +3,6 @@ class Attendee def initialize(info) @name = info[:name] - @budget = info[:budget] + @budget = info[:budget].delete('$').to_i end end \ No newline at end of file diff --git a/lib/auction.rb b/lib/auction.rb index 4d53f20..05b85e5 100644 --- a/lib/auction.rb +++ b/lib/auction.rb @@ -1,15 +1,100 @@ +require 'date' + class Auction attr_reader :items def initialize @items = [] + @date = Date.today + end + + def date + @date.strftime('%d/%m/%Y') end def add_item(item) - @items.unshift(item) + @items << item end def item_names - raise NotImplementedError + names = [] + @items.each do |item| + names << item.name + end + names + end + + def unpopular_items + unpopular_items = [] + @items.each do |item| + if item.bids.empty? + unpopular_items << item + end + end + unpopular_items + end + + def potential_revenue + potential_revenue = 0 + @items.each do |item| + if item.current_high_bid != nil + potential_revenue += item.current_high_bid + end + end + potential_revenue + end + + def bidders + names = [] + @items.each do |item| + all_bidders_for_item = item.bids.keys + + all_bidders_for_item.each do |bidder| + if !names.include?(bidder.name) + names << bidder.name + end + end + end + names + end + + def bidder_objects + bidder_objects = [] + @items.each do |item| + all_bidders_for_item = item.bids.keys + + all_bidders_for_item.each do |bidder| + if !bidder_objects.include?(bidder) + bidder_objects << bidder + end + end + end + bidder_objects + end + + # i left my notes here so you could see my process for creating this method. + # i wrote the notes as a guide, then the test, then came back and wrote the code + def bidder_info + #start with an empty hash + bidder_info = {} + + #get a list of all bidder objects + all_bidders = bidder_objects + + #enumerate though all_bidders + all_bidders.each do |bidder| + #get their budget + budget = bidder.budget + #get a list of items they have bid on + items = [] + @items.each do |item| + if item.bids.keys.include?(bidder) + items << item + end + end + #create and add key, value pair to initial hash + bidder_info[bidder] = {:budget => budget, :items => items} + end + bidder_info end end \ No newline at end of file diff --git a/lib/item.rb b/lib/item.rb index e198fd3..74fa97a 100644 --- a/lib/item.rb +++ b/lib/item.rb @@ -1,7 +1,29 @@ -class Items - attr_reader :name +class Item + attr_reader :name, :bids, :closed def initialize(name) - @name = "Chalkware Piggy Bank" + @name = name + @bids = {} + @closed = false + end + + def closed? + @closed + end + + def add_bid(attendee, bid) + if @closed == false + @bids[attendee] = bid + end + end + + def current_high_bid + @bids.max_by do |attendee, bid| + bid + end[1] + end + + def close_bidding + @closed = true end end \ No newline at end of file diff --git a/spec/auction_spec.rb b/spec/auction_spec.rb index 31d12e0..b76b97e 100644 --- a/spec/auction_spec.rb +++ b/spec/auction_spec.rb @@ -1,11 +1,19 @@ require './lib/item' 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') + allow(Date).to receive(:today).and_return(Date.new(2020, 02, 24)) @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 @@ -14,6 +22,7 @@ it 'has attributes' do expect(@auction.items).to eq([]) + expect(@auction.date).to eq('24/02/2020') end it 'add_item' do @@ -29,4 +38,98 @@ expect(@auction.item_names).to eq(["Chalkware Piggy Bank", "Bamboo Picture Frame"]) end + + describe '#unpopular_items' do + it 'returns all items that have no bids' do + @auction.add_item(@item1) + @auction.add_item(@item2) + @auction.add_item(@item3) + @auction.add_item(@item4) + @auction.add_item(@item5) + expect(@auction.unpopular_items).to eq([@item1, @item2, @item3, @item4, @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]) + end + end + + describe '#potential_revenue' do + it 'returns the sum of each items highest bid' do + @auction.add_item(@item1) + @auction.add_item(@item3) + @auction.add_item(@item4) + + @item1.add_bid(@attendee2, 20) + @item1.add_bid(@attendee1, 22) + @item3.add_bid(@attendee2, 15) + @item4.add_bid(@attendee3, 50) + + expect(@auction.potential_revenue).to eq(87) + end + end + + describe '#bidders' do + it 'returns an array of all the current bidders names' do + @auction.add_item(@item1) + @auction.add_item(@item3) + @auction.add_item(@item4) + + @item1.add_bid(@attendee2, 20) + @item1.add_bid(@attendee1, 22) + @item3.add_bid(@attendee2, 15) + @item4.add_bid(@attendee3, 50) + + expect(@auction.bidders).to contain_exactly('Megan', 'Bob', 'Mike') + end + end + + describe '#bidder_objects' do + it 'returns an array of all attendee objects that have a bid placed' do + @auction.add_item(@item1) + @auction.add_item(@item3) + @auction.add_item(@item4) + + @item1.add_bid(@attendee2, 20) + @item1.add_bid(@attendee1, 22) + @item3.add_bid(@attendee2, 15) + @item4.add_bid(@attendee3, 50) + + expect(@auction.bidder_objects).to contain_exactly(@attendee1, @attendee2, @attendee3) + end + end + + describe '#bidder_info' do + it 'returns a nested hash of bidder information' do + @auction.add_item(@item1) + @auction.add_item(@item3) + @auction.add_item(@item4) + + @item1.add_bid(@attendee2, 20) + @item1.add_bid(@attendee1, 22) + @item3.add_bid(@attendee2, 15) + @item4.add_bid(@attendee3, 50) + + bidder_info = @auction.bidder_info + + expect(bidder_info).to be_a(Hash) + bidder_info.each do |attendee, info| + expect(attendee).to be_a(Attendee) + expect(info).to be_a(Hash) + + expect(info.keys).to contain_exactly(:budget, :items) + expect(info[:budget]).to eq(attendee.budget) + expect(info[:items]).to be_an(Array) + + info[:items].each do |item| + expect(item).to be_an(Item) + bids = item.bids + bidders = bids.keys + expect(bidders.include?(attendee)).to be true + end + end + end + end end \ No newline at end of file diff --git a/spec/item_spec.rb b/spec/item_spec.rb index 4073cc4..a866ce3 100644 --- a/spec/item_spec.rb +++ b/spec/item_spec.rb @@ -4,6 +4,8 @@ before(:each) do @item1 = Item.new('Chalkware Piggy Bank') @item2 = Item.new('Bamboo Picture Frame') + @attendee1 = Attendee.new({name: 'Megan', budget: '$50'}) + @attendee2 = Attendee.new({name: 'Bob', budget: '$75'}) end it 'exists' do @@ -13,5 +15,44 @@ it 'has attributes' do expect(@item1.name).to eq("Chalkware Piggy Bank") expect(@item2.name).to eq("Bamboo Picture Frame") + expect(@item1.bids).to eq({}) + expect(@item2.bids).to eq({}) + expect(@item1.closed?).to be false + expect(@item2.closed?).to be false + end + + describe '#add_bid' do + it 'adds a bid to an item from a specific person' do + expect(@item1.bids).to eq({}) + + @item1.add_bid(@attendee2, 20) + @item1.add_bid(@attendee1, 22) + + expect(@item1.bids).to eq({@attendee2=>20, @attendee1=>22}) + end + end + + describe '#current_high_bid' do + it 'returns the current highest bid for the item' do + @item1.add_bid(@attendee2, 20) + @item1.add_bid(@attendee1, 22) + + expect(@item1.current_high_bid).to eq(22) + end + end + + describe '#close_bidding' do + it 'prevents future bids from being added once run' do + expect(@item1.closed?).to be false + + @item1.add_bid(@attendee2, 20) + expect(@item1.bids).to eq({@attendee2=>20}) + + @item1.close_bidding + expect(@item1.closed?).to be true + + @item1.add_bid(@attendee1, 22) + expect(@item1.bids).to eq({@attendee2=>20}) + end end end \ No newline at end of file