-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.java
More file actions
116 lines (106 loc) · 2.83 KB
/
Message.java
File metadata and controls
116 lines (106 loc) · 2.83 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
/**
* Simulates a message sent between two people
* @author Joel Hempel
* @since October 19th 2021
*
*/
public class Message {
public enum Status{Unread, Read, Starred};
private String text, sender, recipient;
private Status received;
private static int messageCount = 0;
private static int lengthOfMessages = 0;
/**
* creates a new text message
* @param text the String to send to the recipient
* @param username the sender of the message
* @param recipient the recipient of the message
* @param received the status of the message
*/
public Message(String text, String username, String recipient, Status received) {
if (text == null || username == null || recipient == null) {
throw new NullPointerException("text, username and recipient must not be null");
}
this.text = text;
this.sender = username;
this.recipient = recipient;
this.received = received;
messageCount++;
lengthOfMessages += text.length();
}
/**
* sends message given message, sender and recipient
* @param text the text to be sent
* @param username the sender of the text
* @param recipient the recipient of the text
*/
public Message(String text, String username, String recipient) {
this(text, username, recipient, Message.Status.Unread);
}
/**
* returns the message text
* @return the message text
*/
public String getText() {
return this.text;
}
/**
* returns the sender
* @return String the sender of the message
*/
public String getSender() {
return this.sender;
}
/**
* return the recipient
* @return String the recipient of the message
*/
public String getRecipient() {
return this.recipient;
}
/**
* return the Status of the message
* @return Status the status of the message
*/
public Status getStatus() {
return this.received;
}
/**
* return the amount of messages sent
* @return int amount of messages sent
*/
public static int getMessageCount() {
return messageCount;
}
/**
* return the total length of characters sent including new line characters
* @return int total number of characters sent
*/
public static int getTotalTextLength() {
return lengthOfMessages;
}
/**
* set messages to starred or read
* @param received the status the message should be set to
*/
public void setStatus(Status received) {
if (received == null) {
throw new NullPointerException();
}
if (received != Message.Status.Unread) {
this.received = received;
//leave unread as unread
//prevent going back from read or starred to unread;
}
}
/**
* set the toString output
* @return String the summary of the Message class
*/
public String toString() {
String output = "";
output += "\nUsername Sender: " + this.sender + "\nUsername Recipient: " + this.recipient +
"\nStatus: " + this.received +"\nText: " + this.text + "\n\n";
return output;
}
}