Class: TicketSales::TicketCenter

Inherits:
Object
  • Object
show all
Defined in:
lib/ticket_sales/ticket_center.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTicketCenter

Returns a new instance of TicketCenter.



23
24
25
26
# File 'lib/ticket_sales/ticket_center.rb', line 23

def initialize
  @tickets = TicketList.new
  @properties = {}
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



21
22
23
# File 'lib/ticket_sales/ticket_center.rb', line 21

def properties
  @properties
end

#ticketsObject (readonly)

Returns the value of attribute tickets.



21
22
23
# File 'lib/ticket_sales/ticket_center.rb', line 21

def tickets
  @tickets
end

Instance Method Details

#add(ticket, count = 1, promotion = Promotion::NoPromotion.new) ⇒ Object



28
29
30
31
# File 'lib/ticket_sales/ticket_center.rb', line 28

def add(ticket, count = 1, promotion = Promotion::NoPromotion.new)
  @tickets.add ticket
  @properties[ticket] = TicketProperty.new count, promotion
end

#available?(ticket, count = 1) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/ticket_sales/ticket_center.rb', line 33

def available?(ticket, count = 1)
  @tickets.include? ticket and enough_tickets? ticket, count
end

#most_bought_ticketsObject



47
48
49
# File 'lib/ticket_sales/ticket_center.rb', line 47

def most_bought_tickets
  @properties.sort_by { |ticket, property| -property.bought_count }.map(&:first)
end

#sell(user, ticket, count = 1) ⇒ Object

Raises:



37
38
39
40
41
42
43
44
45
# File 'lib/ticket_sales/ticket_center.rb', line 37

def sell(user, ticket, count = 1)
  raise UnavailableTicket.new unless available? ticket, count

  price = get_price ticket
  if user.can_pay? count * price
    count.times { user.buy ticket, price }
    decrease_count ticket, count
  end
end