Untitled
unknown
plain_text
a year ago
3.1 kB
4
Indexable
import datetime class Note: note_count = 0 def __init__(self, text, tag): self.text = text self.tag = tag self.date = datetime.datetime.now() Note.note_count += 1 self.ID = Note.note_count def match(self, keyword): return keyword in self.text or keyword in self.tag class Notebook: def __init__(self): self.notes = [] def new_note(self, text, tag): self.notes.append(Note(text, tag)) def modify_text(self, note_id, new_text): for note in self.notes: if note.ID == note_id: note.text = new_text def modify_tag(self, note_id, new_tag): for note in self.notes: if note.ID == note_id: note.tag = new_tag def search(self, keyword): matching_notes = [note for note in self.notes if note.match(keyword)] return matching_notes class Menu: def __init__(self): self.notebook = Notebook() self.options = { "1": self.show_notes, "2": self.search_notes, "3": self.add_note, "4": self.modify_note, "5": self.quit } @staticmethod def show_menu(): print("Menu:") print("1. Show notes") print("2. Search notes") print("3. Add note") print("4. Modify note") print("5. Quit") def run(self): while True: self.show_menu() choice = input("Enter your choice: ") action = self.options.get(choice) if action: action() else: print("Invalid choice. Please try again.") def show_notes(self): for note in self.notebook.notes: print(f"ID: {note.ID}, Date: {note.date}, Tag: {note.tag}, Text: {note.text}") def search_notes(self): keyword = input("Enter keyword to search: ") matching_notes = self.notebook.search(keyword) if matching_notes: print("Matching notes:") for note in matching_notes: print(f"ID: {note.ID}, Date: {note.date}, Tag: {note.tag}, Text: {note.text}") else: print("No matching notes found.") self.show_notes() # Show all notes after search def add_note(self): text = input("Enter note text: ") tag = input("Enter note tag: ") self.notebook.new_note(text, tag) print("Note added successfully.") def modify_note(self): note_id = int(input("Enter note ID to modify: ")) new_text = input("Enter new text: ") new_tag = input("Enter new tag: ") self.notebook.modify_text(note_id, new_text) self.notebook.modify_tag(note_id, new_tag) print("Note modified successfully.") @staticmethod def quit(): print("Exiting the program.") exit() if __name__ == "__main__": menu = Menu() menu.run()
Editor is loading...
Leave a Comment