Maruf_Hasan

Phonebook 30.6.2019

Jun 30th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.42 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. class phonebook//Base Class
  6. {
  7. public:
  8. string name;
  9. string mobile;
  10. string email;
  11. phonebook()
  12. {
  13. name=" ";
  14. mobile=" ";
  15. email=" ";
  16. }
  17. virtual void modifynumber(string nam){};
  18. virtual void modifyname(string nam){};
  19. virtual void display(string nam){};
  20. virtual void add(string name){};
  21. virtual void delete_number(string name){};
  22. virtual void clear_all(){};
  23. virtual void send_messages(string name){};
  24. virtual void display_sent_messages(string name){};
  25. };
  26. class addnum:public phonebook
  27. {
  28. public:
  29. void add(string name);
  30. };
  31.  
  32. //to display or modify existing number
  33. class existnum:public phonebook
  34. {
  35. void modifynumber(string nam);
  36. void modifyname(string nam);
  37. void display(string nam);
  38. };
  39.  
  40. //to delete a particular number
  41. class deletenum:public phonebook
  42. {
  43. void delete_number(string name);
  44. };
  45. //to clear the whole phonebook
  46. class clear_phonebook: public phonebook
  47. {
  48. void clear_all();
  49. };
  50. void addnum:: add(string name)
  51. {
  52. fstream fin;
  53. //ofstream out;
  54. fin.open("file1.csv",ios::in | ios::app);
  55. //out.open("Edited.csv",ios::out);
  56. vector<string>row;
  57. string line,word,temp,num;
  58. cout<<"Enter the Number"<<endl;
  59. cin>>num;
  60. cout<<"Want to add email?"<<endl;
  61. cout<<"1.Yes"<<" "<<"2.NO"<<endl;
  62. int x;
  63. cin>>x;
  64. if(x==1)
  65. {
  66. cout<<"Enter the email"<<endl;
  67. string email;
  68. cin>>email;
  69. fin<<name<<","<<num<<","<<email<<endl;
  70. }
  71. else
  72. {
  73. email=" ";
  74. fin<<name<<","<<num<<","<<email<<endl;
  75. }
  76. fin.close();
  77. }
  78.  
  79. void clear_phonebook::clear_all()
  80. {
  81. remove("file1.csv");
  82. return;
  83. }
  84. void existnum::display(string name)
  85. {
  86. fstream fin;
  87. fin.open("file1.csv",ios::in);
  88. vector<string>row;
  89. string line,word,temp;
  90. int cnt=0;
  91. while(getline(fin,line))
  92. {
  93. row.clear();
  94. //getline(fin,line);
  95. stringstream s(line);
  96. while(getline(s,word,','))
  97. {
  98. row.push_back(word);
  99. }
  100. if(name==row[0]){
  101. cnt=1;
  102. cout<<row[1]<<endl<<row[2]<<endl;
  103. }
  104. }
  105. fin.close();
  106. if(cnt==0)
  107. {
  108. cout<<"Not found"<<endl;
  109. }
  110. }
  111. void existnum:: modifyname(string nam)
  112. {
  113. fstream fin,fout;
  114. fin.open("file1.csv",ios::in);
  115. fout.open("file2.csv",ios::out | ios::app);
  116. vector<string>row;
  117. string line,word,temp;
  118. int cnt=0;
  119. while(getline(fin,line))
  120. {
  121. row.clear();
  122. // getline(fin,line);
  123. stringstream s(line);
  124. while(getline(s,word,','))
  125. {
  126. row.push_back(word);
  127. }
  128. if(nam==row[0]){
  129. cnt=1;
  130. cout<<"Enter the Modified Name"<<endl;
  131. string newname;
  132. cin>>newname;
  133. fout<<newname<<","<<row[1]<<","<<row[2]<<endl;
  134. //break;9
  135. }
  136. else
  137. {
  138. fout<<row[0]<<","<<row[1]<<","<<row[2]<<endl;
  139. }
  140. }
  141. fin.close();
  142. fout.close();
  143. remove("file1.csv");
  144. rename("file2.csv","file1.csv");
  145. }
  146.  
  147. void existnum:: modifynumber(string nam)
  148. {
  149. fstream fin,fout;
  150. fin.open("file1.csv",ios::in);
  151. fout.open("file2.csv",ios::out | ios::app);
  152. vector<string>row;
  153. string line,word,temp;
  154. int cnt=0;
  155. while(getline(fin,line))
  156. {
  157. row.clear();
  158. // getline(fin,line);
  159. stringstream s(line);
  160. while(getline(s,word,','))
  161. {
  162. row.push_back(word);
  163. }
  164. if(nam==row[0]){
  165. cnt=1;
  166. cout<<"Enter the Modified number"<<endl;
  167. string newnum;
  168. cin>>newnum;
  169. fout<<row[0]<<","<<newnum<<","<<row[2]<<endl;
  170. //break;9
  171. }
  172. else
  173. {
  174. fout<<row[0]<<","<<row[1]<<","<<row[2]<<endl;
  175. }
  176. }
  177. fin.close();
  178. fout.close();
  179. remove("file1.csv");
  180. rename("file2.csv","file1.csv");
  181. }
  182. void deletenum:: delete_number(string nam)
  183. {
  184. fstream fin,fout;
  185. fin.open("file1.csv",ios::in);
  186. fout.open("file2.csv",ios::out | ios::app);
  187. vector<string>row;
  188. string line,word,temp;
  189. int cnt=0;
  190. while(getline(fin,line))
  191. {
  192. row.clear();
  193. // getline(fin,line);
  194. stringstream s(line);
  195. while(getline(s,word,','))
  196. {
  197. row.push_back(word);
  198. }
  199. if(nam==row[0]){
  200. continue;
  201. }
  202. else
  203. {
  204. fout<<row[0]<<","<<row[1]<<","<<row[2]<<endl;
  205. }
  206. }
  207. fin.close();
  208. fout.close();
  209. remove("file1.csv");
  210. rename("file2.csv","file1.csv");
  211. }
  212. class message :public phonebook
  213. {
  214. void send_messages(string name);
  215. void display_sent_messages(string name);
  216. };
  217. void message::send_messages(string name)
  218. {
  219. fstream fin,fout;
  220. fin.open("file1.csv",ios::in);
  221. fout.open("msg.csv",ios::out | ios::app);
  222. vector<string>row;
  223. string line,word,temp;
  224. int cnt=0;
  225. while(getline(fin,line))
  226. {
  227. row.clear();
  228. // getline(fin,line);
  229. stringstream s(line);
  230. while(getline(s,word,','))
  231. {
  232. row.push_back(word);
  233. }
  234. if(name==row[0]){
  235. cout<<"Enter your text."<<endl;
  236. cout<<"To: "<<endl<<row[0]<<endl<<row[1]<<endl;
  237.  
  238. string text;
  239. // cin>>text;
  240. getchar();
  241. getline(cin,text);
  242. fout<<row[0]<<','<<row[1]<<','<<text<<endl;
  243. break;
  244. }
  245. }
  246. fin.close();
  247. fout.close();
  248. }
  249. void message::display_sent_messages(string name)
  250. {
  251.  
  252. fstream fin;
  253. fin.open("msg.csv",ios::in);
  254. vector<string>row;
  255. string line,word,temp;
  256. int cnt=0;
  257. while(getline(fin,line))
  258. {
  259. row.clear();
  260. //getline(fin,line);
  261. stringstream s(line);
  262. while(getline(s,word,','))
  263. {
  264. row.push_back(word);
  265. }
  266. if(name==row[0]){
  267. cnt=1;
  268. cout<<row[2]<<endl;
  269. cout<<endl<<endl;
  270. }
  271. }
  272. fin.close();
  273. if(cnt==0)
  274. {
  275. cout<<"Not found"<<endl;
  276. }
  277. }
  278. class oop
  279. {
  280.  
  281. public:
  282. int x;
  283. void operator>(const oop& op)
  284. {
  285. if(op.x==1)
  286. {
  287. cout<<"Contact Added Succesfully"<<endl;
  288. }
  289. else if(op.x==2)
  290. {
  291. cout<<"Contact Modified Successfully"<<endl;
  292. }
  293. else if(op.x==3)
  294. {
  295. cout<<"Contact deleted succesfully"<<endl;
  296. }
  297. else if(op.x==4)
  298. {
  299. cout<<"Contact List Cleared SuccessFully"<<endl;
  300. }
  301. else if(op.x==5)
  302. {
  303. cout<<"Message sent successfully"<<endl;
  304. }
  305. }
  306. };
  307. int main()
  308. {
  309. phonebook *phn;
  310. existnum ex;
  311. addnum addnumbers;
  312. deletenum deletenumbers;
  313. clear_phonebook clr;
  314. message msg;
  315. cout<<"*********** ";
  316. cout<<"Welcome to Phonebook"<<"***********"<<endl;
  317. cout<<"********** Menu ****************"<<endl;
  318. cout<<endl<<endl;
  319.  
  320. cout<<endl<<endl;
  321. cout<<"1.Add New Contact"<<" "<<"2.Search for Contact"<<" "<<"3.Modify Contact"<<endl;
  322. cout<<endl;
  323. cout<<"4.Delete Contact"<<" "<<"5.To send message and view messages "<<" 6.Clear Phonebook"<<endl;
  324. cout<<endl;
  325. cout<<"7.Exit the Phonebook"<<endl;
  326.  
  327. int choice;
  328. oop xx,yy;
  329. while(true)
  330. {
  331. cout<<"Enter Your choice"<<endl;
  332. cout<<endl;
  333. cin>>choice;
  334. if(choice==1)
  335. {
  336. phn=&addnumbers;
  337. cout<<"Enter the Name"<<endl;
  338. string name;
  339. cin>>name;
  340. phn->add(name);
  341. yy.x=1;
  342. xx>yy;
  343. }
  344. else if(choice==2)
  345. {
  346. phn=&ex;
  347. string name;
  348. cout<<"Enter the name of contact to search"<<endl;
  349. cin>>name;
  350. phn->display(name);
  351. cout<<endl;
  352. }
  353. else if(choice==3)
  354. {
  355.  
  356. phn=&ex;
  357. cout<<"Enter 1 to modify number."<<endl;
  358. cout<<"Enter 2 to modify name."<<endl;
  359. int n;
  360. cin>>n;
  361. if(n==1)
  362. {
  363. cout<<"Enter the name to modify the number"<<endl;
  364. string name;
  365. cin>>name;
  366. phn->modifynumber(name);
  367.  
  368. }
  369. else
  370. {
  371. cout<<"Enter the name U want to modify"<<endl;
  372. string name;
  373. cin>>name;
  374. phn->modifyname(name);
  375. }
  376. yy.x=2;
  377. xx>yy;
  378. }
  379. else if(choice==4)
  380. {
  381. phn=&deletenumbers;
  382. cout<<"Enter the name of contact to delete"<<endl;
  383. string name;
  384. cin>>name;
  385. phn->delete_number(name);
  386. yy.x=3;
  387. xx>yy;
  388. }
  389. else if(choice==6)
  390. {
  391. cout<<"Are You Sure to clear the Phonebook"<<endl;
  392. cout<<"Enter Y/y for Yes"<<endl;
  393. cout<<"Enter N/n for No"<<endl;
  394. char c;
  395. cin>>c;
  396. if(c=='Y' || c=='y')
  397. {
  398. phn=&clr;
  399. phn->clear_all();
  400. yy.x=4;
  401. xx>yy;
  402. }
  403. else
  404. {
  405. continue;
  406. }
  407. }
  408. else if(choice==5)
  409. {
  410. cout<<"Enter 1 to send message"<<endl;
  411. cout<<"Enter 2 to view send messages"<<endl;
  412. int x;
  413. cin>>x;
  414. if(x==1)
  415. {
  416. phn=&msg;
  417. cout<<"Enter the name of contact to send message"<<endl;
  418. string name;
  419. cin>>name;
  420. phn->send_messages(name);
  421. cout<<endl;
  422. yy.x=5;
  423. xx.x=0;
  424. xx>yy;
  425. }
  426. else
  427. {
  428. phn=&msg;
  429. cout<<"Enter the name of contact to view sent messages"<<endl;
  430. string name;
  431. cin>>name;
  432. phn->display_sent_messages(name);
  433. cout<<endl;
  434. }
  435. }
  436. else if(choice==7)
  437. {
  438. cout<<"Thank You"<<endl;
  439. break;
  440. }
  441. }
  442. return 0;
  443. }
Add Comment
Please, Sign In to add comment