Guest User

Mini-assignment #2

a guest
Dec 29th, 2013
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.35 KB | None | 0 0
  1. # DSH's mini-assignment 2!
  2. # Super awesome to-do list of doom
  3.  
  4. class ToDoList
  5.     attr_accessor :todo
  6.  
  7.     @@filename = "save.todo"
  8.     def filename
  9.         @@filename
  10.     end
  11.  
  12.     def initialize
  13.         puts "Welcome to DSH's Ruby to-do list!"
  14.  
  15.         self.todo = []
  16.         data = self.load
  17.         if data != ""
  18.             self.todo = Marshal.load(data) #Marshal is used to deserialize arrays
  19.         end
  20.         self.printTodoList
  21.     end
  22.  
  23.     def addTodo(newTodo)
  24.         self.todo.push(newTodo)
  25.     end
  26.  
  27.     def remove(index)
  28.         self.todo.delete_at(index.to_i)
  29.     end
  30.  
  31.     def edit(index, newText)
  32.         self.todo[index] = newText
  33.     end
  34.  
  35.     def printTodoList
  36.         if self.todo.length == 0
  37.             puts "\nYou have no items in your to-do list!"
  38.         else
  39.             print "\nYour current to-do list:\n"
  40.             self.todo.each_with_index{|item, index| puts "#{index+1}: #{item}"}
  41.         end
  42.     end
  43.  
  44.     def load
  45.         file = File.new(self.filename, "a+")
  46.         data = ""
  47.         while (line = file.gets)
  48.             data += line
  49.         end
  50.         file.close
  51.         return data
  52.     end
  53.  
  54.     def save
  55.         out_file = File.open(self.filename, "w") { |f| f.puts(Marshal.dump(self.todo)) } #Marshal is used to serialize arrays
  56.     end
  57. end
  58.  
  59. list = ToDoList.new
  60. input = "0" #0 does nothing
  61. while input != "4" #This part is the "flow" of the program. This is essentially the UI of the console
  62.     if input == "0"
  63.         #Do nothing, we just don't wanna fall into the else
  64.     elsif input == "1" #Add item
  65.         puts "\nAwesome! What do you want to add to your list?"
  66.         newItem = gets.chomp
  67.         list.addTodo(newItem)
  68.     elsif input == "2" #Edit item
  69.         if list.todo.length > 0 #This is kind of important here because the user will get stuck in a loop if there are no items
  70.             puts "\nWhich item would you like to edit? (Enter the number to the left of the item!)"
  71.             indexToEdit = gets.chomp.to_i
  72.             while indexToEdit > list.todo.length
  73.                 list.printTodoList
  74.                 puts "\nThat is not a valid index! Please select an index to edit."
  75.                 indexToEdit = gets.chomp.to_i
  76.             end
  77.             puts "\nWhat would you like your item to say now?"
  78.             newText = gets.chomp
  79.             list.edit(indexToEdit-1, newText)
  80.         else
  81.             puts "You don't have any items to edit!"
  82.         end
  83.     elsif input == "3" #Remove
  84.         if list.todo.length > 0 #This is kind of important here because the user will get stuck in a loop if there are no items
  85.             puts "\nWhich item would you like to remove? (Enter the number to the left of the item)"
  86.             indexToDelete = gets.chomp.to_i
  87.             while indexToDelete > list.todo.length
  88.                 list.printTodoList
  89.                 puts "\nThat is not a valid index! Please select an index to remove."
  90.                 indexToDelete = gets.chomp.to_i
  91.             end
  92.             indexToDelete -= 1
  93.             puts "\nAre you sure you want to remove '#{list.todo[indexToDelete]}'? (Yes or no)"
  94.             confirmation = gets.chomp.downcase
  95.             if confirmation.index("y") == 0
  96.                 list.remove(indexToDelete)
  97.                 puts "\nThe item was successfully removed!"
  98.             else
  99.                 puts "\nAlright then, the item was not removed!"
  100.             end
  101.         else
  102.             puts "You don't have any items to remove!"
  103.         end
  104.     else
  105.         #We didn't hit any of the elses so the thing the user entered doesn't make sense, I guess.
  106.         puts "\nUnknown command. Please select a number from 1 to 4 that corresponds with your desired action."
  107.     end
  108.  
  109.     if input != "0"
  110.         list.printTodoList
  111.     end
  112.  
  113.     puts "\nWhat do you want to do next?"
  114.     puts "1: Add new item"
  115.     puts "2: Edit item"
  116.     puts "3: Remove item"
  117.     puts "4: Save and quit"
  118.     input = gets.chomp
  119. end
  120. list.save
Advertisement
Add Comment
Please, Sign In to add comment