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

Elysa Ward #7

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1e460ee
Got item_spec tests passing
elysableu Nov 12, 2024
16bee99
Correct auction to pass tests
elysableu Nov 12, 2024
e64a3ff
Correct attendee to pass tests
elysableu Nov 12, 2024
18d6f57
Add tests for item bids
elysableu Nov 12, 2024
f6ce45d
Add test for current_high_bid
elysableu Nov 12, 2024
85ca5b2
Update before(:each) block
elysableu Nov 12, 2024
6d3fe87
Add test for unpopular_items
elysableu Nov 12, 2024
0c2ed63
Add test for potential revenue
elysableu Nov 12, 2024
c2a6004
Add add_bid method and bids instance variable
elysableu Nov 12, 2024
72833f2
Add missing method calls in current_high_bid test
elysableu Nov 12, 2024
d5e0f59
Add current_high_bid method
elysableu Nov 12, 2024
194874b
Add missing require statement
elysableu Nov 12, 2024
41f381e
Add unpopular items method
elysableu Nov 12, 2024
d3c7529
Add potential_revenue method
elysableu Nov 12, 2024
9afff6a
Add tests for close_bidding
elysableu Nov 12, 2024
07e2b4b
Add close_bidding method and closed attribute
elysableu Nov 12, 2024
2018655
Adjust add_bid method to only take bids when bidding is not closed
elysableu Nov 12, 2024
e3b3335
Add test for bidders
elysableu Nov 12, 2024
61baf4b
Add test for bidder_info
elysableu Nov 12, 2024
23a68fd
Add bidders method
elysableu Nov 12, 2024
35dca47
Add test for helper method list_bidder_objects
elysableu Nov 12, 2024
e013c4e
Add list_bidder_objects helper method and adjust bidders to use this …
elysableu Nov 12, 2024
6c7a509
Add bidder_info method
elysableu Nov 12, 2024
6f6012e
Add tests for close_auction
elysableu Nov 12, 2024
c06ad34
Add forgotten it line
elysableu Nov 12, 2024
e271ba8
Add spend method test to handle changes in budget
elysableu Nov 12, 2024
932ad1a
Add spend method to handle budget changes
elysableu Nov 12, 2024
bd23456
adjust can_afford
elysableu Nov 12, 2024
25795ae
Add can_afford helper method
elysableu Nov 12, 2024
d8fa74f
Add close_auction method using can_afford helper method
elysableu 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
6 changes: 5 additions & 1 deletion lib/attendee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ class Attendee

def initialize(info)
@name = info[:name]
@budget = info[:budget]
@budget = info[:budget].split("$")[1].to_i
end

def spend(cost)
@budget -= cost
end
end
102 changes: 100 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,106 @@ 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.select do |item|
item.bids.empty?
end
unpopular
end

def potential_revenue
revenue = 0
@items.each do |item|
if !unpopular_items.include?(item)
revenue += item.current_high_bid
end
end
revenue
end

def bidders
bidders = list_bidder_objects

return bidder_names = bidders.map do |bidder|
bidder.name
end
end

def bidder_info
bidder_info = {}
bidder_objects = list_bidder_objects

bidder_objects.each do |bidder|
@items.each do |item|
if item.bids.keys.include?(bidder)
if !bidder_info.keys.include?(bidder)
bidder_info[bidder] = {budget: bidder.budget,
items: [item]}
else
bidder_info[bidder][:items] << item
end
end
end
end
bidder_info
end

# def date
# Date.today.to_s
# end

def close_auction
sales = {}
@items.each do |item|
item.close_bidding # Close bidding on all items

if item.bids.empty?
sales[item] = 'Not Sold'
else
item.bids.select do |bidder, bid|
if bid == item.current_high_bid && can_afford(bidder, bid)
sales[item] = bidder
else
sales[item] = 'Not Sold'
end
end
end
end
sales
end

# Helper Method that returns an array of the attendee objects that bid
def list_bidder_objects
bidder_objects = []

@items.each do |item|
item_bidders = item.bids.keys

item_bidders.each do |bidder|
if !bidder_objects.include?(bidder)
bidder_objects << bidder
end
end
end
bidder_objects
end

def can_afford(bidder, bid)
if bidder.budget >= bid
bidder.spend(bid)
true
else
false
end
end

end
29 changes: 26 additions & 3 deletions lib/item.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
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 add_bid(attendee, bid)
if @closed == false
bids[attendee] = bid
else
return "Bidding Closed!"
end
end

def current_high_bid
highest = @bids.max_by do |attendee, bid|
bid
end
highest[1]
end

def close_bidding
@closed = true

"Closed!"
end
end
5 changes: 5 additions & 0 deletions spec/attendee_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@
expect(@attendee.name).to eq('Megan')
expect(@attendee.budget).to eq(50)
end

it 'can spend budget' do
@attendee.spend(22)
expect(@attendee.budget).to eq(28)
end
end
163 changes: 163 additions & 0 deletions spec/auction_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
require './lib/item'
require './lib/attendee'
require './lib/auction'

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')

@attendee1 = Attendee.new({name: 'Megan', budget: '$50'})
@attendee2 = Attendee.new({name: 'Bob', budget: '$75'})
@attendee3 = Attendee.new({name: 'Mike', budget: '$100'})
@auction = Auction.new
end

Expand All @@ -29,4 +37,159 @@

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

# Iteration 2

describe "#unpopular/revenue" do
before(:each) do
@auction.add_item(@item1)
@auction.add_item(@item2)
@auction.add_item(@item3)
@auction.add_item(@item4)
@auction.add_item(@item5)
end

describe "#unpopular_items" do
it 'can return a list of unpopular items (no bids)' do
@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

describe "#potential_revenue" do
it 'can calculate the auctions potential revenue' do
@item1.add_bid(@attendee2, 20)
@item1.add_bid(@attendee1, 22) # Will only count highest bid
@item4.add_bid(@attendee3, 50)
@item3.add_bid(@attendee2, 15)

expect(@auction.potential_revenue).to eq(87)
end
end
end

# Iteration 3
describe "#bidders/bidder_info" do
before(:each) 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)
@item4.add_bid(@attendee3, 50)
@item3.add_bid(@attendee2, 15)
@item2.add_bid(@attendee1, 28)
end

describe "#bidders" do
it 'can return an Array of bidders names' do
expect(@auction.bidders).to eq(["Bob", "Megan", "Mike"])
end

describe "#list_bidder_objects" do
it 'can return a list of attendee objects that bid' do
expect(@auction.list_bidder_objects).to eq([@attendee2, @attendee1, @attendee3])
end
end
end

describe "#bidder_info" do
it 'can return a hash of bidders info' do
expect(@auction.bidder_info).to eq({
@attendee1 => {
budget: 50,
items: [@item1, @item2]
},
@attendee2 => {
budget: 75,
items: [@item1, @item3]
},
@attendee3 => {
budget: 100,
items: [@item4]
}
})
end
end
end

# Iteration 4

# describe "#date" do
# it 'can return the date of the auction' do
# test_date = double("any date")

# allow(test_date).to receive(:date).and_return("24/02/2020")
# expect(@auction.date).to eq("24/02/2020")
# end
# end

describe "#close_auction" do
before(:each) 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)
@item4.add_bid(@attendee3, 50)
@item3.add_bid(@attendee2, 15)
@item2.add_bid(@attendee1, 28)
end

it 'can close bidding on all items' do
@auction.close_auction

expect(@item1.closed).to eq true
expect(@item2.closed).to eq true
expect(@item3.closed).to eq true
expect(@item4.closed).to eq true
expect(@item5.closed).to eq true
end

it 'can return a hash of sold items' do
expect(@auction.close_auction).to eq({
@item1 => @attendee1,
@item2 => @attendee1,
@item3 => @attendee2,
@item4 => @attendee3,
@item5 => 'Not Sold'
})
end

it 'can only sell items if an attendee can afford the bid' do
@item5.add_bid(@attendee1, 15)

expect(@auction.close_auction).to eq({
@item1 => @attendee1,
@item2 => @attendee1,
@item3 => @attendee2,
@item4 => @attendee3,
@item5 => 'Not Sold'
})
end

# helper method to determine if an attendee can afford the bid
describe "#can_afford" do
it 'can determine if an attendee can afford a bid' do
expect(@auction.can_afford(@attendee1, 22)).to be true
expect(@auction.can_afford(@attendee1, 28)).to be true

@item5.add_bid(@attendee1, 15)
expect(@auction.can_afford(@attendee1, 15)).to be false
end
end
end
end
Loading