module CartHelper def get_cart_info # retrieve cart info from session cart = session[:cart] item_on_cart = session[:item_on_cart] return cart, item_on_cart end # save the cart info into session def save_cart_info (cart, item_on_cart) session[:cart] = cart session[:item_on_cart] = item_on_cart end # clear the current shopping cart def clear_cart # TODO: Clear the current cart by remove its information # from the session end # add a new product to cart def add_product_to_cart (product_id) # retrieve cart info from session cart, item_on_cart = get_cart_info if item_on_cart.nil? item_on_cart = 0 end if cart.nil? cart = Hash.new(0) end # get the quatity of the product quatity = cart[product_id.to_s]; if quatity.nil? cart[product_id] = 1; else cart[product_id] = quatity + 1; end item_on_cart = item_on_cart + 1 # write back to session save_cart_info cart, item_on_cart end # remove a product from the cart def remove_product_from_cart (product_id) # TODO: Remove a product from the shopping cart # after removing the product, don't forget to update the # item_on_cart information # Hints: Hash.delete is a function to remove a key from # a hash object end end