Last active
October 13, 2015 17:22
-
-
Save volpeo/743752c4b8505bfacd5e to your computer and use it in GitHub Desktop.
Shopping Cart
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
$catalog = [ | |
{ | |
name: "spidercochon", | |
price: 20, | |
stock: 0, | |
ref: "001" | |
}, | |
{ | |
name: "superslip", | |
price: 20, | |
stock: 1, | |
ref: "002" | |
}, | |
{ | |
name: "xnikitaBellucci", | |
price: 15, | |
stock: 2, | |
ref: "003" | |
}, | |
{ | |
name: "xena", | |
price: 5, | |
stock: 25, | |
ref: "004" | |
}, | |
{ | |
name: "dora l'exploratrice", | |
price: 5, | |
stock: 25, | |
ref: "005" | |
}, | |
{ | |
name: "Unkut", | |
price: 10, | |
stock: 25, | |
ref: "006" | |
} | |
] | |
puts "Bienvenue sur votre gestionnaire de catalogue" | |
puts "Tapez m pour accéder au menu" | |
answer = "" | |
cart = [] | |
while answer != "q" | |
print ">" | |
answer = gets.chomp | |
case answer | |
when "m" | |
puts " a - ajouter un tee-shirt au panier" | |
puts " b - afficher le panier" | |
puts " p - imprimer un reçu" | |
puts " h - aide" | |
puts " l - afficher le catalogue" | |
puts " q - quitter" | |
puts " d - vider le panier" | |
when "a" | |
puts " Renseigner l'identifiant du teeshirt" | |
print "réf>" | |
ref = gets.chomp | |
found = $catalog.find { |teeshirt| teeshirt[:ref] == ref } | |
if found | |
if found[:stock] > 0 | |
cart << found | |
#arr.each_index.select{|i| arr[i] == 'x'} | |
# trouver l'index de l'element found dans $catalog | |
index = $catalog.index(found) | |
# parcourir $catalog pour trouver l'élément | |
$catalog[index][:stock] -= 1 | |
# enlever 1 du stock | |
else | |
puts "article epuisé" | |
end | |
else | |
puts "reférence non trouvé" | |
end | |
when "b" | |
puts "votre panier contient #{cart.count} article(s)" | |
cart.each do |item| | |
puts "réf:#{item[:ref]} - #{item[:name]} : #{item[:price]} $" | |
end | |
when "d" | |
cart = [] | |
puts "Votre panier est vide" | |
when "p" | |
puts " Votre facture :" | |
cart.each do |item| | |
puts "réf:#{item[:ref]} - #{item[:name]} : #{item[:price]} $" | |
end | |
sum = cart.reduce(0) do |sum, item| | |
sum + item[:price] | |
end | |
puts "Votre total est de #{sum} $" | |
when "l" | |
$catalog.each_with_index do |teeshirt, index| | |
puts "#{index + 1} - #{teeshirt[:name]} : #{teeshirt[:price]} $ - ref:#{teeshirt[:ref]} - stock: #{teeshirt[:stock]}" | |
end | |
when "q" | |
puts "Merci d'avoir utilisé le gestionnnaire" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment