-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ds_proj_v5.cpp
857 lines (706 loc) · 20 KB
/
Ds_proj_v5.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
#include<algorithm>
#include<iostream>
#include <string>
#include <vector>
#include<fstream>
using namespace std;
int users_count;
class user;
vector <user> users;
vector <int> free_ids;
class admin
{
void createAccount(string name)
{}
void deleteAccount(int Id)
{}
void preview(int Id)
{}
};
struct comments
{
int id;
string comment;
};
class post
{
private:
vector <int> likes;
vector <comments> comment;
public:
string text;
int pid;
post(string text)
{
this->text = text;
}
void like_post(int f_id) {
likes.push_back(f_id);
if (likes.size() - 1 != 0){
cout << "and other " <<likes.size() - 1;
}
}
int total_likes() {
return likes.size();
}
void commenting(int f_id, string f_comment) {
comments temp;
temp.id = f_id;
temp.comment = f_comment;
comment.push_back(temp);
}
void delete_like(int f_id) {
std::vector<int>::iterator it;
//int pos;
// Element to be searched
// std::find function call
//it = std::find(likes.begin(), likes.end(), f_id); //5665
/*if (it != likes.end())
{
pos=it - likes.begin() + 1 ;
}*/
likes.erase(it);
}
int comment_count(void)//count total number of comments
{
return comment.size();
}
void delete_comment(int f_id, string f_comment) {
comments temp;
temp.id = f_id;
temp.comment = f_comment;
vector<int>::iterator it;
//it = std::find(likes.begin(), likes.end(), temp); //5665
//comment.erase(remove(comment.begin(), comment.end(), it), comment.end());
}
/*string show_post() {
return this->text;
}*/
void show_post(/*int user_id, int post_id*/); /*{
cout<<this->text<<endl;
cout<<"likes : "<<likes.size();
cout<<"Comments : "<<endl;
for(int i=0;i<comment.size();i++)
{
cout<<users[comment[i].id].getName()<<" : "<<comment[i].comment<<endl;
}
}*/
/*
string show_likes()
{
string c;
for (int i = likes.size() - 1; i > -1; i--)
{
int temp1=
c += my_map[temp1]+'/n';// assume my map by ids
}
}
*/
/*
post(string text);// constructor
void like_post(int f_id);// add friend id to like list
void delete_like(int f_id);
int total_likes();//count total likes for a post
void commenting(int f_id, string f_comment);//add comment to post & who write it in comment vector
int comment_count(void);//count total number of comments
void delete_comment(int f_id,sting f_comment);
string show_post();// show post string & its likes & comments
string show_likes();//names of friends liking post
*/
friend class user;
};
class user {
private:
int id;
string name;
int age;//can be int but separate it in days/month/year
vector<post> posts;
vector<int>friends;
vector<int>id_friend_request;
vector<int>waiting_acceptance;
//bool flag_user = false;
public:
user(){};
user(string name);
user(int age, string name);//constructor
void accept_friend(int id);//see list of request & preview data & decide accept(make sure to delete it from request list) or no
void add_friend(string name_to_add/*or use his name*/);//wait for acceptance & add it to waiting list
void write_post(string post);
void like_f_post(int f_id, int f_post_id);
void write_comment(string f_comment, int f_id, int f_post_id);//comment for friend's post
void preview_all_posts();
void preview_profile();//preview user data/friends names/posts
void preview_friend(string f_name); //preview friend data + friend posts
int get_id();
void delete_friend(); // Must delete its comments and likes too
void delete_post();
void user::accept_friend();
static int get_id(string find_name);
friend void my_graph();
string getName(){
return name;
}
void static delete_user(int user_to_del)
{
///loop on friends and delete his likes and comments
for(int i=0;i<users[user_to_del].friends.size();i++)
{
auto it=std::find(users[users[user_to_del].friends[i]].friends.begin(),users[users[user_to_del].friends[i]].friends.end(),user_to_del);
users[users[user_to_del].friends[i]].friends.erase(it);
for(int j=0;j<users[users[user_to_del].friends[i]].posts.size();j++){
/* while(find(users[users[user_to_del].friends[i]].posts[j].comment.begin(),users[users[user_to_del].friends[i]].posts[j].comment.end(),user_to_del)!=users[users[user_to_del].friends[i]].posts[j].comment.end()){
auto it1=std::find(users[users[user_to_del].friends[i]].posts[j].comment.begin(),users[users[user_to_del].friends[i]].posts[j].comment.end(),user_to_del);
users[users[user_to_del].friends[i]].posts[j].comment.erase(it1);}
}
*/
for(int k=0;k<users[users[user_to_del].friends[i]].posts[j].comment.size();k++){
if(users[users[user_to_del].friends[i]].posts[j].comment[k].id==user_to_del){
users[users[user_to_del].friends[i]].posts[j].comment.erase(k+users[users[user_to_del].friends[i]].posts[j].comment.begin());
}
}
}
for(int j=0;j<users[users[user_to_del].friends[i]].posts.size();j++)
{
while(find(users[users[user_to_del].friends[i]].posts[j].likes.begin(),users[users[user_to_del].friends[i]].posts[j].likes.end(),user_to_del)!=users[users[user_to_del].friends[i]].posts[j].likes.end())
{
auto it2=std::find(users[users[user_to_del].friends[i]].posts[j].likes.begin(),users[users[user_to_del].friends[i]].posts[j].likes.end(),user_to_del);
users[users[user_to_del].friends[i]].posts[j].likes.erase(it2);
}
}
}
for(int l=0;l<users.size();l++){
if(find(users[l].id_friend_request.begin(),users[l].id_friend_request.end(),user_to_del)!=users[l].id_friend_request.end()){
auto it5=std::find(users[l].id_friend_request.begin(),users[l].id_friend_request.end(),user_to_del);
users[l].id_friend_request.erase(it5);
}
}
users[user_to_del].posts.clear();
users[user_to_del].friends .clear();
users[user_to_del].waiting_acceptance .clear();
users[user_to_del].id_friend_request .clear();
free_ids.push_back(user_to_del);
///if (std::find(users[this->id].friends.begin(), users[this->id].friends.end(),frnd)!=users[this->id].friends.end())
}
// these set functions are just used for testing and will be deleted
void setName(string name2)
{
name = name2;
}
void setId(int id2)
{
id = id2;
}
/*void setAge(string age2)
{
age = age2;
}*/
// these set functions are just used for testing and will be deleted
bool checkpost(int post){
bool exist = false;
for (int i = 0; i < users[this->id].posts.size(); i++){
if (post == i){
exist = true;
break;
}
}
return exist;
}
bool checkfrnd(int frnd){
if (std::find(users[this->id].friends.begin(), users[this->id].friends.end(),frnd)!=users[this->id].friends.end())
{
return true;
}
else return false;
}
bool checksys(int frnd){
//system exist checker
bool exist = false;
for (int i = 0; i < users.size(); i++){
if (frnd == users[i].get_id()){
exist = true;
break;
}
}
return exist;
}
friend class post;
};
user::user(int age, string name)
{
this->name = name;
this->age = age;
if(free_ids.size()!=0){
this->id = free_ids[free_ids.size()-1];
}
else{
this->id = users_count;
users_count++;
}
}
int user::get_id(string find_name)
{
for (int i = 0; i<users.size(); i++)
{
if (users[i].name == find_name)
{
return i;
// break;
}
}
}
void user::add_friend(string name_to_add)
{
int id_to_add = get_id(name_to_add);
if (!checksys(id_to_add)){ cout << name_to_add << " doesn't exist\n"; }
else if (!checkfrnd(id_to_add)){
//body of function
users[id_to_add].id_friend_request.push_back(this->id);
}
else{ cout << name_to_add << " is already your friend\n"; }
}
void user::write_post(string write_post)
{
post temp(write_post);
posts.push_back(temp);
}
void user::preview_all_posts()
{
for (int i = 0; i<posts.size(); i++)
{
cout << i << " : " ; posts[i].show_post(); cout<< endl;
}
}
void user::like_f_post(int f_id, int f_post_id)
{
if (users[f_id].posts.size() == 0){ cout << "your friend " << users[f_id].getName() << "has no posts yet\n"; }
else{
///check that if i liked it before
if (std::find(users[f_id].posts[f_post_id].likes.begin(), users[f_id].posts[f_post_id].likes.end(),this->id)!=users[f_id].posts[f_post_id].likes.end())
{
cout<<"You already have liked it before"<<endl;
}
else{
int store_id = this->id;
cout << "you ";
users[f_id].posts[f_post_id].like_post(store_id);
cout << " like this post\n";
}
}
}
void user:: write_comment(string f_comment, int f_id, int f_post_id)//comment for friend's post
{
users[f_id].posts[f_post_id].commenting(this->id /*da id bta3 men elly 3ml l comment ,,elly hoa called the function*/, f_comment);
}
void user::preview_profile()
{
int my_id = (*this).get_id();
cout << "My ID : " << my_id << endl;
cout << "My username : " << users[my_id].name << endl;
cout << "My age : " << users[my_id].age << endl;
cout << "My friends : " << endl;
for (int i = 0; i<users[my_id].friends.size(); i++)
{
cout << i << " : " << users[(users[my_id].friends[i])].name << endl;
}
cout << "My Posts : " << endl;
users[my_id].preview_all_posts();
}
void user::preview_friend(string f_name)
{
int f_id = get_id(f_name);
if(checkfrnd(f_id))
{
cout << "ID : " << f_id << endl;
cout << "Username : " << users[f_id].name << endl;
cout << "Age : " << users[f_id].age << endl;
cout << "Friends : " << endl;
for (int i = 0; i<users[f_id].friends.size(); i++)
{
cout << i << " : " << users[(users[f_id].friends[i])].name << endl;
}
cout << "Posts : " << endl;
users[f_id].preview_all_posts();
}
else cout<<"This name is not your friend"<<endl;
}
int user::get_id()
{
return this->id;
}
void user::delete_friend()
{
string name_to_delete;
if (users[this->id].friends.size() == 0){ cout << "you don't have any friends ya ba2es :P \n"; }
else{
cout << "enter friend name\n";
cin >> name_to_delete;
int id_to_del = get_id(name_to_delete);
if (checkfrnd(id_to_del)){
//body of function
auto it = find(users[this->id].friends.begin(), users[this->id].friends.end(), id_to_del); //5665
users[this->id].friends.erase(it);
auto it2 = find(users[id_to_del].friends.begin(), users[id_to_del].friends.end(),this->id);
users[id_to_del].friends.erase(it2);
}
else{ cout << name_to_delete << " is not your friend\n"; }
}
}
void user::delete_post()
{
int post_index;
if (users[this->id].posts.size() == 0){ cout << "you don't have any posts ya Negm :P \n"; }
else{
cout << "enter post ID\n";
cin >> post_index;
if (checkpost(post_index)){
cout << users[this->id].posts[post_index].text << endl;//to be deleted
users[this->id].posts.erase(posts.begin() + post_index);
}
else{ cout << " can't find post\n"; }
}
}
void user::accept_friend(){
if (id_friend_request.size() != 0){
for (int i = 0; i<id_friend_request.size(); i++){
cout << users[id_friend_request[i]].name << endl;
cout << "Accept:1 , Ignore:2 , Delete:3" << endl;
int x;
cin >> x;
if (x == 1){
users[this->id].friends.push_back(id_friend_request[i]);
users[id_friend_request[i]].friends.push_back(this->id);
cout<<users[id_friend_request[i]].name<<" now is your friend "<<endl;
users[this->id].id_friend_request.erase(id_friend_request.begin() + i);
}
else if (x == 2);
else if (x == 3){
users[this->id].id_friend_request.erase(id_friend_request.begin() + i);
}
}
}
else if (id_friend_request.size() == 0)
{
cout << "No Friend requests yet\n";
}
}
class syst{
int userid;
string username;
public:
syst(){};
void setName(string name2){
username = name2;
}
int findId(string myname){
for (int i = 0; i < users.size(); i++){
if (myname == users[i].getName()){
return users[i].get_id();
}
}
return 999;
}
string findname(int myid){
for (int i = 0; i < users.size(); i++){
if (myid == users[i].get_id()){
return users[i].getName();
}
}
}
bool name_exist(string myname){
for (int i = 0; i < users.size(); i++){
if (myname == users[i].getName()){
return true;
}
}
return false;
}
void signin(){
userid = findId(username);
int choice;
bool signout = false;
int tempint;
string tempstrg;
do{
cout << "----------------------\n";
cout << "\n\n Welcome!! "<<username<<endl;
cout << "enter 1 to write post\n";
cout << "enter 2 to Delete post\n";
cout << "enter 3 to Add friend\n";
cout << "enter 4 to Delete friend\n";
cout << "enter 5 to Accept Friend\n";
cout << "enter 6 to view a Friend\n";
cout << "enter 7 to view your posts\n";
cout << "enter 8 to View Profile\n";
cout << "enter 9 to write comment\n";
cout << "enter 10 to like post\n";
cout << "enter 11 to sign out\n";
cin >> choice;
switch (choice){
case 1:
cout << "enter post\n";
getline(std::cin >> std::ws, tempstrg);
users[userid].write_post(tempstrg);
break;
case 2:
users[userid].delete_post();
break;
case 3:
cout << "enter friend name\n";
cin >> tempstrg;
cout<<"Friend Request Sent"<<endl;
users[userid].add_friend(tempstrg);
break;
case 4:
users[userid].delete_friend();
break;
case 5:
users[userid].accept_friend();
break;
case 6:
cout << "enter friend name\n";
cin >> tempstrg;
users[userid].preview_friend(tempstrg);
break;
case 7:
cout << "My Posts : " << endl;
users[userid].preview_all_posts();
break;
case 8:
users[userid].preview_profile();
break;
case 9:
cout << "comment on : \n";
cout << "Your own post : 1,\n";
cout << "your Friend's post : 2\n";
cin >> tempint; // errorhandle for empty input using do while
if (tempint == 1){
int postid;
cout << "enter post number: \n";
cin >> postid;
cout << "write a comment : ... \n";
getline(std::cin >> std::ws, tempstrg);
users[userid].write_comment(tempstrg,userid,postid);
}
else if (tempint == 2){
cout << "enter friend's name : \n";
string s;
cin>>s;
tempint=user::get_id(s);
int postid;
cout << "enter post number: \n";
cin >> postid;
cout << "write a comment : ... \n";
getline(std::cin >> std::ws, tempstrg);
users[userid].write_comment(tempstrg,tempint,postid);
}
break;
case 10:
cout << "please enter your friend name : \n";
cin >> tempstrg;
tempint = findId(tempstrg);
cout << "please enter your friend's post id : \n";
int postid;
cin >> postid;
users[userid].like_f_post(tempint, postid);
break;
case 11: signout = true; break;
}
} while (!signout);
}
void adminMode(){
userid = findId(username);
int choice;
bool signout = false;
int tempint;
string tempstrg;
do{
cout << "\n ---------Admin Mode------\n";
cout << "\n\n Welcome !!\n";
cout << "enter 1 to create user\n";
cout << "enter 2 to Delete user\n";
cout << "enter 3 to Delete user friend\n";
cout << "enter 4 to Delete post\n";
cout << "enter 5 to view user posts\n";
cout << "enter 6 to View user Profile\n";
cout << "enter 7 to sign out\n";
cin >> choice;
switch (choice){
{ case 1:
bool unique = false;
int name_counter = 1;
string ok = "n";
do{
cout << "Enter user Name: \n";
cin >> tempstrg;
if (name_exist(tempstrg)){
cout << "Name is already taken! \n\n";
string suggest_name;
do{
ok = "n";
char numstr[21]; // enough to hold all numbers up to 64-bits
sprintf_s(numstr, "%d", name_counter);
suggest_name = tempstrg + numstr;
if (findId(suggest_name) == 999){
unique = true;
cout << "suggested name '" << suggest_name << "' proceed? (y/n)" << endl;
cin >> ok;
if (ok == "y" || ok == "Y"){
tempstrg = suggest_name;
}
}
name_counter++;
} while (!unique);
}
else{
ok = "y";
}
} while (ok == "n" || ok == "N");
cout << "user name is " << tempstrg << endl;
cout << "Enter user Age: \n";
cin >> tempint;
if(free_ids.size()==0){
user temp(tempint, tempstrg);
users.push_back(temp);
}
else{
int exist_id = free_ids[free_ids.size()-1];
user temp(tempint, tempstrg);
free_ids.pop_back();
users[exist_id]=temp;
}
/* if(free_ids.size()!=0){
this->id = free_ids[free_ids.size()-1];
free_ids.pop_back();
}fat
else{*/
break;
}
case 2:
cout << "Enter user name : ";
cin >> tempstrg;
if (name_exist(tempstrg))
{
int user_to_del;
user_to_del = user::get_id(tempstrg);
// auto it = find(users.begin(), users.end(), user_to_del); //5665
// users.erase(users.begin()+user_to_del);
user::delete_user(user_to_del);
cout << "user is deleted \n";
}
else { cout << "user not found! \n";}
break;
case 3:
cout << "Enter user name : ";
cin >> tempstrg;
if (name_exist(tempstrg))
{
userid = findId(tempstrg);
users[userid].delete_friend();
}
else { cout << "user not found! \n";}
break;
case 4:
cout << "Enter user name : ";
cin >> tempstrg;
if (name_exist(tempstrg))
{
userid = findId(tempstrg);
users[userid].delete_post();
}
else { cout << "user not found! \n";}
break;
case 5:
cout << "Enter user name : ";
cin >> tempstrg;
if (name_exist(tempstrg))
{
userid = findId(tempstrg);
cout << "My Posts : " << endl;
users[userid].preview_all_posts();
}
else { cout << "user not found! \n";}
break;
case 6:
cout << "Enter user name : ";
cin >> tempstrg;
if (name_exist(tempstrg)){
userid = findId(tempstrg);
users[userid].preview_profile();
}
else { cout << "user not found! \n";}
break;
case 7: signout = true; break;
}
} while (!signout);
}
};
void post:: show_post(/*int user_id, int post_id*/){
cout<<this->text<<endl;
cout<<"likes : "<<likes.size()<<endl;
cout<<"Comments : "<<comment.size()<<endl;
for(int i=0;i<comment.size();i++)
{
cout<<users[comment[i].id].getName()<<" : "<<comment[i].comment<<endl;
}
}
void my_graph(){
ofstream graph("graph.csv");
int my_counting=users_count;
int my_friend=0;
graph<<",";
for(int i=0;i<my_counting;i++){
graph<<"\t"<<users[i].name<<",";
}
graph<<endl;
for(int i=0;i<users.size();i++)
{
graph<<users[i].name<<",";
for(int j=0;j<users.size();j++)
{
if(find(users[i].friends.begin(),users[i].friends.end(),users[j].id)!=users[i].friends.end())
{
graph<<"\t"<<"1"<<",";
}
else
{
graph<<"\t"<<"0"<<",";
}
}
graph<<endl;
}
}
int main()
{
bool exit = false;
int index;
syst def;
user A, B;
post temp2("Temp post");
temp2.pid = 1000;
string temp;
//A.setName("osama");
// A.setId (0);
// B.setName ("gomaa");
//B.setId (1);
//users.push_back(A);
//users.push_back(B);
do{
cout << "enter 1 for admin\n";
cout << "enter 2 for user\n";
cout << "enter 3 to exit\n";
cin >> index;
if (index == 1){ def.adminMode();
}
else if(index==2){
cout << "sign in with your name\n";
cin >> temp;
def.setName(temp);
def.signin();
}
else{
exit = true;
my_graph();
}
} while (!exit);
return(0);
}