Created
June 19, 2015 01:49
-
-
Save JFFail/91b708663ab504122cc5 to your computer and use it in GitHub Desktop.
Solution to Reddit Daily Programmer #218
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
#!/usr/env/ruby | |
#Defining the object for my list. | |
class Todo | |
#Just create an empty array off the bat. | |
def initialize | |
@list = Array.new | |
end | |
#Function to add new items. | |
def add_item(newItem) | |
@list.push(newItem) | |
end | |
#Function to delete an item. | |
def delete_item(badItem) | |
@list.delete(badItem) | |
end | |
#Function to view all items. | |
def view_list | |
@list.each do |x| | |
puts "#{x}" | |
end | |
end | |
end | |
#Create a new object. | |
my_list = Todo.new | |
#Greeting... | |
puts "Welcome to the Super Awesome Todo List!!!" | |
while true | |
#Get what the user wants to do. | |
puts "\nEnter an option: 1, 2, 3, or 4!" | |
puts "1.) Add an item to the list" | |
puts "2.) Remove an item from the list." | |
puts "3.) View your list" | |
puts "4.) Quit" | |
print "> " | |
choice = gets.chomp | |
#See if it can be converted. | |
num = Integer(choice) rescue nil | |
#Act accordingly. | |
if num != nil && num >= 1 && num <= 4 | |
#Take the appropriate action. | |
if num == 1 | |
puts "\nEnter what you'd like to add to the list." | |
print "> " | |
addition = gets.chomp | |
my_list.add_item(addition) | |
elsif num == 2 | |
puts "\nEnter the item you'd like to remove from the list." | |
print "> " | |
removal = gets.chomp | |
my_list.delete_item(removal) | |
elsif num == 3 | |
puts "\nYour current list is:" | |
my_list.view_list | |
else | |
break | |
end | |
else | |
puts "\n'#{choice}' is an invalid option!" | |
end | |
end | |
puts "\nThanks for using this stellar application!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment