Created
April 23, 2018 16:23
-
-
Save rodloboz/3209320d01d6154b3b4bce0df5edf355 to your computer and use it in GitHub Desktop.
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
# Create List of items (STORE) | |
# => Hash key: name of item, value: price of item (integer) | |
# Welcome user | |
# Show user list of of items | |
# LOOP (more choices) | |
#--- LOOP | |
# Ask user to pick item | |
# Get user choice | |
# validate that choice exists | |
#--- END LOOP | |
# Add item to cart (CART) => Hash similar to Store | |
# Ask user if they wish to continue | |
# END LOOP | |
# Display total amount | |
require "pry-byebug" | |
STORE = { | |
"kiwi" => 1.25, | |
"banana" => 0.5, | |
"mango" => 4, | |
"asparagus" => 9 | |
} | |
CART = {} | |
def show_items | |
STORE.each do |item, value| | |
puts "#{item}: #{value}€" | |
end | |
end | |
def checkout | |
puts "-------BILL---------" | |
CART.each do |item, hash| | |
puts "#{item}: #{hash[:price]}€ x #{hash[:quantity]}" | |
end | |
puts "TOTAL: #{total}€" | |
end | |
def total | |
# sum = 0 | |
# CART.each do |item, value| | |
# sum += value | |
# end | |
# sum | |
CART.values.reduce(0) do |acc, hash| | |
acc += hash[:price] * hash[:quantity] | |
end | |
end | |
def add_item_to_cart(item) | |
if CART.key?(item) | |
CART[item][:quantity] += 1 | |
else | |
CART[item] = { | |
price: STORE[item], | |
quantity: 1 | |
} | |
end | |
end | |
puts "Welcome to Instacart:" | |
puts "_____________________" | |
show_items | |
loop do | |
puts "Please pick an item:" | |
choice = gets.chomp | |
until STORE.key?(choice) | |
puts "Item doesn't exist." | |
puts "Pleace pick an item:" | |
choice = gets.chomp | |
end | |
add_item_to_cart(choice) | |
puts "Do you want to add another item? (quit to checkout):" | |
choice = gets.chomp | |
break if choice == "quit" | |
end | |
checkout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment