Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Mini Project 2 for 40daysofruby
- # Author: iowa116
- # Date: 12/29/2013
- # Prompt: A console-style to-do list. The core functionality should be the ability
- # to add new items, view added items (even after you close and re-open the
- # program), and remove items.
- #This class contains the methods for adding/removing/saving items in the to do list
- class ToDoList
- attr_accessor :todo
- def filename
- @@filename = "savelist.todo"
- end
- #This method initializes the to do list
- def initialize
- puts "Welcome to iowa116's Ruby ToDo list!"
- #Load list if one already exists
- self.todo = []
- data = self.load
- if data != ""
- self.todo = data
- end
- end
- #This method adds an item to the list todo
- def addItem
- puts "Please enter below what you would like to add to your list:"
- entry = gets.chomp
- self.todo.push(entry)
- end
- #This method edits an existing item on the list
- def editItem
- if self.todo.length == 0
- puts "\nThere are no items to edit in your list!"
- else
- puts "What item would you like to edit?"
- itemNum = gets.chomp.to_i - 1
- #Test user input
- while self.todo[itemNum] == nil or itemNum == -1
- puts "Sorry, that list item does not exists. Please enter a different number:"
- itemNum = gets.chomp.to_i - 1
- end
- puts "What would you like this list item to say now?"
- newItem = gets.chomp
- self.todo.delete_at(itemNum)
- self.todo.insert(itemNum, newItem)
- end
- end
- #This method deletes an existing item on the list
- def deleteItem
- if self.todo.length == 0
- puts "\nThere are no items in your list to delete!"
- else
- puts "What item would you like to delete?"
- itemNum = gets.chomp.to_i - 1
- #Test user input
- while self.todo[itemNum] == nil or itemNum == -1
- puts "Sorry, that list item does not exists. Please enter a different number:"
- itemNum = gets.chomp.to_i - 1
- end
- puts "Are you sure you want to delete '#{self.todo[itemNum]}'? (yes or no)"
- res = gets.chomp.downcase
- if res == "yes" or res == "y"
- self.todo.delete_at(itemNum)
- puts "\nThe requested item has been deleted!"
- elsif res == "no" or res == "no"
- puts "\nOK this item will not be removed!"
- end
- end
- end
- #This method takes a list item and switches it with another
- def changeOrder
- if self.todo.length < 2
- puts "There is either one or no list items. No need to change order!\n"
- else
- puts "What list item number would you like to move?"
- firstItem = gets.chomp.to_i - 1
- #Test user input
- while self.todo[firstItem] == nil or firstItem == -1
- puts "Sorry, that list item does not exists. Please enter a different number:"
- firstItem = gets.chomp.to_i - 1
- end
- puts "What number would you like this item? (note current item will go to first item's place)"
- secondItem = gets.chomp.to_i - 1
- #Test user input
- while self.todo[secondItem] == nil or secondItem == -1
- puts "Sorry, that list item does not exists. Please enter a different number:"
- secondItem = gets.chomp.to_i - 1
- end
- placeholder = self.todo[firstItem]
- self.todo[firstItem] = self.todo[secondItem]
- self.todo[secondItem] = placeholder
- puts "\nItems have been switched"
- end
- end
- #This method marks a list item as DONE!
- def finishItem
- if self.todo.length == 0
- puts "\nThere are no items in your list to mark as complete"
- else
- puts "What item number did you finish?"
- itemNum = gets.chomp.to_i - 1
- #Test user input
- while self.todo[itemNum] == nil or itemNum == -1
- puts "Sorry, that list item does not exists. Please enter a different number:"
- itemNum = gets.chomp.to_i - 1
- end
- #Test to see if already marked as done
- while self.todo[itemNum].split(//).last(5).join("").to_s == "DONE!"
- puts "This item has already been completed! Try a different number or 0 to exit:"
- itemNum = gets.chomp.to_i
- itemNum -= 1
- end
- #Mark item as done
- if itemNum != -1
- self.todo[itemNum] = self.todo[itemNum] + " --> DONE!"
- end
- end
- end
- #prints list
- def printCurrentList
- if self.todo.length == 0
- puts "\nYou do not have any items in your list!"
- else
- puts "\nYour current ToDo list:\n"
- self.todo.each_with_index do |item, index|
- puts "#{index+1}: #{item}"
- end
- end
- end
- #This method loads the list from the filename savelist.todo
- def load
- file = File.open(self.filename, "a+")
- todolist = ""
- while (line = file.gets)
- todolist+=line
- end
- file.close
- return Marshal.load(todolist)
- end
- #This method saves the todo list to the file
- def saveList
- save_file = File.open(self.filename, "w") do |file|
- file.puts(Marshal.dump(self.todo))
- end
- end
- end
- #Create new list and initialize input
- list = ToDoList.new
- input = 0
- #User interface for ToDo List with While loop
- while input != 6
- if input == 0
- #do nothing for 0
- elsif input == 1
- list.addItem
- elsif input == 2
- list.editItem
- elsif input == 3
- list.deleteItem
- elsif input == 4
- list.changeOrder
- elsif input == 5
- list.finishItem
- else
- puts "\nWe're sorry but the number you entered does not do anything. Please enter a number 1 to 6:"
- end
- #print list
- list.printCurrentList
- #Prompt user for input
- puts "\nWhat do you want to do next?"
- puts "1: Add new item"
- puts "2: Edit item"
- puts "3: Remove item"
- puts "4: Change list order"
- puts "5: Mark item as complete"
- puts "6: Save and quit"
- input = gets.chomp.to_i
- end
- puts "\nThe file '#{list.filename}' has been saved. Goodbye!\n"
- list.saveList
Advertisement
Add Comment
Please, Sign In to add comment