001package horstmann.ch02_mail;
002/**
003   Connects a phone to the mail system. The purpose of this
004   class is to keep track of the state of a connection, since
005   the phone itself is just a source of individual key presses.
006 */
007@SuppressWarnings("all")
008public class Connection
009{
010        /**
011      Construct a Connection object.
012      @param s a MailSystem object
013      @param p a Telephone object
014         */
015        public Connection(MailSystem s, Telephone p)
016        {
017                system = s;
018                phone = p;
019                resetConnection();
020        }
021
022        /**
023      Respond to the user's pressing a key on the phone touchpad
024      @param key the phone key pressed by the user
025         */
026        public void dial(String key)
027        {
028                if (state == CONNECTED)
029                        connect(key);
030                else if (state == RECORDING)
031                        login(key);
032                else if (state == CHANGE_PASSCODE)
033                        changePasscode(key);
034                else if (state == CHANGE_GREETING)
035                        changeGreeting(key);
036                else if (state == MAILBOX_MENU)
037                        mailboxMenu(key);
038                else if (state == MESSAGE_MENU)
039                        messageMenu(key);
040        }
041
042        /**
043      Record voice.
044      @param voice voice spoken by the user
045         */
046        public void record(String voice)
047        {
048                if (state == RECORDING || state == CHANGE_GREETING)
049                        currentRecording += voice;
050        }
051
052        /**
053      The user hangs up the phone.
054         */
055        public void hangup()
056        {
057                if (state == RECORDING)
058                        currentMailbox.addMessage(new Message(currentRecording));
059                resetConnection();
060        }
061
062        /**
063      Reset the connection to the initial state and prompt
064      for mailbox number
065         */
066        private void resetConnection()
067        {
068                currentRecording = "";
069                accumulatedKeys = "";
070                state = CONNECTED;
071                phone.speak(INITIAL_PROMPT);
072        }
073
074        /**
075      Try to connect the user with the specified mailbox.
076      @param key the phone key pressed by the user
077         */
078        private void connect(String key)
079        {
080                if (key.equals("#"))
081                {
082                        currentMailbox = system.findMailbox(accumulatedKeys);
083                        if (currentMailbox != null)
084                        {
085                                state = RECORDING;
086                                phone.speak(currentMailbox.getGreeting());
087                        }
088                        else
089                                phone.speak("Incorrect mailbox number. Try again!");
090                        accumulatedKeys = "";
091                }
092                else
093                        accumulatedKeys += key;
094        }
095
096        /**
097      Try to log in the user.
098      @param key the phone key pressed by the user
099         */
100        private void login(String key)
101        {
102                if (key.equals("#"))
103                {
104                        if (currentMailbox.checkPasscode(accumulatedKeys))
105                        {
106                                state = MAILBOX_MENU;
107                                phone.speak(MAILBOX_MENU_TEXT);
108                        }
109                        else
110                                phone.speak("Incorrect passcode. Try again!");
111                        accumulatedKeys = "";
112                }
113                else
114                        accumulatedKeys += key;
115        }
116
117        /**
118      Change passcode.
119      @param key the phone key pressed by the user
120         */
121        private void changePasscode(String key)
122        {
123                if (key.equals("#"))
124                {
125                        currentMailbox.setPasscode(accumulatedKeys);
126                        state = MAILBOX_MENU;
127                        phone.speak(MAILBOX_MENU_TEXT);
128                        accumulatedKeys = "";
129                }
130                else
131                        accumulatedKeys += key;
132        }
133
134        /**
135      Change greeting.
136      @param key the phone key pressed by the user
137         */
138        private void changeGreeting(String key)
139        {
140                if (key.equals("#"))
141                {
142                        currentMailbox.setGreeting(currentRecording);
143                        currentRecording = "";
144                        state = MAILBOX_MENU;
145                        phone.speak(MAILBOX_MENU_TEXT);
146                }
147        }
148
149        /**
150      Respond to the user's selection from mailbox menu.
151      @param key the phone key pressed by the user
152         */
153        private void mailboxMenu(String key)
154        {
155                if (key.equals("1"))
156                {
157                        state = MESSAGE_MENU;
158                        phone.speak(MESSAGE_MENU_TEXT);
159                }
160                else if (key.equals("2"))
161                {
162                        state = CHANGE_PASSCODE;
163                        phone.speak("Enter new passcode followed by the # key");
164                }
165                else if (key.equals("3"))
166                {
167                        state = CHANGE_GREETING;
168                        phone.speak("Record your greeting, then press the # key");
169                }
170        }
171
172        /**
173      Respond to the user's selection from message menu.
174      @param key the phone key pressed by the user
175         */
176        private void messageMenu(String key)
177        {
178                if (key.equals("1"))
179                {
180                        String output = "";
181                        Message m = currentMailbox.getCurrentMessage();
182                        if (m == null) output += "No messages." + "\n";
183                        else output += m.getText() + "\n";
184                        output += MESSAGE_MENU_TEXT;
185                        phone.speak(output);
186                }
187                else if (key.equals("2"))
188                {
189                        currentMailbox.saveCurrentMessage();
190                        phone.speak(MESSAGE_MENU_TEXT);
191                }
192                else if (key.equals("3"))
193                {
194                        currentMailbox.removeCurrentMessage();
195                        phone.speak(MESSAGE_MENU_TEXT);
196                }
197                else if (key.equals("4"))
198                {
199                        state = MAILBOX_MENU;
200                        phone.speak(MAILBOX_MENU_TEXT);
201                }
202        }
203
204        private MailSystem system;
205        private Mailbox currentMailbox;
206        private String currentRecording;
207        private String accumulatedKeys;
208        private Telephone phone;
209        private int state;
210
211        private static final int DISCONNECTED = 0;
212        private static final int CONNECTED = 1;
213        private static final int RECORDING = 2;
214        private static final int MAILBOX_MENU = 3;
215        private static final int MESSAGE_MENU = 4;
216        private static final int CHANGE_PASSCODE = 5;
217        private static final int CHANGE_GREETING = 6;
218
219        private static final String INITIAL_PROMPT =
220                        "Enter mailbox number followed by #";
221        private static final String MAILBOX_MENU_TEXT =
222                        "Enter 1 to listen to your messages\n"
223                                        + "Enter 2 to change your passcode\n"
224                                        + "Enter 3 to change your greeting";
225        private static final String MESSAGE_MENU_TEXT =
226                        "Enter 1 to listen to the current message\n"
227                                        + "Enter 2 to save the current message\n"
228                                        + "Enter 3 to delete the current message\n"
229                                        + "Enter 4 to return to the main menu";
230}
231
232
233
234
235
236
237
238
239
240
241