1 /**
2
3
4
5 */
6 public class Connection
7 {
8 /**
9
10 @param s
11 @param p
12 */
13 public Connection(MailSystem s, Telephone p)
14 {
15 system = s;
16 phone = p;
17 resetConnection();
18 }
19
20 /**
21
22 @param key
23 */
24 public void dial(String key)
25 {
26 if (state == CONNECTED)
27 connect(key);
28 else if (state == RECORDING)
29 login(key);
30 else if (state == CHANGE_PASSCODE)
31 changePasscode(key);
32 else if (state == CHANGE_GREETING)
33 changeGreeting(key);
34 else if (state == MAILBOX_MENU)
35 mailboxMenu(key);
36 else if (state == MESSAGE_MENU)
37 messageMenu(key);
38 }
39
40 /**
41
42 @param voice
43 */
44 public void record(String voice)
45 {
46 if (state == RECORDING || state == CHANGE_GREETING)
47 currentRecording += voice;
48 }
49
50 /**
51
52 */
53 public void hangup()
54 {
55 if (state == RECORDING)
56 currentMailbox.addMessage(new Message(currentRecording));
57 resetConnection();
58 }
59
60 /**
61
62
63 */
64 private void resetConnection()
65 {
66 currentRecording = "";
67 accumulatedKeys = "";
68 state = CONNECTED;
69 phone.speak(INITIAL_PROMPT);
70 }
71
72 /**
73
74 @param key
75 */
76 private void connect(String key)
77 {
78 if (key.equals("#"))
79 {
80 currentMailbox = system.findMailbox(accumulatedKeys);
81 if (currentMailbox != null)
82 {
83 state = RECORDING;
84 phone.speak(currentMailbox.getGreeting());
85 }
86 else
87 phone.speak("Incorrect mailbox number. Try again!");
88 accumulatedKeys = "";
89 }
90 else
91 accumulatedKeys += key;
92 }
93
94 /**
95
96 @param key
97 */
98 private void login(String key)
99 {
100 if (key.equals("#"))
101 {
102 if (currentMailbox.checkPasscode(accumulatedKeys))
103 {
104 state = MAILBOX_MENU;
105 phone.speak(MAILBOX_MENU_TEXT);
106 }
107 else
108 phone.speak("Incorrect passcode. Try again!");
109 accumulatedKeys = "";
110 }
111 else
112 accumulatedKeys += key;
113 }
114
115 /**
116
117 @param key
118 */
119 private void changePasscode(String key)
120 {
121 if (key.equals("#"))
122 {
123 currentMailbox.setPasscode(accumulatedKeys);
124 state = MAILBOX_MENU;
125 phone.speak(MAILBOX_MENU_TEXT);
126 accumulatedKeys = "";
127 }
128 else
129 accumulatedKeys += key;
130 }
131
132 /**
133
134 @param key
135 */
136 private void changeGreeting(String key)
137 {
138 if (key.equals("#"))
139 {
140 currentMailbox.setGreeting(currentRecording);
141 currentRecording = "";
142 state = MAILBOX_MENU;
143 phone.speak(MAILBOX_MENU_TEXT);
144 }
145 }
146
147 /**
148
149 @param key
150 */
151 private void mailboxMenu(String key)
152 {
153 if (key.equals("1"))
154 {
155 state = MESSAGE_MENU;
156 phone.speak(MESSAGE_MENU_TEXT);
157 }
158 else if (key.equals("2"))
159 {
160 state = CHANGE_PASSCODE;
161 phone.speak("Enter new passcode followed by the # key");
162 }
163 else if (key.equals("3"))
164 {
165 state = CHANGE_GREETING;
166 phone.speak("Record your greeting, then press the # key");
167 }
168 }
169
170 /**
171
172 @param key
173 */
174 private void messageMenu(String key)
175 {
176 if (key.equals("1"))
177 {
178 String output = "";
179 Message m = currentMailbox.getCurrentMessage();
180 if (m == null) output += "No messages." + "\n";
181 else output += m.getText() + "\n";
182 output += MESSAGE_MENU_TEXT;
183 phone.speak(output);
184 }
185 else if (key.equals("2"))
186 {
187 currentMailbox.saveCurrentMessage();
188 phone.speak(MESSAGE_MENU_TEXT);
189 }
190 else if (key.equals("3"))
191 {
192 currentMailbox.removeCurrentMessage();
193 phone.speak(MESSAGE_MENU_TEXT);
194 }
195 else if (key.equals("4"))
196 {
197 state = MAILBOX_MENU;
198 phone.speak(MAILBOX_MENU_TEXT);
199 }
200 }
201
202 private MailSystem system;
203 private Mailbox currentMailbox;
204 private String currentRecording;
205 private String accumulatedKeys;
206 private Telephone phone;
207 private int state;
208
209 private static final int DISCONNECTED = 0;
210 private static final int CONNECTED = 1;
211 private static final int RECORDING = 2;
212 private static final int MAILBOX_MENU = 3;
213 private static final int MESSAGE_MENU = 4;
214 private static final int CHANGE_PASSCODE = 5;
215 private static final int CHANGE_GREETING = 6;
216
217 private static final String INITIAL_PROMPT =
218 "Enter mailbox number followed by #";
219 private static final String MAILBOX_MENU_TEXT =
220 "Enter 1 to listen to your messages\n"
221 + "Enter 2 to change your passcode\n"
222 + "Enter 3 to change your greeting";
223 private static final String MESSAGE_MENU_TEXT =
224 "Enter 1 to listen to the current message\n"
225 + "Enter 2 to save the current message\n"
226 + "Enter 3 to delete the current message\n"
227 + "Enter 4 to return to the main menu";
228 }