Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. class Router
  2.  
  3. # initializing a router object that takes a controller
  4. # controller needs to come from outside, because it will store data (it already stores the repo!)
  5. def initialize(controller)
  6. @controller = controller
  7. @running = true
  8. end
  9.  
  10. # the method that will run the software loop
  11. def run
  12. while @running == true do
  13. # calling private methods of our Router class
  14. user_choice = display_choices
  15. route_to(user_choice)
  16. end
  17. # if the loop ends, say goodbye
  18. puts "Bye bye"
  19. end
  20.  
  21. private
  22.  
  23. def route_to(user_choice)
  24. # route the user to the right action in the controller
  25. case user_choice
  26. when 1 then @controller.create_new_task
  27. when 2 then @controller.display_all_tasks
  28. when 3 then @controller.finish_a_task
  29. when 4 then @controller.delete_a_task
  30. else
  31. # if user presses any other key, set @running to false to stop the loop
  32. @running = false
  33. end
  34. end
  35.  
  36. def display_choices
  37. # print all the choices for the user
  38. # ***TODO***: Do we want the welcome message display every time? Or should we move it somewhere else?
  39. puts "Welcome to the TODO MACHINE 300"
  40. puts "What is your command?"
  41. puts "Press 1 to create a new task"
  42. puts "Press 2 to show task list"
  43. puts "Press 3 to complete task"
  44. puts "Press 4 to delete a task"
  45. puts "Press any other key to exit"
  46. return gets.chomp.to_i
  47. end
  48. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement