Learning by doing: Reading books and trying to understand the (code) examples
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.9 KiB

  1. import sys
  2. from notebook import Notebook, Note
  3. class Menu:
  4. '''Display a menu and respond to choices when run.'''
  5. def __init__(self):
  6. self.notebook = Notebook()
  7. self.choices = {
  8. "1": self.show_notes,
  9. "2": self.search_notes,
  10. "3": self.add_note,
  11. "4": self.modify_note,
  12. "5": self.quit
  13. }
  14. def display_menu(self):
  15. print("""
  16. Notebook Menu
  17. 1. Show all Notes
  18. 2. Search Notes
  19. 3. Add Note
  20. 4. Modify Note
  21. 5. Quit """)
  22. def run(self):
  23. '''Display the menu and respond to choices.'''
  24. while True:
  25. self.display_menu()
  26. choice = input("Enter an option: ")
  27. action = self.choices.get(choice)
  28. if action:
  29. action()
  30. else:
  31. print("{0} is not a valid choice".format(choice))
  32. def show_notes(self, notes=None):
  33. if not notes:
  34. notes = self.notebook.notes
  35. for note in notes:
  36. print("{0}: {1}\n{2}".format(note.id, note.tags, note.memo))
  37. def search_notes(self):
  38. filter = input("Search for: ")
  39. notes = self.notebook.search(filter)
  40. self.show_notes(notes)
  41. def add_note(self):
  42. memo = input("Enter a memo: ")
  43. self.notebook.new_note(memo)
  44. print("Your note has been added.")
  45. def modify_note(self):
  46. id = input("Enter a note id: ")
  47. memo = input("Enter a memo: ")
  48. tags = input("Enter tags: ")
  49. if memo:
  50. if not self.notebook.modify_memo(id, memo):
  51. print("Note with id {0} doesn't exist.".format(id))
  52. if tags:
  53. if not self.notebook.modify_tags(id, tags):
  54. print("Note with id {0} doesn't exist.".format(id))
  55. def quit(self):
  56. print("Thank you for using your notebook today.")
  57. sys.exit(0)
  58. if __name__ == "__main__":
  59. Menu().run()