Advertisement
Maruf_Hasan

phonebook first completion

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