Created
January 28, 2019 21:38
-
-
Save caioertai/4ac8314dbb427867547f79f724c8b932 to your computer and use it in GitHub Desktop.
This file contains 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
puts '--------------------' | |
puts 'Welcome to Instacart' | |
puts '--------------------' | |
store_items = { | |
kiwi: 1.25, | |
banana: 0.5, | |
mango: 4.0, | |
asparagus: 9.0 | |
} | |
puts "In our store today:" | |
store_items.each do |name, price| | |
puts "#{name}: R$#{price}" | |
end | |
bill_total = Hash.new(0) | |
# Change cart to be a hash of { item: quantity } | |
# { kiwi: 4, mango: 5 } | |
loop do | |
puts "Which item? (or 'quit' to checkout)" | |
user_input = gets.chomp | |
break if user_input == 'quit' | |
item_price = store_items[user_input.to_sym] | |
# Ask the user for quantity | |
# print message for quantity | |
# get the quantity input | |
puts "How many #{user_input} do you want to add?" | |
item_quantity = gets.chomp.to_i | |
# increment the cart quantity for each item | |
bill_total[user_input.to_sym] += item_quantity | |
end | |
# Diplay the shop list | |
bill_total_price = 0 | |
puts "-------BILL---------" | |
bill_total.each do |name, quantity| | |
price = store_items[name] | |
total_price = price * quantity | |
bill_total_price += total_price | |
puts "#{name}: #{quantity} X R$#{price} = R$#{total_price}" | |
end | |
# Calculate the bill_total_price from cart before displaying checkout | |
puts "TOTAL: R$#{bill_total_price}" | |
puts "--------------------" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment