Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # DSH's mini-assignment 2!
- # Super awesome to-do list of doom
- class ToDoList
- attr_accessor :todo
- @@filename = "save.todo"
- def filename
- @@filename
- end
- def initialize
- puts "Welcome to DSH's Ruby to-do list!"
- self.todo = []
- data = self.load
- if data != ""
- self.todo = Marshal.load(data) #Marshal is used to deserialize arrays
- end
- end
- def addTodo(newTodo)
- self.todo.push(newTodo)
- end
- def remove(index)
- self.todo.delete_at(index.to_i)
- end
- def edit(index, newText)
- self.todo[index] = newText
- end
- def printTodoList
- if self.todo.length == 0
- puts "\nYou have no items in your to-do list!"
- else
- print "\nYour current to-do list:\n"
- self.todo.each_with_index{|item, index| puts "#{index+1}: #{item}"}
- end
- end
- def load
- file = File.new(self.filename, "a+")
- data = ""
- while (line = file.gets)
- data += line
- end
- file.close
- return data
- end
- def save
- out_file = File.open(self.filename, "w") { |f| f.puts(Marshal.dump(self.todo)) } #Marshal is used to serialize arrays
- end
- end
- list = ToDoList.new
- input = "0" #0 does nothing
- while input != "4" #This part is the "flow" of the program. This is essentially the UI of the console
- if input == "0"
- #Do nothing, we just don't wanna fall into the else
- elsif input == "1" #Add item
- puts "\nAwesome! What do you want to add to your list?"
- newItem = gets.chomp
- list.addTodo(newItem)
- elsif input == "2" #Edit item
- 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
- puts "\nWhich item would you like to edit? (Enter the number to the left of the item!)"
- indexToEdit = gets.chomp.to_i
- while indexToEdit > list.todo.length
- list.printTodoList
- puts "\nThat is not a valid index! Please select an index to edit."
- indexToEdit = gets.chomp.to_i
- end
- puts "\nWhat would you like your item to say now?"
- newText = gets.chomp
- list.edit(indexToEdit-1, newText)
- else
- puts "You don't have any items to edit!"
- end
- elsif input == "3" #Remove
- 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
- puts "\nWhich item would you like to remove? (Enter the number to the left of the item)"
- indexToDelete = gets.chomp.to_i
- while indexToDelete > list.todo.length
- list.printTodoList
- puts "\nThat is not a valid index! Please select an index to remove."
- indexToDelete = gets.chomp.to_i
- end
- indexToDelete -= 1
- puts "\nAre you sure you want to remove '#{list.todo[indexToDelete]}'? (Yes or no)"
- confirmation = gets.chomp.downcase
- if confirmation.index("y") == 0
- list.remove(indexToDelete)
- puts "\nThe item was successfully removed!"
- else
- puts "\nAlright then, the item was not removed!"
- end
- else
- puts "You don't have any items to remove!"
- end
- else
- #We didn't hit any of the elses so the thing the user entered doesn't make sense, I guess.
- puts "\nUnknown command. Please select a number from 1 to 4 that corresponds with your desired action."
- end
- list.printTodoList
- puts "\nWhat do you want to do next?"
- puts "1: Add new item"
- puts "2: Edit item"
- puts "3: Remove item"
- puts "4: Save and quit"
- input = gets.chomp
- end
- list.save
Advertisement
Add Comment
Please, Sign In to add comment