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

Patrick Shim Final IC #8

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
3 changes: 2 additions & 1 deletion lib/attendee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Attendee

def initialize(info)
@name = info[:name]
@budget = info[:budget]
@budget = info[:budget].delete('$').to_i
@items = []
end
end
82 changes: 79 additions & 3 deletions lib/auction.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,91 @@
require 'pry'

class Auction
attr_reader :items
attr_reader :items, :item_names

def initialize
@items = []
@item_names = []
@bids = {}
end

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

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

def unpopular_items
unpopular_items_array = []

@items.each do |item|
if item.bids.empty?
unpopular_items_array << item
end
end
unpopular_items_array
end

def bidders
bidders = []
@items.each do |item|
if !item.bids.empty?
item.bids.each do |bid|
bidders << bid[0].name
end
end
end
bidders
end

def bidder_info
hash = {}
@items.each do |item|
if !item.bids.empty?
item.bids.each do |bidder, amount|
if hash[bidder]
subhash = hash[bidder]
else
subhash = {}
subhash[:budget] = bidder.budget
subhash[:items] = []
end
subhash[:items] << item
hash[bidder] = subhash
end
end
end
hash
end

def date
@date = Date.today.strftime("%d/%m/%Y")
end

def close_auction
hash = {}
@items.each do |item|
item.close_bidding
item.bids.each do |bidder|
if bidder[0].budget > bidder[1]
end
end
end
hash
end

def potential_revenue
total = 0
@items.each do |item|
if !item.bids.empty?
total += item.bids.values.max
end
end
total
end
end
24 changes: 21 additions & 3 deletions lib/item.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
class Items
attr_reader :name
class Item
attr_reader :name, :bids

def initialize(name)
@name = "Chalkware Piggy Bank"
@name = name
@bids = {}
@close_bidding = false
end

def add_bid(attendee, bid)
if @close_bidding == false
@bids[attendee] = bid
else
return "Bidding time is over"
end
end

def current_high_bid
@bids.values.max
end

def close_bidding
@close_bidding = true
end
end
3 changes: 3 additions & 0 deletions spec/attendee_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require './lib/attendee'
require './lib/auction'
require './lib/attendee'
require 'pry'

RSpec.describe Attendee do
before(:each) do
Expand Down
135 changes: 135 additions & 0 deletions spec/auction_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
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')
@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 +36,132 @@

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

it 'can add_bid to the bids hash and return the hash' do
@auction.add_item(@item1)
@auction.add_item(@item2)
@auction.add_item(@item3)
@auction.add_item(@item4)
@auction.add_item(@item5)

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

it 'can return unpopular_items - no bids' do
@auction.add_item(@item1)
@auction.add_item(@item2)
@auction.add_item(@item3)
@auction.add_item(@item4)
@auction.add_item(@item5)

expect(@item1.bids).to eq({})

@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(@attendee3, 50)

expect(@auction.unpopular_items).to eq([@item2, @item5])
end

it 'can return unpopular_items - no bids' do
@auction.add_item(@item1)
@auction.add_item(@item2)
@auction.add_item(@item3)
@auction.add_item(@item4)
@auction.add_item(@item5)

expect(@item1.bids).to eq({})

@item1.add_bid(@attendee1, 22)
@item1.add_bid(@attendee2, 20)
@item4.add_bid(@attendee3, 50)

expect(@auction.bidders).to eq(["Megan", "Bob", "Mike"])
end

it 'can return key of Attendee object pointing to a value of a Hash.
Subhash key of :budget(symbol) => budget(integer)
:items => items the attendee has bid on(array)' do

@auction.add_item(@item1)
@auction.add_item(@item2)
@auction.add_item(@item3)
@auction.add_item(@item4)
@auction.add_item(@item5)

expect(@item1.bids).to eq({})

@item1.add_bid(@attendee1, 22)
@item1.add_bid(@attendee2, 20)
@item2.add_bid(@attendee2, 20)
@item2.add_bid(@attendee3, 50)
@item4.add_bid(@attendee3, 50)

expect(@auction.bidder_info).to eq({
@attendee1 => {:budget => 50, :items => [@item1]},
@attendee2 => {:budget => 75, :items => [@item1, @item2]},
@attendee3 => {:budget => 100, :items => [@item2, @item4]}
})
end

it "can return date of aution in dd/mm/yyyy" do
allow(Date).to receive(:today).and_return Date.new(2020, 2, 24)
expect(@auction.date).to eq("24/02/2020")

allow(Date).to receive(:today).and_return Date.new(1991, 8, 17)
expect(@auction.date).to eq("17/08/1991")

allow(Date).to receive(:today).and_return Date.new(1964, 11, 05)
expect(@auction.date).to eq("05/11/1964")

allow(Date).to receive(:today).and_return Date.new(2092, 2, 24)
expect(@auction.date).to eq("24/02/2092")
end

it "can return #auction.potential_revenue: is the sum of each item's highest bid." do
@auction.add_item(@item1)
@auction.add_item(@item2)
@auction.add_item(@item3)
@auction.add_item(@item4)
@auction.add_item(@item5)

expect(@item1.bids).to eq({})

@item1.add_bid(@attendee2, 20)
@item1.add_bid(@attendee1, 22)

expect(@item1.bids).to eq({
@attendee2 => 20,
@attendee1 => 22
})

expect(@item1.current_high_bid).to eq(22)

@item4.add_bid(@attendee3, 50)
@item3.add_bid(@attendee2, 15)

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

xit 'can Hash with each Item object as a key pointing to a value of the Attendee object who won the item.' do
@auction.add_item(@item1)
@auction.add_item(@item2)
@auction.add_item(@item3)
@auction.add_item(@item4)
@auction.add_item(@item5)

expect(@item1.bids).to eq({})

@item1.add_bid(@attendee1, 22)
@item1.add_bid(@attendee2, 20)
@item2.add_bid(@attendee2, 20)
@item2.add_bid(@attendee3, 50)
@item4.add_bid(@attendee3, 50)

expect(@auction.close_auction).to eq()
end
end
46 changes: 46 additions & 0 deletions spec/item_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,63 @@
require './lib/item'
require './lib/auction'
require './lib/attendee'

RSpec.describe Item 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'})
end

it 'exists' do
expect(@item1).to be_a(Item)
expect(@item2).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

it 'can add items to item bids' 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

it 'can get the current high bid' do
expect(@item1.bids).to eq({})

@item1.add_bid(@attendee2, 20)
@item1.add_bid(@attendee1, 22)

expect(@item1.current_high_bid).to eq(22)
end

it 'no longer accepts new bid once bidding has closed' 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
})

@item1.close_bidding

expect(@item1.add_bid(@attendee1, 22)).to eq("Bidding time is over")
end
end
Empty file added true
Empty file.