Created
October 13, 2020 15:10
-
-
Save arthur-littm/cc3ea21fb7ce70f556b5247177eb3c80 to your computer and use it in GitHub Desktop.
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
def print_gift_list(array) | |
# Array of hashes | |
array.each_with_index do |item, i| | |
# Item is a hash: | |
# eg: { name: 'shoes', bought: true } | |
if item[:bought] == true | |
puts "#{i + 1}. [X] #{item[:name]}" | |
else | |
puts "#{i + 1}. [ ] #{item[:name]}" | |
end | |
end | |
end | |
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
# TODO: you can build your christmas list program here! | |
# STEP 1. MEMU | |
# 1. puts -> welcome message to the user | |
puts "Welcome to your 🎁 list" | |
# Initialize a variable for the loop to start | |
user_choice = '' | |
until user_choice == 'quit' | |
# INSIDE THE LOOP, GIVE POSSIBILITY TO CHANGE user_choice (otherwise endless loop ☹️) | |
# 2. puts -> ask the user which action he wants to do | |
puts "Which action [list|add|delete|quit]?" | |
# 3. get the user choice and store (gets.chomp) | |
user_choice = gets.chomp | |
# 4. depending on user input display the right message | |
if user_choice == 'list' | |
puts "TODO: List later" | |
elsif user_choice == 'add' | |
puts "TODO: Add something later" | |
elsif user_choice == 'delete' | |
puts "TODO: Delete something later" | |
elsif user_choice == 'quit' | |
puts "Bye 👋" | |
else | |
puts "I don't understand" | |
end | |
end | |
# comment: cmd + / | |
# NO LOOP YET | |
# Good luck 🤘🏻 | |
# case user_choice | |
# when 1 | |
# puts "TODO: List later" | |
# when 2 | |
# puts "TODO: Add something later" | |
# when 3 | |
# puts "TODO: Delete something later" | |
# when 4 | |
# puts "Bye 👋" | |
# end | |
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
require_relative 'gift_list' | |
# TODO: you can build your christmas list program here! | |
# 1. create a new variable with gifts inside (Array for now) | |
gift_list = ['shoes', 'watch', 'phone'] | |
puts "Welcome to your 🎁 list" | |
user_choice = '' | |
until user_choice == 'quit' | |
puts "Which action [list|add|delete|quit]?" | |
user_choice = gets.chomp | |
# STEP 2: Build list feature | |
if user_choice == 'list' | |
puts "You list of gifts to buy:" | |
# 2. print the list of gifts | |
print_gift_list(gift_list) | |
# STEP 3: Build add feature | |
elsif user_choice == 'add' | |
# 1. ask the user what they want to add | |
puts "Type a new gift to add:" | |
# 2a. get new gift as a string | |
gift_added = gets.chomp | |
# 2b. push the new gift in the array | |
gift_list << gift_added | |
# 3. show the list again (optional) | |
print_gift_list(gift_list) | |
elsif user_choice == 'delete' | |
# STEP 4: Delete a gift feature | |
# 1a. list all items | |
print_gift_list(gift_list) | |
# 1b. Ask the user what item to delete (ask for -> number) | |
puts "Please provide the number of the gift to remove:" | |
unwanted_item = gets.chomp.to_i - 1 | |
# 2. check if the number is valid (is this a gift in our list?) | |
if unwanted_item >= gift_list.length | |
puts "Sorry that doesn't match a gift" | |
else | |
# 3. if it exists | |
# delete the corresponding gift from the array | |
gift_list.delete_at(unwanted_item) | |
end | |
elsif user_choice == 'quit' | |
puts "Bye 👋" | |
else | |
puts "I don't understand" | |
end | |
end | |
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
require_relative 'gift_list' | |
# MARK AS DONE FEATURE | |
# - 1. [X] shoes | |
# - 2. [ ] laptop | |
# 1. Change the gift list to an Array of Hashes | |
gift_list = [ | |
{ name: 'shoes', bought: true }, | |
{ name: 'watch', bought: false }, | |
{ name: 'phone', bought: false } | |
] | |
puts "Welcome to your 🎁 list" | |
user_choice = '' | |
until user_choice == 'quit' | |
puts "Which action [list|add|delete|mark|quit]?" | |
user_choice = gets.chomp | |
if user_choice == 'list' | |
puts "You list of gifts to buy:" | |
print_gift_list(gift_list) | |
elsif user_choice == 'add' | |
puts "Type a new gift to add:" | |
gift_added = gets.chomp # String | |
gift_hash = { name: gift_added, bought: false } | |
gift_list << gift_hash # String not a Hash :( | |
print_gift_list(gift_list) | |
elsif user_choice == 'delete' | |
print_gift_list(gift_list) | |
puts "Please provide the number of the gift to remove:" | |
unwanted_item = gets.chomp.to_i - 1 | |
if unwanted_item >= gift_list.length | |
puts "Sorry that doesn't match a gift" | |
else | |
gift_list.delete_at(unwanted_item) | |
end | |
# 1. Create a new option in the menu (new elsif) | |
elsif user_choice == 'mark' | |
# 2. In the option | |
# display the list of gifts | |
print_gift_list(gift_list) | |
# ask the user which gift he/she's bought (ask for: number) | |
puts "Which gift have you bought (number please):" | |
marked_item = gets.chomp.to_i - 1 | |
# check if the number is valid | |
if marked_item >= gift_list.length | |
# if not display error message | |
puts "Sorry that doesn't match a gift" | |
else | |
# if valid: | |
# find the item at this index | |
item = gift_list[marked_item] | |
# switch the `bought` of the item to true | |
item[:bought] = true | |
end | |
elsif user_choice == 'quit' | |
puts "Bye 👋" | |
else | |
puts "I don't understand" | |
end | |
end | |
# arr = ['a','b','c'] | |
# arr[1] # 'b' | |
# arr[1] = 'z' | |
# city = {name: 'Paris', population: 200000 } | |
# city[:population] = 20000010 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment