-
Notifications
You must be signed in to change notification settings - Fork 1
/
class_user_menu
114 lines (85 loc) · 2.52 KB
/
class_user_menu
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
import datetime
import sys
last_id=0
class Note:
def __init__(self,memo,tags = ''):
self.memo = memo
self.tags = tags
self.creation_date = datetime.date.today()
global last_id
last_id +=1
self.id = last_id
def match(self,filter):
return filter in self.memo or filter in self.tags
class Notebook:
def __init__(self):
self.notes = []
def new_note(self,memo,tags=''):
self.notes.append(Note(memo,tags))
def modify_memo(self,note_id,memo):
for note in self.notes:
if note.id == note_id:
note.memo = memo
break
def modify_tags(self,note_id,tags):
for note in self.notes:
if note.id == note_id:
note.tags = tags
break
def search(self,filter):
return [note for note in self.notes if note.match(filter)]
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:
action()
else:
print('{} 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('{}:{}\n{}'.format(note.id,note.tags,note.memo))
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 = 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 yout notebook')
sys.exit(0)
if __name__ == "__main__":
Menu().run()