Skip to content

Commit

Permalink
Moved MessageStatus enum into its own file.
Browse files Browse the repository at this point in the history
  • Loading branch information
KostaNikolaou committed Dec 4, 2023
1 parent 5c7d32a commit 3adce74
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/commsProj/ChatMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package commsProj;
import java.util.Date;
import java.util.UUID;

import java.io.Serializable;

import java.text.SimpleDateFormat;

public class ChatMessage implements Serializable {
private static final long serialVersionUID = 3852684761703377574L;
private String id;
private Date timestamp;
private String sender;
private String contents;
private MessageStatus status;

public ChatMessage(String userId, String contents, MessageStatus status) {
id = UUID.randomUUID().toString();
timestamp = new Date();
sender = userId;
this.contents = contents;
this.status = status;
}

public ChatMessage(String userId, Date timestamp, String contents) {
id = UUID.randomUUID().toString();
this.timestamp = timestamp;
sender = userId;
this.contents = contents;
status = MessageStatus.Delivered;
}

public ChatMessage(String line) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd-yy HH:mm:ss");
//starts at 2nd char and is length 36
try {
id = line.substring(1, 37);

String userId = "";
int i = 39;
while (line.charAt(i) != '@') {
userId += line.charAt(i);
i++;
}
sender = userId;
i++;
String date = "";
while (line.charAt(i) != ']') {
date += line.charAt(i);
i++;
}
timestamp = simpleDateFormat.parse(date);
i += 2;
contents = line.substring(i);
status = MessageStatus.Delivered;
} catch(Exception e) {

}

}

public String getId() {
return id;
}

public Date getTimestamp() {
return timestamp;
}

public String getSender() {
return sender;
}

public String contents() {
return contents;
}

public MessageStatus getStatus() {
return status;
}

public void setStatus(MessageStatus status) {
this.status = status;
}

public String toString() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd-yy HH:mm:ss");
return '[' + id + "][" + sender + '@' + simpleDateFormat.format(timestamp) + "]:" + contents;
}
}
4 changes: 4 additions & 0 deletions src/commsProj/MessageStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package commsProj;
public enum MessageStatus {
Pending, Delivered
}

0 comments on commit 3adce74

Please sign in to comment.