Software & Finance





Visual C++ - Phone Book Directory Management System





Here is the Visual C++ Source Code for Phone Book Management.


Source Code


// TestPhBk.cpp : Defines the entry point for the console application.

//

 

#include <iostream>

#include <iomanip>

 

#include <map>

#include <list>

#include <string>

#include <iostream>

#include <algorithm>

 

using namespace std;

 

class CPhoneBookInfo;

 

// Map container to hold PhoneType to PhoneNo or Vice-Versa

typedef map<string, string>         TMapPhoneByType;

typedef pair<string, string>    TPairPhoneByType;

 

// Common MultiMap container to hold PhoneBook information either for Name/PhoneNo

// Can hold same Name, PhNos to hold duplicate entries

// If required, can be managed with unique key string based on distinct phone no (or) User details.

typedef multimap<string, CPhoneBookInfo>    TMultiMapPhoneBook;

typedef pair<string, CPhoneBookInfo>            TPairPhoneBook;

 

typedef TMapPhoneByType::iterator               TMapPhoneByTypeIt;

// Common List container to collect the various phones per user

typedef list<TMapPhoneByTypeIt>                       TMapPhoneByTypeItList;

 

typedef TMultiMapPhoneBook::iterator            TMultiMapPhoneBookIt;

// Common List container to collect the Phone Directory details based on

// 1.User details <FirstName>[|MI|LN|Addr] format

// 2.Phone number

typedef list<TMultiMapPhoneBookIt>              TMultiMapPhoneBookItList;

 

// Class to hold per user details.

class CPhoneBookInfo

{

public:

      CPhoneBookInfo()

      {

            ClearData();

      }

 

      CPhoneBookInfo(string FN, string MI, string LN, string Addr, string PhType, string PhNo)

      {

            // Invalid information verification, ignore if wrong

            if (FN.length() <= 0 || PhNo.length() <= 0)

                  return;

 

            // Valid information available to add phone info

            m_strFirstName = FN;

            m_strMiddleInitial = MI.length() > 0? MI : "";

            m_strLastName = LN.length() > 0? LN : "";

            m_strAddress = Addr.length() > 0? Addr : "";

           

            AddPhoneEntry(PhType, PhNo);

      }

 

      virtual ~CPhoneBookInfo()

      {

            ClearData();

      }

 

      // Attributes

private:

      string                  m_strFirstName;

      string                  m_strMiddleInitial;

      string                  m_strLastName;

      string                  m_strAddress;

 

      TMapPhoneByType   m_mapPhoneByType;

      int               m_nCntPhone;

 

      // Methods

 

      void ClearData()

      {

            m_strFirstName.empty();

            m_strMiddleInitial.empty();

            m_strLastName.empty();

            m_strAddress.empty();

 

            m_mapPhoneByType.clear();

            m_nCntPhone = 0;

      }

 

public:

      // Set Property

      void SetFirstName(const string strFirstName) { m_strFirstName = strFirstName; }

      void SetMiddleInitial(const string strMiddleInitial) { m_strMiddleInitial = strMiddleInitial; }

      void SetLastName(const string strLastName) { m_strLastName = strLastName; }

      void SetAddress(const string strAddress) { m_strAddress = strAddress; }

 

      // Get Property

      string GetFirstName() const { return m_strFirstName; }

      string GetMiddleInitial() const { return m_strMiddleInitial; }

      string GetLastName() const { return m_strLastName; }

      string GetAddress() const { return m_strAddress; }

     

      // Useful to create unique key contraint based on User details.

      string GetUniqueKey() const

      {

            return      m_strFirstName + "|" +  m_strMiddleInitial +  "|"

                        m_strLastName + "|" +  m_strAddress;

      }

 

      void AddPhoneEntry(string typePhone, string PhoneNo)

      {

            //Assume PhoneType as 'Other' if no valid type info

            m_mapPhoneByType.insert(TPairPhoneByType(typePhone.length() > 0? typePhone : "Other", PhoneNo));

            m_nCntPhone = m_mapPhoneByType.size();

      }

 

      bool UpdatePhoneEntry(string typePhone, string PhoneNo)

      {

            bool bOk = false;

 

            TMapPhoneByTypeIt it = m_mapPhoneByType.find(typePhone.c_str());

            if (it != m_mapPhoneByType.end())

            {

                  m_mapPhoneByType[typePhone] = PhoneNo;

                  bOk = true;

            }

            else

                  AddPhoneEntry(typePhone, PhoneNo);

 

            return bOk;

      }

 

      // Returns the phone number associate per user.

      TMapPhoneByTypeItList GetPhoneEntries()

      {

            TMapPhoneByTypeItList TMPhByTypeIL;

            for (TMapPhoneByTypeIt it = m_mapPhoneByType.begin();

                  it != m_mapPhoneByType.end(); it++)

                  TMPhByTypeIL.push_back( it );

            return TMPhByTypeIL;

      }

 

      bool DelPhoneEntry(string typePhone)

      {

            bool bDelete = false;

 

            TMapPhoneByTypeIt it = m_mapPhoneByType.find(typePhone.c_str());

            if (it != m_mapPhoneByType.end())

            {

                  m_mapPhoneByType.erase(it);

                  bDelete = true;

            }

 

            return bDelete;

      }

 

      void PrintPhoneBookInfo()

      {

            cout << endl;

            cout << "Firstname      : " << m_strFirstName << endl;

            cout << "Middle Initial : " << m_strMiddleInitial << endl;

            cout << "Lastname       : " << m_strLastName << endl;

            cout << "Address        : " << m_strAddress << endl;

            cout << "Phone List     : ";

            for (TMapPhoneByTypeIt it = m_mapPhoneByType.begin();

                  it != m_mapPhoneByType.end(); it++)

                  cout << "(" << it->first << ") " << it->second << " ";

            cout << endl;

      }

 

      void PrintTabularFmtPhoneBookInfo(bool bWithHeaders = false)

      {

            if (bWithHeaders == true)

            {

                  cout << endl;

                  cout << "Firstname    MI Lastname     Address      Phone List" << endl;

                  cout << "================================================================================" << endl;

            }

            cout << setiosflags(ios_base::left) << setw(13) << m_strFirstName << setw(3);

            cout << m_strMiddleInitial << setw(13) << m_strLastName << setw(13) << m_strAddress;

            for (TMapPhoneByTypeIt it = m_mapPhoneByType.begin();

                  it != m_mapPhoneByType.end(); it++)

                  cout << "(" << it->first << ") " << it->second << " ";

            cout << endl;

      }

};

 

// Class to hold Telephone Directory.

class CPhoneDirectory

{

public:

      CPhoneDirectory()

      {

            ClearDirectory();

      }

 

      virtual ~CPhoneDirectory()

      {

            ClearDirectory();

      }

 

      // Attributes

private:

      // Having Name<->PhNo container

      TMultiMapPhoneBook            m_PhoneBookByName;

      // Having PhNo<->Name container, for easy reverse lookup processing.

      TMultiMapPhoneBook            m_PhoneBookByPhNo;

 

      // Methods

      void ClearDirectory()

      {

            m_PhoneBookByName.clear();

            m_PhoneBookByPhNo.clear();

      }

 

      void GetUniqueSrchKey_NameType(string& SrchKey)

      {

            int i, nSepCount = 0;

            string::size_type nQueryPos = 0;

           

            for (i = 0; i < 3; i++)

            {

                  nQueryPos = SrchKey.find("|", nQueryPos);

                  if (string::npos != nQueryPos)

                        nSepCount++;

                  else

                        break;

            }

            for (i = nSepCount; i < 3; i++)

                  SrchKey += "|";

      }

 

public:

 

      bool AddDirectoryEntry(string EntryData, CPhoneBookInfo PhBkEntry, bool DefByName = true ) //* false -> DefByPhNo

      {

            bool bOk = false;

 

            // Invalid information verification, ignore if wrong

            if (EntryData.length() <= 0 || PhBkEntry.GetUniqueKey().length() <= 0)

            {

                  cout << "WARNING : Please make sure to give valid Phone Book input information!" << endl;

                  return bOk;

            }

 

            if (!DefByName)

            {

                  m_PhoneBookByPhNo.insert(TPairPhoneBook(EntryData, PhBkEntry));

                  m_PhoneBookByName.insert(TPairPhoneBook(PhBkEntry.GetUniqueKey(), PhBkEntry));

            }

            else

            {

                  m_PhoneBookByName.insert(TPairPhoneBook(PhBkEntry.GetUniqueKey(), PhBkEntry));

                  TMapPhoneByTypeItList TMPhByTypeIL = PhBkEntry.GetPhoneEntries();

                  for (; !TMPhByTypeIL.empty(); TMPhByTypeIL.pop_front() )

                        m_PhoneBookByPhNo.insert(TPairPhoneBook(TMPhByTypeIL.front()->second, PhBkEntry));

            }

 

            return bOk;

      }

 

      bool DelDirectoryEntry(string EntryData, bool DefByName = true

    // false -> DefByPhNo

)

      {

            bool bOk = false;

 

            // Invalid information verification, ignore if wrong

            if (EntryData.length() <= 0)

            {

                  cout << "WARNING : Please make sure to give valid Phone Book input information!" << endl;

                  return bOk;

            }

 

            if (!DefByName)

            {

                  string EntryByName = "";

                  TMultiMapPhoneBookIt itPhBk = m_PhoneBookByPhNo.find(EntryData.c_str());

                  if (itPhBk != m_PhoneBookByPhNo.end())

                  {

                        EntryByName.assign(itPhBk->second.GetUniqueKey());

                        m_PhoneBookByPhNo.erase(itPhBk);

                        bOk = true;

                       

                        // Also delete the relevant map entry from PhoneBook By Name collection

                        TMultiMapPhoneBookIt itPhBk = m_PhoneBookByName.find(EntryByName.c_str());

                        if (itPhBk != m_PhoneBookByName.end())

                        {

                              m_PhoneBookByName.erase(itPhBk);

                              bOk = true;

                        }

                  }

            }

            else

            {

                  TMultiMapPhoneBookIt itPhBk = m_PhoneBookByName.find(EntryData.c_str());

                  if (itPhBk != m_PhoneBookByName.end())

                  {

                        TMapPhoneByTypeItList TMPhByTypeIL = itPhBk->second.GetPhoneEntries();

                        for (; !TMPhByTypeIL.empty(); TMPhByTypeIL.pop_front() )

                              m_PhoneBookByPhNo.erase(TMPhByTypeIL.front()->second);

 

                        m_PhoneBookByName.erase(itPhBk);

                        bOk = true;

                  }

            }

 

            return bOk;

      }

 

      TMultiMapPhoneBookItList FindDirectoryEntry(string EntryData,

            bool DefByName = true ,

            bool bExactMatch = true)

      {

            TMultiMapPhoneBookItList TMMPhBkByTypeIL;

            // Invalid information verification, ignore if wrong

            if (EntryData.length() <= 0)

            {

                  cout << "WARNING : Please make sure to give valid Phone Book input information!" << endl;

                  return TMMPhBkByTypeIL;

            }

 

            pair<TMultiMapPhoneBookIt, TMultiMapPhoneBookIt> rangeList;

            if (!DefByName)

            {

                  if (!bExactMatch)

                  {     // Slows down when there would be huge data as we walkthru each and every item

                        for (TMultiMapPhoneBookIt itPhBk = m_PhoneBookByPhNo.begin();

                              itPhBk != m_PhoneBookByPhNo.end(); itPhBk++)

                        {

                              if (itPhBk->first.find(EntryData) != string::npos)

                                    TMMPhBkByTypeIL.push_back(itPhBk);

                        }

                  }

                  else

                  {

                        for (rangeList = m_PhoneBookByPhNo.equal_range(EntryData);

                              rangeList.first != rangeList.second && rangeList.first != m_PhoneBookByPhNo.end();

                              ++rangeList.first )

                              TMMPhBkByTypeIL.push_back(rangeList.first);

                  }

            }

            else

            {

                  if (!bExactMatch)

                  {     // Slows down when there would be huge data as we walkthru each and every item

                        for (TMultiMapPhoneBookIt itPhBk = m_PhoneBookByName.begin();

                              itPhBk != m_PhoneBookByName.end(); itPhBk++)

                        {

                              if (itPhBk->first.find(EntryData) != string::npos)

                                    TMMPhBkByTypeIL.push_back(itPhBk);

                        }

                  }

                  else

                  {

                        GetUniqueSrchKey_NameType(EntryData);

                        for (rangeList = m_PhoneBookByName.equal_range(EntryData);

                              rangeList.first != rangeList.second && rangeList.first != m_PhoneBookByName.end();

                              ++rangeList.first )

                              TMMPhBkByTypeIL.push_back(rangeList.first);

                  }

            }

            return TMMPhBkByTypeIL;

      }

 

      void SearchDirectory(string EntryData,

            bool bSrchByName = true, // false -> bSrchByPhNo,

            bool bExactMatch = true) //false -> Any part of Details

      {

            // Invalid information verification, ignore if wrong

            if (EntryData.length() <= 0)

            {

                  cout << "WARNING : Please make sure to give valid Phone Book Search information!" << endl;

                  return;

            }

 

            string EntryDataKey = EntryData;

            TMultiMapPhoneBookItList TMMPhBkByTypeIL = FindDirectoryEntry(EntryDataKey, bSrchByName, bExactMatch);

            cout << endl << (bSrchByName? "Phone Book information by Name: " : "Phone Book information by Number: ");

            cout << EntryData << endl;

            //cout << "--------------------------------------------------------------------------------" << endl;

            if (TMMPhBkByTypeIL.empty())

                  cout << "Sorry, no matches found!" << endl;

            else

            {

                  cout << TMMPhBkByTypeIL.size() << (bExactMatch? " (Exact)" : " (Closest)") << " matches found!" << endl;

                  cout << "--------------------------------------------------------------------------------";

                  PrintDirectoryHeader();

                  for (; !TMMPhBkByTypeIL.empty(); TMMPhBkByTypeIL.pop_front() )

                        TMMPhBkByTypeIL.front()->second.PrintTabularFmtPhoneBookInfo(false);

            }

            cout << endl;

      }

 

      void PrintDirectoryHeader()

      {

            cout << endl;

            cout << "Firstname    MI Lastname     Address      Phone List" << endl;

            cout << "================================================================================" << endl;

      }

 

      void PrintDirectory(bool bListByName = true, bool bListByPhNo = true)

      {

            if (bListByName)

            {

                  PrintDirectoryHeader();

                  for (TMultiMapPhoneBookIt itPhBk = m_PhoneBookByName.begin();

                        itPhBk != m_PhoneBookByName.end(); itPhBk++)

                        itPhBk->second.PrintTabularFmtPhoneBookInfo();

            }

           

            if (bListByPhNo)

            {

                  PrintDirectoryHeader();

                  for (TMultiMapPhoneBookIt itPhBk = m_PhoneBookByPhNo.begin();

                        itPhBk != m_PhoneBookByPhNo.end(); itPhBk++)

                        itPhBk->second.PrintTabularFmtPhoneBookInfo();

            }

      }

};

 

int main()

{

      bool bPhBk = true;

     

      if (bPhBk)

      {

            CPhoneBookInfo cPBK1("Bala", "", "Raghupathi","222 Birch Rd", "Cell", "7327634068");

            CPhoneBookInfo cPBK2("Bala", "R", "Raghavan","200 New Rd", "Work", "7327634069");

            CPhoneBookInfo cPBK3("John", "S", "Black","2 Wood Ave", "Home", "92314070");

            CPhoneBookInfo cPBK4("Kumar", "S", "Shiv","1 S Wood Ave", "Cell", "9234634071");

            cPBK4.AddPhoneEntry("Other", "7327634072");

            CPhoneBookInfo cPBK5("Rita", "", "","", "Work", "7327634073");

            CPhoneBookInfo cPBK6("Rita", "", "","", "Cell", "7327634074");

            CPhoneBookInfo cPBK7("Rita", "R", "","", "Cell", "7327634075");

            cPBK7.AddPhoneEntry("Home", "7327634032");

            CPhoneBookInfo cPBK8("Joe", "R", "","4 West St", "Cell", "7327634075");

            CPhoneBookInfo cPBK9("Fred", "", "S","", "Work", "7327634075");

            CPhoneBookInfo cPBK10("Muruga", "", "K","6 Green ST", "Cell", "7327634075");

 

            CPhoneDirectory cPhoneDirectory;

            cPhoneDirectory.AddDirectoryEntry(cPBK1.GetUniqueKey(), cPBK1);

            cPhoneDirectory.AddDirectoryEntry(cPBK2.GetUniqueKey(), cPBK2);

            cPhoneDirectory.AddDirectoryEntry(cPBK3.GetUniqueKey(), cPBK3);

            cPhoneDirectory.AddDirectoryEntry(cPBK1.GetUniqueKey(), cPBK1);

            cPhoneDirectory.AddDirectoryEntry(cPBK4.GetUniqueKey(), cPBK4);

            cPhoneDirectory.AddDirectoryEntry(cPBK5.GetUniqueKey(), cPBK5);

            cPhoneDirectory.AddDirectoryEntry(cPBK6.GetUniqueKey(), cPBK6);

            cPhoneDirectory.AddDirectoryEntry(cPBK7.GetUniqueKey(), cPBK7);

            cPhoneDirectory.AddDirectoryEntry(cPBK8.GetUniqueKey(), cPBK8);

            cPhoneDirectory.AddDirectoryEntry(cPBK9.GetUniqueKey(), cPBK9);

            cPhoneDirectory.AddDirectoryEntry(cPBK10.GetUniqueKey(), cPBK10);

            //cPhoneDirectory.DelDirectoryEntry("7327634072", false);

     

            cPhoneDirectory.PrintDirectory();

            cPhoneDirectory.SearchDirectory("Rita");

            cPhoneDirectory.SearchDirectory("Rita", true, false);

            cPhoneDirectory.SearchDirectory("7327634075", false);

            cPhoneDirectory.SearchDirectory("923", false, false);

            cPhoneDirectory.SearchDirectory("No Name");

            cPhoneDirectory.SearchDirectory("No PhNo", false);

      }

 

      return 0;

}

 

 

Output


 

Firstname    MI Lastname     Address      Phone List

================================================================================

Bala         R  Raghavan     200 New Rd   (Work) 7327634069

Bala            Raghupathi   222 Birch Rd (Cell) 7327634068

Bala            Raghupathi   222 Birch Rd (Cell) 7327634068

Fred            S                         (Work) 7327634075

Joe          R               4 West St    (Cell) 7327634075

John         S  Black        2 Wood Ave   (Home) 92314070

Kumar        S  Shiv         1 S Wood Ave (Cell) 9234634071 (Other) 7327634072

Muruga          K            6 Green ST   (Cell) 7327634075

Rita         R                            (Cell) 7327634075 (Home) 7327634032

Rita                                      (Work) 7327634073

Rita                                      (Cell) 7327634074

 

Firstname    MI Lastname     Address      Phone List

================================================================================

Rita         R                            (Cell) 7327634075 (Home) 7327634032

Bala            Raghupathi   222 Birch Rd (Cell) 7327634068

Bala            Raghupathi   222 Birch Rd (Cell) 7327634068

Bala         R  Raghavan     200 New Rd   (Work) 7327634069

Kumar        S  Shiv         1 S Wood Ave (Cell) 9234634071 (Other) 7327634072

Rita                                      (Work) 7327634073

Rita                                      (Cell) 7327634074

Rita         R                            (Cell) 7327634075 (Home) 7327634032

Joe          R               4 West St    (Cell) 7327634075

Fred            S                         (Work) 7327634075

Muruga          K            6 Green ST   (Cell) 7327634075

John         S  Black        2 Wood Ave   (Home) 92314070

Kumar        S  Shiv         1 S Wood Ave (Cell) 9234634071 (Other) 7327634072

 

Phone Book information by Name: Rita

2 (Exact) matches found!

--------------------------------------------------------------------------------

Firstname    MI Lastname     Address      Phone List

================================================================================

Rita                                      (Work) 7327634073

Rita                                      (Cell) 7327634074

 

 

Phone Book information by Name: Rita

3 (Closest) matches found!

--------------------------------------------------------------------------------

Firstname    MI Lastname     Address      Phone List

================================================================================

Rita         R                            (Cell) 7327634075 (Home) 7327634032

Rita                                      (Work) 7327634073

Rita                                      (Cell) 7327634074

 

 

Phone Book information by Number: 7327634075

4 (Exact) matches found!

--------------------------------------------------------------------------------

Firstname    MI Lastname     Address      Phone List

================================================================================

Rita         R                            (Cell) 7327634075 (Home) 7327634032

Joe          R               4 West St    (Cell) 7327634075

Fred            S                         (Work) 7327634075

Muruga          K            6 Green ST   (Cell) 7327634075

 

 

Phone Book information by Number: 923

2 (Closest) matches found!

--------------------------------------------------------------------------------

Firstname    MI Lastname     Address      Phone List

================================================================================

John         S  Black        2 Wood Ave   (Home) 92314070

Kumar        S  Shiv         1 S Wood Ave (Cell) 9234634071 (Other) 7327634072

 

 

Phone Book information by Name: No Name

Sorry, no matches found!

 

 

Phone Book information by Number: No PhNo

Sorry, no matches found!