Created
July 11, 2016 19:49
-
-
Save RobertoBarros/f8631e1861b3397f4538a182cb1f8fe0 to your computer and use it in GitHub Desktop.
instacart
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
def validate_item_exist?(products, item) | |
!products[item].nil? | |
end | |
def validate_item_stock?(products, item, quant) | |
stock = products[item].first | |
return stock >= quant | |
end | |
def add_item_cart(carrinho, item, quant) | |
carrinho << [item, quant] | |
carrinho | |
end |
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
# cadastrar os itens e quantidades e preço no estoque | |
# listar os itens disponiveis p compra | |
# perguntar qual item e quantidade | |
# colocar os itens e quantidades no carrinho | |
# calcular o preço total | |
# fazer controle de estoque | |
require_relative "carrinho" | |
# 2 - listar itens | |
produtos = { | |
"banana" => [12, 4.00], | |
"laranja" => [36, 5.00], | |
"limao" => [24, 3.00], | |
"pera" => [16, 6.00], | |
"goiaba" => [48, 5.00] | |
} | |
# 3 - mostrar itens disponiveis | |
puts "Lista de itens disponiveis" | |
produtos.each do |fruta, quantidade_valor| | |
puts "#{fruta} custa #{quantidade_valor.last} e temos #{quantidade_valor.first}" | |
end | |
carrinho = [] | |
loop do | |
puts "qual item" | |
item = gets.chomp | |
break if item == "" | |
if validate_item_exist?(produtos, item) | |
p "produto encontrado" | |
else | |
p "produto indisponível" | |
next | |
end | |
puts "qual a quantidade?" | |
quantidade = gets.chomp.to_i | |
if validate_item_stock?(produtos,item,quantidade) | |
p "Temos em estoque" | |
else | |
p "Não temos essa quantidade em estoque" | |
end | |
carrinho = add_item_cart(carrinho,item, quantidade) | |
end | |
total = 0 | |
carrinho.each do |item_carrinho| | |
item = item_carrinho.first | |
quantidade = item_carrinho.last | |
custo_unitario = produtos[item].last | |
sub_total = quantidade * custo_unitario | |
total = total + sub_total | |
puts "voce comprou #{quantidade} #{item} com custo unitario e #{custo_unitario} de um sub total #{sub_total}" | |
end | |
puts "custo total #{total}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment