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

Matt Haefling #3

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
2 changes: 1 addition & 1 deletion lib/attendee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ class Attendee

def initialize(info)
@name = info[:name]
@budget = info[:budget]
@budget = info[:budget].delete("^0-9").to_i
end
end
54 changes: 52 additions & 2 deletions lib/auction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,60 @@ def initialize
end

def add_item(item)
@items.unshift(item)
@items << item
end

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

def unpopular_items
unpopular = []
@items.each do |item|
if item.bids == {}
unpopular << item
end
end
unpopular
end

def potential_revenue
revenue = 0
@items.each do |item|
high_bid = item.current_high_bid
if high_bid != nil
revenue += high_bid
end
end
revenue
end

def bidders
bidder_names = []
@items.each do |item|
item.bids.each do |name|
if bidder_names.include?(name[0].name)
next
else
bidder_names << name[0].name
end
end
end
bidder_names
end

def bidder_info
bidder_info = Hash.new do |bidder_info, attendee|
bidder_info[attendee] = { budget: attendee.budget, items: [] }
end
@items.each do |item|
item.bids.each_key do |attendee|
bidder_info[attendee][:items] << item
end
end
# require 'pry'; binding.pry
bidder_info
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 = {}
@closed = false
end

def add_bid(attendee, bid)
if @closed == false
@bids[attendee] = bid
else
puts "Auction has closed for this time"
end
end

def current_high_bid
high_bid = []
@bids.each do |bid|
high_bid << bid[1]
end
high_bid.max
end

def close_bidding
@closed = true
end
end
133 changes: 121 additions & 12 deletions spec/auction_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require './lib/item'
require './lib/attendee'
require './lib/auction'

RSpec.describe Auction do
Expand All @@ -8,25 +9,133 @@
@auction = Auction.new
end

it 'exists' do
expect(@auction).to be_a(Auction)
describe "#initialize" do
it 'exists' do
expect(@auction).to be_a(Auction)
end

it 'has attributes' do
expect(@auction.items).to eq([])
end
end

describe "#add_item" do
it 'add_item' do
@auction.add_item(@item1)
@auction.add_item(@item2)

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

describe "#item_names" do
it 'item_names' do
@auction.add_item(@item1)
@auction.add_item(@item2)

expect(@auction.item_names).to eq(["Chalkware Piggy Bank", "Bamboo Picture Frame"])
end
end

it 'has attributes' do
expect(@auction.items).to eq([])
describe "#unpopular_items" do
it 'returns a list of items with no bids' do
item3 = Item.new('Homemade Chocolate Chip Cookies')
item4 = Item.new('2 Days Dogsitting')
item5 = Item.new('Forever Stamps')
attendee1 = Attendee.new({name: 'Megan', budget: '$50'})
attendee2 = Attendee.new({name: 'Bob', budget: '$75'})
attendee3 = Attendee.new({name: 'Mike', budget: '$100'})
@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])
end
end

it 'add_item' do
@auction.add_item(@item1)
@auction.add_item(@item2)
describe "#potential_revenue" do
it 'returns the sum of each items highest bid' do
item3 = Item.new('Homemade Chocolate Chip Cookies')
item4 = Item.new('2 Days Dogsitting')
item5 = Item.new('Forever Stamps')
attendee1 = Attendee.new({name: 'Megan', budget: '$50'})
attendee2 = Attendee.new({name: 'Bob', budget: '$75'})
attendee3 = Attendee.new({name: 'Mike', budget: '$100'})
@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)
item3.add_bid(attendee2, 15)
expect(@auction.potential_revenue).to eq(87)
end
end

expect(@auction.items).to eq([@item1, @item2])
describe "#bidders" do
it 'returns a list of bidders name in an array with string values' do
item3 = Item.new('Homemade Chocolate Chip Cookies')
item4 = Item.new('2 Days Dogsitting')
item5 = Item.new('Forever Stamps')
attendee1 = Attendee.new({name: 'Megan', budget: '$50'})
attendee2 = Attendee.new({name: 'Bob', budget: '$75'})
attendee3 = Attendee.new({name: 'Mike', budget: '$100'})
@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)
item3.add_bid(attendee2, 15)
expect(@auction.bidders).to eq(["Bob", "Megan", "Mike"])
end
end

it 'item_names' do
@auction.add_item(@item1)
@auction.add_item(@item2)
describe "#bidder_info" do
it 'returns a hash value of attendes objects as keys with nested hash values of their budget and items bid on' do
item3 = Item.new('Homemade Chocolate Chip Cookies')
item4 = Item.new('2 Days Dogsitting')
item5 = Item.new('Forever Stamps')
attendee1 = Attendee.new({name: 'Megan', budget: '$50'})
attendee2 = Attendee.new({name: 'Bob', budget: '$75'})
attendee3 = Attendee.new({name: 'Mike', budget: '$100'})
@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)
item3.add_bid(attendee2, 15)

expect(@auction.item_names).to eq(["Chalkware Piggy Bank", "Bamboo Picture Frame"])
expected = {
attendee1 => {
:budget => 50,
:items => [@item1]
},
attendee2 => {
:budget => 75,
:items => [@item1, item3]
},
attendee3 => {
:budget => 100,
:items => [item4]
}
}

expect(@auction.bidder_info).to eq(expected)
end
end

end
52 changes: 47 additions & 5 deletions spec/item_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,59 @@
require './lib/item'
require './lib/attendee'

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

it 'exists' do
expect(@item1).to be_a(Item)
describe "#initialize" do
it 'exists' do
expect(@item1).to be_a(Item)
end

it 'has attributes' do
expect(@item1.name).to eq("Chalkware Piggy Bank")
expect(@item2.name).to eq("Bamboo Picture Frame")
end
end

describe "#bids" do
it 'keeps track of bids on an item' do
expect(@item1.bids).to eq({})
end
end

describe "#add_bid" do
it 'allows attendees to place bids on an item' do
attendee1 = Attendee.new({name: 'Megan', budget: '$50'})
attendee2 = Attendee.new({name: 'Bob', budget: '$75'})
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 of an item' do
attendee1 = Attendee.new({name: 'Megan', budget: '$50'})
attendee2 = Attendee.new({name: 'Bob', budget: '$75'})
@item1.add_bid(attendee2, 20)
@item1.add_bid(attendee1, 22)
expect(@item1.current_high_bid).to eq(22)
end
end

it 'has attributes' do
expect(@item1.name).to eq("Chalkware Piggy Bank")
expect(@item2.name).to eq("Bamboo Picture Frame")
describe "#close_bidding" do
it 'confirms that if bidding on an item is closed, no more bids are accepted' do
attendee1 = Attendee.new({name: 'Megan', budget: '$50'})
attendee2 = Attendee.new({name: 'Bob', budget: '$75'})
expect(@item1.bids).to eq({})
@item1.add_bid(attendee2, 20)
@item1.close_bidding
@item1.add_bid(attendee1, 22)
expect(@item1.bids).to eq({attendee2 => 20})
end
end
end