public class Connection
1: /**
2: Connects a phone to the mail system. The purpose of this
3: class is to keep track of the state of a connection, since
4: the phone itself is just a source of individual key presses.
5: */
6: public class Connection
7: {
8: /**
9: Construct a Connection object.
10: @param s a MailSystem object
11: @param p a Telephone object
12: */
13: public Connection(MailSystem s, Telephone p)
14: {
15: system = s;
16: phone = p;
17: resetConnection();
18: }
20: /**
21: Respond to the user's pressing a key on the phone touchpad
22: @param key the phone key pressed by the user
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: }
40: /**
41: Record voice.
42: @param voice voice spoken by the user
43: */
44: public void record(String voice)
45: {
46: if (state == RECORDING || state == CHANGE_GREETING)
47: currentRecording += voice;
48: }
50: /**
51: The user hangs up the phone.
52: */
53: public void hangup()
54: {
55: if (state == RECORDING)
56: currentMailbox.addMessage(new Message(currentRecording));
57: resetConnection();
58: }
60: /**
61: Reset the connection to the initial state and prompt
62: for mailbox number
63: */
64: private void resetConnection()
65: {
66: currentRecording = "";
67: accumulatedKeys = "";
68: state = CONNECTED;
69: phone.speak(INITIAL_PROMPT);
70: }
72: /**
73: Try to connect the user with the specified mailbox.
74: @param key the phone key pressed by the user
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: }
94: /**
95: Try to log in the user.
96: @param key the phone key pressed by the user
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: }
115: /**
116: Change passcode.
117: @param key the phone key pressed by the user
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: }
132: /**
133: Change greeting.
134: @param key the phone key pressed by the user
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: }
147: /**
148: Respond to the user's selection from mailbox menu.
149: @param key the phone key pressed by the user
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: }
170: /**
171: Respond to the user's selection from message menu.
172: @param key the phone key pressed by the user
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: }
202: private MailSystem system;
203: private Mailbox currentMailbox;
204: private String currentRecording;
205: private String accumulatedKeys;
206: private Telephone phone;
207: private int state;
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;
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: }