Skip to content

Commit 4d0cf85

Browse files
committed
'implemented search by word, time and newest'
1 parent 0168c87 commit 4d0cf85

File tree

5 files changed

+239
-0
lines changed

5 files changed

+239
-0
lines changed

src/main/java/tutorial/Chat.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package tutorial;
2+
import java.util.ArrayList;
3+
4+
/**
5+
* Copied from Branch Chat Initiation
6+
**/
7+
public class Chat {
8+
protected String name;
9+
protected String chatID;
10+
protected String senderUsername;
11+
12+
// chat's conversation history
13+
protected ArrayList<Message> convHist;
14+
15+
16+
/**
17+
* Get the chat's name
18+
* @return name
19+
*/
20+
public String getName(){
21+
return this.name;
22+
}
23+
24+
/**
25+
* Get the chat's ID
26+
* @return chatID
27+
*/
28+
public String getChatID(){
29+
return this.chatID;
30+
}
31+
32+
/**
33+
* Get the sender's username
34+
* @return senderUsername
35+
*/
36+
public String getSenderUsername(){
37+
return this.senderUsername;
38+
}
39+
40+
/**
41+
* Return this conversation history of the chat
42+
* @return convHist
43+
*/
44+
public ArrayList<Message> getConvHist(){
45+
return new ArrayList<Message>(this.convHist);
46+
}
47+
48+
/**
49+
* Add a message to the chat's conversation history when a message is sent or received
50+
* @param message Message that is sent or received
51+
*/
52+
public void addtoconvHist(Message message){
53+
this.convHist.add(message);
54+
}
55+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package tutorial;
2+
import java.time.LocalDateTime;
3+
/**
4+
* Copied from Branch Conversation History
5+
* **/
6+
public abstract class Message {
7+
/**
8+
* ID of message sender (UUID)
9+
*/
10+
private final String senderID;
11+
/**
12+
* Time message was sent
13+
*/
14+
private final LocalDateTime timestamp;
15+
/**
16+
* ID of message (UUID)
17+
*/
18+
private final String msgID;
19+
20+
21+
/**
22+
* Construct a new message
23+
* @param senderID ID of sender
24+
* @param timestamp time message was sent
25+
* @param msgID ID of message
26+
*/
27+
public Message(String senderID, LocalDateTime timestamp, String msgID) {
28+
this.senderID = senderID;
29+
this.timestamp = timestamp;
30+
this.msgID = msgID;
31+
}
32+
33+
/**
34+
* Gets ID of sender
35+
* @return ID of sender
36+
*/
37+
public String getSenderID() {
38+
return senderID;
39+
}
40+
41+
/**
42+
* Gets message timestamp
43+
* @return message timestamp
44+
*/
45+
public LocalDateTime getTimestamp() {
46+
return timestamp;
47+
}
48+
49+
/**
50+
* Gets ID of message
51+
* @return ID of message
52+
*/
53+
public String getMsgID() {
54+
return msgID;
55+
}
56+
57+
//Implemented this method from James' code -Emma
58+
public abstract String getMsgContent();
59+
60+
61+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package tutorial;
2+
3+
import java.time.LocalDateTime;
4+
import java.util.ArrayList;
5+
6+
7+
/**
8+
* The input boundary for the login use case.
9+
*/
10+
// Note: The interface that the SearchUseCase implements
11+
// It specifies what the input (arguments) and output (return type) are.
12+
13+
public interface SearchInputBoundary {
14+
ArrayList<Message> SearchBykeyword(Chat c, String word);
15+
16+
ArrayList<Message> SearchBytime(Chat c, LocalDateTime time);
17+
18+
Message SearchByNewest(ArrayList<Message> ar);
19+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package tutorial;
2+
3+
import java.time.LocalDateTime;
4+
import java.util.ArrayList;
5+
6+
7+
public class SearchUseCase implements SearchInputBoundary {
8+
private final Chat c;
9+
private int size;
10+
11+
private Message temp;
12+
13+
protected ArrayList<Message> Found;
14+
15+
/**
16+
* / The "output" of this use case.
17+
*/
18+
// Note: This could also be a fully-fledged class if we need to return
19+
// information to the controller.
20+
21+
22+
public SearchUseCase(Chat c) {
23+
this.c = c;
24+
this.size= c.convHist.size();
25+
}
26+
27+
28+
public ArrayList<Message> SearchBykeyword(Chat c, String word) {
29+
for(int i=0; i<size; i++){
30+
temp = c.convHist.get(i);
31+
if(temp.getMsgContent().indexOf(word) != -1){
32+
Found.add(temp);
33+
}
34+
}
35+
return Found;
36+
}
37+
38+
public ArrayList<Message> SearchBytime(Chat c, LocalDateTime time){
39+
for(int i=0; i<size; i++){
40+
temp = c.convHist.get(i);
41+
if(temp.getTimestamp() == time){
42+
Found.add(temp);
43+
}
44+
}
45+
return Found;
46+
}
47+
48+
public Message SearchByNewest(ArrayList<Message> ar){
49+
if(ar.isEmpty()){
50+
return null;
51+
}
52+
53+
int ar_size = ar.size();
54+
Message greater = ar.get(0);
55+
for(int i=1; i<ar_size; i++){
56+
if((greater.getTimestamp()).compareTo(ar.get(i).getTimestamp()) < 0){
57+
greater = ar.get(i);
58+
}
59+
}
60+
return greater;
61+
}
62+
63+
64+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package tutorial;
2+
import java.time.LocalDateTime;
3+
4+
/**
5+
* A message with text as its content (child of Message)
6+
*/
7+
public class TextMessage extends Message {
8+
/**
9+
* Text content of message
10+
*/
11+
private String msgContent;
12+
13+
/**
14+
* Construct a text message
15+
* @param senderID ID of sender
16+
* @param msgContent text content
17+
* @param timestamp message timestamp
18+
* @param msgID ID of message
19+
*/
20+
public TextMessage(String senderID, String msgContent, LocalDateTime timestamp, String msgID) {
21+
super(senderID, timestamp, msgID);
22+
this.msgContent = msgContent;
23+
}
24+
25+
/**
26+
* Gets text content of a message
27+
* @return message content
28+
*/
29+
public String getMsgContent() {
30+
return msgContent;
31+
}
32+
33+
/**
34+
* Sets text content of a message
35+
* @param msgContent updated message content
36+
*/
37+
public void setMsgContent(String msgContent) {
38+
this.msgContent = msgContent;
39+
}
40+
}

0 commit comments

Comments
 (0)