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