Mr_HO1A

PhoneBook

Nov 13th, 2018
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.33 KB | None | 0 0
  1. // Author - Nfinity [Aman]
  2. // System - One
  3. // Date - 11/13/2018 {mm/dd/yyyy}
  4.  
  5. #include <iostream>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <algorithm>
  9.  
  10. using namespace std;
  11.  
  12. class Contact{
  13.     private:
  14.         string name;
  15.         string number;
  16.         int numberId;
  17.         int isSet = 0;
  18.     public:
  19.         void setContact(string name,string number,int numberId){
  20.             this->name = name;
  21.             this->number = number;
  22.             this->numberId = numberId;
  23.             cout<<"Contact Saved"<<endl;
  24.         }
  25.         int getIsSet(){
  26.             return this->isSet;
  27.         }
  28.         string getNumber(){
  29.             return this->number;
  30.         }
  31.         string getName(){
  32.             return this->name;
  33.         }
  34. };
  35.  
  36. class PhoneBook{
  37.     private:
  38.         const static int contactListLength = 100;
  39.         Contact contacts[100];
  40.         int numberId = 0;
  41.     public:
  42.         int checkIfSaved(string number){
  43.             int i = 0;
  44.             for(i = 0;i<contactListLength;i++){
  45.                 if(contacts[i].getIsSet() == 0){
  46.                     break;
  47.                 }
  48.                 else{
  49.                     if(number.compare(contacts[i].getNumber()) == 0){
  50.                         return 1;
  51.                     }
  52.                     else{
  53.                         continue;
  54.                     }
  55.                 }
  56.             }
  57.             return 0;
  58.         }
  59.         void saveContact(){
  60.             string name,number;
  61.             cout<<"Enter Contact Name : ";
  62.             cin>>name;
  63.             cout<<"Enter Contact Number : ";
  64.             cin>>number;
  65.             if(checkIfSaved(number) == 0){
  66.                 contacts[numberId].setContact(name,number,numberId);
  67.                 this->numberId++;
  68.                 pause();
  69.                 clearScreen();
  70.             }
  71.             else{
  72.                 cout<<"Contact Already Present! With number "<<number<<endl;
  73.             }
  74.         }
  75.         void printAllContacts(){
  76.             clearScreen();
  77.             int i = 0;
  78.             cout<<"Contacts"<<endl;
  79.             printLines();
  80.             for(i=0;i<numberId;i++){
  81.                 cout<<"Name : "<<contacts[i].getName()<<endl;
  82.                 cout<<"Number : "<<contacts[i].getNumber()<<endl;
  83.             }
  84.             printLines();
  85.             pause();
  86.             clearScreen();
  87.         }
  88.         void printContactWithIndex(int index){
  89.             cout<<"Name : "<<contacts[index].getName()<<endl;
  90.             cout<<"Number : "<<contacts[index].getNumber()<<endl;
  91.         }
  92.         void searchUsingNumber(){
  93.             clearScreen();
  94.             string number;
  95.             cout<<"Enter atleast starting 3 Numbers to search"<<endl;
  96.             cout<<"Enter Number To Search : ";
  97.             cin>>number;
  98.             printLines();
  99.             int lenghtOfEnteredNumber = number.length();
  100.             int i = 0, flag = 0;
  101.             for(i=0; i < numberId; i++){
  102.                 string numberSplited = contacts[i].getNumber().substr(0,lenghtOfEnteredNumber);
  103.                 if(number.compare(numberSplited) == 0){
  104.                     printLines();
  105.                     printContactWithIndex(i);
  106.                     printLines();
  107.                     flag++;
  108.                 }
  109.             }
  110.             if(flag == 0){
  111.                 cout<<"No Match(s) Found!"<<endl;
  112.             }
  113.             pause();
  114.             clearScreen();
  115.         }
  116.         void searchUsingName(){
  117.             clearScreen();
  118.             string name;
  119.             cout<<"Enter atleast starting 3 Character to search"<<endl;
  120.             cout<<"Enter Name To Search : ";
  121.             cin>>name;
  122.             // Convert Name To LowerCase
  123.             name = converToLower(name);
  124.             int lenghtOfEnteredName = name.length();
  125.             int i = 0, flag = 0;
  126.             for(i=0; i < numberId; i++){
  127.                 string nameSplitted = converToLower(contacts[i].getName().substr(0,lenghtOfEnteredName));
  128.                 if(name.compare(nameSplitted) == 0){
  129.                     printLines();
  130.                     printContactWithIndex(i);
  131.                     printLines();
  132.                     flag++;
  133.                 }
  134.             }
  135.             if(flag == 0){
  136.                 cout<<"No Match(s) Found!"<<endl;
  137.             }
  138.             pause();
  139.             clearScreen();
  140.         }
  141.         string converToLower(string s){
  142.             string data = s;
  143.             transform(data.begin(), data.end(), data.begin(), ::tolower);
  144.             return data;
  145.         }
  146.         void printLines(){
  147.             int i=0,lineCount = 25;
  148.             for(i = 0; i <= lineCount; i++){
  149.                 cout<<"=";
  150.             }
  151.             cout<<"\n";
  152.         }
  153.         void printHelp(){
  154.             cout<<"Actions"<<endl;
  155.             cout<<"Press 1 : To Save Contact"<<endl;
  156.             cout<<"Press 2 : To Display Contacts"<<endl;
  157.             cout<<"Press 3 : To Search Using Number"<<endl;
  158.             cout<<"Press 4 : To Search Using Name"<<endl;
  159.             cout<<"Press 99 : To Exit"<<endl;
  160.             actions();
  161.         }
  162.         void actions(){
  163.             int action = 0;
  164.             cout<<"Action : ";
  165.             cin>>action;
  166.             if(action == 1){
  167.                 saveContact();
  168.             }
  169.             else if(action == 2){
  170.                 printAllContacts();
  171.             }
  172.             else if(action == 3){
  173.                 searchUsingNumber();
  174.             }
  175.             else if(action == 4){
  176.                 searchUsingName();
  177.             }
  178.             else if(action == 99){
  179.                 exit(0);
  180.             }
  181.             else{
  182.                 cout<<"Please Enter A Valid Option"<<endl;
  183.             }
  184.         }
  185.         void clearScreen(){
  186.             system("CLS");
  187.         }
  188.         void pause(){
  189.             system("pause");
  190.         }
  191.         PhoneBook(){
  192.             while(1==1){
  193.                 printHelp();
  194.             }
  195.         }
  196. };
  197.  
  198. int main(){
  199.     PhoneBook pb;
  200. }
Add Comment
Please, Sign In to add comment