Skip to content

Instantly share code, notes, and snippets.

@Papillard
Last active October 23, 2017 16:02
Show Gist options
  • Save Papillard/92beccf5b1e8e7007eb7c3e3b3eb5f64 to your computer and use it in GitHub Desktop.
Save Papillard/92beccf5b1e8e7007eb7c3e3b3eb5f64 to your computer and use it in GitHub Desktop.
InstaCart - Reboot batch #100
cart = {}
items = {
"pomme" => {price: 3.5, stock: 100},
"orange" => {price: 1.5, stock: 20},
"quinoa" => {price: 5.0, stock: 30},
"pain" => {price: 1.2, stock: 10}
}
# Boucle de prise de commande
while true
puts "Les produits disponibles sont:"
items.each do |name, infos|
puts "> #{name} (€#{infos[:price]}) - #{infos[:stock]} dispos"
end
puts "Quel produit veux-tu ajouter? 'exit' pour sortir."
user_choice = gets.chomp
if user_choice == "exit"
break
elsif items.has_key? user_choice
puts "Combien en veux-tu ?"
quantity = gets.chomp.to_i
available_stock = items[user_choice][:stock]
# Check if quantity available and ask user for correction
while quantity > available_stock
puts "Juste #{available_stock} #{user_choice}s disponibles. Combien en veux-tu ?"
quantity = gets.chomp.to_i
end
# Add to cart
if cart.has_key? user_choice
cart[user_choice] += quantity
else
cart[user_choice] = quantity
end
# Decrement stock
items[user_choice][:stock] -= quantity
else
puts "Invalid choice."
end
end
# Calcul du ticket de caisse
puts "-" * 20
sum = 0
cart.each do |name, quantity|
price = items[name][:price]
subtotal = price * quantity
puts "- #{quantity} #{name} (#{price}€ each) => #{subtotal}€"
sum += subtotal
end
puts "=> TOTAL: #{sum}€"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment