Skip to content

Instantly share code, notes, and snippets.

@caioertai
Created January 28, 2019 21:49
Show Gist options
  • Save caioertai/f7c9ae9275ebd2cbfe6c5360cd256a6f to your computer and use it in GitHub Desktop.
Save caioertai/f7c9ae9275ebd2cbfe6c5360cd256a6f to your computer and use it in GitHub Desktop.
# Items and prices
store_items = {
kiwi: 1.25,
banana: 0.5,
mango: 4.0,
asparagus: 9.0
}
# Create a shopping cart for the user
shopping_cart = Hash.new(0)
puts 'Welcome to Instacart'
# List the available items
puts "In our store today:"
store_items.each do |name, price|
puts "#{name}: R$#{price}"
end
# Loop asks adding items to cart.
loop do
puts "Which item? (or 'quit' to checkout)"
user_input = gets.chomp
# Breaks if user wants to 'quit'
break if user_input == 'quit'
puts "How many #{user_input} do you want to add?"
item_quantity = gets.chomp.to_i
# Increments the cart by the quantity decided by the user
shopping_cart[user_input.to_sym] += item_quantity
end
# Diplay the shop list
# And calculate the shopping_cart_price
shopping_cart_price = 0
puts "-------BILL---------"
shopping_cart.each do |name, quantity|
price = store_items[name]
total_price = price * quantity
shopping_cart_price += total_price
puts "#{name}: #{quantity} X R$#{price} = R$#{total_price}"
end
# Displays checkout
puts "TOTAL: R$#{shopping_cart_price}"
puts "--------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment