this program seems to be working well so far, but I know it can be cleaner and more efficients with certain functions/methods. As I'm still beginning to code any feedback would be very welcome.
# Creating class named contacts with methods for editing and adding new
# contacts. Also there are static methods for showing a specific or all
# contacts data and for showing if there's no contact saved on the address book.
class Contact:
def __init__(self, name, surname, number, email):
self.name = name
self.surname = surname
self.number = number
self.email = email
def edit_name(self, name):
self.name = name
return self.name
def edit_surname(self, surname):
self.surname = surname
return self.surname
def edit_number(self, number):
self.number = number
return self.number
def edit_email(self, email):
self.email = email
return self.email
@classmethod
def add(cls, name, surname, number, email):
return cls(name, surname, number, email)
# This method prints prints all data of a specific entry it index is provided
# otherwise it prints data of all entries saved in address book (fullnames,
# numbers and emails)
@staticmethod
def summary(index=0):
if index == 0:
for j in range(0, len(address_book)):
print(address_book[j].name, address_book[j].surname, end=' / ')
print(address_book[j].number, '/', address_book[j].email)
else:
print(address_book[index].name, address_book[index].surname, end=' / ')
print(address_book[index].number, '/', address_book[index].email)
print()
return None
# Prints only the names saved in the address book
@staticmethod
def saved():
print('CONTACTS SAVED: ', end='')
for j in range(0, len(address_book)):
print(j, address_book[j].name, end=' || ')
return None
# Prompts the user if there's no contact saved in address book
@staticmethod
def isempty(list):
if len(list) == 0:
print('NO CONTACT SAVED\n')
return True
return False
The program now prompts the user for an option ranging from 0-5. Each option has a funcion in the address book: add, modify, delete, view, view all, finish.
address_book = []
msg_error = '{}Invalid option{}'.format('\033[31m', '\033[m' # Red text
access = input('Press any key to access')
while True:
print('=-=-===-=-=-=-=- CONTACTS MENU -=-=-=-=-=-==-==')
print("""[ 1 ] ADD [ 3 ] DELETE [ 5 ] VIEW ALL
[ 2 ] MODIFY [ 4 ] VIEW [ 0 ] FINISH""")
option = input('>>> ')
#This if and elif blocks check if user input ranges from 0-5
if not option.isnumeric():
print(msg_error)
continue
elif option not in '012345':
print(msg_error)
continue
# If between 0-5, convert value to integer and...
else:
option = int(option)
if option == 0:
print('>>> Program ended\n')
break
# Add new contact
elif option == 1:
name = input('Name: ').capitalize().strip()
surname = input('Surname: ').capitalize().strip()
number = input('Number: ').strip()
email = input('Email: ').strip().lower()
# Trasnform into Contact class and append to address book
address_book.append(Contact.add(name, surname, number, email))
print('Contact saved\n')
# Modify a contact
elif option == 2:
if Contact.isempty(address_book):
continue
Contact.saved()
name_index = int(input('\nModify which name? '))
print('\nModify which entry?')
entry_index = int(input('[ 1 ] NAME [ 2 ] SURNAME [ 3 ] NUMBER [ 4 ] EMAIL\n>>>'))
# Use object methods to modify within the list address book
# User wants to modify name
if entry_index == 1:
modification = input('New name: ').capitalize().strip()
address_book[name_index].edit_name(modification)
# User wants to modify surname
elif entry_index == 2:
modification = input('New surname: ').capitalize().strip()
address_book[name_index].edit_surname(modification)
# User wants to modify number
elif entry_index == 3:
modification = input('New number: ').strip()
address_book[name_index].edit_number(modification)
# User wants to modify email
elif entry_index == 4:
modification = input('New email: ').lower().strip()
address_book[name_index].edit_email(modification)
print('Modification saved\n')
# Delete a contact
elif option == 3:
if Contact.isempty(address_book):
continue
Contact.saved()
name_index = int(input('\nWhich contact delete? '))
del address_book[name_index]
print('Contact deleted')
# View specific contact details
elif option == 4:
if Contact.isempty(address_book):
continue
Contact.saved()
index = int(input('\nContact position: '))
Contact.summary(index)
# View details of all contacts
elif option == 5:
if Contact.isempty(address_book):
continue
Contact.summary()