Skip to content

Instantly share code, notes, and snippets.

Created May 17, 2016 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d3d82d2e6f547be88096e6c8fe423553 to your computer and use it in GitHub Desktop.
Save anonymous/d3d82d2e6f547be88096e6c8fe423553 to your computer and use it in GitHub Desktop.
module Menu
def menu
" Welcome to the TodoList Program!
This menu will help you use the Task List System
1) Add
2) Show
3) Write to a File
4) Read recorded files tasks
5) Delete a task
Q) Quit "
end
def show
menu
end
end
module Promptable
def prompt(message = 'What would you like to do?', symbol = ':> ')
print message
print symbol
gets.chomp
end
end
class List
attr_reader :all_tasks
def initialize
@all_tasks = []
end
def add(task)
@all_tasks << task
end
def show
@all_tasks
end
def write_to_file(filename)
IO.write(filename, @all_tasks.map(&:to_s).join("\n"))
end
def read_from_a_file(filename)
File.open(filename, "r") do |f|
f.each do |line|
puts line
end
end
end
def delete (filename)
@all_tasks.delete(filename)
end
end
class Task
attr_reader :description
def initialize(description)
@description = description
end
def to_s
description.to_s
end
end
if __FILE__ == $PROGRAM_NAME
include Menu
include Promptable
my_list = List.new
puts "please choose from the following list:"
until "q" == (user_input = prompt(show))
case user_input
when "1" then
my_list.add(Task.new(prompt('What do you want to do buddy ?')))
puts my_list.show
when "2" then
puts my_list.show
when "3" then
my_list.write_to_file(prompt("What name do you want to give ?"))
when "4" then
my_list.read_from_a_file(prompt("What file do you want to read ?"))
when "5" then
puts my_list.show
my_list.delete(prompt("What task would you like to delete ?"))
end
prompt('Press enter to continue', '')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment