Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Cart
- def initialize(items, price, recipes)
- @items=[]
- @price=[]
- @recipes=[]
- end
- def add_item
- puts 'Would you like to add a new item?(y/n)'
- add=gets.chomp
- if add=='y'
- puts 'What would you like to add to your cart?'
- new_item=gets.chomp
- @items=@items.push(new_item)
- puts 'How much is it?'
- item_price=gets.chomp
- @price=@price.push(item_price.to_f)
- total=@price.inject{|sum,x| sum + x}
- puts 'is this item part of a recipe? (y/n)'
- recipe_add=gets.chomp
- if recipe_add=='y'
- puts 'what recipe is this a part of?'
- recipe=gets.chomp
- @recipes=@recipes.push(recipe, @items.pop)
- else
- add_item
- end
- add_item
- else
- checkout
- end
- end
- def checkout
- puts 'Are you ready to checkout? (y/n)'
- cart_checkout=gets.chomp
- if cart_checkout=='y'
- puts 'Your shopping cart:'
- puts @items
- puts total=@price.inject{|sum,x| sum + x}
- puts 'your total: '+ total.to_s
- else
- puts 'would you like to see your recipes?(y/n)'
- see_recipe=gets.chomp
- if see_recipe=='y'
- puts @recipes
- checkout
- end
- add_item
- end
- end
- end
- cart=Cart.new([], [], [])
- cart.add_item
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement