Guest User

Untitled

a guest
Oct 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.57 KB | None | 0 0
  1. class ToDoList
  2.  
  3. def initialize
  4. puts "What's your name, good sir?"
  5. name = gets.strip
  6. puts greeting(name) #this uses the greeting below
  7.  
  8. puts "1.) Make A To-Do List \n2.) Load A To-Do List\n"
  9. while do_action = gets.chomp
  10. case do_action
  11. when "1"
  12. make_list #this should optionally take params, like name.
  13. when "2"
  14. get_list #This should ask for a file name that gets passed as a param to the 'get_list' method like get_list('grocery')
  15. else
  16. puts "I'm sorry, I don't understand. You can only enter from the options! :)"
  17. end
  18. end
  19. end
  20.  
  21. def greeting(name)
  22. unless name.nil?
  23. case Time.now.hour
  24. when (6...12)
  25. time_phase = "morning"
  26. when (12...16)
  27. time_phase = "afternoon"
  28. when (16...20)
  29. time_phase = "evening"
  30. when (20..24) || (1...6)
  31. time_phase = "night"
  32. end
  33. return "\nGood #{time_phase}, Master #{name}! What would you like me to do?"
  34. end
  35. end
  36.  
  37. #OK...so these methods are terrible. I don't have time to refactor them and they should be broken up into multiple methods, one for each 'concern' or action
  38. #Also, the if/case statements are a terrible mess. I'll finish the refactor later so you can see a better way to do this.
  39. def make_list
  40. puts "Okay! What name would you like to name the list?"
  41. topic_header = gets.chomp.upcase
  42. file = File.new("#{topic_header}.txt", 'w')
  43. file << "////////////////////////// #{topic_header} ////////////////////////// \n"
  44. puts "\nAlright, #{name}! \nTemme the list items!"
  45. i = 0
  46. i2 = 0
  47. i3 = 0
  48. while input = gets
  49. i += 1
  50. if input =~ /^END/
  51. # if i == 1
  52. # num_tasks = "task"
  53. # elsif i == 2
  54. # num_tasks = "task"
  55. # else
  56. # num_tasks = "tasks"
  57. # end
  58. file << "\n----------------------------------------------------------------------------------------------------------------------"
  59. file << "\n #{"#"*i} Main Tasks: \t Total: #{i - 1} Fin: 0 left: #{i - 1}"
  60. file << "\n #{"#"*i} Sub Tasks: \t\t Total:#{i2} Fin:0 left:#{i2}"
  61. file << "\n----------------------------------------------------------------------------------------------------------------------"
  62. break
  63. end
  64. puts ">> " + input
  65. file << "\n#{i}.) " + input.strip + " [_] \n"
  66. puts "Would you like any other items under this? Please answer in yes or no."
  67. do_action2 = gets.chomp
  68. case do_action2.downcase
  69. when 'yes'
  70. i2 -= 1
  71. #i3 -= 1
  72. puts "Okay! Tell me then!"
  73. while input2 = gets
  74. i2 += 1
  75. #i3 += 1
  76. if input2 =~ /^END/
  77. #file << "\n\t#{i3} tasks, 0 finished, #{i3} left.\n"
  78. #i3 = 0
  79. break
  80. end
  81. puts ">> \t" + input2
  82. file << "\t #{i2 + 1}-> " + input2.strip + " ||\n"
  83. end
  84. puts "Enter the next list item!"
  85. when 'no'
  86. puts "Okay, as you wish!"
  87. puts "Enter the next list item then!"
  88. else
  89. puts "Awwwwhhh man...you shouldn't have done that. Program terminating in 3..2..1"
  90. return
  91. end
  92. end
  93. puts "Alrighto, #{name}o! Your to-do list has been prepared using Ruby magic. Goodbye for now! :D"
  94. file.close
  95. break
  96.  
  97. end
  98.  
  99. def get_list
  100. puts "Which file would you like to read, bro?"
  101. #1) you could even use the Ruby "File" lib here (in place of IO) to list files in
  102. # the current dir and choose one.
  103. #2) This is extremely error-prone. What if they enter a non-existent file? An integer? etc.
  104. commnd = gets.strip
  105. filelines = IO.readlines("#{commnd}.txt")
  106. puts "\nYour file has been loaded, good sir. What would you like to do with it?\n\n"
  107.  
  108. # OK.. so there are at least 2 better ways to structure this:
  109. # 1) Break the various actions into methods (i.e. "list_tasks","number_tasks_remaining",etc)
  110. # and call them inside 'get_list'
  111. # 2) Keep this code below as a method, but pass the action in as a parameter (i.e. list_action(params) )
  112. puts "1.) List the number of tasks left. \n2.) List each task. \n3.) List completed tasks. \n4.) List incomplete tasks. \n5.) Mark a task complete."
  113. while do_action3 = gets.strip
  114. case do_action3
  115. when "1"
  116. if filelines[-3] =~ /Main Tasks/
  117. puts filelines[-3]
  118. else
  119. puts "Sorry, this file doesn't specify main tasks."
  120. end
  121. if filelines[-2] =~ /Sub Tasks/
  122. puts filelines[-2]
  123. else
  124. puts "Sorry, this file doesn't specify sub tasks."
  125. end
  126. break
  127. when "2"
  128. $task_lines = []
  129. def capture_task(val)
  130. $task_lines << val
  131. end
  132. i4 = 0
  133. filelines.each do |line|
  134. i4 += 1
  135. if line =~ /\.\)/ || line =~ /->/
  136. if line =~ /\.\)/
  137. puts "\n" + line
  138. elsif line =~ /->/
  139. puts line
  140. end
  141. capture_task(line)
  142. end
  143. end
  144. if $task_lines[0] == $task_lines[-1]
  145. puts "No tasks found."
  146. end
  147. break
  148. when "3"
  149. $fin_lines = []
  150. def capture_fin(val)
  151. $fin_lines << val
  152. end
  153. filelines.each do |fin_task|
  154. if fin_task =~ /\[X\]/ || fin_task =~ /\|X\|/
  155. puts fin_task
  156. capture_fin(fin_task)
  157. #else
  158. #puts "Looks like you haven't completed anything!" if fin_task == filelines[-1]
  159. end
  160. end
  161. if $fin_lines[0] == $fin_lines[-1] && $fin_lines[0].class != String
  162. puts "No completed tasks found!"
  163. end
  164. break
  165. when "4"
  166. $left_lines = []
  167. def capture_left(val)
  168. $left_lines << val
  169. end
  170. filelines.each do |left_task|
  171. if left_task =~ /\[_\]/ || left_task =~ /\|\|/
  172. puts left_task
  173. capture_left(left_task)
  174. #else
  175. #puts "Looks like you've completed everything!" if left_task == filelines[-1]
  176. end
  177. end
  178. if $left_lines[0] == $left_lines[-1] && $left_lines[0].class != String
  179. puts "No incomplete tasks found!"
  180. end
  181. break
  182. when "5"
  183. puts "Which task would you like to mark as completed?"
  184. filelines.each do |left_task|
  185. puts left_task if left_task =~ /\[_\]/ || left_task =~ /\|\|/
  186. end
  187. numenum = (total_tasks, fin_tasks, left_tasks = filelines[-3].scan(/\d+/).each)
  188. subnumenum = (total_subtasks, fin_subtasks, left_subtasks = filelines[-2].scan(/\d+/).each)
  189. total_tasks, total_subtasks = numenum.next.to_i, subnumenum.next.to_i
  190. fin_tasks, fin_subtasks = numenum.next.to_i, subnumenum.next.to_i
  191. left_tasks, left_subtasks = numenum.next.to_i, subnumenum.next.to_i
  192. # puts total_tasks
  193. # puts fin_tasks
  194. # puts left_tasks
  195. while do_action4 = gets.chomp
  196. if do_action4 =~ /^END/
  197. file2 = File.new("#{commnd}.txt", "w")
  198. file2.puts $capt_fin
  199. break
  200. else
  201. if do_action4 =~ /\.\)/
  202. fin_tasks += 1
  203. left_tasks -= 1
  204. total_tasks = fin_tasks + left_tasks
  205. elsif do_action4 =~ /->/
  206. fin_subtasks += 1
  207. left_subtasks -= 1
  208. total_subtasks = fin_subtasks + left_subtasks
  209. end
  210.  
  211. puts "Do you want to mark another task as completed?"
  212. $capt_fin = []
  213. end
  214. if do_action4 =~ /\.\)/
  215. do_action4.gsub!(/\./, '\.')
  216. do_action4.gsub!(/\)/, '\)')
  217. end
  218. if do_action4 =~ /->/
  219. if do_action4.length == 3
  220. do_action4 = " " + do_action4
  221. end
  222. end
  223.  
  224. filelines.each do |left_task|
  225. if left_task =~ Regexp.new(do_action4)
  226. if left_task =~ /\.\)/
  227. left_task.gsub!(/\[_\]/,"[X] \t\t(DONE MOFO!)")
  228. puts "BTW, Congratulations for finishing a main task! :D"
  229. # fin_tasks += 1
  230. # left_tasks -= 1
  231. # total_tasks = fin_tasks + left_tasks
  232. # filelines[-3].gsub!(Regexp.new("Fin:" + (fin_tasks - 1).to_s), "Fin:#{fin_tasks}")
  233. # filelines[-3].gsub!(Regexp.new("left:" + (left_tasks - 1).to_s), "#left:{left_tasks}")
  234. elsif left_task =~ /->/
  235. left_task.gsub!(/\|\|/, "|X|")
  236. puts "BTW, you're one step ahead! :D"
  237. end
  238. $capt_fin << left_task
  239. elsif left_task =~ /Main Tasks/
  240. left_task.gsub!(Regexp.new("Fin: " + (fin_tasks - 1).to_s), "Fin: #{fin_tasks}")
  241. left_task.gsub!(Regexp.new("left: " + (left_tasks + 1).to_s), "left: #{left_tasks}")
  242. $capt_fin << left_task
  243. elsif left_task =~ /Sub Tasks/
  244. left_task.gsub!(Regexp.new("Fin: " + (fin_subtasks - 1).to_s), "Fin: #{fin_subtasks}")
  245. left_task.gsub!(Regexp.new("left: " + (left_subtasks + 1).to_s), "left: #{left_subtasks}")
  246. $capt_fin << left_task
  247. else
  248. $capt_fin << left_task
  249. end
  250. end
  251. end
  252. break
  253. end
  254. end
  255. break
  256. end
  257.  
  258. end
Add Comment
Please, Sign In to add comment