View difference between Paste ID: i401eUr0 and VUYgSvGt
SHOW: | | - or go back to the newest paste.
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
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
20+
21
22
	def addTodo(newTodo)
23
		self.todo.push(newTodo)
24
	end
25
26
	def remove(index)
27
		self.todo.delete_at(index.to_i)
28
	end
29
30
	def edit(index, newText)
31
		self.todo[index] = newText
32
	end
33
34
	def printTodoList
35
		if self.todo.length == 0
36
			puts "\nYou have no items in your to-do list!"
37
		else
38
			print "\nYour current to-do list:\n"
39
			self.todo.each_with_index{|item, index| puts "#{index+1}: #{item}"}
40
		end
41
	end
42
43
	def load
44
		file = File.new(self.filename, "a+")
45
		data = ""
46
		while (line = file.gets)
47
			data += line
48
		end
49
		file.close
50
		return data
51
	end
52
53
	def save
54
		out_file = File.open(self.filename, "w") { |f| f.puts(Marshal.dump(self.todo)) } #Marshal is used to serialize arrays
55
	end
56
end
57
58
list = ToDoList.new
59
input = "0" #0 does nothing
60
while input != "4" #This part is the "flow" of the program. This is essentially the UI of the console
61
	if input == "0"
62
		#Do nothing, we just don't wanna fall into the else
63
	elsif input == "1" #Add item
64
		puts "\nAwesome! What do you want to add to your list?"
65
		newItem = gets.chomp
66
		list.addTodo(newItem)
67
	elsif input == "2" #Edit item
68
		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
69
			puts "\nWhich item would you like to edit? (Enter the number to the left of the item!)"
70
			indexToEdit = gets.chomp.to_i
71
			while indexToEdit > list.todo.length
72
				list.printTodoList
73
				puts "\nThat is not a valid index! Please select an index to edit."
74
				indexToEdit = gets.chomp.to_i
75
			end
76
			puts "\nWhat would you like your item to say now?"
77
			newText = gets.chomp
78
			list.edit(indexToEdit-1, newText)
79
		else
80
			puts "You don't have any items to edit!"
81
		end
82
	elsif input == "3" #Remove
83
		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
84
			puts "\nWhich item would you like to remove? (Enter the number to the left of the item)"
85
			indexToDelete = gets.chomp.to_i
86
			while indexToDelete > list.todo.length
87
				list.printTodoList
88
				puts "\nThat is not a valid index! Please select an index to remove."
89
				indexToDelete = gets.chomp.to_i
90
			end
91
			indexToDelete -= 1
92
			puts "\nAre you sure you want to remove '#{list.todo[indexToDelete]}'? (Yes or no)"
93
			confirmation = gets.chomp.downcase
94
			if confirmation.index("y") == 0
95
				list.remove(indexToDelete)
96
				puts "\nThe item was successfully removed!"
97
			else
98
				puts "\nAlright then, the item was not removed!"
99
			end
100
		else
101
			puts "You don't have any items to remove!"
102
		end
103
	else
104
		#We didn't hit any of the elses so the thing the user entered doesn't make sense, I guess.
105
		puts "\nUnknown command. Please select a number from 1 to 4 that corresponds with your desired action."
106
	end
107
108
	list.printTodoList
109-
	if input != "0"
109+
110-
		list.printTodoList
110+
111
	puts "2: Edit item"
112
	puts "3: Remove item"
113
	puts "4: Save and quit"
114
	input = gets.chomp
115
end
116
list.save