Starting – Contact Management System
A Contact Management System is software which is used for manage the contact information. This system allows user to perform various operations such as adding new contacts, deleting existing contacts, updating contact information, searching for specific contacts, and displaying all stored contacts.
Features of the Project
- Contact Record:
- Each contact record typically includes attributes such as contact ID, name, phone number, email address, and address.
- Functionalities:
- Add Contact: Allows adding a new contact record to the system.
- Delete Contact: Deletes an existing contact record based on contact ID.
- Update Contact: Modifies information (e.g., phone number, email) of an existing contact.
- Search Contact: Searches for a contact record based on contact ID or name.
- Display All Contacts: Displays all contact records stored in the system.
- Data Management:
- Utilizes data structures like arrays, linked lists, or dynamic arrays (vectors) to store and manage contact records.
- Implements file handling for data persistence (optional).
C++ Codes for Contact Management System
#include <iostream> #include <vector> #include <algorithm> // for std::find_if class Contact { private: int contactID; std::string name; std::string phoneNumber; std::string email; std::string address; public: Contact(int id, const std::string& n, const std::string& phone, const std::string& email, const std::string& addr) : contactID(id), name(n), phoneNumber(phone), email(email), address(addr) {} int getContactID() const { return contactID; } std::string getName() const { return name; } std::string getPhoneNumber() const { return phoneNumber; } std::string getEmail() const { return email; } std::string getAddress() const { return address; } void displayDetails() const { std::cout << "Contact ID: " << contactID << "\n"; std::cout << "Name: " << name << "\n"; std::cout << "Phone Number: " << phoneNumber << "\n"; std::cout << "Email: " << email << "\n"; std::cout << "Address: " << address << "\n"; std::cout << "------------------\n"; } void updatePhoneNumber(const std::string& newPhoneNumber) { phoneNumber = newPhoneNumber; } void updateEmail(const std::string& newEmail) { email = newEmail; } void updateAddress(const std::string& newAddress) { address = newAddress; } }; class ContactManagementSystem { private: std::vector<Contact> contacts; int nextContactID = 1; // Start contact IDs from 1 and increment public: void addContact(const std::string& name, const std::string& phoneNumber, const std::string& email, const std::string& address) { contacts.emplace_back(nextContactID++, name, phoneNumber, email, address); } void deleteContact(int contactID) { auto it = std::find_if(contacts.begin(), contacts.end(), [contactID](const Contact& c) { return c.getContactID() == contactID; }); if (it != contacts.end()) { contacts.erase(it); std::cout << "Contact with ID " << contactID << " deleted.\n"; } else { std::cout << "Contact with ID " << contactID << " not found.\n"; } } void updateContact(int contactID, const std::string& newPhoneNumber, const std::string& newEmail, const std::string& newAddress) { auto it = std::find_if(contacts.begin(), contacts.end(), [contactID](const Contact& c) { return c.getContactID() == contactID; }); if (it != contacts.end()) { it->updatePhoneNumber(newPhoneNumber); it->updateEmail(newEmail); it->updateAddress(newAddress); std::cout << "Contact with ID " << contactID << " updated.\n"; } else { std::cout << "Contact with ID " << contactID << " not found.\n"; } } void searchContact(int contactID) const { auto it = std::find_if(contacts.begin(), contacts.end(), [contactID](const Contact& c) { return c.getContactID() == contactID; }); if (it != contacts.end()) { it->displayDetails(); } else { std::cout << "Contact with ID " << contactID << " not found.\n"; } } void displayAllContacts() const { for (const auto& contact : contacts) { contact.displayDetails(); } } }; int main() { ContactManagementSystem cms; int choice; int contactID; std::string name; std::string phoneNumber; std::string email; std::string address; do { std::cout << "\nContact Management System Menu\n"; std::cout << "1. Add Contact\n"; std::cout << "2. Delete Contact\n"; std::cout << "3. Update Contact\n"; std::cout << "4. Search Contact\n"; std::cout << "5. Display All Contacts\n"; std::cout << "6. Exit\n"; std::cout << "Enter your choice: "; std::cin >> choice; switch (choice) { case 1: std::cout << "Enter name, phone number, email, address: "; std::cin.ignore(); // To ignore the newline character left by std::cin std::getline(std::cin, name); std::getline(std::cin, phoneNumber); std::getline(std::cin, email); std::getline(std::cin, address); cms.addContact(name, phoneNumber, email, address); std::cout << "Contact added successfully.\n"; break; case 2: std::cout << "Enter contact ID to delete: "; std::cin >> contactID; cms.deleteContact(contactID); break; case 3: std::cout << "Enter contact ID, new phone number, new email, and new address: "; std::cin >> contactID; std::cin.ignore(); // To ignore the newline character left by std::cin std::getline(std::cin, phoneNumber); std::getline(std::cin, email); std::getline(std::cin, address); cms.updateContact(contactID, phoneNumber, email, address); break; case 4: std::cout << "Enter contact ID to search: "; std::cin >> contactID; cms.searchContact(contactID); break; case 5: std::cout << "Displaying all contacts:\n"; cms.displayAllContacts(); break; case 6: std::cout << "Exiting...\n"; break; default: std::cout << "Invalid choice. Please try again.\n"; } } while (choice != 6); return 0; }