Created
July 11, 2017 16:10
-
-
Save luizfonseca/2ba5f55505ad2f40de054203cc71283d to your computer and use it in GitHub Desktop.
[Reboot] Instacart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1) print "Welcome to the Instacart" | |
# 2) Define the items on the store (Hashes) | |
# 3) Initialize an empty cart | |
# 3) Print the items | |
# 4) Ask the user for one item until he types "quit" | |
# 5) If the item is not on store, do something | |
# 8) if user types "quit", then Display the order (BILL) | |
# 9) Checkout. | |
# Our items on store | |
items = { | |
"apple" => 1.40, | |
"kiwi" => 2.00, | |
"orange" => 1.50, | |
"watermelon" => 6.00 | |
} | |
# Just pretty printing using LJUST (applies some spaces or leading zeros if you want) | |
items.each do |key, value| | |
puts "- #{key.ljust(20)} -------- $#{value}" | |
end | |
# My empty shop cart | |
cart = [] | |
# Default command when the program starts | |
user_choice = nil | |
# We run until the user types quit | |
until user_choice == "quit" | |
puts "Which item do you want to add? (type 'quit' to checkout)" | |
# kiwi, orange | |
user_choice = gets.chomp | |
# .keys => [Array of Keys] | |
# ["kiwi", "orange", "apples"].include?("kiwi") | |
if items.keys.include?(user_choice) | |
# if user_choice == "kiwi" | |
# items["kiwi"] => 2.0 | |
cart << items[user_choice] | |
end | |
end | |
# Print the checkout | |
# Check the documentation for REDUCE | |
# It reduce the enumeration (array in this case) to a common element, applying binary operations | |
# (we are summing everyone in the array, returning only the result of this sum, ok? | |
puts "The Bill is $#{cart.reduce(:+)}" | |
# -------------- END of script ------------- # | |
# puts 'Item' + ''.ljust(18) + '| Cost' + "\n\n" | |
# items.each do |key, value| | |
# puts "- #{key.ljust(20)}| $#{value}" | |
# end | |
# Fetch gets the VALUE of the key if he founds it | |
# Otherwise, he returns false. | |
# items.fetch("Kiwi", false) # Fetch => 2.00 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment