-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInbox.java
More file actions
200 lines (164 loc) · 4.13 KB
/
Inbox.java
File metadata and controls
200 lines (164 loc) · 4.13 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
//DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE
package boxes;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import elements.Message;
import elements.Server;
import elements.User;
/**
* Represents an inbox.
* @author leylayayladere
* @version 1.0
*/
public class Inbox extends Box {
/**
* Keeps track of all messages which are currently received but not read.
*/
private Stack<Message> unread = new Stack<Message>();
/**
* Keeps track of all messages which are read.
*/
private Queue<Message> read = new LinkedList<Message>();
/**
* Creates a <code>Inbox</code> with specified <code>owner</code>.
* @param owner <code>User</code> of this <code>Inbox</code>.
*/
public Inbox(User owner) {
super(owner);
}
/**
* Receives messages only which are sent from friends via <code>Server</code>.
* @param server Functions as the mechanism where all non-received messages are stored.
* @param time Stamp time receiving of the messages.
*/
public void receiveMessages(Server server, int time) {
Iterator<Message> itr = server.getMsgs().iterator();
while(itr.hasNext()) {
Message m = itr.next();
if(m.getReceiver().getInbox().equals(this)) {
if(m.getReceiver().isFriendWith(m.getSender())) {
unread.push(m);
m.setTimeStampReceived(time);
server.setCurrentSize(server.getCurrentSize() - m.getBody().length());
itr.remove();
}
}
}
}
/**
* Reads required number of messages.
* @param num Required number of messages.
* @param time Stamp time reading of the <code>Message</code>.
* @return An integer data type.
*/
public int readMessages(int num, int time) {
int numOfMsgs = 0;
if(num == 0) {
while(!unread.empty()) {
Message m = unread.pop();
this.getRead().add(m);
m.setTimeStampRead(time);
time++;
numOfMsgs++;
}
}
while(num != 0) {
if(!unread.empty()) {
Message m = unread.pop();
this.getRead().add(m);
m.setTimeStampRead(time);
time++;
numOfMsgs++;
}
num--;
}
if(numOfMsgs > 1) {
return numOfMsgs;
}else{
return 1;
}
}
/**
* Reads messages which are sent from specified <code>User</code>.
* @param sender Required <code>User</code> who sent messages.
* @param time Stamp time reading of the <code>Message</code>.
* @return An integer data type.
*/
public int readMessages(User sender, int time) {
int numOfMsgs = 0;
Stack<Message> temp = new Stack<Message>();
while(!this.getUnread().empty()) {
Message m = unread.pop();
if(m.getSender().getId() == sender.getId()) {
numOfMsgs++;
this.getRead().add(m);
m.setTimeStampRead(time);
time++;
}else {
temp.add(m);
}
}
while(!temp.empty()) {
Message m = temp.pop();
this.getUnread().add(m);
}
if(numOfMsgs > 1) {
return numOfMsgs;
}else{
return 1;
}
}
/**
* Reads a <code>Message</code> with specified id.
* @param msgId Id of the <code>Message</code>.
* @param time Stamp time reading of the <code>Message</code>.
*/
public void readMessage(int msgId, int time) {
Stack<Message> temp = new Stack<>();
while(!unread.empty()) {
Message m = unread.pop();
if(m.getID() == msgId) {
m.setTimeStampRead(time);
this.getRead().add(m);
break;
}
temp.push(m);
}
while(!temp.empty()) {
Message m = temp.pop();
unread.push(m);
}
}
/**
* Retrieves the value of a <code>unread</code> <i>stack<i>.
* @return A stack data type.
*/
public Stack<Message> getUnread() {
return this.unread;
}
/**
* Sets the value of a <code>unread</code> <i>stack<i>.
* @param unread A variable of type stack.
*/
public void setRead(Stack<Message> unread) {
this.unread = unread;
}
/**
* Retrieves the value of a <code>read</code> <i>queue<i>.
* @return A queue data type.
*/
public Queue<Message> getRead() {
return this.read;
}
/**
* Sets the value of a <code>read</code> <i>queue<i>.
* @param read A variable of type queue.
*/
public void setRead(Queue<Message> read) {
this.read = read;
}
}
//DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE