Last active
February 1, 2016 14:10
-
-
Save carolineartz/a76b59d2b47960b9e740 to your computer and use it in GitHub Desktop.
Example Solution for Students -- Dev Bootcamp Guided Pairing 2.2: Create an Electronic Grocery List
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
# Advanced solution w/ commandline input | |
# | |
def new_list | |
{} | |
end | |
def add(item, quantity, list) | |
return list[item] = quantity unless list[item] | |
puts "#{item.upcase} is already on your list! Use command `update` to change the quantity." | |
end | |
def remove(item, list) | |
list.delete(item) { |i| puts "Whoops! #{i.upcase} wasn't on your list!" } | |
end | |
def update(item, quantity, list) | |
return list[item] = quantity if list[item] | |
puts "Whoops! #{item.upcase} wasn't on your list! Use `add` to add your item." | |
end | |
def print(list) | |
list.each { |item, quantity| puts "#{item.upcase} - #{quantity}" } | |
end | |
def help | |
puts <<-helpdocs | |
The following commands are available. Separate input with a single space, e.g.: | |
add 10 chips | |
add <number> <item> add an item to your list | |
remove <item> remove an item from your list | |
update <item> <quantity> update an item's quantity on your list | |
help show this list of commands | |
quit exit the program | |
helpdocs | |
end | |
def run | |
puts "Hello! Lets get ready to shop!\n"\ | |
"Type 'help' to see a list of available commands." | |
list = new_list | |
input = "" | |
until input[0] == "quit" | |
puts "\nWhat would you like to do?\n" | |
input = gets.chomp.split(" ") | |
case input[0] | |
when 'add' then add(input[2], input[1], list) | |
when 'remove' then remove(input[1], list) | |
when 'update' then update(input[1], input[2], list) | |
when 'help' then help; next | |
else puts "Try again..." | |
end | |
puts "\n*******************" | |
puts "This is your list:" | |
print(list) | |
end | |
end | |
run |
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
# Requirements | |
# | |
# 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!) | |
# Initial solution | |
# | |
def new_list(items) | |
list = {} | |
items.split.each { |item| add(item, "1", list) } | |
print(list) | |
end | |
def add(item, quantity, list) | |
if list[item] | |
puts "#{item.upcase} is already on your list! Use `update` to change the quantity." | |
else | |
list[item] = quantity | |
end | |
print(list) | |
end | |
def remove(item, list) | |
if list[item] | |
list.delete(item) | |
else | |
puts "Whoops! #{item.upcase} wasn't on your list!" | |
end | |
print(list) | |
end | |
def update(item, quantity, list) | |
if list[item] | |
list[item] = quantity | |
else | |
puts "Whoops! #{item.upcase} wasn't on your list! Use `add` to add your item." | |
end | |
print(list) | |
end | |
def print(list) | |
puts '-- LIST --' | |
list.each do |item, quantity| | |
puts "#{item.upcase} - #{quantity}" | |
end | |
end | |
my_list = new_list("Milk Butter Eggs") | |
# Tests | |
puts my_list == {"Milk" => "1", "Butter" => "1", "Eggs" => "1"} | |
puts add("Carrots", "10", my_list) == {"Milk" => "1", "Butter" => "1", "Eggs" => "1", "Carrots" => "10"} | |
puts add("Carrots", "20", my_list) == {"Milk" => "1", "Butter" => "1", "Eggs" => "1", "Carrots" => "10"} | |
puts add("Apples", "15", my_list) == {"Milk" => "1", "Butter" => "1", "Eggs" => "1", "Carrots" => "10", "Apples" => "15"} | |
puts remove("Oranges", my_list) == {"Milk" => "1", "Butter" => "1", "Eggs" => "1", "Carrots" => "10", "Apples" => "15"} | |
puts remove("Apples", my_list) == {"Milk" => "1", "Butter" => "1", "Eggs" => "1", "Carrots" => "10"} | |
puts update("Carrots", "20", my_list) == {"Milk" => "1", "Butter" => "1", "Eggs" => "1", "Carrots" => "20"} | |
puts update("Oranges", "20", my_list) == {"Milk" => "1", "Butter" => "1", "Eggs" => "1", "Carrots" => "20"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment