001package horstmann.ch05_mailgui;
002/**
003   Connect a phone to the mail system.
004 */
005@SuppressWarnings("all")
006public 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                thePhone = 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                currentRecording += voice;
047        }
048
049        /**
050      The user hangs up the phone.
051         */
052        public void hangup()
053        {
054                if (state == RECORDING)
055                        currentMailbox.addMessage(new Message(currentRecording));
056                resetConnection();
057        }
058
059        /**
060      Reset the connection to the initial state and prompt
061      for mailbox number
062         */
063        private void resetConnection()
064        {
065                currentRecording = "";
066                accumulatedKeys = "";
067                state = CONNECTED;
068                thePhone.speak(initialPrompt);
069        }
070
071        /**
072      Try to connect the user with the specified mail box.
073      @param key the phone key pressed by the user
074         */
075        private void connect(String key)
076        {
077                if (key.equals("#"))
078                {
079                        currentMailbox = system.findMailbox(accumulatedKeys);
080                        if (currentMailbox != null)
081                        {
082                                state = RECORDING;
083                                thePhone.speak(currentMailbox.getGreeting());
084                        }
085                        else
086                                thePhone.speak("Incorrect mailbox number. Try again!");
087                        accumulatedKeys = "";
088                }
089                else
090                        accumulatedKeys += key;
091        }
092
093        /**
094      Try to log in the user.
095      @param key the phone key pressed by the user
096         */
097        private void login(String key)
098        {
099                if (key.equals("#"))
100                {
101                        if (currentMailbox.checkPasscode(accumulatedKeys))
102                        {
103                                state = MAILBOX_MENU;
104                                thePhone.speak(mailboxMenu);
105                        }
106                        else
107                                thePhone.speak("Incorrect passcode. Try again!");
108                        accumulatedKeys = "";
109                }
110                else
111                        accumulatedKeys += key;
112        }
113
114        /**
115      Change passcode.
116      @param key the phone key pressed by the user
117         */
118        private void changePasscode(String key)
119        {
120                if (key.equals("#"))
121                {
122                        currentMailbox.setPasscode(accumulatedKeys);
123                        state = MAILBOX_MENU;
124                        thePhone.speak(mailboxMenu);
125                        accumulatedKeys = "";
126                }
127                else
128                        accumulatedKeys += key;
129        }
130
131        /**
132      Change greeting.
133      @param key the phone key pressed by the user
134         */
135        private void changeGreeting(String key)
136        {
137                if (key.equals("#"))
138                {
139                        currentMailbox.setGreeting(currentRecording);
140                        currentRecording = "";
141                        state = MAILBOX_MENU;
142                        thePhone.speak(mailboxMenu);
143                }
144        }
145
146        /**
147      Respond to the user's selection from mailbox menu.
148      @param key the phone key pressed by the user
149         */
150        private void mailboxMenu(String key)
151        {
152                if (key.equals("1"))
153                {
154                        state = MESSAGE_MENU;
155                        thePhone.speak(messageMenu);
156                }
157                else if (key.equals("2"))
158                {
159                        state = CHANGE_PASSCODE;
160                        thePhone.speak("Enter new passcode followed by the # key");
161                }
162                else if (key.equals("3"))
163                {
164                        state = CHANGE_GREETING;
165                        thePhone.speak("Record your greeting, then press the # key");
166                }
167        }
168
169        /**
170      Respond to the user's selection from message menu.
171      @param key the phone key pressed by the user
172         */
173        private void messageMenu(String key)
174        {
175                if (key.equals("1"))
176                {
177                        String output = "";
178                        Message m = currentMailbox.getCurrentMessage();
179                        if (m == null) output += "No messages." + "\n";
180                        else output += m.getText() + "\n";
181                        output += messageMenu;
182                        thePhone.speak(output);
183                }
184                else if (key.equals("2"))
185                {
186                        currentMailbox.saveCurrentMessage();
187                        thePhone.speak(messageMenu);
188                }
189                else if (key.equals("3"))
190                {
191                        currentMailbox.removeCurrentMessage();
192                        thePhone.speak(messageMenu);
193                }
194                else if (key.equals("4"))
195                {
196                        state = MAILBOX_MENU;
197                        thePhone.speak(mailboxMenu);
198                }
199        }
200
201        private MailSystem system;
202        private Mailbox currentMailbox;
203        private String currentRecording;
204        private String accumulatedKeys;
205        private Telephone thePhone;
206        private int state;
207
208        private static final int DISCONNECTED = 0;
209        private static final int CONNECTED = 1;
210        private static final int RECORDING = 2;
211        private static final int MAILBOX_MENU = 3;
212        private static final int MESSAGE_MENU = 4;
213        private static final int CHANGE_PASSCODE = 5;
214        private static final int CHANGE_GREETING = 6;
215
216        private static final String initialPrompt =
217                        "Please enter mailbox number followed by #";
218        private static final String mailboxMenu =
219                        "Enter 1 to listen to your messages\n"
220                                        + "Enter 2 to change your passcode\n"
221                                        + "Enter 3 to change your greeting";
222        private static final String messageMenu =
223                        "Enter 1 to listen to the current message\n"
224                                        + "Enter 2 to save the current message\n"
225                                        + "Enter 3 to delete the current message\n"
226                                        + "Enter 4 to return to the main menu";
227}
228
229
230
231
232
233
234
235
236
237
238