Skip to content

Instantly share code, notes, and snippets.

@caioertai
Created April 19, 2022 16:05
Show Gist options
  • Save caioertai/21fdfdc3ce7ec8794885b3aee20d7582 to your computer and use it in GitHub Desktop.
Save caioertai/21fdfdc3ce7ec8794885b3aee20d7582 to your computer and use it in GitHub Desktop.
# - Create and assign to a variable, the store items
# fruit price
store_items = { kiwi: 1.25, apple: 9, coconut: 0.5 }
# - Create a cart
cart = { kiwi: 0, apple: 0, coconut: 0 }
puts "----------------------"
puts " Welcome to Instacart "
puts "----------------------"
# - Print the store items
# key value
store_items.each do |fruit, price|
puts "#{fruit} ... $#{price}"
end
puts "--------------------"
loop do
# - ask the user to add an item or quit
puts "Which item? (or 'quit' to checkout)"
user_input = gets.chomp
if user_input == "quit" || user_input == "q"
puts "Thank you. Proceed to checkout."
break
else
puts "How many you want to add?"
user_quantity = gets.chomp.to_i
cart[user_input.to_sym] += user_quantity
end
end
puts "--------------BILL----------------"
bill_sum = 0
cart.each do |fruit, quantity|
price = store_items[fruit]
subtotal = quantity * price
bill_sum += subtotal
puts "#{fruit}: #{quantity} X $#{price} = $#{subtotal}"
end
puts " TOTAL: $#{bill_sum}"
puts "----------------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment