001: /**
002: Connects a phone to the mail system. The purpose of this
003: class is to keep track of the state of a connection, since
004: the phone itself is just a source of individual key presses.
005: */
006: public class Connection
007: {
008: /**
009: Construct a Connection object.
010: @param s a MailSystem object
011: @param p a Telephone object
012: */
013: public Connection(MailSystem s, Telephone p)
014: {
015: system = s;
016: phone = p;
017: resetConnection();
018: }
019:
020: /**
021: Respond to the user's pressing a key on the phone touchpad
022: @param key the phone key pressed by the user
023: */
024: public void dial(String key)
025: {
026: if (state == CONNECTED)
027: connect(key);
028: else if (state == RECORDING)
029: login(key);
030: else if (state == CHANGE_PASSCODE)
031: changePasscode(key);
032: else if (state == CHANGE_GREETING)
033: changeGreeting(key);
034: else if (state == MAILBOX_MENU)
035: mailboxMenu(key);
036: else if (state == MESSAGE_MENU)
037: messageMenu(key);
038: }
039:
040: /**
041: Record voice.
042: @param voice voice spoken by the user
043: */
044: public void record(String voice)
045: {
046: if (state == RECORDING || state == CHANGE_GREETING)
047: currentRecording += voice;
048: }
049:
050: /**
051: The user hangs up the phone.
052: */
053: public void hangup()
054: {
055: if (state == RECORDING)
056: currentMailbox.addMessage(new Message(currentRecording));
057: resetConnection();
058: }
059:
060: /**
061: Reset the connection to the initial state and prompt
062: for mailbox number
063: */
064: private void resetConnection()
065: {
066: currentRecording = "";
067: accumulatedKeys = "";
068: state = CONNECTED;
069: phone.speak(INITIAL_PROMPT);
070: }
071:
072: /**
073: Try to connect the user with the specified mailbox.
074: @param key the phone key pressed by the user
075: */
076: private void connect(String key)
077: {
078: if (key.equals("#"))
079: {
080: currentMailbox = system.findMailbox(accumulatedKeys);
081: if (currentMailbox != null)
082: {
083: state = RECORDING;
084: phone.speak(currentMailbox.getGreeting());
085: }
086: else
087: phone.speak("Incorrect mailbox number. Try again!");
088: accumulatedKeys = "";
089: }
090: else
091: accumulatedKeys += key;
092: }
093:
094: /**
095: Try to log in the user.
096: @param key the phone key pressed by the user
097: */
098: private void login(String key)
099: {
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: 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: }
131:
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: }
146:
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: }
169:
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: }
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: }
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239: