Created
April 30, 2024 17:06
-
-
Save RobertoBarros/c6b5c4bd1fa4f5d26b2003234e9270fb to your computer and use it in GitHub Desktop.
batch_1680_reboot_giftslist_part2
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 'colored' | |
require 'csv' | |
FILEPATH = 'gifts.csv' | |
# Os gifts estão em um array de hash como: | |
# [ | |
# { name: 'Meia', bought: true }, | |
# { name: 'iPhone', bought: false } | |
#] | |
def list(gifts) | |
gifts.each_with_index do |gift, index| | |
bought = gift[:bought] ? '[X]' : '[ ]' | |
puts "#{index + 1} - #{bought} #{gift[:name]}" | |
end | |
end | |
def mark(gifts) | |
# mostrar todos os gifts | |
list(gifts) | |
# Perguntar qual o número do gift para marcar | |
puts "Which item have you bought (give the index)?" | |
index = gets.chomp.to_i - 1 | |
# Alterar o item dos gifts, com a opção bought como verdadeira | |
if index >= 0 && index < gifts.size | |
gift = gifts[index] | |
# gift é um hash do tipo { name: 'Caneca', bought: false } | |
gift[:bought] = !gift[:bought] | |
save_csv(gifts) | |
else | |
puts "Invalid gift number".red | |
end | |
end | |
def add(gifts) | |
# Perguntar qual presente adicionar | |
puts "What gift would you like to add?" | |
name = gets.chomp | |
# Adicionar o presente na lista | |
gifts << { name: name, bought: false } | |
# salvar os gifts no csv | |
save_csv(gifts) | |
# Mostrar a lista atualizada | |
list(gifts) | |
end | |
def delete(gifts) | |
# Mostrar os gifts já cadastrados | |
list(gifts) | |
# Perguntar qual o número do gift | |
puts "What gift number would you like to delete?" | |
index = gets.chomp.to_i - 1 | |
# Apagar da lista pelo index se existir | |
if index >= 0 && index < gifts.size | |
gifts.delete_at(index) | |
save_csv(gifts) | |
else | |
puts "Invalid gift number".red | |
end | |
end | |
def load_csv | |
gifts = [] | |
CSV.foreach(FILEPATH) do |row| | |
gifts << { name: row[0], bought: row[1] == 'true' } | |
end | |
gifts | |
end | |
def save_csv(gifts) | |
CSV.open(FILEPATH, "wb") do |csv| | |
gifts.each do |gift| | |
csv << [gift[:name], gift[:bought]] | |
end | |
end | |
end | |
gifts_list = [] | |
gifts_list = load_csv if File.exist?('gifts.csv') | |
# Mensagem de boas vindas | |
puts "Welcome to your Christmas Shopping list" | |
# LOOP | |
loop do | |
# Perguntar qual a ação [list|add|delete|quit] | |
puts '-' * 30 | |
puts "which action do you want to be execute [list|add|delete|mark|quit]".bold.green | |
input = gets.chomp.downcase | |
system "clear" | |
# Executar a ação | |
case input | |
when "list" then list(gifts_list) | |
when "add" then add(gifts_list) | |
when "mark" then mark(gifts_list) | |
when "delete" then delete(gifts_list) | |
when "quit" then break | |
else | |
# Se quit sair do loop | |
puts "invalid input" | |
end | |
# FIM DO LOOP | |
end | |
# Mensagem de término | |
puts "Thanks and goodbye" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment