Created
July 28, 2025 19:22
-
-
Save RobertoBarros/05fa6d2b58bd245ec172652fce435ad7 to your computer and use it in GitHub Desktop.
batch 2100 - Reboot - Instacart - Part 2
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
| # mensagem de boas vindas | |
| puts "-" * 30 | |
| puts "Welcome to Instacart" | |
| puts "-" * 30 | |
| store = { | |
| "kiwi" => 1.25, | |
| "banana" => 0.5, | |
| "mango" => 4, | |
| "asparagus" => 9 | |
| } | |
| # Apresentar os produtos com o respectivo preço | |
| puts "In our store today" | |
| store.each do |item, price| | |
| puts "#{item}: #{price}€" | |
| end | |
| puts "-" * 30 | |
| cart = [] | |
| # LOOP até o `quit` | |
| loop do | |
| # Perguntar o produto ou `quit` | |
| puts "Which item? (or 'quit' to checkout)" | |
| product = gets.chomp.downcase | |
| break if product == "quit" | |
| if store.key?(product) | |
| # Perguntar a quantidade | |
| puts "How many ?" | |
| quantity = gets.chomp.to_i | |
| # Adicionar ao carrinho produto e quantidade | |
| # cart é um array de hashes no formato: | |
| # [{name: "kiwi", quantity: 2},{name: "mango", quantity: 5},{name: "banana", quantity: 3}] | |
| cart << {name: product, quantity: quantity } | |
| else | |
| # Mostrar erro se produto inexistente | |
| puts "sorry, we don't have #{product} today" | |
| end | |
| # FIM DO LOOP | |
| end | |
| # Somar os valores dos produtos no carrinho | |
| sum = 0 | |
| puts ("-" * 10) + "Bill" + ("-" * 10) | |
| # product é um hash no formato {name: "kiwi", quantity: 2} | |
| cart.each do |product| | |
| name = product[:name] | |
| price = store[name] | |
| quantity = product[:quantity] | |
| subtotal = price * quantity | |
| sum += subtotal | |
| # Mostrar o subtotal no formato: kiwi: 2 X 1.25€ = 2.5€ | |
| puts "#{name}: #{quantity} x #{price}€ = #{subtotal}€" | |
| end | |
| # Mostrar o total | |
| puts "TOTAL: #{sum}€" | |
| puts "-" * 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment