Skip to content

Instantly share code, notes, and snippets.

@celeen
Last active October 7, 2015 04:11
Show Gist options
  • Save celeen/674f0208a878bf8f5e97 to your computer and use it in GitHub Desktop.
Save celeen/674f0208a878bf8f5e97 to your computer and use it in GitHub Desktop.
# Create a new list
# Add an item with a quantity to the list
# Remove an item from the list
# Update quantities for items in your list
# Print the list (Consider how to make it look nice!)
# DEFINE 'create_list' which takes no arguments:
# create/return a new, empty, hash
#
# DEFINE 'add', which takes three arguments, item (a string) and quantity (an integer), and list (a hash):
# add item as a key and quantity as a value pair to the list
#
# DEFINE 'remove', which takes two arguments, an item (string), and a list (hash):
# delete item key/value pair from hash
#
# DEFINE 'update', which takes three arguments, an item (string), a new quantity (integer) and a list(hash):
# reassign the key (item) with the value (the new quantity)
#
# DEFINE print_list, which takes one argument, a hash
# iterate over the hash, for each key/value pair DO the following:
# print the key, then a hyphen surrounded by spaces, then the value, then a newline
#
def create_list
Hash.new
end
def add(item, quantity, list)
if list[item]
"#{item.upcase} is already on the list. It says you need #{list[item]}."
else
list[item] = quantity
end
end
def remove(item, list)
list.delete(item)
end
def update(item, quantity, list)
list[item] = quantity
end
def print_list(list)
if list.empty?
puts "There's nothing here yet!"
end
list.each do |item, quantity|
puts "#{item.upcase} - #{quantity}"
end
puts ""
end
def help
"You can use any of the following commands on your list!
When you make the command, fill in the bracketed arguments with specifics.
ex: 'add 5 turnips'
Remember to separate each word by (only) one space.
add [number] [item]
remove [item]
update [item] to [quantity]
help
quit
"
end
def run
puts "Hello! Welcome to Grocery List v2.0"
user_list = create_list
puts "We have created a grocery list for you. Type 'help' to see a list of available commands."
user_input = ""
while user_input[0] != "quit"
puts "What can I do for you?"
user_input = gets.chomp.split(" ")
puts ""
case user_input[0]
when 'add'
add(user_input[2], user_input[1], user_list)
when 'remove'
remove(user_input[1], user_list)
when 'update'
update(user_input[1], user_input[3])
when 'help'
print help
else
puts "I'm sorry, I didn't understand you. Make sure you capslock isn't on."
end
puts "Got it! Here's what your list looks like now:"
print_list(user_list)
end
puts "Goodbye! Come back soon!"
end
# either run the driver tests, OR the program, with the #run method
run
# ___________DRIVER CODE_____________
# new_list = create_list
# p new_list == {}
# add("broccoli", 7, new_list)
# p new_list == {"broccoli" => 7}
# remove("broccoli", new_list)
# p new_list == {}
# update("broccoli", 10, new_list)
# p new_list == {"broccoli" => 10}
# add('spinach', 5, new_list)
# add('eggplant', 22, new_list)
# add('cherry tomatoes', 9000, new_list)
# print_list(new_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment