sohom15

Phone book 1

Sep 6th, 2014
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.34 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include<string>
  4. #include<iomanip>
  5. #include<fstream>
  6. #include<stdio.h>
  7. #include<cstdlib>
  8. //#include<string.h>
  9.  
  10.  
  11. using namespace std;
  12. const string na("NOT ASSIGNED");
  13. class Name
  14. {
  15.     private:
  16.         void replace_null_name();
  17.     public:
  18.         string first_name;
  19.         string middle_name;
  20.         string last_name;
  21.     public:
  22.         Name()
  23.         {
  24.             first_name.assign("") ;
  25.             middle_name.assign(na);
  26.             last_name.assign(na);
  27.         }
  28.         void take_name();
  29. };
  30. void Name::take_name()
  31. {
  32.     cin.ignore();
  33.     cout<<"\nEnter the First Name (mandatory field)\n";
  34.     getline(cin,first_name);
  35.     cout<<"\nEnter the Middle Name\n";
  36.     getline(cin,middle_name);
  37.     cout<<"\nEnter the Last Name\n";
  38.     getline(cin,last_name);
  39.     replace_null_name();
  40. }
  41. void Name::replace_null_name()
  42. {
  43.     if(first_name.empty())
  44.         {
  45.             first_name.assign("");
  46.             cout<<"pls enter a valid name \n";
  47.             take_name();
  48.         }
  49.     if(middle_name.empty())
  50.         middle_name.assign("");
  51.     if(last_name.empty())
  52.         last_name.assign("");
  53. }
  54. class Phone_No
  55. {
  56.     private:
  57.         void validate_mobile();
  58.         void validate_landline();
  59.     public:
  60.         string landline,mobile;
  61.     public:
  62.         Phone_No()
  63.         {
  64.             landline.assign(na);
  65.             mobile.assign(na);
  66.         }
  67.         void take_mobile_no();
  68.         void take_landline();
  69. };
  70. void Phone_No::take_mobile_no()
  71. {
  72.     cout<<"\n Enter the mobile number (mandatory field) \n";
  73.     getline(cin,mobile);
  74.     validate_mobile();
  75. }
  76. void Phone_No::take_landline()
  77. {
  78.     cout<<"\n Enter the Land line number (with code)\n";
  79.     getline(cin,landline);
  80.     validate_landline();
  81. }
  82. void Phone_No::validate_mobile()
  83. {
  84.     if(!mobile.empty())
  85.     {
  86.         if((mobile.length())!=10)
  87.         {
  88.             cout<<"invalid input";
  89.             mobile.assign(na);
  90.         }
  91.     }
  92.     if(mobile==na)
  93.         {
  94.             cout<<"enter at least one phone number\n";
  95.             take_mobile_no();
  96.         }
  97. }
  98. void Phone_No::validate_landline()
  99. {
  100.     if(!landline.empty())
  101.     {
  102.         if((landline.length())!=11)
  103.         {
  104.             cout<<"invalid input\n";
  105.             landline.assign(na);
  106.         }
  107.     }
  108.     if(landline==na)
  109.         {
  110.             cout<<"enter one phone no";
  111.             take_landline();
  112.         }
  113. }
  114. class Personal_Info:public Phone_No,public Name
  115. {
  116.     private:
  117.         void replace_null_value();
  118.     public:
  119.         string email;
  120.     public:
  121.         Personal_Info()
  122.         {
  123.             email.assign(na);
  124.         }
  125.         Personal_Info(string f_n,string m_n,string l_n,string mo,string la,string e_m)
  126.         {
  127.             first_name.assign(f_n);
  128.             middle_name.assign(m_n);
  129.             last_name.assign(l_n);
  130.             mobile.assign(mo);
  131.             landline.assign(la);
  132.             email.assign(e_m);
  133.         }
  134.         void take_full_info();
  135.         void show_full_info();
  136. };
  137. void Personal_Info::replace_null_value()
  138. {
  139.     if(email.empty())
  140.         email.assign("No Email");
  141. }
  142. void Personal_Info::take_full_info()
  143. {
  144.     cin.ignore();
  145.     cout<<"_____ENTER CONTACT DETAILS_____ \n";
  146.     take_name();
  147.     take_mobile_no();
  148.     take_landline();
  149.     cin.get();
  150.     cout<<"\n Enter the E-mail address \n";
  151.     getline(cin,email);
  152.     replace_null_value();
  153. }
  154. void Personal_Info::show_full_info()
  155. {
  156.     cout<<"\n____DISPLAYING DETAILS_____\n";
  157.     cout<<"Name of Contact :";
  158.     cout<<first_name<<" "<<middle_name<<" "<<last_name<<endl;
  159.     cout<<setw(2)<<"Mobile number :";
  160.     cout<<mobile<<endl;
  161.     cout<<setw(10)<<"Landline number :";
  162.     cout<<landline<<endl;
  163.     cout<<setw(11)<<"E-mail address :";
  164.     cout<<email<<endl;
  165. }
  166. class Phonebook
  167. {
  168.     private:
  169.          void save_info();
  170.     protected:
  171.         Personal_Info contact;
  172.     public:
  173.         void search_name();
  174.         void search_no();
  175.         void edit();
  176.         void show_all();
  177.         void get_data();
  178. };
  179. void Phonebook::save_info()
  180. {
  181.     ofstream filout;
  182.     filout.open("data.txt",ios::out|ios::app|ios::binary|ios::ate);
  183.     int ch;
  184.  
  185.     if(!filout)
  186.     {
  187.         cout<<"\n cannot open file \n";
  188.     }
  189.     cout<<"\n do you want to save this info (1. yes / 2. no) ?\n";
  190.     cin>>ch;
  191.     if(ch==1)
  192.     {
  193.         filout<<contact.first_name<<"~"<<contact.middle_name<<contact.last_name;
  194.         filout<<"~"<<contact.mobile<<"~"<<contact.landline<<"~";
  195.         filout<<contact.email<<"~|"<<endl;
  196.  
  197.     }
  198.     else if(ch==2)
  199.         cout<<"\n info discarded \n";
  200.     filout.close();
  201. }
  202. void Phonebook::get_data()
  203. {
  204.     contact.take_full_info();
  205.     contact.show_full_info();
  206.     save_info();
  207. }
  208. void Phonebook::edit()
  209. {
  210.  
  211. }
  212. void Phonebook::show_all()
  213. {
  214.     ifstream filout;
  215.     filout.open("data.txt");
  216.     filout.seekg(0,ios::beg);
  217.     while(filout.good())
  218.     {
  219.         cout<<"\n Displaying details ......\n";
  220.     }
  221. }
  222. void Phonebook::search_name()
  223. {
  224.     int token;
  225.     string field,line;
  226.     char line1[50],line2[50];
  227.     cout<<"Enter the first name of the person you wanna search ?? \n";
  228.     cin.ignore();
  229.     getline(cin,field);
  230.     cout<<"person to be searched is "<<field;
  231.     cin.get();
  232.     token=0;
  233.     ifstream readfil;
  234.     readfil.open("data.txt");
  235.     if(!readfil)
  236.         {
  237.             cout<<"file does not exist";
  238.         }
  239.     while(!readfil.eof())
  240.     {
  241.  
  242.             readfil.getline(line1,12,'~');
  243.             if(line1==field)
  244.             {
  245.                 token=1;
  246.                 readfil.seekg(0,ios::beg);
  247.                 readfil.getline(line2,50,'|');
  248.                 cout<<"the person to be found is \n"<<line2;
  249.                 break;
  250.              
  251.             }
  252.     }
  253.         if(token==0)
  254.           cout<<"\n\n.....data not found...";
  255.  
  256.  
  257. }
  258. void search_no()
  259. {
  260. //comment.
  261.  
  262. }
  263. void gui()
  264. {
  265.     int ch;
  266.     Phonebook p;
  267.     while(1)
  268.         {
  269.             cout<<"********PHONE BOOK***********\n";
  270.             cout<<"1) Add new record\n";
  271.             cout<<"2) Display All Records\n";
  272.             cout<<"3) Search Telephone No.\n";
  273.             cout<<"4) Search Person Name\n";
  274.             cout<<"5) Update Telephone No.\n";
  275.             cout<<"6) Exit\n";
  276.             cout<<"Choose your choice : ";
  277.             cin>>ch;
  278.             switch(ch)
  279.                 {
  280.                     case 1:
  281.                         p.get_data();
  282.                         break;
  283.                     case 2:
  284.                         p.show_all();
  285.                         break;
  286.                     case 3:
  287.                         search_no();
  288.                         break;
  289.                     case 4:
  290.                         p.search_name();
  291.                         break;
  292.                     case 5:break;
  293.                     case 6:exit(1);
  294.                     default:cout<<"\n Enter a valid choice";
  295.                 }
  296.         }
  297. }
  298. int main()
  299. {
  300.     gui();
  301.     return 0;
  302. }
Advertisement
Add Comment
Please, Sign In to add comment