Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. class Cart
  2. def initialize(items, price, recipes)
  3. @items=[]
  4. @price=[]
  5. @recipes=[]
  6. end
  7.  
  8. def add_item
  9. puts 'Would you like to add a new item?(y/n)'
  10. add=gets.chomp
  11. if add=='y'
  12. puts 'What would you like to add to your cart?'
  13. new_item=gets.chomp
  14. @items=@items.push(new_item)
  15. puts 'How much is it?'
  16. item_price=gets.chomp
  17. @price=@price.push(item_price.to_f)
  18. total=@price.inject{|sum,x| sum + x}
  19. puts 'is this item part of a recipe? (y/n)'
  20. recipe_add=gets.chomp
  21. if recipe_add=='y'
  22. puts 'what recipe is this a part of?'
  23. recipe=gets.chomp
  24. @recipes=@recipes.push(recipe, @items.pop)
  25. else
  26. add_item
  27.  
  28. end
  29.  
  30. add_item
  31. else
  32. checkout
  33.  
  34.  
  35. end
  36. end
  37. def checkout
  38. puts 'Are you ready to checkout? (y/n)'
  39. cart_checkout=gets.chomp
  40. if cart_checkout=='y'
  41. puts 'Your shopping cart:'
  42. puts @items
  43. puts total=@price.inject{|sum,x| sum + x}
  44. puts 'your total: '+ total.to_s
  45. else
  46. puts 'would you like to see your recipes?(y/n)'
  47. see_recipe=gets.chomp
  48. if see_recipe=='y'
  49. puts @recipes
  50. checkout
  51.  
  52. end
  53. add_item
  54. end
  55.  
  56. end
  57. end
  58.  
  59.  
  60. cart=Cart.new([], [], [])
  61. cart.add_item
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement