forked from raghaddmahgoub/OOP-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessenger.java
More file actions
404 lines (366 loc) · 14.9 KB
/
Messenger.java
File metadata and controls
404 lines (366 loc) · 14.9 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
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;
import java.sql.Timestamp;
import java.time.LocalDateTime;
public class Messenger {
Scanner in=new Scanner(System.in);
private static int MessengerID=0;
public int getMessengerid() {
return Messengerid;
}
public List<Integer> getNoUnreadConversations() {
return noUnreadConversations;
}
private int Messengerid;
private List<Conversation> conversations;
private List<Integer> noUnreadConversations;
User user;
public Messenger(User user) {
MessengerID++;
Messengerid=MessengerID;
this.user=user;
this.conversations =user.getConversations();
}
public void addconversations(Conversation con){
conversations.add(con);
}
public void MessengerFeed() {
/////// Conversations ops.
user.getMessenger().sortConversations();
user.getMessenger().displayConversations();
System.out.println("Welcome to Messenger");
System.out.println("1-Create new conversation");
System.out.println("2- Find conversation");
System.out.println("3- Search in conversations by keyword");
System.out.println("4- Delete Conversation");
System.out.println("5- Exit Messenger");
System.out.println("Choice:");
int choice =0;
Boolean validate=new Boolean(false);
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 number of participants:");
Long no=new Long(0);
while (true) {
System.out.println("Please enter number of participants (digits only): ");
String input = in.next();
if (!input.matches("[0-9]+")) {
System.out.println("Number of participants should contain digits only. Please try again.");
} else {
no = Long.parseLong(input);
break; // Exit the loop if the input contains only digits
}}
String username;
ArrayList<User>participants=new ArrayList<>();
participants.add(user);
Boolean check=false;
for(int i=1;i<no;i++)
{
System.out.println("Enter Participant "+i);
username=in.next();
for (User friend:user.getFriends()) {
if(friend.getUserName().equals(username)) {
participants.add(friend);
check=true;
break;
}
}
}
if(check==false)
{
System.out.println("Invalid User");
MessengerFeed();
}
Conversation conversation=new Conversation(participants);
//user.getMessenger().NewConversation(conversation);
//
conversation.displayMessages(conversation);
for (User participant: conversation.getParticipants()) {
participant.setConversations(conversation);
}
Options(conversation);
for (User participant: conversation.getParticipants()) {
participant.getMessenger().conversations.add(conversation);
}
break;
case 2:
displayConversations();
System.out.println("Enter ID:");
int id;
Long No=new Long(0);
while (true) {
System.out.println("Please enter conv ID (digits only): ");
String input = in.next();
if (!input.matches("[0-9]+")) {
System.out.println("conv ID should contain digits only. Please try again.");
} else {
No = Long.parseLong(input);
id=No.intValue();
break; // Exit the loop if the input contains only digits
}}
Conversation conv=findConversation(id);
if(conv !=null) {
conv.displayMessages(conv);
//conv.markAsRead();
}
else {
System.out.println("You don't have any conversations yet");
MessengerFeed();
}
Options(conv); break;
case 3:
System.out.println("Enter keyword:");
String keyword = in.next();
List<Conversation> matchingConversations =searchConversations(keyword);
System.out.println("Matching conversations:");
for (Conversation con : matchingConversations ) {
System.out.println("Conversation ID: " + con.getConversation_id());
System.out.println("Conversation participants: " + con.getParticipants());
}
break;
case 4:
displayConversations();
System.out.println("Enter conversation ID:");
int convID=in.nextInt();
user.getMessenger().deleteConversation(convID);
break;
case 5:
break;
default:
System.out.println("Invalid Choice");
}
if(choice==5)
backToFeed();
MessengerFeed();
}
//// message options
public void Options(Conversation conversation){
System.out.println("1- Add a message");
System.out.println("2- Delete a message");
System.out.println("3- Edit a message");
System.out.println("4- Search in conversation");
System.out.println("5- Exit Conversation");
int Choice;
System.out.println("Enter Value:");
int choice =0;
Boolean validate=new Boolean(false);
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 Content:");
String content=in.next();
conversation.Add_Message(user.getUserID(),content);
break;
case 2:
conversation.DeleteMessage(conversation);break;
case 3:
conversation.displayMessages(conversation);
System.out.println("Enter MessageID:");
int id=in.nextInt();
System.out.println("Enter new content:");
String NewContent=in.next();
for (Messages mes:conversation.getMessages()) {
if(mes.getMesID()==(id))
{
mes.EditMessage(NewContent);
}
}
break;
case 4:
System.out.println("Enter Keyword:");
String keyword=in.next();
ArrayList<Messages>matching=searchMessagesInConversation(conversation,keyword);
for (Messages mes: matching) {
System.out.println(mes.getMesID());
System.out.println(mes.content);
}
break;
case 5:
MessengerFeed();break;
default:
System.out.println("Invalid Choice");
}
Options(conversation);
}
public void backToFeed() {
Feed feed = new Feed(user);
}
////////messenger methods///////
// new conversation
public int newConversation(ArrayList<User> participants) {
int newConvId = conversations.size() + 1;
Conversation conversation = new Conversation(participants);
conversations.add(conversation);
return newConvId;
}
public void NewConversation(Conversation conv) {
int newConvId = conversations.size() + 1;
conversations.add(conv);
}
// display all conversations
public void displayConversations() {
System.out.println("Conversations:");
for (Conversation conversation : conversations) {
System.out.println("Conversation ID: " + conversation.getConversation_id());
System.out.println("Conversation Participants:");
for (User participants:conversation.getParticipants()) {
System.out.println(participants.getUserName());
}
System.out.println("------------------------");
}
}
// delete conversation
public void deleteConversation(int conversationId) {
boolean removed = conversations.removeIf(conv -> conv.getConversation_id() == conversationId);
if (removed) {
System.out.println("Conversation " + conversationId + " deleted successfully.");
} else {
System.out.println("Conversation " + conversationId + " not found.");
}
}
// sort conversations based on timestamp
public void sortConversations() {
conversations.sort(Comparator.comparing(Conversation::getTime).reversed());
}
// get unread messages count for a user
public int getUnreadMessagesCount(int userId) {
int count = 0;
for (Conversation conversation : conversations) {
if (conversation.isParticipant(userId) && !conversation.isRead()) {
count = count + conversation.getNoUnreadMessages().size();
}
}
return count;
}
// search in conversations by keyword
public List<Conversation> searchConversations(String keyword) {
List<Conversation> matchingConversations = new ArrayList<>();
for (Conversation conversation : conversations) {
if (conversation.containsKeyword(keyword)) {
matchingConversations.add(conversation);
}
}
return matchingConversations;
}
// search messages in conversation by keyword
public ArrayList<Messages> searchMessagesInConversation(Conversation con , String keyword) {
if (con != null) {
return con.searchMessages(con,keyword);
} else {
System.out.println("Conversation " + con.getConversation_id() + " not found.");
return new ArrayList<>();
}
}
// Get conversation history
public List<Messages> getConversationHistory(int conversationId) {
Conversation conversation = findConversation(conversationId);
if (conversation != null) {
return conversation.getMessages();
} else {
System.out.println("Conversation " + conversationId + " not found.");
return new ArrayList<>();
}
}
// edit a message in a conversation
public void editMessageInConversation(int conversationId, int messageId, String newContent) {
Conversation conversation = findConversation(conversationId);
if (conversation != null) {
for (Messages mes : conversation.getMessages()) {
if (mes.getId() == messageId) {
mes.EditMessage(newContent);
}
}
} else {
System.out.println("Conversation " + conversationId + " not found.");
}
}
// react to a message in conversation
public void reactToMessageInConversation(int conversationId, int messageId, String reaction) {
Messages mes = new Messages();
Conversation conversation = findConversation(conversationId);
if (conversation != null) {
mes.addReact();
} else {
System.out.println("Conversation " + conversationId + " not found.");
}
}
public Conversation findConversation(int conversationId) {
for (Conversation conversation : conversations) {
if (conversation.getConversation_id() == conversationId) {
return conversation;
}
}
return null;
}
// method to find a conversation BY USERNAME
private Conversation findConvByUserName(String userName) {
for (Conversation conversation : conversations) {
List<User> participants = conversation.getParticipants();
if (participants.contains(userName)) {
return conversation;
}
}
return null;
}
public List<Conversation> getConversations() {
return conversations;
}
public void setConversations(Conversation conversations) {
this.conversations.add(conversations);
}
/*
public int newConversation() {
int newConvId = conversations.size() + 1;
Conversation conversation = new Conversation(newConvId, System.currentTimeMillis());
conversations.add(conversation);
return newConvId;
}
*/ // add message to conversation
/* public void addMessageToConversation(int conversationId, int senderId, int recipientId, String content) {
Conversation conversation = findConversation(conversationId);
conversation.Add_Message(senderId, recipientId, content);
}
// delete message from conversation
public void deleteMessageFromConversation(int conversationId, int messageId) {
Conversation conversation = findConversation(conversationId);
if (conversation != null) {
conversation.DeleteMessage(messageId);
} else {
System.out.println("Conversation " + conversationId + " not found.");
}
}
mark conversation as read
public void markConversationAsRead(int conversationId) {
Conversation conversation = findConversation(conversationId);
if (conversation != null) {
conversation.markAsRead();
} else {
System.out.println("Conversation " + conversationId + " not found.");
}
}*/
/*
public void sortConversations () {
//Sorting conversations based on timestamp from newest to oldest
// conversations.sort(Comparator.comparingLong(Conversation::getTimestamp().reversed());
}*/
}