Skip to content

Instantly share code, notes, and snippets.

@gabriel-dehan
Created October 14, 2014 08:25
Show Gist options
  • Select an option

  • Save gabriel-dehan/b74a6e92deac876a80e1 to your computer and use it in GitHub Desktop.

Select an option

Save gabriel-dehan/b74a6e92deac876a80e1 to your computer and use it in GitHub Desktop.
Shopping Cart Command Line Interface
require_relative "shopping"
# 1 - Elle doit proposer une liste d'articles avec leurs prix
puts "Welcome to The Wagon Market place !"
puts "-------"
MARKET.each do |name, price|
puts "#{name}: #{price} euros"
end
puts ''
# 2 - Elle doit demander a l'utilisateur de choisir des articles
# 3 - Lorsque l'utilisateur choisi un article, il est ajoute a son panier
begin
puts "Please choose an item"
item_choice = gets.chomp
if !item_choice.empty?
puts "Please choose a quantity"
quantity_choice = gets.chomp.to_i
add_to_cart(item_choice, quantity_choice)
end
end while item_choice != ''
# puts "Please choose an item"
# item_choice = gets.chomp
# if !item_choice.empty?
# puts "Please choose a quantity"
# quantity_choice = gets.chomp
# add_to_cart(item_choice, quantity_choice)
# end
# while item_choice != ''
# puts "Please choose an item"
# item_choice = gets.chomp
# if !item_choice.empty?
# puts "Please choose a quantity"
# quantity_choice = gets.chomp
# add_to_cart(item_choice, quantity_choice)
# end
#end
# Optionel : Pour chaque article il y a une quantite
# 4 - Finalement on affiche le ticket de caisse avec :
# - Nom de l'article
# - Quantite
# - La somme totale HT
# - La somme totale TTC
puts "----MONOPRIX----"
puts ""
display_bill
MARKET = {
"t-shirt" => 35,
"jupe" => 35,
"jeans" => 20,
"baskets" => 50
}
SHOPPING_CART = []
def add_to_cart(article, quantity)
# Si article est contenu dans MARKET
if MARKET[article]
articles = [article] * quantity
SHOPPING_CART << articles
SHOPPING_CART.flatten!
else
puts "This article does not exists."
end
end
def display_bill
SHOPPING_CART.each do |item|
puts "#{item} : #{MARKET[item]}"
end
total_ht = compute_bill_total
puts "TOTAL H.T: #{total_ht} euros"
puts "TOTAL T.T.C: #{total_ht * 1.2} euros"
end
def compute_bill_total
sum = 0
SHOPPING_CART.each do |item|
sum += MARKET[item]
end
sum
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment