Advertisement
xxsecure

Menu using classes

Dec 17th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. mport 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. """)
  23. def run(self):
  24. '''Display the menu and respond to choices.'''
  25. while True:
  26. self.display_menu()
  27. choice = input("Enter an option: ")
  28. action = self.choices.get(choice)
  29. if action:
  30. action()
  31. else:
  32. print("{0} is not a valid choice".format(choice))
  33. def show_notes(self, notes=None):
  34. if not notes:
  35. notes = self.notebook.notes
  36. for note in notes:
  37. print("{0}: {1}\n{2}".format(
  38. note.id, note.tags, note.memo))
  39. def search_notes(self):
  40. filter = input("Search for: ")
  41. notes = self.notebook.search(filter)
  42. self.show_notes(notes)
  43. def add_note(self):
  44. memo = input("Enter a memo: ")
  45. self.notebook.new_note(memo)
  46. print("Your note has been added.")
  47. def modify_note(self):
  48. id = input("Enter a note id: ")
  49. memo = input("Enter a memo: ")
  50. tags = input("Enter tags: ")
  51. if memo:
  52. self.notebook.modify_memo(id, memo)
  53. if tags:
  54. self.notebook.modify_tags(id, tags)
  55. def quit(self):
  56. print("Thank you for using your notebook today.")
  57. sys.exit(0)
  58. if __name__ == "__main__":
  59. Menu().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement