Skip to content

Instantly share code, notes, and snippets.

@MarcelSF
Created January 18, 2021 16:02
Show Gist options
  • Save MarcelSF/516fab313f2416426a35b048066743a9 to your computer and use it in GitHub Desktop.
Save MarcelSF/516fab313f2416426a35b048066743a9 to your computer and use it in GitHub Desktop.
# TODO: you can build your instacart program here!
# 1. Greet the user
puts "Welcome to the Batch 531 fruit store!"
# 2. Model the store as a hash
store = {"kiwi" => 2, "apple" => 0.1, "strawberry" => 5, "banana" => 1}
# 3. Display what's available in the store, and iterate over the key/value pairs
store.each do |fruit, value|
puts "#{fruit} - price: $#{value}"
end
# 4. Initialize an empty shopping cart as an array.
cart = []
product = '' # Dummy value, just to start the loop.
until product == 'quit'
# 5. Ask the user what they want to buy and store it in a variable
puts "What do you wish to buy? Type quit to checkout"
product = gets.chomp
# 6. Check if the product is actually in the store.
if store.key?(product)
# 7. If the product is available, add it to the cart
cart << product
else
puts "Sorry, whe don't have #{product} right now"
end
# 8. Ask the user if they want to keep buying or if they wish to checkout
end
# 9. When they type quit, display the bill
bill = 0
cart.each do |product|
bill += store[product]
end
puts "Your total bill is $#{bill}"
# 10. add a quantity -> how many would you like? This will involve extra steps, and you won't be able to just push a string to the cart.
# Think about which data structure allows you to pair 2 values together :)
# 11. calculate the price again, based on the quantity now
# 12. WARNING -> ADVANCED FEATURE. The last step would be to add STOCK into the equation. Each fruit would have a limited supply.
# 13. After the user has selected a quantity, check to see if that quantity is available.
# 14. If it's available, subtract that quantity from the stock.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment