Guest User

Assignment 2

a guest
Dec 31st, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.80 KB | None | 0 0
  1. # Assignment 2 of 40 Days of Ruby
  2. # Author: SirBee
  3. # Date: 2013-12-31
  4.  
  5. puts "\nAssignment 2"
  6. puts "SirBee forged this in a JRuby Enviroment (1.7.9)"
  7. puts "Runtime begins at: " + Time.new.to_s
  8. puts "At the task entry prompt you may enter any of these commands"
  9.  
  10. puts "\n-s:\tSave List"
  11. puts "-e:\tEdit Item"
  12. puts "-d:\tDelete Item"
  13. puts "-q:\tQuit"
  14.  
  15. tasks = []
  16. input = ""
  17.  
  18. if !File.exist?('tasks.txt') # if tasks file not found, create one!
  19.     puts "tasks.txt not found! Forging File..."
  20.     File.new('tasks.txt', 'w') #create file
  21. end
  22.    
  23. tasks = IO.readlines('tasks.txt') # readlines is a function in the IO class that feeds each new line in a file into a container
  24.  
  25. loop do
  26.  
  27.     if tasks.size == 0 # if task container is empty, tell user
  28.         puts "\nYou have no tasks assigned! Please enter Task!"
  29.     else
  30.         puts "\n"
  31.         tasks.each_with_index { |task, index| puts (index+1).to_s + ": " + task.to_s } # print task index and task to console
  32.         puts "\n"
  33.         puts "Please Enter Task."
  34.     end
  35.    
  36.     input = gets.chomp # enter task
  37.    
  38.     # check for option commands in input
  39.     if input == "-q"
  40.         break
  41.     elsif input == "-d"
  42.         puts "Which numbered task would you like to delete?"
  43.         tasks.delete_at(gets.chomp.to_i - 1) # delete task at index
  44.         puts "Task deleted"
  45.     elsif input == "-e"
  46.         puts "Which numbered task would you like to edit?"
  47.         i = (gets.chomp.to_i - 1) # integer variable contains task index
  48.         puts "Please Enter Task."
  49.         tasks[i] = gets.chomp # overwrite that business!
  50.     elsif input == "-s" # IO Save Option
  51.         File.new('tasks.txt', 'w') # if tasks.txt is deleted during runtime, simply create new
  52.         File.open('tasks.txt', 'w') do |file| # for each task in tasks, add new line
  53.             tasks.each { |task| file.puts(task) }
  54.         end
  55.         puts "Tasks Saved!"
  56.     else
  57.         tasks << input # feed string into array
  58.     end
  59.    
  60. end
Advertisement
Add Comment
Please, Sign In to add comment