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

Dustin Peukert #10

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
4d06b72
fix errors
DustinPeukert Nov 12, 2024
1e67b43
fix errors
DustinPeukert Nov 12, 2024
5e98bd1
fix error; add #item_names
DustinPeukert Nov 12, 2024
2d616d6
Merge pull request #1 from DustinPeukert/iteration_1
DustinPeukert Nov 12, 2024
a7de91a
add assertions for new attribute
DustinPeukert Nov 12, 2024
77c8ba6
add new attribute #bids
DustinPeukert Nov 12, 2024
a1cf04c
add test for #add_bid
DustinPeukert Nov 12, 2024
77b9242
write #add_bid
DustinPeukert Nov 12, 2024
e2ef799
writes test for #current_high_bid
DustinPeukert Nov 12, 2024
90f6909
write #current_high_bid method
DustinPeukert Nov 12, 2024
a9514db
add setup and write test for #unpopular_items
DustinPeukert Nov 12, 2024
8bdcb81
write #unpopular_items method
DustinPeukert Nov 12, 2024
268a0c5
write test for #potential_revenue method
DustinPeukert Nov 12, 2024
2a5d1e1
write #potential_revenue method
DustinPeukert Nov 12, 2024
5a6a78b
Merge pull request #2 from DustinPeukert/iteration_2
DustinPeukert Nov 12, 2024
e6e85c6
write test for #bidders method
DustinPeukert Nov 12, 2024
5dcd133
write #bidders method
DustinPeukert Nov 12, 2024
4731dc2
add test for #close_bidding
DustinPeukert Nov 12, 2024
e2eb80d
write #close_bidding method; add logic to #add_bid
DustinPeukert Nov 12, 2024
cac140c
write test for helper method #bidder_objects
DustinPeukert Nov 12, 2024
59fb296
write #bidder_objects helper method
DustinPeukert Nov 12, 2024
77bf0bf
write test for #bidder_info
DustinPeukert Nov 12, 2024
8bd8170
fix syntax error; swap useless logic to make test more robust
DustinPeukert Nov 12, 2024
9091b2b
write #bidder_info method
DustinPeukert Nov 12, 2024
36b6544
Merge pull request #3 from DustinPeukert/iteration_3
DustinPeukert Nov 12, 2024
01223af
write stub for Date.today and add assertion for date attribute
DustinPeukert Nov 12, 2024
6b3613f
fix typo
DustinPeukert Nov 12, 2024
3a5103e
add date attribute and write #date method
DustinPeukert Nov 12, 2024
0ff8777
Merge pull request #4 from DustinPeukert/iteration_4
DustinPeukert Nov 12, 2024
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('$').to_i
end
end
89 changes: 87 additions & 2 deletions lib/auction.rb
Original file line number Diff line number Diff line change
@@ -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
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, :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
103 changes: 103 additions & 0 deletions spec/auction_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
41 changes: 41 additions & 0 deletions spec/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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