Final Project (For Review and help with new line issue)


#1

I did this slightly differently
I wanted to create a class for each task that would allow you to keep all the data together for each item

(i hope this is ok!)

module Menu

def menu
"To Do: 1)Add 2)Show 3)Update 4)Delete 5)Write 6)Read 7)Toggle Status Q)Quit \n"
end

def show
menu
end

end

def prompt(message = “Empty.”, symbol = '> ')
print message
print symbol
gets.chomp
end
class Task

attr_accessor :task
attr_accessor :index
attr_accessor :toggle

def initialize(task,toggle=" ")
  @task=task
  @toggle=toggle
  @index
end
  def to_machine
  "#{represent_status}:#{description}"
end

end

class List
attr_reader :all_tasks

def initialize
end

def Add
puts “New Task:”
$tasks<<Task.new(gets.chomp)
end

def Show
$tasks.each_with_index do |task,i|
if i>0
task.index=i
puts "[#{task.toggle}] #{task.index}. #{task.task}"
end
end
end

def Update
puts "Item No. to Update?"
index=gets.chomp
puts "New Task:“
new_task=gets.chomp
$tasks[index.to_i].task=new_task
$tasks[index.to_i].toggle=” "
end

def Delete
puts "Item No. to Delete?"
index=gets.chomp
$tasks.delete_at(index.to_i)
end

def Write(filename)
machinified=""
$tasks.each_with_index do |task,i|
if i>0
machinified<<"#{task.toggle}/#{task.task}\n"
end
end

IO.write(filename, machinified)

end

def Read(filename)
IO.readlines(filename).each do |line|
read_line=line.split(’/’)
$tasks<<Task.new(read_line[1],read_line[0])
end
end

def Toggle
puts "Item No. to Toggle?“
index=gets.chomp
$tasks[index.to_i].toggle==” “?$tasks[index.to_i].toggle=“X”:$tasks[index.to_i].toggle=” "
self.Show
end
end

include Menu
$tasks = []
$tasks[0] = ""
my_list = List.new

print ">"
until [‘q’].include?(choice=prompt(show).downcase)
case choice
when "1"
my_list.Add
when "2"
my_list.Show
when "3"
my_list.Update
when "4"
my_list.Delete
when '5’
my_list.Write(prompt ‘What is the filename to
write to?’)
when '6’
begin
my_list.Read(prompt ‘What is the filename to
read from?’)
rescue Errno::ENOENT
puts 'File name not found, verify file name
and path.'
end
when "7"
my_list.Toggle
else
puts "Please choose an Option 1-7 or Q for Quit."
end
end

I have a slight issue with a new line being created at the end of written files. which would be a problem if you wanted to load multiple files into memory