class Menu: def __init__(self): self.notebook = Notebook() #dictionary - also called object in python and JS self.choices = { "1": self.show_notes, "2": self.search_notes, "3": self.add_note, "4": self.modify_note, "5": self.quit } def displayMenu(self): #to indent in the same way as type here, use """ print(""" Notebook Menu 1. Show all notes 2. Search notes 3. Add note 4. Modify note 5. Quit """) def run(self): #run forever while True: self.displayMenu() choice = input("Enter an option:") action = self.choices.get(choice) if action: action() else: print("{0} is not a valid choice".format(choice)) def quit(self): print("Thank you for using the notebook.") sys.exit(0) def show_notes(self, notes=None): if not notes: notes = self.notebook.getNotes() for note in notes: self.notebook.print_note(note.getId()) def search_notes(self): filter = input("search for:") notes = self.notebook.search(filter) show_notes(notes) def add_note(self): memo = input("Enter a memo:") self.notebook.new_note(memo) print("Your note has been added") def modify_note(self): noteId = int(input("Enter the note ID")) memo = input("Enter a memo:") tags = input("Enter tags:") if memo: self.notebook.modify_memo(noteId, memo) if tags: self.notebook.modify_tags(noteId, tags)
class Menu: """ Display Menu options to user via Command Line to perform the Notebook operations """ def __init__(self): """ 1. Initialize the Notebook instance 2. Define the mapping for the menu and operations (methods) to be performed """ self.notebook = Notebook() self.choices = { "1": self.show_notes, "2": self.search_notes, "3": self.add_note, "4": self.modify_note, "5": self.modify_tags, "6": self.quit } # Static Method as it only used for display purpose and data operations performed. @staticmethod def display_menu(): """ Menu to be displayed to the user :return: """ print(""" Notebook Menu 1. Show all Notes 2. Search Notes 3. Add Note 4. Modify Note 5. Modify Tags 6. Quit """) def run(self): """ Display the user menu and obtain the user input for the required operation :return: """ self.display_menu() # Obtain user input choice = input("Enter an option: ") action = self.choices.get(choice) if action: action() else: print("{} is not a valid choice, please select a valid choice") # Call the menu again to continue additional operations self.run() def show_notes(self, notes=None, filter_key=False): """ Display all the notes linked to the Notebook :return: ID, memo, tags """ if not notes and not filter_key: notes = self.notebook.Notes elif not notes and filter_key: print("No Notes can be found with the keyword provided") for note in notes: print("{}: {}/{}".format(note.id, note.memo, note.tags)) def search_notes(self): """ Input search keyword from input Search for a user input keyword from the notes and associated tags :return: list of notes with the keyword """ keyword = input("Enter a keyword to search: ") results = self.notebook.search(keyword) self.show_notes(results, True) def add_note(self): """ adds note to an existing notebook :return: Add a note to notebook """ memo = input("Enter the new note: ") self.notebook.new_note(memo) print("New Note has been added") def modify_note(self): """ Modify note for a given note with an ID provided by the user :return: Modify note of a existing Notebook """ note_id = input("Enter the note id: ") memo = input("Enter the modified note: ") result = self.notebook.modify_note(int(note_id), memo) if result: print("Note {} is modified".format(note_id)) else: print("Note ID does not exist, please provide another id") def modify_tags(self): """ Modify tags for a given note with an ID provided by the user :return: Modify tags of a existing Notebook """ note_id = input("Enter the note id: ") tags = input("Enter the modified tag(s): ") result = self.notebook.modify_tag(int(note_id), tags) if result: print("Tag {} is modified".format(note_id)) else: print("Note ID does not exist, please provide another id") @staticmethod def quit(): print("Thank you for using the Notebook app") sys.exit(0)
class Menu: def __init__(self): self.notebook = Notebook() self.choices = { "1": self.show_notes, "2": self.search_notes, "3": self.add_note, "4": self.modify_note, "5": self.quit } def display_menu(self): print(""" Notebook Menu 1. Show all Notes 2. Search Notes 3. Add Note 4. Modify Note 5. Quit """) def run(self): while True: self.display_menu() choice = input("Enter an option: ") action = self.choices.get(choice) if action: try: action() except: print("Something went wrong. Please try again.") else: print("{0} is not a valid choice".format(choice)) def show_notes(self, notes=None): if not notes: notes = self.notebook.notes for note in notes: print("{0}: {1}\n{2}".format(note.id, note.memo, note.tags)) def search_notes(self): filter = input("Search for: ") notes = self.notebook.search(filter) self.show_notes(notes) def add_note(self): memo = input("Enter a memo: ") self.notebook.new_note(memo) print("Your note has been added.") def modify_note(self): id = int(input("Enter a note id: ")) memo = input("Enter a memo: ") tags = input("Enter tags: ") if memo: self.notebook.modify_memo(id, memo) if tags: self.notebook.modify_tags(id, tags) def quit(self): print("Thank you for using the notebook.") sys.exit(0)
class Menu: def __init__(self): self.notebook = Notebook() self.notebook.new_note('Example note', ['Example tag 1', 'Example tag 2']) def run(self): self.showMenu() self.waitForInput() def showMenu(self): print() print('Notebook Menu') print('-------------') print('Choose action:') print('1 - Show all notes') print('2 - Search notes') print('3 - Add a note') print('4 - Modify a note') print('5 - Quit') def waitForInput(self): option = input('Action: ') if option == '1': self.showNotes() elif option == '2': self.searchNote() elif option == '3': self.addNote() elif option == '4': self.modifyNote() elif option != '5': print('Please choose a valid action.') self.waitForInput() def printNotes(self, notes): first = True for note in notes: if not first: print() first = False note.print() def showNotes(self): print() print('All notes') print('---------') if len(self.notebook.notes) == 0: print('There are no notes.') else: self.printNotes(self.notebook.notes) self.run() def searchNote(self): print() print('Search for a note') print('-----------------') text = input('Search for: ') result = self.notebook.search(text) if len(result) == 0: print("No notes found.") else: print() print('Results') print('-------') self.printNotes(self.notebook.search(text)) self.run() def addNote(self): print() print('Add a note') print('----------') memo = input('Enter a note: ') self.saveNoteDialog(Note(memo)) def saveNoteDialog(self, note): print() print('Save note?') print('----------') self.printNotes([note]) print() print('Choose action:') print('1 - Save') print('2 - Edit') print('3 - Add a tag') print('4 - Remove a tag') self.waitForNoteSaveInput(note) def waitForNoteSaveInput(self, note): action = input('Action: ') if action == '1': self.notebook.notes.append(note) print('Note saved.') self.run() elif action == '2': self.editNote(note) elif action == '3': self.addNoteTag(note) elif action == '4': self.removeNoteTag(note) else: print('Please choose a valid action.') self.waitForNoteSaveInput(note) def editNote(self, note): print() print('Edit a note') print('-----------') text = input('New note: ') note.setMemo(text) self.saveNoteDialog(note) def addNoteTag(self, note): print() print('Add note tag') print('------------') tag = input('Tag: ') note.addTag(tag) self.saveNoteDialog(note) def removeNoteTag(self, note): print() print('Remove note tag') print('---------------') tag = input('Tag: ') note.removeTag(tag) self.saveNoteDialog(note) def modifyNote(self): print() print('Modify a note') print('-------------') self.waitForNoteModifyInput() def waitForNoteModifyInput(self): note_id = input('Note id: ') note = self.notebook.remove_note(uuid.UUID(note_id)) if note == False: print('Note not found.') self.waitForNoteModifyInput() else: self.saveNoteDialog(note)