from Contact import Contact, ContactList
def main():
contact_list = ContactList()
# Load contacts from file
file_name = "contacts.txt"
try:
contact_list.load_contacts(file_name)
print("Contacts loaded successfully!")
except FileNotFoundError:
print("No contacts found in file.")
while True:
print("\n1. Add Contact")
print("2. Remove Contact")
print("3. Update Contact")
print("4. Search Contact")
print("5. Show All Contacts")
print("6. Quit Program")
try:
choice = int(input("\nEnter your choice: "))
except ValueError:
print("Invalid input, please enter a number between 1 and 6.")
continue
if choice == 1:
name = input("Enter name: ")
phone_number = input("Enter phone number: ")
contact = Contact(name, phone_number)
contact_list.add_contact(contact)
print("\nContact added successfully!")
elif choice == 2:
name = input("Enter name: ")
contact_list.remove_contact(name)
print("\nContact removed successfully!")
elif choice == 3:
name = input("Enter name: ")
new_phone_number = input("Enter new phone number: ")
contact_list.update_contact(name, new_phone_number)
print("\nContact updated successfully!")
elif choice == 4:
name = input("Enter name: ")
contact = contact_list.search_contact(name)
if contact:
print("\nName: {} \t Phone Number: {}".format(contact.get_name(), contact.get_phone_number()))
else:
print("\nContact not found!")
elif choice == 5:
contact_list.show_all_contacts()
elif choice == 6:
# Save contacts to file before quitting program
contact_list.save_contacts(file_name)
print("\nExiting program...")
break
if __name__ == '__main__':
main()