-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask_2_Code
More file actions
284 lines (220 loc) · 9.78 KB
/
Task_2_Code
File metadata and controls
284 lines (220 loc) · 9.78 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import sys
import queue
def initial_phonebook():
# Get the number of contacts from the user
rows, cols = int(input("Please enter Initial Number of contacts: ")), 6
phone_book = queue.Queue()
for i in range(rows):
print("\nEnter contact %d details in the Following Order (ONLY):" % (i+1))
print("NOTE: * indicates mandatory fields")
print("....................................................................")
temp = queue.Queue()
for j in range(cols):
if j == 0:
# Get the name from the user
name = str(input("Enter name*: "))
if name == '' or name == ' ':
sys.exit("Name is a mandatory field. Process exiting due to blank field...")
temp.put(name)
if j == 1:
# Get the number from the user
number = int(input("Enter number*: "))
temp.put(number)
if j == 2:
# Get the email address from the user
email = str(input("Enter e-mail address: "))
if email == '' or email == ' ':
email = None
temp.put(email)
if j == 3:
# Get the date of birth from the user
dob = str(input("Enter date of birth(dd/mm/yy): "))
if dob == '' or dob == ' ':
dob = None
temp.put(dob)
if j == 4:
# Get the category from the user
category = str(input("Enter category(Family/Friends/Work/Others): "))
if category == '' or category == ' ':
category = None
temp.put(category)
phone_book.put(temp)
return phone_book
def menu():
# Display the menu options to the user
print("\n********************************************************************")
print("\t\t\tSMARTPHONE DIRECTORY")
print("********************************************************************")
print("\tYou can now perform the following operations on this phonebook\n")
print("1. Add a new contact")
print("2. Remove an existing contact")
print("3. Delete all contacts")
print("4. Edit a contact")
print("5. Search Contact")
print("6. Display all contacts")
print("7. Exit phonebook")
# Get the user's choice
choice = int(input("Please enter your choice: "))
return choice
def edit_contact(pb):
contact = str(input("Please enter the name of the contact you wish to edit: "))
for i in range(pb.qsize()):
temp = pb.get()
if contact == temp.queue[0]:
print("....................................................................")
print("\nEnter updated contact details in the following order (ONLY):")
print("NOTE: '*' Indicates Mandatory Fields")
print("....................................................................")
updated_contact = queue.Queue()
updated_contact.put(contact)
for j in range(1, temp.qsize()):
field = temp.queue[j]
if j == 1:
field = int(input("Enter number '*' : "))
if field == '' or field == ' ':
sys.exit("Number is a mandatory field. Process exiting due to blank field...")
elif j == 2:
field = str(input("Enter e-mail address: "))
if field == '' or field == ' ':
field = None
elif j == 3:
field = str(input("Enter date of birth (dd/mm/yy): "))
if field == '' or field == ' ':
field = None
elif j == 4:
field = str(input("Enter category (Family/Friends/Work/Others): "))
if field == '' or field == ' ':
field = None
updated_contact.put(field)
pb.put(updated_contact)
print("Contact has been updated successfully.")
return
else:
pb.put(temp)
print("Contact not found. Please try again.")
def remove_existing(pb):
# Prompt the user to enter the name of the contact to remove
contact = str(input("Please enter the name of the contact you wish to remove: "))
for i in range(pb.qsize()):
# Get the next contact from the phone book
temp = pb.get()
if contact == temp.queue[0]:
print("Contact Found. Contact is now being deleted from the phonebook...")
print("....................................................................")
print("Contact deleted")
return
else:
# Add the contact back to the phone book if it doesn't match the one to be removed
pb.put(temp)
print("Contact not found. Please try again.")
def search_existing(pb):
# Prompt the user to enter the name of the contact to search
name = str(input("Please enter the name of the contact you wish to search: "))
for i in range(pb.qsize()):
# Get the next contact from the phone book
temp = pb.get()
if name == temp.queue[0]:
print("Contact Found!")
print("....................................................................")
print("\nContact Details:")
print("....................................................................")
print("Name: ", temp.queue[0])
print("Number: ", temp.queue[1])
print("E-mail: ", temp.queue[2])
print("D.O.B: ", temp.queue[3])
print("Category: ", temp.queue[4])
print("....................................................................")
return
else:
# Add the contact back to the phone book if it doesn't match the one being searched
pb.put(temp)
print("Contact not found. Please try again.")
def add_contact(pb):
# Create a new contact queue
contact = queue.Queue()
print("Enter contact details in the following order (ONLY):")
print("NOTE: '*' indicates mandatory fields")
print("....................................................................")
# Prompt the user to enter the contact details
name = str(input("Enter name '*' : "))
if name == '' or name == ' ':
sys.exit("Name is a mandatory field. Process exiting due to blank field...")
contact.put(name)
number = int(input("Enter number '*' : "))
contact.put(number)
email = str(input("Enter e-mail address: "))
if email == '' or email == ' ':
email = None
contact.put(email)
dob = str(input("Enter date of birth (dd/mm/yy): "))
if dob == '' or dob == ' ':
dob = None
contact.put(dob)
category = str(input("Enter category (Family/Friends/Work/Others): "))
if category == '' or category == ' ':
category = None
contact.put(category)
pb.put(contact)
print("Contact has been added successfully.")
def display_all(pb):
if pb.empty():
print("Your phonebook is empty.")
return
print("\nPHONEBOOK CONTACTS: ")
print("....................................................................")
for i in range(pb.qsize()):
# Get the next contact from the phone book
temp = pb.get()
print("Contact ", i+1, ":")
print("....................................................................")
print("Name: ", temp.queue[0])
print("Number: ", temp.queue[1])
print("E-mail: ", temp.queue[2])
print("D.O.B: ", temp.queue[3])
print("Category: ", temp.queue[4])
print(".....................................................................")
# Add the contact back to the phone book
pb.put(temp)
def delete_all(pb):
confirm = str(input("Are you sure you want to delete all contacts? (y/n): "))
if confirm.lower() == 'y':
while not pb.empty():
pb.get()
print("All contacts have been deleted successfully.")
return
print("Operation canceled.")
def run_phonebook():
# Create the initial phonebook
pb = initial_phonebook()
while True:
# Display the menu and get user's choice
choice = menu()
if choice == 1:
# Add a new contact
add_contact(pb)
elif choice == 2:
# Remove an existing contact
remove_existing(pb)
elif choice == 3:
# Delete all contacts
delete_all(pb)
elif choice == 4:
# Edit a contact
edit_contact(pb)
elif choice == 5:
# Search for a contact
search_existing(pb)
elif choice == 6:
# Display all contacts
display_all(pb)
elif choice == 7:
# Exit the phonebook
print("********************************************************************")
print("\t\t\tThank you for using SmartPhone Directory")
print("********************************************************************")
sys.exit(0)
else:
# Invalid choice
print("Invalid choice. Please try again.")
# Call the function to run the phonebook
run_phonebook()