Skip to content

Instantly share code, notes, and snippets.

@Haumer
Created January 19, 2021 17:20
Show Gist options
  • Save Haumer/96b409091e5044b5de9203c2dc660dff to your computer and use it in GitHub Desktop.
Save Haumer/96b409091e5044b5de9203c2dc660dff to your computer and use it in GitHub Desktop.
require 'csv'
# 1. Greet/Welcome the user to the Christmas List program!
puts "(¯`·._(¯`·._(¯`·._(¯`·._(¯`·.__.·¯) _.·¯)_.·¯)_.·¯)_.·¯)"
puts "(¯`·._(¯`·._ Welcome to the Christmas List! _.·¯)_.·¯)"
puts "(¯`·._(¯`·._(¯`·._(¯`·._(¯`·.__.·¯) _.·¯)_.·¯)_.·¯)_.·¯)"
options = [
"list",
"add",
"delete",
"mark",
"quit"
]
user_input = ""
# part of 7.)
def display_gifts(array_of_gifts)
array_of_gifts.each_with_index do |gift, index|
# [x]
if gift[:marked]
x = "x"
else
x = " "
end
# x = gift[:marked] ? "x" : " "
if gift[:price] <= 20
budget_type = "£"
else
budget_type = "£££"
end
# budget_type = gift[:price] <= 20 ? "£" : "£££"
puts "#{index + 1}. [#{x}] - #{gift[:name]}, #{budget_type}"
end
end
# part of 13.)
def choose_gift(array_of_gifts)
display_gifts(array_of_gifts)
# 14. which one would you like to mark as 'bought'
puts "What gift would you like to choose? (type the number)"
# 15. get user input and save into a new variable (convert to integer)
gift_index = gets.chomp.to_i
# 16. use the index number to find the correct gift
array_of_gifts[gift_index - 1]
end
def save_csv(array_of_gifts)
csv_options = { col_sep: ',', force_quotes: true, quote_char: '"' }
filepath = 'gifts.csv'
CSV.open(filepath, 'wb', csv_options) do |csv|
# TODO: add gifts to CSV
csv << ["Name","Price","Marked"]
array_of_gifts.each do |gift|
# p [ gift[:name], gift[:price], gift[:marked] ]
csv << [ gift[:name], gift[:price], gift[:marked] ]
end
end
end
def load_csv
csv_options = { col_sep: ',', quote_char: '"', headers: :first_row }
filepath = 'gifts.csv'
output = []
CSV.foreach(filepath, csv_options) do |row|
gift_name = row["Name"]
gift_price = row["Price"].to_i
gift_marked = row["Marked"] == "true"
puts "loaded: #{gift_name} with price #{gift_price}"
output << { name: gift_name, price: gift_price, marked: gift_marked }
end
output
end
# 5. make a new (empty) array which will contain all our christmas gifts
gifts = load_csv
# load data (gifts) from the csv
# 4. create a loop
until user_input == "quit" || user_input == "q"
# 2. display the options (list|add|delete|mark|quit)
puts "Choose: #{options.join(" | ")}"
# 3. get the user input
user_input = gets.chomp
# print `clear`
# TODO: sanitize user input
if options.include?(user_input)
# 6. make an if statement that checks if the user typed 'list'
if user_input == "list"
puts "The following Items are on your list:"
# 7. dispaly the list (use each_with_index) to the user
display_gifts(gifts)
# 8. expand if statement to check if user input is 'add'
elsif user_input == "add"
# 9. ask the user: 'what would you like to add to your Christmas List?'
puts "What is the name of the gift?"
# 10. get the user input
added_gift = gets.chomp.to_s
# 10.1 ask the user for the gift price
puts "How much does #{added_gift} cost?"
# 10.2 get the price of the gift
gift_price = gets.chomp.to_i
# 11. push user input into the gifts array
gifts << {
name: added_gift,
price: gift_price,
marked: false
}
# gifts.push(added_gift)
# 8.5 OPTIONAL: tell the user which gifts he/she has on his/her christmas list
puts "Your list now contains:"
display_gifts(gifts)
# save the change to the csv
save_csv(gifts)
# 12. expand if statement and check if user_input is "mark"
elsif user_input == "mark"
selected_gift = choose_gift(gifts)
# 17. change the value of marked to true
# selected_gift[:marked] = !selected_gift[:marked]
selected_gift[:marked] = true
# 18. expand if statement and check if the user_input is "delete"
elsif user_input == "delete"
# 19. display current list to the user
display_gifts(gifts)
# 20. ask the user 'which gift do you want to choose' (index)
puts "What gift would you like to delete"
# 21. get user input (gets.chomp.to_i - 1)
gift_index = gets.chomp.to_i - 1
# 22. delete the correct gift (.delete_at(index))
gifts.delete_at(gift_index)
# save the change to the csv
save_csv(gifts)
end
else
puts "Please enter a valid input!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment