Class: TicketSales::TicketCenter
- Inherits:
-
Object
- Object
- TicketSales::TicketCenter
- Defined in:
- lib/ticket_sales/ticket_center.rb
Instance Attribute Summary collapse
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
-
#tickets ⇒ Object
readonly
Returns the value of attribute tickets.
Instance Method Summary collapse
- #add(ticket, count = 1, promotion = Promotion::NoPromotion.new) ⇒ Object
- #available?(ticket, count = 1) ⇒ Boolean
-
#initialize ⇒ TicketCenter
constructor
A new instance of TicketCenter.
- #most_bought_tickets ⇒ Object
- #sell(user, ticket, count = 1) ⇒ Object
Constructor Details
#initialize ⇒ TicketCenter
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
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
21 22 23 |
# File 'lib/ticket_sales/ticket_center.rb', line 21 def properties @properties end |
#tickets ⇒ Object (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
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_tickets ⇒ Object
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
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 |