Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- class phonebook//Base Class
- {
- public:
- string name;
- string mobile;
- string email;
- virtual void modifynumber(string nam){};
- virtual void modifyname(string nam){};
- virtual void display(string nam){};
- virtual void add(string name){};
- virtual void delete_number(string name){};
- virtual void clear_all(){};
- virtual void send_messages(string name){};
- virtual void display_sent_messages(string name){};
- };
- class addnum:public phonebook
- {
- public:
- void add(string name);
- };
- //to display or modify existing number
- class existnum:public phonebook
- {
- void modifynumber(string nam);
- void modifyname(string nam);
- void display(string nam);
- };
- //to delete a particular number
- class deletenum:public phonebook
- {
- void delete_number(string name);
- };
- //to clear the whole phonebook
- class clear_phonebook: public phonebook
- {
- void clear_all();
- };
- void addnum:: add(string name)
- {
- fstream fin;
- //ofstream out;
- fin.open("file1.csv",ios::in | ios::app);
- //out.open("Edited.csv",ios::out);
- vector<string>row;
- string line,word,temp,num;
- cout<<"Enter the Number"<<endl;
- cin>>num;
- cout<<"Want to add email?"<<endl;
- cout<<"1.Yes"<<" "<<"2.NO"<<endl;
- int x;
- cin>>x;
- if(x==1)
- {
- cout<<"Enter the email"<<endl;
- string email;
- cin>>email;
- fin<<name<<","<<num<<","<<email<<endl;
- }
- else
- {
- email=" ";
- fin<<name<<","<<num<<","<<email<<endl;
- }
- fin.close();
- }
- void clear_phonebook::clear_all()
- {
- remove("file1.csv");
- return;
- }
- void existnum::display(string name)
- {
- fstream fin;
- fin.open("file1.csv",ios::in);
- vector<string>row;
- string line,word,temp;
- int cnt=0;
- while(getline(fin,line))
- {
- row.clear();
- //getline(fin,line);
- stringstream s(line);
- while(getline(s,word,','))
- {
- row.push_back(word);
- }
- if(name==row[0]){
- cnt=1;
- cout<<row[1]<<endl<<row[2]<<endl;
- }
- }
- fin.close();
- if(cnt==0)
- {
- cout<<"Not found"<<endl;
- }
- }
- void existnum:: modifyname(string nam)
- {
- fstream fin,fout;
- fin.open("file1.csv",ios::in);
- fout.open("file2.csv",ios::out | ios::app);
- vector<string>row;
- string line,word,temp;
- int cnt=0;
- while(getline(fin,line))
- {
- row.clear();
- // getline(fin,line);
- stringstream s(line);
- while(getline(s,word,','))
- {
- row.push_back(word);
- }
- if(nam==row[0]){
- cnt=1;
- cout<<"Enter the Modified Name"<<endl;
- string newname;
- cin>>newname;
- fout<<newname<<","<<row[1]<<","<<row[2]<<endl;
- //break;9
- }
- else
- {
- fout<<row[0]<<","<<row[1]<<","<<row[2]<<endl;
- }
- }
- fin.close();
- fout.close();
- remove("file1.csv");
- rename("file2.csv","file1.csv");
- }
- void existnum:: modifynumber(string nam)
- {
- fstream fin,fout;
- fin.open("file1.csv",ios::in);
- fout.open("file2.csv",ios::out | ios::app);
- vector<string>row;
- string line,word,temp;
- int cnt=0;
- while(getline(fin,line))
- {
- row.clear();
- // getline(fin,line);
- stringstream s(line);
- while(getline(s,word,','))
- {
- row.push_back(word);
- }
- if(nam==row[0]){
- cnt=1;
- cout<<"Enter the Modified number"<<endl;
- string newnum;
- cin>>newnum;
- fout<<row[0]<<","<<newnum<<","<<row[2]<<endl;
- //break;9
- }
- else
- {
- fout<<row[0]<<","<<row[1]<<","<<row[2]<<endl;
- }
- }
- fin.close();
- fout.close();
- remove("file1.csv");
- rename("file2.csv","file1.csv");
- }
- void deletenum:: delete_number(string nam)
- {
- fstream fin,fout;
- fin.open("file1.csv",ios::in);
- fout.open("file2.csv",ios::out | ios::app);
- vector<string>row;
- string line,word,temp;
- int cnt=0;
- while(getline(fin,line))
- {
- row.clear();
- // getline(fin,line);
- stringstream s(line);
- while(getline(s,word,','))
- {
- row.push_back(word);
- }
- if(nam==row[0]){
- continue;
- }
- else
- {
- fout<<row[0]<<","<<row[1]<<","<<row[2]<<endl;
- }
- }
- fin.close();
- fout.close();
- remove("file1.csv");
- rename("file2.csv","file1.csv");
- }
- class message :public phonebook
- {
- void send_messages(string name);
- void display_sent_messages(string name);
- };
- void message::send_messages(string name)
- {
- fstream fin,fout;
- fin.open("file1.csv",ios::in);
- fout.open("msg.csv",ios::out | ios::app);
- vector<string>row;
- string line,word,temp;
- int cnt=0;
- while(getline(fin,line))
- {
- row.clear();
- // getline(fin,line);
- stringstream s(line);
- while(getline(s,word,','))
- {
- row.push_back(word);
- }
- if(name==row[0]){
- cout<<"Enter your text."<<endl;
- cout<<"To: "<<endl<<row[0]<<endl<<row[1]<<endl;
- string text;
- // cin>>text;
- getchar();
- getline(cin,text);
- fout<<row[0]<<','<<row[1]<<','<<text<<endl;
- }
- }
- fin.close();
- fout.close();
- }
- void message::display_sent_messages(string name)
- {
- fstream fin;
- fin.open("msg.csv",ios::in);
- vector<string>row;
- string line,word,temp;
- int cnt=0;
- while(getline(fin,line))
- {
- row.clear();
- //getline(fin,line);
- stringstream s(line);
- while(getline(s,word,','))
- {
- row.push_back(word);
- }
- if(name==row[0]){
- cnt=1;
- cout<<row[2]<<endl;
- cout<<endl<<endl;
- }
- }
- fin.close();
- if(cnt==0)
- {
- cout<<"Not found"<<endl;
- }
- }
- /*template
- class option
- {
- public:
- int choice(int i)
- {
- return i;
- }
- int choice(char i)
- {
- return -1;
- }
- };
- int choiceinput()
- {
- }*/
- int main()
- {
- phonebook *phn;
- existnum ex;
- addnum addnumbers;
- deletenum deletenumbers;
- clear_phonebook clr;
- message msg;
- cout<<"*********** ";
- cout<<"Welcome to Phonebook"<<"***********"<<endl;
- cout<<"********** Menu ****************"<<endl;
- cout<<endl<<endl;
- cout<<endl<<endl;
- cout<<"1.Add New Contact"<<" "<<"2.Search for Contact"<<" "<<"3.Modify Contact"<<endl;
- cout<<endl;
- cout<<"4.Delete Contact"<<" "<<"6.To send message and view messages "<<" 5.Clear Phonebook"<<endl;
- cout<<endl;
- cout<<"7.Exit the Phonebook"<<endl;
- int choice;
- while(true)
- {
- cout<<"Enter Your choice"<<endl;
- cout<<endl;
- cin>>choice;
- if(choice==1)
- {
- phn=&addnumbers;
- cout<<"Enter the Name"<<endl;
- string name;
- cin>>name;
- phn->add(name);
- cout<<"Contact Added succesfully"<<endl;
- }
- else if(choice==2)
- {
- phn=&ex;
- string name;
- cout<<"Enter the name of contact to search"<<endl;
- cin>>name;
- phn->display(name);
- cout<<endl;
- }
- else if(choice==3)
- {
- phn=&ex;
- cout<<"Enter 1 to modify number."<<endl;
- cout<<"Enter 2 to modify name."<<endl;
- int n;
- cin>>n;
- if(n==1)
- {
- cout<<"Enter the name to modify the number"<<endl;
- string name;
- cin>>name;
- phn->modifynumber(name);
- }
- else
- {
- cout<<"Enter the name U want to modify"<<endl;
- string name;
- cin>>name;
- phn->modifyname(name);
- }
- cout<<"Contact Modified succesfully"<<endl;
- }
- else if(choice==4)
- {
- phn=&deletenumbers;
- cout<<"Enter the name of contact to delete"<<endl;
- string name;
- cin>>name;
- phn->delete_number(name);
- cout<<"Contact Deleted succesfully"<<endl;
- }
- else if(choice==6)
- {
- cout<<"Are You Sure to clear the Phonebook"<<endl;
- cout<<"Enter Y/y for Yes"<<endl;
- cout<<"Enter N/n for No"<<endl;
- char c;
- cin>>c;
- if(c=='Y' || c=='y')
- {
- phn=&clr;
- phn->clear_all();
- cout<<"Phonebook cleared succesfully"<<endl;
- }
- else
- {
- continue;
- }
- }
- else if(choice==5)
- {
- cout<<"Enter 1 to send message"<<endl;
- cout<<"Enter 2 to view send messages"<<endl;
- int x;
- cin>>x;
- if(x==1)
- {
- phn=&msg;
- cout<<"Enter the name of contact to send message"<<endl;
- string name;
- cin>>name;
- phn->send_messages(name);
- cout<<endl;
- }
- else
- {
- phn=&msg;
- cout<<"Enter the name of contact to view sent messages"<<endl;
- string name;
- cin>>name;
- phn->display_sent_messages(name);
- cout<<endl;
- }
- }
- else if(choice==7)
- {
- cout<<"Thank You"<<endl;
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement