Guest User

Mini Assignment 2

a guest
Dec 29th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Mini Project 2 for 40daysofruby
  2. # Author: iowa116
  3. # Date: 12/29/2013
  4. # Prompt: A console-style to-do list. The core functionality should be the ability
  5. #         to add new items, view added items (even after you close and re-open the
  6. #         program), and remove items.
  7.  
  8.  
  9. #This class contains the methods for adding/removing/saving items in the to do list
  10. class ToDoList
  11.     attr_accessor :todo
  12.  
  13.     def filename
  14.         @@filename = "savelist.todo"
  15.     end
  16.  
  17.     #This method initializes the to do list
  18.     def initialize
  19.         puts "Welcome to iowa116's Ruby ToDo list!"
  20.         #Load list if one already exists
  21.         self.todo = []
  22.         data = self.load
  23.         if data != ""
  24.             self.todo = data
  25.         end
  26.     end
  27.  
  28.     #This method adds an item to the list todo
  29.     def addItem
  30.         puts "Please enter below what you would like to add to your list:"
  31.         entry = gets.chomp
  32.         self.todo.push(entry)
  33.     end
  34.  
  35.     #This method edits an existing item on the list
  36.     def editItem
  37.         if self.todo.length == 0
  38.             puts "\nThere are no items to edit in your list!"
  39.         else
  40.             puts "What item would you like to edit?"
  41.             itemNum = gets.chomp.to_i - 1
  42.             #Test user input
  43.             while self.todo[itemNum] == nil or itemNum == -1
  44.                 puts "Sorry, that list item does not exists. Please enter a different number:"
  45.                 itemNum = gets.chomp.to_i - 1
  46.             end
  47.             puts "What would you like this list item to say now?"
  48.             newItem = gets.chomp
  49.             self.todo.delete_at(itemNum)
  50.             self.todo.insert(itemNum, newItem)
  51.         end
  52.     end
  53.  
  54.     #This method deletes an existing item on the list
  55.     def deleteItem
  56.         if self.todo.length ==  0
  57.             puts "\nThere are no items in your list to delete!"
  58.         else
  59.             puts "What item would you like to delete?"
  60.             itemNum = gets.chomp.to_i - 1
  61.             #Test user input
  62.             while self.todo[itemNum] == nil or itemNum == -1
  63.                 puts "Sorry, that list item does not exists. Please enter a different number:"
  64.                 itemNum = gets.chomp.to_i - 1
  65.             end
  66.             puts "Are you sure you want to delete '#{self.todo[itemNum]}'? (yes or no)"
  67.             res = gets.chomp.downcase
  68.             if res == "yes" or res == "y"
  69.                 self.todo.delete_at(itemNum)
  70.                 puts "\nThe requested item has been deleted!"
  71.             elsif res == "no" or res == "no"
  72.                 puts "\nOK this item will not be removed!"
  73.             end
  74.         end
  75.     end
  76.  
  77.     #This method takes a list item and switches it with another
  78.     def changeOrder
  79.         if self.todo.length < 2
  80.             puts "There is either one or no list items. No need to change order!\n"
  81.         else
  82.             puts "What list item number would you like to move?"
  83.             firstItem = gets.chomp.to_i - 1
  84.             #Test user input
  85.             while self.todo[firstItem] == nil or firstItem == -1
  86.                 puts "Sorry, that list item does not exists. Please enter a different number:"
  87.                 firstItem = gets.chomp.to_i - 1
  88.             end
  89.             puts "What number would you like this item? (note current item will go to first item's place)"
  90.             secondItem = gets.chomp.to_i - 1
  91.             #Test user input
  92.             while self.todo[secondItem] == nil or secondItem == -1
  93.                 puts "Sorry, that list item does not exists. Please enter a different number:"
  94.                 secondItem = gets.chomp.to_i - 1
  95.             end
  96.             placeholder = self.todo[firstItem]
  97.             self.todo[firstItem] = self.todo[secondItem]
  98.             self.todo[secondItem] = placeholder
  99.             puts "\nItems have been switched"
  100.         end
  101.     end
  102.  
  103.     #This method marks a list item as DONE!
  104.     def finishItem
  105.         if self.todo.length ==  0
  106.             puts "\nThere are no items in your list to mark as complete"
  107.         else
  108.             puts "What item number did you finish?"
  109.             itemNum = gets.chomp.to_i - 1
  110.             #Test user input
  111.             while self.todo[itemNum] == nil or itemNum == -1
  112.                 puts "Sorry, that list item does not exists. Please enter a different number:"
  113.                 itemNum = gets.chomp.to_i - 1
  114.             end
  115.             #Test to see if already marked as done
  116.             while self.todo[itemNum].split(//).last(5).join("").to_s == "DONE!"
  117.                 puts "This item has already been completed! Try a different number or 0 to exit:"
  118.                 itemNum = gets.chomp.to_i
  119.                 itemNum -= 1
  120.             end
  121.             #Mark item as done
  122.             if itemNum != -1
  123.                 self.todo[itemNum] = self.todo[itemNum] + " --> DONE!"
  124.             end
  125.         end
  126.     end
  127.  
  128.     #prints list
  129.     def printCurrentList
  130.         if self.todo.length == 0
  131.             puts "\nYou do not have any items in your list!"
  132.         else
  133.             puts "\nYour current ToDo list:\n"
  134.             self.todo.each_with_index do |item, index|
  135.                 puts "#{index+1}: #{item}"
  136.             end
  137.         end
  138.     end
  139.  
  140.     #This method loads the list from the filename savelist.todo
  141.     def load
  142.         file = File.open(self.filename, "a+")
  143.         todolist = ""
  144.         while (line = file.gets)
  145.             todolist+=line
  146.         end
  147.         file.close
  148.         return Marshal.load(todolist)
  149.     end
  150.  
  151.     #This method saves the todo list to the file
  152.     def saveList
  153.         save_file = File.open(self.filename, "w") do |file|
  154.             file.puts(Marshal.dump(self.todo))
  155.         end
  156.     end
  157.  
  158.  
  159. end
  160.  
  161. #Create new list and initialize input
  162. list = ToDoList.new
  163. input = 0
  164.  
  165. #User interface for ToDo List with While loop
  166. while input != 6
  167.     if input == 0
  168.         #do nothing for 0
  169.     elsif input == 1
  170.         list.addItem
  171.     elsif input == 2
  172.         list.editItem
  173.     elsif input == 3
  174.         list.deleteItem
  175.     elsif input == 4
  176.         list.changeOrder
  177.     elsif input == 5
  178.         list.finishItem
  179.     else
  180.         puts "\nWe're sorry but the number you entered does not do anything. Please enter a number 1 to 6:"
  181.     end
  182.  
  183.     #print list
  184.     list.printCurrentList
  185.     #Prompt user for input
  186.     puts "\nWhat do you want to do next?"
  187.     puts "1: Add new item"
  188.     puts "2: Edit item"
  189.     puts "3: Remove item"
  190.     puts "4: Change list order"
  191.     puts "5: Mark item as complete"
  192.     puts "6: Save and quit"
  193.  
  194.     input = gets.chomp.to_i
  195. end
  196.  
  197. puts "\nThe file '#{list.filename}' has been saved. Goodbye!\n"
  198. list.saveList
Advertisement
Add Comment
Please, Sign In to add comment