-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPost.java
More file actions
432 lines (409 loc) · 17.2 KB
/
Post.java
File metadata and controls
432 lines (409 loc) · 17.2 KB
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
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Post extends Text {
////////////////////////////////////////////**ATTRIBUTES**//////////////////////////////////////
private static int Id=0;
private int postId;
Scanner in =new Scanner(System.in);
private ArrayList<User> taggedUsers = new ArrayList<User>();
//private FriendShip friendship;
private final ArrayList<Comment> comments = new ArrayList<Comment>();
private String privacy;
public void setPostId(int postId) {
this.postId = postId;
}
public void setTaggedUsers(ArrayList<User> taggedUsers) {
this.taggedUsers = taggedUsers;
}
public void setNumberOfComments(int numberOfComments) {
NumberOfComments = numberOfComments;
}
private int NumberOfComments=0;
public int Score=0;
//////////////////////////////////////////**CONSTRUCTORS**///////////////////////////////////////////
public Post() {
timestamp=Timestamp.valueOf(LocalDateTime.now());
postId=Id;
Id++;
//setTimestamp(t);
}
////////////////////////////////////////////**GETTERS**//////////////////////////////////////////
@Override
public int getId() {
return postId;
}
public String getPrivacy() {
return privacy;
}
@Override
public int getReacts() {
return cntReacts;
}
public ArrayList<User> getTaggedUsers() {
return taggedUsers;
}
public ArrayList<Comment> getAllComments() {
return comments;
}
public Comment getComment(int commentId) {
return getAllComments().get(commentId);
}
public int getNumberOfComments() {
return NumberOfComments;
}
////////////////////////////////////////////**SETTERS**//////////////////////////////////////////
public void addReact() {
cntReacts++;
}
public void addtaggedUsers(User user){
taggedUsers.add(user);
}
public void add_comment(Comment com){
comments.add(com);
}
public void addTaggedUser(Post post) {
if (author.getFriends().size()>0) {
for (User friendlist : author.getFriends()) {
System.out.println(friendlist.getUserName());
}
System.out.println("enter your friend name");
String friendName = in.next();
Boolean checker;
checker = new Boolean(false);
for (User friend : author.getFriends()) {
if (friendName.equals(friend.getUserName())) {
this.taggedUsers.add(friend);
Postnotification not=new Postnotification(post,friend);
not.setContent("tagged");
friend.addPostNotifiObject(not);
//mutual posts
checker=true;
FriendShip f=FriendShip.getFriendship(friend,author);
assert f != null;
f.addMutualPost(post);
System.out.println("tagged friends successfully\n");
}
}
if (checker==false){
System.out.println("invalid name");
addTaggedUser(post);}
}
else {
System.out.println("you do not have friends yet");
}
}
public void setPostPrivacy(Post post){
System.out.println("for public press 1 \n for private press 2 \n for default press 3");
Boolean validate=new Boolean(false);
int choice=0;
while(!validate) {
try {
choice=in.nextInt();
validate=true;
} catch (InputMismatchException e) {
System.out.println("invaild choice try again");
System.out.print("Enter a choice :");
in.nextLine();
}
}
switch (choice){
case 1: this.privacy ="public"; break;
case 2: this.privacy ="private"; break;
case 3: this.privacy =post.author.getUserPrivacy(); break;
default:
System.out.println("invalid input");
setPostPrivacy(post);
}
}
public void addComment(User commenter,Post post){
System.out.println("enter what your want to write in a comment");
String cont=in.next();
Comment comment= new Comment(cont);
post.comments.add(comment);
comment.setAuthor(commenter);
Postnotification postnotification=new Postnotification(post,commenter);
postnotification.setContent("commented");
commenter.addPostNotifiObject(postnotification);
System.out.println("your comment added");
System.out.println("======================================================================================");
post.display_comments(commenter,post);
}
public void addReply(int commentId,User replier,Post post){
System.out.println("enter the reply content");
String content = in.next();
Reply newReply= new Reply(content);
post.getComment(commentId).addReply(newReply);
System.out.println("your reply added");
System.out.println("======================================================================================");
newReply.setAuthor(replier);;
Postnotification postnotification=new Postnotification(post,replier);
postnotification.setContent("replied");
replier.addPostNotifiObject(postnotification);
}
//////////////////////////////////////////**METHODS**////////////////////////////////////////////////
public void Expandpost(User friend,Post post){
System.out.println(getAuthor().getUserName());
System.out.println("since "+GetPostTimeInHours());
post.displayContent();
if (post.taggedUsers.size()>0){
System.out.println("tagged friends are: ");
for (User user:getTaggedUsers()) {
System.out.println(user.getUserName());
}
}
System.out.println("number of reacts: ");
System.out.println(getReacts());
System.out.println("number of comments: "+post.comments.size());
System.out.println("1. like the post\n2. view comments\n3. add comment\n4. return to feed");
Boolean validate=new Boolean(false);
int choice = 0;
while(!validate) {
try {
choice=in.nextInt();
validate=true;
} catch (InputMismatchException e) {
System.out.println("invaild choice try again");
System.out.print("Enter a choice :");
in.nextLine();
}
}
switch (choice){
case 1:
addReact();
System.out.println("your like is added");
System.out.println("total reacts on this post: "+post.getReacts());
Postnotification postnotification= new Postnotification(post.author.getPost(postId),friend);
postnotification.setContent("liked");
friend.addPostNotifiObject(postnotification);
post.Expandpost( friend, post);
System.out.println("======================================================================================");
break;
case 2:
post.display_comments(friend,post);
post.Expandpost( friend, post);
break;
case 3:
post.addComment(friend,post);
Expandpost( friend, post);
break;
case 4: break;
default:
System.out.println("invalid input");
post.Expandpost(friend,post);
}
}
public void display_comments (User friend,Post post){
if(post.getAllComments().size()>0) {
for (Comment comment : post.getAllComments()) {
System.out.println("comment id: " + comment.getId());
comment.displayContent();
System.out.println("reacts: " + comment.getReacts());
}
System.out.println("1.add comment\n2. like a comment\n3. view replies on a comment\n4.add a reply on a comment\n5.exit");
Boolean validate=new Boolean(false);
int choice = 0;
while(!validate) {
try {
choice=in.nextInt();
validate=true;
} catch (InputMismatchException e) {
System.out.println("invaild choice try again");
System.out.print("Enter a choice :");
in.nextLine();
}
}
switch (choice){
case 1:
post.addComment(friend,post);
display_comments ( friend, post);
break;
case 2:
System.out.println("enter the comment id you want to like");
int commentId=0;
while (true) {
String input = in.next();
if (!input.matches("[0-9]+")) {
System.out.println("comment id should contain digits only. Please try again.");
}
else {
commentId = Integer.parseInt(input);
break; // Exit the loop if the input contains only digits
}
if (!getAllComments().contains(commentId)) {
System.out.println("Invalid comment ID. Please enter a valid comment ID.");
} else {
break; // Exit the loop if the input is valid
}
}
post.getComment(commentId).addReact();
User liker = post.getComment(commentId).getAuthor();
Postnotification postnotification = new Postnotification(post, liker);
postnotification.setContent("liked");
author.addPostNotifiObject(postnotification);
System.out.println("like added");
getComment(commentId).displayContent();
System.out.println("number of likes: " + post.getComment(commentId).getReacts());
System.out.println("======================================================================================");
post.display_comments(friend,post);
break;
case 3:
System.out.println("enter the comment id you would like to view replies on");
int enteredCommentId=0;
while (true) {
String input = in.next();
if (!input.matches("[0-9]+")) {
System.out.println("comment id should contain digits only. Please try again.");
}
else {
enteredCommentId = Integer.parseInt(input);
break; // Exit the loop if the input contains only digits
}
if (!getAllComments().contains(enteredCommentId)) {
System.out.println("Invalid comment ID. Please enter a valid comment ID.");
} else {
break; // Exit the loop if the input is valid
}
}
post.display_replies(enteredCommentId,friend,post);
System.out.println("======================================================================================");
post.display_comments ( friend, post);
break;
case 4:
System.out.println("enter the comment id you would like to add replies to");
int commId = 0;
while (true) {
String input = in.next();
if (!input.matches("[0-9]+")) {
System.out.println("comment id should contain digits only. Please try again.");
}
else {
commId = Integer.parseInt(input);
break; // Exit the loop if the input contains only digits
}
if (!getAllComments().contains(commId)) {
System.out.println("Invalid comment ID. Please enter a valid comment ID.");
} else {
break; // Exit the loop if the input is valid
}
}
post.addReply(commId,friend,post);
post.display_comments ( friend, post);
break;
case 5:break;
default:
System.out.println("invalid input");
post.display_comments(friend,post);
}
}
else
System.out.println("there is no comments on this post yet");
}
public void display_replies (int commentId,User friend,Post post){
if (getComment(commentId).getUserReplies().size()>0){
for (Reply reply : post.getComment(commentId).getUserReplies()) {
System.out.println("reply id: "+reply.getId());
reply.displayContent();
System.out.println("number of reacts: "+reply.getReacts());
}
System.out.println("1. like a reply\n2. add reply\n3. exit");
Boolean validate=new Boolean(false);
int choice = 0;
while(!validate) {
try {
choice=in.nextInt();
validate=true;
} catch (InputMismatchException e) {
System.out.println("invaild choice try again");
System.out.print("Enter a choice :");
in.nextLine();
}
}
switch (choice) {
case 1:
System.out.println("enter the reply id you want to like: ");
int replyId = 0;
while (true) {
String input = in.next();
if (!input.matches("[0-9]+")) {
System.out.println("reply id should contain digits only. Please try again.");
} else {
replyId = Integer.parseInt(input);
break; // Exit the loop if the input contains only digits
}
if (!getComment(commentId).getUserReplies().contains(replyId)) {
System.out.println("Invalid reply ID. Please enter a valid comment ID.");
} else {
break; // Exit the loop if the input is valid
}
}
Reply reply = post.getComment(commentId).getReply(replyId);
reply.addReact();
System.out.println("like added");
System.out.println(reply.getContent());
System.out.println("number of reacts: "+reply.getReacts());
/////////send notifi
User liker = reply.getAuthor();
Postnotification postnotification = new Postnotification(post, liker);
postnotification.setContent("liked");
post.author.addPostNotifiObject(postnotification);
post.display_replies ( commentId, friend, post);
break;
case 2:
post.addReply(commentId,friend,post);
post.display_replies ( commentId, friend, post);
break;
case 3: break;
default:
post.display_replies(commentId,friend,post);
}
}
else{
System.out.println("this comment has no replies yet");
}
}
//===omar=====================Don't touch====================================
public long GetPostTimeInHours (){
Timestamp t=Timestamp.valueOf(LocalDateTime.now());
long x=t.getTime()-timestamp.getTime();
x=x/3600000;
return x;
}
public long GetPostTimeInMin (){
Timestamp t=Timestamp.valueOf(LocalDateTime.now());
long x=t.getTime()-timestamp.getTime();
x=x/60000;
return x;
}
public static int getTimeScore (long x) {
if (x >= 72) {
return -50;
} else if (x >= 48) {
return 2;
} else if (x >= 24) {
return 3;
} else if (x >= 12) {
return 4;
} else if (x >= 6) {
return 5;
} else if (x >= 3) {
return 6;
} else if (x >= 1) {
return 7;
} else {
return 8;
}
}
public static Comparator<Post> ScoreComparator =new Comparator<Post>() {
@Override
public int compare(Post o1, Post o2) {
int Score1 =o1.Score;
int Score2 =o2.Score;
return Score2-Score1;
}
};
//====================================================================
}