1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
#include <iostream> #include <unordered_map> #include <string> class Customer { public: Customer(const std::string& name, const std::string& email) : name(name), email(email) {} void updateEmail(const std::string& newEmail) { email = newEmail; } void display() const { std::cout << "Name: " << name << ", Email: " << email << std::endl; } private: std::string name; std::string email; }; class CRMSystem { public: void addCustomer(const std::string& id, const std::string& name, const std::string& email) { if (customers.find(id) == customers.end()) { customers[id] = Customer(name, email); std::cout << "Customer added: " << name << std::endl; } else { std::cout << "Customer ID already exists!" << std::endl; } } void updateCustomerEmail(const std::string& id, const std::string& newEmail) { if (customers.find(id) != customers.end()) { customers[id].updateEmail(newEmail); std::cout << "Email updated for Customer ID: " << id << std::endl; } else { std::cout << "Customer ID not found!" << std::endl; } } void viewCustomer(const std::string& id) const { if (customers.find(id) != customers.end()) { customers.at(id).display(); } else { std::cout << "Customer ID not found!" << std::endl; } } private: std::unordered_map<std::string, Customer> customers; }; int main() { CRMSystem crm; // Adding customers crm.addCustomer("001", "Alice Johnson", "alice@example.com"); crm.addCustomer("002", "Bob Smith", "bob@example.com"); // Viewing customer details crm.viewCustomer("001"); crm.viewCustomer("002"); // Updating email crm.updateCustomerEmail("001", "alice.johnson@example.com"); // Viewing updated details crm.viewCustomer("001"); return 0; } |
Explanation:
- Customer Class:
- Represents a customer with
name
andemail
. updateEmail(const std::string& newEmail)
: Updates the customer’s email address.display() const
: Displays the customer’s details.
- Represents a customer with
- CRMSystem Class:
- Manages multiple customers using an unordered map (
std::unordered_map
). addCustomer(const std::string& id, const std::string& name, const std::string& email)
: Adds a new customer if the ID does not already exist.updateCustomerEmail(const std::string& id, const std::string& newEmail)
: Updates the email of an existing customer.viewCustomer(const std::string& id) const
: Displays the details of a customer based on their ID.
- Manages multiple customers using an unordered map (
- main Function:
- Demonstrates adding customers, viewing their details, updating email addresses, and viewing updated details.
Compilation:
AdvertisementTo compile the program, use:
1g++ crm_system.cpp -o crm_system
Run the program with:
1./crm_system
- Demonstrates adding customers, viewing their details, updating email addresses, and viewing updated details.