Skip to content

Instantly share code, notes, and snippets.

@stefbowerman
Created July 7, 2014 22:15
Show Gist options
  • Select an option

  • Save stefbowerman/11870e4c253f70ceca16 to your computer and use it in GitHub Desktop.

Select an option

Save stefbowerman/11870e4c253f70ceca16 to your computer and use it in GitHub Desktop.
Simple shopping cart class written in coffeescript
# Items have the following properties : ID (string), size (string), sizeDisplay (string), price (int)
class ShoppingCart
constructor: (@shipping = 5) ->
@items = []
_listItems: -> @items
addItem: (item) ->
@items.push item
$(window).trigger 'shopping-cart:item-added', [item]
removeItem: (index) ->
@items.splice index, 1
$(window).trigger 'shopping-cart:item-removed', [index]
calculateTotal: ->
total = 0
for i in [0...@items.length] by 1
total += @items[i].price
total + @shipping;
getTotal: -> do @calculateTotal
numItems: -> @items.length
getItem: (i) -> @items[i]
getItems: -> @items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment