Here is my finished final project!


#1

So I took the ruby course and I am pretty new to programming. So of course I won't know any advanced techniques, but I would really appreciate any suggestions or feedback for a beginner. Thank you

-Andy Olson

# This class controls the flow of the program and user input
class Control
def initialize
	@master_list = List.new
	@completed_list = List.new
	File.open("todo_list.txt", "r").each { |io| @master_list.add_task_from_file(io) }
	File.open("completed_list.txt", "r").each { |io| @master_list.add_completed_task_from_file(io) }
	self.ask_user
end

def ask_user
	print "Please enter a command (Type Help for list of commands): "
	user_input = gets.chomp
	self.command(user_input)
end

def command(user_input)
	case user_input.upcase
	when "ADD"
		@master_list.add_task
	when "LIST"
		@master_list.show_tasks
	when "DELETE"
		@master_list.show_tasks
		print "Enter the number of the task to delete:"
		number = gets.chomp
		@master_list.delete(number)
	when "HELP"
		puts "ADD, LIST, QUIT, DELETE, COMPLETE, COMPLETED, ALL"
	when "COMPLETE"
		print "Enter the number of the task to complete:"
		number = gets.chomp
		@master_list.complete(number)
		@master_list.delete(number)
	when "COMPLETED"
		@master_list.show_completed
	when "ALL"
		puts "Tasks:"
		@master_list.show_tasks
		puts "Completed Tasks:"
		@master_list.show_completed
	when "QUIT"
		@master_list.save
		return "Program Ended"
	else
		puts "Not a reconized command"
	end
	self.ask_user
end
end




#This class creates a list that holds and manages tasks
class List
def initialize
	@all_tasks = []
	@completed_tasks = []
end

def complete(number)
	@completed_tasks << @all_tasks[number.to_i]

end

def add_task_from_file(task)
	@all_tasks << Task.new(task.to_s)
end

def add_completed_task_from_file(task)
		@completed_tasks << Task.new(task.to_s)
	end

def add_task
	puts "Enter a description for this task:"
	description = gets.chomp
	this_task = Task.new(description)
	@all_tasks << this_task
	self.show_tasks
end

def show_tasks
	puts ""
	@all_tasks.each_with_index { |task, i| 
		puts "#{i}.#{task.description}"
	}
	puts ""
end
def show_completed
	puts ""
	@completed_tasks.each_with_index { |task, i| 
		puts "#{i}.#{task.description}"
	}
	puts ""
end

def delete(number)
	@all_tasks.delete_at(number.to_i)
	self.show_tasks
end

def save
	list_file = File.open("todo_list.txt", "w")
	@all_tasks.each { |item| list_file.puts item.description }
	list_file = File.open("completed_list.txt", "w")
	@completed_tasks.each { |item| list_file.puts item.description }
end
end




# This is the task class. It creates and manages at the task level.
class Task
attr_reader :description
attr_reader :time
def initialize(description)
	@description = description
	@time = DateTime.now
end
end


program = Control.new