Python3

Python script: Contact Manager Application (with CRUD operations)


class Contact:
def __init__(self, name, phone_number, email):
self.name = name
self.phone_number = phone_number
self.email = email

class ContactManager:
def __init__(self):
self.contacts = []

def add_contact(self, contact):
self.contacts.append(contact)

def update_contact(self, old_name, new_contact):
for index, contact in enumerate(self.contacts):
if contact.name == old_name:
self.contacts[index] = new_contact
break

def delete_contact(self, name):
for contact in self.contacts:
if contact.name == name:
self.contacts.remove(contact)
break

def get_contact(self, name):
for contact in self.contacts:
if contact.name == name:
return contact
return None

# Sample Usage
contact1 = Contact("Alice", "1234567890", "alice@example.com")
contact2 = Contact("Bob", "9876543210", "bob@example.com")

contact_manager = ContactManager()
contact_manager.add_contact(contact1)
contact_manager.add_contact(contact2)

print("Contacts:")
for contact in contact_manager.contacts:
print(contact.name, contact.phone_number, contact.email)

updated_contact = Contact("Alice", "9999999999", "alice@example.com")
contact_manager.update_contact("Alice", updated_contact)

print("\nContacts after update:")
for contact in contact_manager.contacts:
print(contact.name, contact.phone_number, contact.email)

contact_manager.delete_contact("Bob")

print("\nContacts after deletion:")
for contact in contact_manager.contacts:
print(contact.name, contact.phone_number, contact.email)

searched_contact = contact_manager.get_contact("Alice")
if searched_contact:
print("\nSearched Contact:")
print(searched_contact.name, searched_contact.phone_number, searched_contact.email)
else:
print("\nContact not found.")

A Contact class is defined to represent a contact with attributes like name, phone number, and email. A ContactManager class manages a list of contacts and provides methods to add, update, delete, and get contacts.

In the sample usage section, two contacts are created and added to the ContactManager. The contacts are then displayed, one of the contacts is updated, the updated list is displayed, a contact is deleted, the updated list is displayed again, and finally, a contact is searched by name.

This script demonstrates a basic Contact Manager application with CRUD operations.