-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailbox.java
107 lines (98 loc) · 1.89 KB
/
Mailbox.java
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
/**
A mailbox contains messages that can be listed, kept or discarded.
*/
public class Mailbox
{
/**
Creates Mailbox object.
@param aPasscode passcode number
@param aGreeting greeting string
*/
public Mailbox(String aPasscode, String aGreeting)
{
passcode = aPasscode;
greeting = aGreeting;
newMessages = new MessageQueue();
keptMessages = new MessageQueue();
}
/**
Check if the passcode is correct.
@param aPasscode a passcode to check
@return true if the supplied passcode matches the mailbox passcode
*/
public boolean checkPasscode(String aPasscode)
{
return aPasscode.equals(passcode);
}
/**
Add a message to the mailbox.
@param aMessage the message to be added
*/
public void addMessage(Message aMessage)
{
newMessages.add(aMessage);
}
/**
Get the current message.
@return the current message
*/
public Message getCurrentMessage()
{
if (newMessages.size() > 0)
return newMessages.peek();
else if (keptMessages.size() > 0)
return keptMessages.peek();
else
return null;
}
/**
Remove the current message from the mailbox.
@return the message that has just been removed
*/
public Message removeCurrentMessage()
{
if (newMessages.size() > 0)
return newMessages.remove();
else if (keptMessages.size() > 0)
return keptMessages.remove();
else
return null;
}
/**
Save the current message.
*/
public void saveCurrentMessage()
{
Message m = removeCurrentMessage();
if (m != null)
keptMessages.add(m);
}
/**
Change mailbox’s greeting.
@param newGreeting the new greeting string
*/
public void setGreeting(String newGreeting)
{
greeting = newGreeting;
}
/**
Change mailbox’s passcode.
@param newPasscode the new passcode
*/
public void setPasscode(String newPasscode)
{
passcode = newPasscode;
}
/**
Get the mailbox’s greeting.
@return the greeting
*/
public String getGreeting()
{
return greeting;
}
private MessageQueue newMessages;
private MessageQueue keptMessages;
private String greeting;
private String passcode;
}