-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes_manager.py
More file actions
118 lines (77 loc) · 2.99 KB
/
Copy pathnotes_manager.py
File metadata and controls
118 lines (77 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import json
import os
FILE_NAME = "notes.json"
class NotesManager:
def __init__(self):
self.notes = self.load_notes()
def load_notes(self):
if not os.path.exists(FILE_NAME):
return []
with open(FILE_NAME, "r") as file:
return json.load(file)
def save_notes(self):
with open(FILE_NAME, "w") as file:
json.dump(self.notes, file, indent=4)
def add_note(self):
title = input("Enter note title: ").strip()
content = input("Enter note content: ").strip()
new_note = {
"title": title,
"content": content
}
self.notes.append(new_note)
self.save_notes()
print("Note added successfully! ✅")
def view_notes(self):
if not self.notes:
print("No notes found.")
return
print("\n===== YOUR NOTES =====")
for i, note in enumerate(self.notes, start=1):
print(f"{i}. {note['title']}")
print(f" {note['content']}")
def search_notes(self):
keyword = input("Enter keyword to search: ").strip().lower()
found = False
for note in self.notes:
if keyword in note["title"].lower() or keyword in note["content"].lower():
print("\n===== NOTE FOUND =====")
print(f"Title : {note['title']}")
print(f"Content : {note['content']}")
found = True
if not found:
print("No matching notes found.")
def update_note(self):
self.view_notes()
if not self.notes:
return
try:
choice = int(input("Enter note number to update: "))
if choice < 1 or choice > len(self.notes):
print("Invalid note number.")
return
note = self.notes[choice - 1]
new_title = input(f"Enter new title ({note['title']}): ").strip()
new_content = input(f"Enter new content ({note['content']}): ").strip()
if new_title:
note["title"] = new_title
if new_content:
note["content"] = new_content
self.save_notes()
print("Note updated successfully! ✅")
except ValueError:
print("Please enter a valid number.")
def delete_note(self):
self.view_notes()
if not self.notes:
return
try:
choice = int(input("Enter note number to delete: "))
if choice < 1 or choice > len(self.notes):
print("Invalid note number.")
return
deleted_note = self.notes.pop(choice - 1)
self.save_notes()
print(f"Deleted: {deleted_note['title']} ✅")
except ValueError:
print("Please enter a valid number.")