-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConversation.java
More file actions
159 lines (141 loc) · 4.49 KB
/
Conversation.java
File metadata and controls
159 lines (141 loc) · 4.49 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
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
import java.util.Iterator;
public class Conversation {
Scanner in = new Scanner(System.in);
//Attributes
private static int conversation_id=0;
private int convID;
public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
public void setMessages(ArrayList<Messages> messages) {
Messages = messages;
}
public Timestamp timestamp;
private ArrayList<Messages> Messages = new ArrayList<>();
private ArrayList<User> Participants = new ArrayList<>();
ArrayList <Integer> noUnreadMessages=new ArrayList<>();
private String Status;
public int last = Messages.lastIndexOf(Messages)+1;
//Constructor
public Conversation(){
conversation_id++;
}
public Conversation(ArrayList<User>participants) {
conversation_id++;
convID=conversation_id;
timestamp = Timestamp.valueOf(getTime());
this.Messages = new ArrayList<>();
this.Participants = participants;
Status = GroupOrPrivateChat();
noUnreadMessages=null;
}
//Methods
public void Add_Message(int senderID,String content) {
Messages.add(new Messages(senderID,content));
// noUnreadMessages.remove(Integer.valueOf(mes.getId()));
}
public void Sort_Messages() {
Messages.sort(new sorting().reversed());
}
public void DeleteMessage(Conversation con) {
displayMessages(con);
System.out.println("Enter Message ID:");
int ID=in.nextInt();
for (Messages mes:con.getMessages()) {
if(mes.getMesID()==ID){
{
con.getMessages().remove(mes);
//mes.setContent(null);
System.out.println("Message Deleted Successfully");
break;
}
}
}
}
public String GroupOrPrivateChat() {
if (this.Participants.size() > 2) {
return "Group Chat";
}
return "Private Chat";
}
/* public void markAsRead() {
noUnreadMessages.clear();
}*/
public boolean isRead() {
return noUnreadMessages.isEmpty();
}
public boolean isParticipant(int userId) {
return Participants.contains(String.valueOf(userId));
}
private void ViewParticipants() {
System.out.println(getParticipants());
}
// method to search within the conv
public boolean containsKeyword(String keyword) {
for (Messages message : getMessages()) {
if (message.getContent().contains(keyword)) {
return true;
}
}
return false;
}
public void displayMessages(Conversation conv){
for (Messages mes: conv.getMessages()) {
System.out.println(mes.getMesID());
System.out.println(mes.content);
}
}
public ArrayList<Messages> searchMessages(Conversation conv,String keyword) {
ArrayList<Messages> matchingMessages = new ArrayList<>();
for (Messages message : conv.getMessages()) {
if (message.getContent().contains(keyword)) {
matchingMessages.add(message);
}
}
return matchingMessages;
}
public LocalDateTime getTime(){
LocalTime systemTime = LocalTime.now();
LocalDate currentDate = LocalDate.now();
LocalDateTime timestamp = LocalDateTime.of(currentDate, systemTime);
return timestamp;
}
//getters & setters
public int getConversation_id() {
return convID;
}
public ArrayList<Messages> getMessages() {
return Messages;
}
public void setMessages(Messages messages) {
Messages.add(messages);
}
public ArrayList<User> getParticipants() {
return Participants;
}
public void setParticipants(User participants) {
Participants.add(participants);
}
public ArrayList<Integer> getNoUnreadMessages() {
return noUnreadMessages;
}
public void setNoUnreadMessages(int noUnreadMessages) {
this.noUnreadMessages.add(noUnreadMessages);
}
public String getStatus() {
return Status;
}
public void setStatus(String status) {
Status = status;
}
}
class sorting implements Comparator<Messages> {
public int compare(Messages mes1, Messages mes2) {
return mes1.Timestamp.compareTo(mes2.Timestamp);
}
}