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

Shadeau Christensen #12

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
17 changes: 15 additions & 2 deletions lib/attendee.rb
Original file line number Diff line number Diff line change
@@ -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
33 changes: 31 additions & 2 deletions lib/auction.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'date'

class Auction
attr_reader :items

Expand All @@ -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
28 changes: 25 additions & 3 deletions lib/item.rb
Original file line number Diff line number Diff line change
@@ -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
87 changes: 85 additions & 2 deletions spec/auction_spec.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
21 changes: 21 additions & 0 deletions spec/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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