File size: 5,505 Bytes
5ab8a9f
1
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyP2zlSVYJ4J50S2+/fCcECX"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","execution_count":1,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"kAF1ZFoEGPqX","executionInfo":{"status":"ok","timestamp":1736242306993,"user_tz":-360,"elapsed":64167,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"cc642356-ad04-4339-e3a0-90e237a9d84b"},"outputs":[{"output_type":"stream","name":"stdout","text":["\n","--- Contact Management System ---\n","1. Add Contact\n","2. View Contacts\n","3. Search Contact\n","4. Update Contact\n","5. Delete Contact\n","6. Exit\n","Choose an option: 1\n","Enter contact name: walid\n","Enter phone number: 01792103884\n","Enter email address: walidmunsi@gmail.com\n","Contact added successfully!\n","\n","--- Contact Management System ---\n","1. Add Contact\n","2. View Contacts\n","3. Search Contact\n","4. Update Contact\n","5. Delete Contact\n","6. Exit\n","Choose an option: 2\n","\n","--- Contact List ---\n","Name: walid, Phone: 01792103884, Email: walidmunsi@gmail.com\n","-------------------\n","\n","\n","--- Contact Management System ---\n","1. Add Contact\n","2. View Contacts\n","3. Search Contact\n","4. Update Contact\n","5. Delete Contact\n","6. Exit\n","Choose an option: 6\n","Contacts saved. Goodbye!\n"]}],"source":["# Mini project combining concepts learned\n","import os\n","\n","class ContactManager:\n","    def __init__(self, file_name=\"contacts.txt\"):\n","        self.file_name = file_name\n","        self.contacts = {}\n","        self.load_contacts()\n","\n","    def load_contacts(self):\n","        if os.path.exists(self.file_name):\n","            with open(self.file_name, \"r\") as file:\n","                for line in file:\n","                    name, phone, email = line.strip().split(\",\")\n","                    self.contacts[name] = {\"phone\": phone, \"email\": email}\n","\n","    def save_contacts(self):\n","        with open(self.file_name, \"w\") as file:\n","            for name, details in self.contacts.items():\n","                file.write(f\"{name},{details['phone']},{details['email']}\\n\")\n","\n","    def add_contact(self):\n","        name = input(\"Enter contact name: \")\n","        if name in self.contacts:\n","            print(\"Contact already exists.\")\n","            return\n","        phone = input(\"Enter phone number: \")\n","        email = input(\"Enter email address: \")\n","        self.contacts[name] = {\"phone\": phone, \"email\": email}\n","        print(\"Contact added successfully!\")\n","\n","    def view_contacts(self):\n","        if not self.contacts:\n","            print(\"No contacts found.\")\n","            return\n","        print(\"\\n--- Contact List ---\")\n","        for name, details in self.contacts.items():\n","            print(f\"Name: {name}, Phone: {details['phone']}, Email: {details['email']}\")\n","        print(\"-------------------\\n\")\n","\n","    def search_contact(self):\n","        name = input(\"Enter contact name to search: \")\n","        if name in self.contacts:\n","            details = self.contacts[name]\n","            print(f\"Name: {name}, Phone: {details['phone']}, Email: {details['email']}\")\n","        else:\n","            print(\"Contact not found.\")\n","\n","    def update_contact(self):\n","        name = input(\"Enter contact name to update: \")\n","        if name in self.contacts:\n","            phone = input(\"Enter new phone number: \")\n","            email = input(\"Enter new email address: \")\n","            self.contacts[name] = {\"phone\": phone, \"email\": email}\n","            print(\"Contact updated successfully!\")\n","        else:\n","            print(\"Contact not found.\")\n","\n","    def delete_contact(self):\n","        name = input(\"Enter contact name to delete: \")\n","        if name in self.contacts:\n","            del self.contacts[name]\n","            print(\"Contact deleted successfully!\")\n","        else:\n","            print(\"Contact not found.\")\n","\n","    def main_menu(self):\n","        while True:\n","            print(\"\\n--- Contact Management System ---\")\n","            print(\"1. Add Contact\")\n","            print(\"2. View Contacts\")\n","            print(\"3. Search Contact\")\n","            print(\"4. Update Contact\")\n","            print(\"5. Delete Contact\")\n","            print(\"6. Exit\")\n","            choice = input(\"Choose an option: \")\n","            if choice == \"1\":\n","                self.add_contact()\n","            elif choice == \"2\":\n","                self.view_contacts()\n","            elif choice == \"3\":\n","                self.search_contact()\n","            elif choice == \"4\":\n","                self.update_contact()\n","            elif choice == \"5\":\n","                self.delete_contact()\n","            elif choice == \"6\":\n","                self.save_contacts()\n","                print(\"Contacts saved. Goodbye!\")\n","                break\n","            else:\n","                print(\"Invalid choice. Please try again.\")\n","\n","if __name__ == \"__main__\":\n","    manager = ContactManager()\n","    manager.main_menu()"]},{"cell_type":"code","source":[],"metadata":{"id":"8-9g8oXoGeBr"},"execution_count":null,"outputs":[]}]}